mogodb

package module
v0.0.3 Latest Latest
Warning

This package is not in the latest version of its module.

Go to latest
Published: Feb 21, 2026 License: MIT Imports: 10 Imported by: 0

README

mogodb

Common database utilities that can be used across various Go projects.

Most features are mainly focused on PostgreSQL with the support of PostGIS, using pgx as the database driver.

  • Support for PostgreSQL and PostGIS
  • Transparently handle transactions through context.Context
  • WithTx function to wrap anything within a transaction
  • Ready to be used with sqlc and goose examples

Examples

Here are some code snippets demonstrating the usage of the package. You may also refer to the tests for more examples.

Also check out example directory for more examples.

New Connection

Start by creating the handler:

conn := NewConn(ctx, &ConnParams{
    Addr: "postgres://user:pass@localhost:5432/dbname",
    HasPostgis: true,
})
defer conn.Close(ctx)
Transaction Management

Transaction management is done through the context.Context object. They can be nested and each level will join the outer transaction. Outer most transaction will be committed or rolled back.

// Functions are safe to call even if not in a transaction.
conn.BeginTx(ctx)
defer RollbackTx(ctx)

Tx(ctx)
InTx(ctx)
CommitTx(ctx)
conn.WithTx Helper Method

conn.WithTx helper, provides a convenient way to run a function within a transaction

conn.WithTx(ctx, func(ctx context.Context) error {
    // Do some database operations here
    if err != nil {
        // If an error is returned, the transaction will be rolled back
        return err
    }

    // If nil is returned, the transaction will be committed
    return nil
})

conn.WithTx can be nested, just like the transaction management functions.

conn.WithTx(ctx, func(ctx context.Context) error {
    conn.WithTx(ctx, func(ctx context.Context) error {
        // Do some database operations here
        return nil
    })
    return nil
})
Generic WithTx Function

Generic WithTx function is also provided for convenience, allowing the caller to return an additional result from inner function.

// A generic WithTx function is also provided for convenience
res, err := WithTx(ctx, conn, func(ctx context.Context) (T, error) {
    // Do some database operations here
    return res, nil
})

Documentation

Overview

Package mogodb provides utilities for working with databases. Currently supports PostgreSQL with PostGIS.

Index

Constants

This section is empty.

Variables

This section is empty.

Functions

func CommitTx

func CommitTx(ctx context.Context) error

CommitTx commits the transaction stored in the context, if any.

func InTx

func InTx(ctx context.Context) bool

InTx checks if the context has an active transaction.

func MigrateDownAll

func MigrateDownAll(conn *Conn, fs fs.FS, dir string) error

MigrateDownAll rolls back all applied migrations in the database using the embedded migration files.

func MigrateUpAll

func MigrateUpAll(conn *Conn, fs fs.FS, dir string) error

MigrateUpAll applies all pending migrations to the database using the embedded migration files.

func NewConnURL

func NewConnURL(p *ConnURLParams) string

NewConnURL creates a connection URL from the parameters.

func RollbackTx

func RollbackTx(ctx context.Context) error

RollbackTx rolls back the transaction stored in the context, if any.

func Tx

func Tx(ctx context.Context) pgx.Tx

Tx retrieves transaction from context.

func WithTx

func WithTx[T any](ctx context.Context, conn *Conn, fn func(ctx context.Context) (T, error)) (T, error)

WithTx is a generic helper version of conn.WithTx that can be used to return a value from the function as well.

Types

type Conn

type Conn struct {
	// contains filtered or unexported fields
}

Conn represents a connection to the mogodb.

func NewConn

func NewConn(ctx context.Context, p *ConnParams) (*Conn, error)

NewConn creates a new database connection using the provided parameters.

func (*Conn) BeginTx

func (conn *Conn) BeginTx(ctx context.Context) (context.Context, error)

BeginTx starts a new transaction and returns a new context containing the transaction.

func (*Conn) Close

func (c *Conn) Close()

Close the database connection.

func (*Conn) Handle

func (c *Conn) Handle() *sql.DB

Handle returns a *sql.DB instance for compatibility with database/sql package, acting as a connection handle.

func (*Conn) Ping

func (c *Conn) Ping(ctx context.Context) error

Ping checks the database connection.

func (*Conn) Pool

func (c *Conn) Pool() *pgxpool.Pool

Pool returns the underlying pgxpool.Pool.

func (*Conn) WithTx

func (conn *Conn) WithTx(ctx context.Context, fn func(ctx context.Context) error) error

WithTx executes the given function within a transaction context, starting a new transaction if one does not already exist.

type ConnParams

type ConnParams struct {
	Addr       string
	HasPostgis bool
}

ConnParams holds the parameters for connecting to the mogodb.

type ConnURLParams

type ConnURLParams struct {
	// Type of connection.
	Type ConnURLType
	// Host to connect to.
	Host string
	// Port to connect on.
	Port string
	// Username to connect with.
	User string
	// Password to connect with.
	Password string
	// DB name to connect to.
	DB string
	// IsSSL is true if connection is over SSL.
	IsSSL bool
}

ConnURLParams represents the parameters of the connection URL.

type ConnURLType

type ConnURLType string

ConnURLType represents the type of the connection URL.

const (
	// ConnURLTypePostgres is for connecting to a PostgreSQL database.
	ConnURLTypePostgres ConnURLType = "postgres"
)

Directories

Path Synopsis
gen

Jump to

Keyboard shortcuts

? : This menu
/ : Search site
f or F : Jump to
y or Y : Canonical URL