Documentation ¶
Index ¶
- func Transaction(ctx context.Context) *sql.Tx
- func WriteAndDispatch[T event.Source](db *Database, ctx context.Context, entities []T, ...) (finalErr error)
- type Database
- func (db *Database) Close() error
- func (db *Database) ExecContext(ctx context.Context, query string, args ...any) (sql.Result, error)
- func (db *Database) Migrate(modules ...MigrationsModule) error
- func (db *Database) QueryContext(ctx context.Context, query string, args ...any) (*sql.Rows, error)
- func (db *Database) QueryRowContext(ctx context.Context, query string, args ...any) *sql.Row
- func (db *Database) WithTransaction(ctx context.Context) (context.Context, *sql.Tx, bool)
- type MigrationsModule
Constants ¶
This section is empty.
Variables ¶
This section is empty.
Functions ¶
func Transaction ¶
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 (*Database) ExecContext ¶ added in v1.2.0
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 (*Database) QueryRowContext ¶ added in v1.2.0
func (*Database) WithTransaction ¶
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.