sqlite

package
v1.2.0 Latest Latest
Warning

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

Go to latest
Published: Dec 21, 2023 License: GPL-3.0 Imports: 12 Imported by: 0

Documentation

Index

Constants

This section is empty.

Variables

This section is empty.

Functions

func Transaction

func Transaction(ctx context.Context) *sql.Tx

Retrieve the transaction in the given context if any, or nil if it doesn't have one.

func WriteAndDispatch

func WriteAndDispatch[T event.Source](
	db *Database,
	ctx context.Context,
	entities []T,
	switcher func(context.Context, event.Event) error,
) (finalErr error)

Helpers to handle database writes from an array of event sources and handle events dispatching. It will open and manage a transaction if none exist in the given context. This way, we make sure event handlers participates in the same transaction so they are resolved as a whole.

There's no way to add this method to the DB without type conversion so this is the easiest way for now. Without the generics, I will always have to convert an array of entities to []event.Source which is not very convenient.

Types

type Database

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

Handle to a sqlite database with useful helper methods on it :)

func Open

func Open(dsn string, logger log.Logger, bus bus.Dispatcher) (*Database, error)

Opens a connection to a sqlite database file.

func (*Database) Close

func (db *Database) Close() error

Close the underlying database.

func (*Database) ExecContext added in v1.2.0

func (db *Database) ExecContext(ctx context.Context, query string, args ...any) (sql.Result, error)

func (*Database) Migrate

func (db *Database) Migrate(modules ...MigrationsModule) error

Migrates the opened database to the latest version.

func (*Database) QueryContext added in v1.2.0

func (db *Database) QueryContext(ctx context.Context, query string, args ...any) (*sql.Rows, error)

func (*Database) QueryRowContext added in v1.2.0

func (db *Database) QueryRowContext(ctx context.Context, query string, args ...any) *sql.Row

func (*Database) WithTransaction

func (db *Database) WithTransaction(ctx context.Context) (context.Context, *sql.Tx, bool)

Creates and enhance the given context with a transaction if no one exists yet. The returned boolean indicates if the transaction has been created by this call with true and if it returns false, it means the transaction has been initiated early.

The caller MUST commit or rollback it.

If the transaction could not be created, this method will panic.

type MigrationsModule added in v1.1.0

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

Represents a single module for database migrations.

func NewMigrationsModule added in v1.1.0

func NewMigrationsModule(name string, dir string, fs fs.FS) MigrationsModule

Builds a new migrations module with the given module name (used as a migrations history table name prefix) and the directory where migrations are stored in the given filesystem.

Directories

Path Synopsis

Jump to

Keyboard shortcuts

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