Documentation ¶
Overview ¶
Package csql helps create and manage database connections
Index ¶
- Constants
- Variables
- func CtxWithTx(parentCtx context.Context, db *sql.DB, dialect string) (context.Context, *sql.Tx, error)
- func NewDBConnection(lc *clifecycle.Lifecycle, config Config, logger clogger.Logger) (*sql.DB, error)
- func TxFromCtx(ctx context.Context) (*sql.Tx, error)
- type Config
- type ConfigMigrations
- type Migrations
- type Migrator
- type NewMigratorParams
- type Querier
- type TxMiddleware
Constants ¶
const ( MigrationsSourceDir = "dir" MigrationsSourceEmbed = "embed" )
MigrationsSource are valid options for csql.migrations.source configuration option. Use "dir" to load migrations from the local filesystem. Use "embed" to load migrations from the embedded directory in the binary.
const ( MigrationsDirectionUp = "up" MigrationsDirectionDown = "down" )
MigrationsDirection are valid options for csql.migrations.direction configuration option. Use "up" when running forward migrations and "down" when rolling back migrations.
Variables ¶
var WireModule = wire.NewSet( NewDBConnection, NewQuerier, NewMigrator, LoadConfig, NewTxMiddleware, wire.Struct(new(NewMigratorParams), "*"), )
WireModule can be used as part of google/wire setup.
Functions ¶
func CtxWithTx ¶ added in v0.7.0
func CtxWithTx(parentCtx context.Context, db *sql.DB, dialect string) (context.Context, *sql.Tx, error)
CtxWithTx creates a context with a new database transaction. Any queries run using Querier will be run within this transaction.
func NewDBConnection ¶
func NewDBConnection(lc *clifecycle.Lifecycle, config Config, logger clogger.Logger) (*sql.DB, error)
NewDBConnection creates and returns a new database connection. The connection is closed when the app exits.
Types ¶
type Config ¶ added in v0.5.0
type Config struct { Dialect string `toml:"dialect"` DSN string `toml:"dsn"` Migrations ConfigMigrations `toml:"migrations"` MaxOpenConnections *int `toml:"max_open_connections"` }
Config configures the csql module
type ConfigMigrations ¶ added in v0.7.0
ConfigMigrations configures the migrations
type Migrations ¶ added in v0.7.0
Migrations is a collection of .sql files that represent the database schema
type Migrator ¶
type Migrator struct {
// contains filtered or unexported fields
}
Migrator can run database migrations by running the provided migrations in the migrations dir
func NewMigrator ¶
func NewMigrator(p NewMigratorParams) *Migrator
NewMigrator creates a new Migrator
type NewMigratorParams ¶
type NewMigratorParams struct { DB *sql.DB Migrations Migrations Config Config Logger clogger.Logger }
NewMigratorParams holds the params needed for NewMigrator
type Querier ¶ added in v0.7.0
type Querier interface { WithIn() Querier Get(ctx context.Context, dest interface{}, query string, args ...interface{}) error Select(ctx context.Context, dest interface{}, query string, args ...interface{}) error Exec(ctx context.Context, query string, args ...interface{}) (sql.Result, error) }
Querier provides a set of helpful methods to run database queries. It can be used to run parameterized queries and scan results into Go structs or slices.
type TxMiddleware ¶ added in v0.7.0
type TxMiddleware struct {
// contains filtered or unexported fields
}
TxMiddleware is a chttp.Middleware that wraps an HTTP request in a database transaction. If the request succeeds (i.e. 2xx or 3xx response code), the transaction is committed. Else, the transaction is rolled back.
func NewTxMiddleware ¶ added in v0.7.0
NewTxMiddleware creates a new TxMiddleware