Documentation
¶
Index ¶
- func ApplyMigrations(ctx context.Context, db *sql.DB, dir string, logger *zap.Logger) error
- func ApplyMigrationsDirs(ctx context.Context, db *sql.DB, dirs []string, logger *zap.Logger) error
- type Client
- type FindOption
- type HTTPGateway
- type QueryBuilder
- func (qb *QueryBuilder) Alias(a string) *QueryBuilder
- func (qb *QueryBuilder) AndWhere(expr string, args ...any) *QueryBuilder
- func (qb *QueryBuilder) Build() (string, []any)
- func (qb *QueryBuilder) GetMany(ctx context.Context, dest any) error
- func (qb *QueryBuilder) GetOne(ctx context.Context, dest any) error
- func (qb *QueryBuilder) GroupBy(cols ...string) *QueryBuilder
- func (qb *QueryBuilder) InnerJoin(table string, on string) *QueryBuilder
- func (qb *QueryBuilder) Join(table string, on string) *QueryBuilder
- func (qb *QueryBuilder) LeftJoin(table string, on string) *QueryBuilder
- func (qb *QueryBuilder) Limit(n int) *QueryBuilder
- func (qb *QueryBuilder) Offset(n int) *QueryBuilder
- func (qb *QueryBuilder) OrWhere(expr string, args ...any) *QueryBuilder
- func (qb *QueryBuilder) OrderBy(exprs ...string) *QueryBuilder
- func (qb *QueryBuilder) Select(cols ...string) *QueryBuilder
- func (qb *QueryBuilder) Where(expr string, args ...any) *QueryBuilder
- type RQLiteAdapter
- type RQLiteManager
- func (r *RQLiteManager) ApplyMigrations(ctx context.Context, dir string) error
- func (r *RQLiteManager) ApplyMigrationsDirs(ctx context.Context, dirs []string) error
- func (r *RQLiteManager) GetConnection() *gorqlite.Connection
- func (r *RQLiteManager) Start(ctx context.Context) error
- func (r *RQLiteManager) Stop() error
- type Repository
- type TableNamer
- type Tx
Constants ¶
This section is empty.
Variables ¶
This section is empty.
Functions ¶
func ApplyMigrations ¶
ApplyMigrations scans a directory for *.sql files, orders them by numeric prefix, and applies any that are not yet recorded in schema_migrations(version).
func ApplyMigrationsDirs ¶
ApplyMigrationsDirs applies migrations from multiple directories. - Gathers *.sql files from each dir - Parses numeric prefix as the version - Errors if the same version appears in more than one dir (to avoid ambiguity) - Sorts globally by version and applies those not yet in schema_migrations
Types ¶
type Client ¶
type Client interface {
// Query runs an arbitrary SELECT and scans rows into dest (pointer to slice of structs or []map[string]any).
Query(ctx context.Context, dest any, query string, args ...any) error
// Exec runs a write statement (INSERT/UPDATE/DELETE).
Exec(ctx context.Context, query string, args ...any) (sql.Result, error)
// FindBy/FindOneBy provide simple map-based criteria filtering.
FindBy(ctx context.Context, dest any, table string, criteria map[string]any, opts ...FindOption) error
FindOneBy(ctx context.Context, dest any, table string, criteria map[string]any, opts ...FindOption) error
// Save inserts or updates an entity (single-PK).
Save(ctx context.Context, entity any) error
// Remove deletes by PK (single-PK).
Remove(ctx context.Context, entity any) error
// Repositories (generic layer). Optional but convenient if you use Go generics.
Repository(table string) any
// Fluent query builder for advanced querying.
CreateQueryBuilder(table string) *QueryBuilder
// Tx executes a function within a transaction.
Tx(ctx context.Context, fn func(tx Tx) error) error
}
Client is the high-level ORM-like API.
func NewClientFromAdapter ¶
func NewClientFromAdapter(adapter *RQLiteAdapter) Client
NewClientFromAdapter is convenient if you already created the adapter.
type FindOption ¶
type FindOption func(q *QueryBuilder)
FindOption customizes Find queries.
func WithGroupBy ¶
func WithGroupBy(cols ...string) FindOption
func WithJoin ¶
func WithJoin(kind, table, on string) FindOption
func WithLimit ¶
func WithLimit(n int) FindOption
func WithOffset ¶
func WithOffset(n int) FindOption
func WithOrderBy ¶
func WithOrderBy(exprs ...string) FindOption
func WithSelect ¶
func WithSelect(cols ...string) FindOption
type HTTPGateway ¶
type HTTPGateway struct {
// Client is the ORM-like rqlite client to execute operations against.
Client Client
// BasePath is the prefix for all routes, e.g. "/v1/db".
// If empty, defaults to "/v1/db". A trailing slash is trimmed.
BasePath string
// Optional: Request timeout. If > 0, handlers will use a context with this timeout.
Timeout time.Duration
}
HTTPGateway exposes the ORM Client as a set of HTTP handlers.
func NewHTTPGateway ¶
func NewHTTPGateway(c Client, base string) *HTTPGateway
NewHTTPGateway constructs a new HTTPGateway with sensible defaults.
func (*HTTPGateway) RegisterRoutes ¶
func (g *HTTPGateway) RegisterRoutes(mux *http.ServeMux)
RegisterRoutes registers all handlers onto the provided mux under BasePath.
type QueryBuilder ¶
type QueryBuilder struct {
// contains filtered or unexported fields
}
QueryBuilder implements a fluent SELECT builder with joins, where, etc.
func (*QueryBuilder) Alias ¶
func (qb *QueryBuilder) Alias(a string) *QueryBuilder
func (*QueryBuilder) AndWhere ¶
func (qb *QueryBuilder) AndWhere(expr string, args ...any) *QueryBuilder
func (*QueryBuilder) Build ¶
func (qb *QueryBuilder) Build() (string, []any)
Build returns the SQL string and args for a SELECT.
func (*QueryBuilder) GetMany ¶
func (qb *QueryBuilder) GetMany(ctx context.Context, dest any) error
GetMany executes the built query and scans into dest (pointer to slice).
func (*QueryBuilder) GetOne ¶
func (qb *QueryBuilder) GetOne(ctx context.Context, dest any) error
GetOne executes the built query and scans into dest (pointer to struct or map) with LIMIT 1.
func (*QueryBuilder) GroupBy ¶
func (qb *QueryBuilder) GroupBy(cols ...string) *QueryBuilder
func (*QueryBuilder) InnerJoin ¶
func (qb *QueryBuilder) InnerJoin(table string, on string) *QueryBuilder
func (*QueryBuilder) Join ¶
func (qb *QueryBuilder) Join(table string, on string) *QueryBuilder
func (*QueryBuilder) LeftJoin ¶
func (qb *QueryBuilder) LeftJoin(table string, on string) *QueryBuilder
func (*QueryBuilder) Limit ¶
func (qb *QueryBuilder) Limit(n int) *QueryBuilder
func (*QueryBuilder) Offset ¶
func (qb *QueryBuilder) Offset(n int) *QueryBuilder
func (*QueryBuilder) OrWhere ¶
func (qb *QueryBuilder) OrWhere(expr string, args ...any) *QueryBuilder
func (*QueryBuilder) OrderBy ¶
func (qb *QueryBuilder) OrderBy(exprs ...string) *QueryBuilder
func (*QueryBuilder) Select ¶
func (qb *QueryBuilder) Select(cols ...string) *QueryBuilder
func (*QueryBuilder) Where ¶
func (qb *QueryBuilder) Where(expr string, args ...any) *QueryBuilder
type RQLiteAdapter ¶
type RQLiteAdapter struct {
// contains filtered or unexported fields
}
RQLiteAdapter adapts RQLite to the sql.DB interface
func NewRQLiteAdapter ¶
func NewRQLiteAdapter(manager *RQLiteManager) (*RQLiteAdapter, error)
NewRQLiteAdapter creates a new adapter that provides sql.DB interface for RQLite
func (*RQLiteAdapter) Close ¶
func (a *RQLiteAdapter) Close() error
Close closes the adapter connections
func (*RQLiteAdapter) GetManager ¶
func (a *RQLiteAdapter) GetManager() *RQLiteManager
GetManager returns the underlying RQLite manager for advanced operations
func (*RQLiteAdapter) GetSQLDB ¶
func (a *RQLiteAdapter) GetSQLDB() *sql.DB
GetSQLDB returns the sql.DB interface for compatibility with existing storage service
type RQLiteManager ¶
type RQLiteManager struct {
// contains filtered or unexported fields
}
RQLiteManager manages an RQLite node instance
func NewRQLiteManager ¶
func NewRQLiteManager(cfg *config.DatabaseConfig, discoveryCfg *config.DiscoveryConfig, dataDir string, logger *zap.Logger) *RQLiteManager
NewRQLiteManager creates a new RQLite manager
func (*RQLiteManager) ApplyMigrations ¶
func (r *RQLiteManager) ApplyMigrations(ctx context.Context, dir string) error
ApplyMigrationsFromManager is a convenience helper bound to RQLiteManager.
func (*RQLiteManager) ApplyMigrationsDirs ¶
func (r *RQLiteManager) ApplyMigrationsDirs(ctx context.Context, dirs []string) error
ApplyMigrationsDirs is the multi-dir variant on RQLiteManager.
func (*RQLiteManager) GetConnection ¶
func (r *RQLiteManager) GetConnection() *gorqlite.Connection
GetConnection returns the RQLite connection
type Repository ¶
type Repository[T any] interface { Find(ctx context.Context, dest *[]T, criteria map[string]any, opts ...FindOption) error FindOne(ctx context.Context, dest *T, criteria map[string]any, opts ...FindOption) error Save(ctx context.Context, entity *T) error Remove(ctx context.Context, entity *T) error // Builder helpers Q() *QueryBuilder }
Repository provides typed entity operations for a table.
type TableNamer ¶
type TableNamer interface {
TableName() string
}
TableNamer lets a struct provide its table name.
type Tx ¶
type Tx interface {
Query(ctx context.Context, dest any, query string, args ...any) error
Exec(ctx context.Context, query string, args ...any) (sql.Result, error)
CreateQueryBuilder(table string) *QueryBuilder
// Optional: scoped Save/Remove inside tx
Save(ctx context.Context, entity any) error
Remove(ctx context.Context, entity any) error
}
Tx mirrors Client but executes within a transaction.