adapter

package
v1.3.0 Latest Latest
Warning

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

Go to latest
Published: Oct 23, 2026 License: MIT Imports: 3 Imported by: 0

Documentation

Overview

Package adapter defines the generic, schema-driven persistence contracts for GoModularAuth.

Unlike static domain repository interfaces, an Adapter executes generic CRUD operations parameterized by model name and declarative query conditions. This decouples storage drivers (PostgreSQL, SQLite, MySQL, Memory) from individual plugin domain types.

Index

Constants

This section is empty.

Variables

View Source
var (
	// ErrNotFound is returned when a record matching the query criteria does not exist.
	ErrNotFound = errors.New("adapter: record not found")
	// ErrConflict is returned when an insert or update violates a unique constraint or primary key.
	ErrConflict = errors.New("adapter: record conflict")
)

Functions

This section is empty.

Types

type Adapter

type Adapter interface {
	// RegisterSchema provides the compiled schema to the adapter, allowing
	// it to map fields, understand constraints, and prepare statements or tables.
	RegisterSchema(ctx context.Context, s *schema.Schema) error

	// Create inserts a single record into the given model. It returns the persisted
	// data (including database defaults or auto-generated IDs).
	// Returns ErrConflict on unique constraint violations.
	Create(ctx context.Context, model string, data map[string]any) (map[string]any, error)

	// FindOne retrieves the first record matching all conditions, or ErrNotFound if none match.
	FindOne(ctx context.Context, model string, where []Condition) (map[string]any, error)

	// FindMany retrieves all records matching the query criteria.
	FindMany(ctx context.Context, q Query) ([]map[string]any, error)

	// Update modifies records matching the conditions and returns the number of rows affected.
	Update(ctx context.Context, model string, where []Condition, update map[string]any) (int64, error)

	// Delete removes records matching the conditions and returns the number of rows affected.
	Delete(ctx context.Context, model string, where []Condition) (int64, error)

	// Count returns the number of records matching the given conditions.
	Count(ctx context.Context, model string, where []Condition) (int64, error)

	// Transaction executes fn atomically within a database transaction.
	// Nested transactions join the parent transaction.
	Transaction(ctx context.Context, fn func(ctx context.Context, tx Adapter) error) error
}

Adapter is the polymorphic storage engine interface implemented by databases.

All database adapters (Postgres, SQLite, MySQL, Memory) implement this single interface, allowing plugins to declare new models and fields without needing engine-specific repository implementations.

type Condition

type Condition struct {
	Field    string
	Operator Operator
	Value    any
}

Condition represents a single predicate applied to a model field.

func Eq

func Eq(field string, val any) Condition

Eq creates an equality condition (field = val).

func Gt

func Gt(field string, val any) Condition

Gt creates a greater-than condition (field > val).

func Gte

func Gte(field string, val any) Condition

Gte creates a greater-than-or-equal condition (field >= val).

func In

func In(field string, vals ...any) Condition

In creates an IN condition (field IN (vals...)).

func IsNotNull

func IsNotNull(field string) Condition

IsNotNull creates an IS NOT NULL condition.

func IsNull

func IsNull(field string) Condition

IsNull creates an IS NULL condition.

func Lt

func Lt(field string, val any) Condition

Lt creates a less-than condition (field < val).

func Lte

func Lte(field string, val any) Condition

Lte creates a less-than-or-equal condition (field <= val).

func Ne

func Ne(field string, val any) Condition

Ne creates an inequality condition (field != val).

type Operator

type Operator string

Operator represents a logical comparison operator for query conditions.

const (
	OpEq        Operator = "="
	OpNe        Operator = "!="
	OpGt        Operator = ">"
	OpGte       Operator = ">="
	OpLt        Operator = "<"
	OpLte       Operator = "<="
	OpIn        Operator = "IN"
	OpLike      Operator = "LIKE"
	OpIsNull    Operator = "IS NULL"
	OpIsNotNull Operator = "IS NOT NULL"
)

type Query

type Query struct {
	Model  string
	Where  []Condition
	Limit  int
	Offset int
	SortBy string
	Desc   bool
}

Query defines search, sorting, and pagination parameters for finding multiple records.

type SchemaExporter

type SchemaExporter interface {
	// ExportSchema returns the raw DDL statements required to provision the registered schema.
	ExportSchema(ctx context.Context) ([]string, error)
}

SchemaExporter is an optional capability interface implemented by storage adapters that can export their compiled schema definitions as executable DDL statements (e.g. SQL, SurrealQL).

Directories

Path Synopsis
Package bridge implements a store.Store adapter on top of adapter.Adapter.
Package bridge implements a store.Store adapter on top of adapter.Adapter.
Package memory provides an in-memory implementation of adapter.Adapter.
Package memory provides an in-memory implementation of adapter.Adapter.
Package sqladapter provides generic SQL dialect handling and a base relational adapter.
Package sqladapter provides generic SQL dialect handling and a base relational adapter.

Jump to

Keyboard shortcuts

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