Documentation
¶
Index ¶
- Constants
- Variables
- func InitStore[Store any, StoreTx any](impl StoreImpl[Store, StoreTx], base *StoreBase[Store, StoreTx], ...) error
- type FnInitAppData
- type Migration
- type Queryable
- type StoreAPI
- type StoreBase
- func (s *StoreBase[Store, StoreTx]) Close()
- func (s StoreBase[Store, StoreTx]) DBErr(err error, where string) error
- func (s *StoreBase[Store, StoreTx]) IsConflict(err error) bool
- func (s *StoreBase[Store, StoreTx]) Transact(fn func(tx StoreTx) error) error
- func (s *StoreBase[Store, StoreTx]) WithCtx(ctx context.Context) Store
- type StoreImpl
Constants ¶
View Source
const VERSION_SCHEMA string = `
CREATE TABLE IF NOT EXISTS migration (
version INTEGER NOT NULL DEFAULT 1
);
`
Variables ¶
View Source
var ErrAlreadyExists = errors.New("already-exists")
View Source
var ErrConflict = errors.New("db-conflict")
View Source
var ErrNotFound = errors.New("not-found")
Functions ¶
Types ¶
type FnInitAppData ¶
FnInitAppData is an optional callback to run after the migration, within the same transaction.
type Migration ¶
type Migration struct {
Version int // Monotonically increasing schema version
SQL string // Query to uprade the schema
Update FnInitAppData // Callback to run after the migration (can be nil)
}
Migration is a schema migration to pass to InitStoreLib
type Queryable ¶
type Queryable interface {
Exec(query string, args ...any) (sql.Result, error)
Query(query string, args ...any) (*sql.Rows, error)
QueryRow(query string, args ...any) *sql.Row
Prepare(query string) (*sql.Stmt, error)
}
The common read-only parts of sql.DB and sql.Tx interfaces
type StoreAPI ¶
type StoreAPI[Store any, StoreTx any] interface { // Transact performs a transactional update function Transact(fn func(txn StoreTx) error) error // WithCtx returns the same Store interface, bound to a specific cancellable Context WithCtx(ctx context.Context) Store // Close closes the database. Close() }
StoreAPI must be included in your Store interface, along with your public Store methods.
type StoreBase ¶
type StoreBase[Store any, StoreTx any] struct { Txn Queryable // Query the DB inside/outside a Transaction RawDB *sql.DB // Bypass Txn and directly access the DB Ctx context.Context IsPostgres bool // contains filtered or unexported fields }
StoreBase must be included in your Store imeplentation struct
func (*StoreBase[Store, StoreTx]) Close ¶
func (s *StoreBase[Store, StoreTx]) Close()
Close closes the connection to the store for clean shutdown.
func (*StoreBase[Store, StoreTx]) IsConflict ¶
Click to show internal directories.
Click to hide internal directories.