db

package
v0.14.0 Latest Latest
Warning

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

Go to latest
Published: Jul 27, 2026 License: Apache-2.0 Imports: 18 Imported by: 0

Documentation

Index

Constants

This section is empty.

Variables

View Source
var (
	ErrNotStruct    = errors.New("model must be a struct")
	ErrNoPrimaryKey = errors.New("model must have a primary key field tagged with db:\"...primary\"")
	ErrNotFound     = errors.New("record not found")
)

Functions

func EncodeCursor added in v0.11.0

func EncodeCursor(cursor PageCursor) string

func MySQLOpen

func MySQLOpen(url string) (*sql.DB, error)

func NewPool

func NewPool(ctx context.Context, cfg PoolConfig) (*pgxpool.Pool, error)

func SetSlowThreshold added in v0.11.0

func SetSlowThreshold(d time.Duration)

func TursoOpen

func TursoOpen(url string) (*sql.DB, error)

Types

type ColumnValue

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

func Col

func Col(col string, val any) ColumnValue

type Constraint added in v0.13.0

type Constraint struct {
	Type    string   // "UNIQUE", "INDEX", "CHECK"
	Columns []string // column names in order
	Name    string   // optional constraint name
}

Constraint represents a table-level constraint such as UNIQUE(a,b).

type FieldInfo

type FieldInfo struct {
	Column       string
	GoName       string
	Primary      bool
	Auto         bool
	Required     bool
	Default      string
	Index        bool
	Unique       bool
	Skip         bool
	FieldType    reflect.Type
	Tags         reflect.StructTag
	TypeOverride string // type=DECIMAL(10,2), type=JSONB, type=TEXT[]
	FK           string // fk=users.id
}

type MySQLTable

type MySQLTable[T any] struct {
	// contains filtered or unexported fields
}

func NewMySQLTable

func NewMySQLTable[T any](db *sql.DB, tableName string) (*MySQLTable[T], error)

func NewMySQLTableFromURL

func NewMySQLTableFromURL[T any](url string, tableName string) (*MySQLTable[T], error)

func (*MySQLTable[T]) AutoInit

func (t *MySQLTable[T]) AutoInit(ctx context.Context) error

func (*MySQLTable[T]) Close

func (t *MySQLTable[T]) Close() error

func (*MySQLTable[T]) Count added in v0.14.0

func (t *MySQLTable[T]) Count(ctx context.Context) (int64, error)

func (*MySQLTable[T]) CountScoped added in v0.14.0

func (t *MySQLTable[T]) CountScoped(ctx context.Context, tenantField, tenantID string) (int64, error)

func (*MySQLTable[T]) Create

func (t *MySQLTable[T]) Create(ctx context.Context, entity *T) error

func (*MySQLTable[T]) CreateScoped added in v0.10.0

func (t *MySQLTable[T]) CreateScoped(ctx context.Context, entity *T, tenantField string, tenantID string) error

func (*MySQLTable[T]) DB

func (t *MySQLTable[T]) DB() *sql.DB

func (*MySQLTable[T]) Delete

func (t *MySQLTable[T]) Delete(ctx context.Context, id any) error

func (*MySQLTable[T]) DeleteScoped added in v0.10.0

func (t *MySQLTable[T]) DeleteScoped(ctx context.Context, id any, tenantField string, tenantID string) error

func (*MySQLTable[T]) Get

func (t *MySQLTable[T]) Get(ctx context.Context, id any) (*T, error)

func (*MySQLTable[T]) GetScoped added in v0.10.0

func (t *MySQLTable[T]) GetScoped(ctx context.Context, id any, tenantField string, tenantID string) (*T, error)

func (*MySQLTable[T]) List

func (t *MySQLTable[T]) List(ctx context.Context) ([]T, error)

func (*MySQLTable[T]) ListPaginated added in v0.14.0

func (t *MySQLTable[T]) ListPaginated(ctx context.Context, limit, offset int) ([]T, error)

func (*MySQLTable[T]) ListScoped added in v0.10.0

func (t *MySQLTable[T]) ListScoped(ctx context.Context, tenantField string, tenantID string) ([]T, error)

func (*MySQLTable[T]) ListScopedPaginated added in v0.14.0

func (t *MySQLTable[T]) ListScopedPaginated(ctx context.Context, tenantField, tenantID string, limit, offset int) ([]T, error)

func (*MySQLTable[T]) QueryKeyset added in v0.6.0

func (t *MySQLTable[T]) QueryKeyset(ctx context.Context, cursor string, size int, orderBy string, where map[string]any) ([]T, string, error)

func (*MySQLTable[T]) Update

func (t *MySQLTable[T]) Update(ctx context.Context, id any, patch map[string]any) (*T, error)

func (*MySQLTable[T]) UpdateScoped added in v0.10.0

func (t *MySQLTable[T]) UpdateScoped(ctx context.Context, id any, patch map[string]any, tenantField string, tenantID string) (*T, error)

type PageCursor added in v0.11.0

type PageCursor struct {
	LastValue string `json:"v"`
	LastID    string `json:"id,omitempty"`
}

func ParseCursor added in v0.11.0

func ParseCursor(encoded string) (PageCursor, error)

type PaginatedResponse added in v0.11.0

type PaginatedResponse[T any] struct {
	Items      []T    `json:"items"`
	NextCursor string `json:"next_cursor,omitempty"`
	HasMore    bool   `json:"has_more"`
}

func NewPaginatedResponse added in v0.11.0

func NewPaginatedResponse[T any](items []T, nextCursor string) PaginatedResponse[T]

type PoolConfig

type PoolConfig struct {
	URL               string
	MaxConns          int32
	MinConns          int32
	MaxConnLifetime   time.Duration
	MaxConnIdleTime   time.Duration
	HealthCheckPeriod time.Duration
	StatementTimeout  time.Duration
	ApplicationName   string
}

func DefaultPoolConfig

func DefaultPoolConfig() PoolConfig

type PreparedTable added in v0.14.0

type PreparedTable[T any] struct {
	*Table[T]
	// contains filtered or unexported fields
}

func NewPreparedTable added in v0.14.0

func NewPreparedTable[T any](pool *pgxpool.Pool, tableName string) (*PreparedTable[T], error)

func (*PreparedTable[T]) Count added in v0.14.0

func (pt *PreparedTable[T]) Count(ctx context.Context) (int64, error)

func (*PreparedTable[T]) Delete added in v0.14.0

func (pt *PreparedTable[T]) Delete(ctx context.Context, id any) error

func (*PreparedTable[T]) Exists added in v0.14.0

func (pt *PreparedTable[T]) Exists(ctx context.Context, column string, value any) (bool, error)

func (*PreparedTable[T]) Get added in v0.14.0

func (pt *PreparedTable[T]) Get(ctx context.Context, id any) (*T, error)

func (*PreparedTable[T]) List added in v0.14.0

func (pt *PreparedTable[T]) List(ctx context.Context) ([]T, error)

type Table

type Table[T any] struct {
	// contains filtered or unexported fields
}

func NewTable

func NewTable[T any](pool *pgxpool.Pool, tableName string) (*Table[T], error)

func (*Table[T]) AutoInit

func (t *Table[T]) AutoInit(ctx context.Context) error

func (*Table[T]) BatchInsert

func (t *Table[T]) BatchInsert(ctx context.Context, entities []T) error

func (*Table[T]) Count

func (t *Table[T]) Count(ctx context.Context, where ...ColumnValue) (int64, error)

func (*Table[T]) Create

func (t *Table[T]) Create(ctx context.Context, entity *T) error

func (*Table[T]) CreateScoped added in v0.10.0

func (t *Table[T]) CreateScoped(ctx context.Context, entity *T, tenantField string, tenantID string) error

func (*Table[T]) Delete

func (t *Table[T]) Delete(ctx context.Context, id any) error

func (*Table[T]) DeleteScoped added in v0.10.0

func (t *Table[T]) DeleteScoped(ctx context.Context, id any, tenantField string, tenantID string) error

func (*Table[T]) ExecRaw

func (t *Table[T]) ExecRaw(ctx context.Context, sql string, args ...any) (int64, error)

func (*Table[T]) Exists

func (t *Table[T]) Exists(ctx context.Context, column string, value any) (bool, error)

func (*Table[T]) FindBy

func (t *Table[T]) FindBy(ctx context.Context, column string, value any) (*T, error)

func (*Table[T]) Get

func (t *Table[T]) Get(ctx context.Context, id any) (*T, error)

func (*Table[T]) GetScoped added in v0.10.0

func (t *Table[T]) GetScoped(ctx context.Context, id any, tenantField string, tenantID string) (*T, error)

func (*Table[T]) Increment

func (t *Table[T]) Increment(ctx context.Context, id any, column string, amount int64) error

func (*Table[T]) List

func (t *Table[T]) List(ctx context.Context) ([]T, error)

func (*Table[T]) ListScoped added in v0.10.0

func (t *Table[T]) ListScoped(ctx context.Context, tenantField string, tenantID string) ([]T, error)

func (*Table[T]) PrimaryKey

func (t *Table[T]) PrimaryKey() string

func (*Table[T]) QueryKeyset added in v0.6.0

func (t *Table[T]) QueryKeyset(ctx context.Context, cursor string, size int, orderBy string, where map[string]any) ([]T, string, error)

func (*Table[T]) QueryPaginated

func (t *Table[T]) QueryPaginated(ctx context.Context, page, size int, orderBy string) ([]T, int64, error)

func (*Table[T]) QueryWhere

func (t *Table[T]) QueryWhere(ctx context.Context, where map[string]any, orderBy string, limit, offset int) ([]T, error)

func (*Table[T]) ResolveColumn

func (t *Table[T]) ResolveColumn(jsonKey string) string

func (*Table[T]) ResolvePatch

func (t *Table[T]) ResolvePatch(patch map[string]any) map[string]any

func (*Table[T]) TableInfo

func (t *Table[T]) TableInfo() *TableInfo

func (*Table[T]) Transaction

func (t *Table[T]) Transaction(ctx context.Context, fn func(tx pgx.Tx) error) error

func (*Table[T]) Update

func (t *Table[T]) Update(ctx context.Context, id any, patch map[string]any) (*T, error)

func (*Table[T]) UpdateScoped added in v0.10.0

func (t *Table[T]) UpdateScoped(ctx context.Context, id any, patch map[string]any, tenantField string, tenantID string) (*T, error)

func (*Table[T]) Upsert

func (t *Table[T]) Upsert(ctx context.Context, entity *T, conflictColumn string) error

type TableConstraints added in v0.13.0

type TableConstraints interface {
	Constraints() []Constraint
}

TableConstraints is an optional interface that structs can implement to declare table-level constraints (composite unique, foreign keys, etc.).

type TableInfo

type TableInfo struct {
	Name       string
	Fields     []FieldInfo
	PrimaryKey string
}

func ParseStruct

func ParseStruct[T any]() (*TableInfo, error)

func ParseStructReflect

func ParseStructReflect(typ reflect.Type) (*TableInfo, error)

ParseStructReflect parses struct tags from a reflect.Type (non-generic version).

type TursoTable

type TursoTable[T any] struct {
	// contains filtered or unexported fields
}

func NewTursoTable

func NewTursoTable[T any](url string, tableName string) (*TursoTable[T], error)

func NewTursoTableFrom

func NewTursoTableFrom[T any](db *sql.DB, tableName string, info *TableInfo) (*TursoTable[T], error)

func (*TursoTable[T]) AutoInit

func (t *TursoTable[T]) AutoInit(ctx context.Context) error

func (*TursoTable[T]) Close

func (t *TursoTable[T]) Close() error

func (*TursoTable[T]) Count added in v0.14.0

func (t *TursoTable[T]) Count(ctx context.Context) (int64, error)

func (*TursoTable[T]) CountScoped added in v0.14.0

func (t *TursoTable[T]) CountScoped(ctx context.Context, tenantField, tenantID string) (int64, error)

func (*TursoTable[T]) Create

func (t *TursoTable[T]) Create(ctx context.Context, entity *T) error

func (*TursoTable[T]) CreateScoped added in v0.10.0

func (t *TursoTable[T]) CreateScoped(ctx context.Context, entity *T, tenantField string, tenantID string) error

func (*TursoTable[T]) DB added in v0.14.0

func (t *TursoTable[T]) DB() *sql.DB

func (*TursoTable[T]) Delete

func (t *TursoTable[T]) Delete(ctx context.Context, id any) error

func (*TursoTable[T]) DeleteScoped added in v0.10.0

func (t *TursoTable[T]) DeleteScoped(ctx context.Context, id any, tenantField string, tenantID string) error

func (*TursoTable[T]) Get

func (t *TursoTable[T]) Get(ctx context.Context, id any) (*T, error)

func (*TursoTable[T]) GetScoped added in v0.10.0

func (t *TursoTable[T]) GetScoped(ctx context.Context, id any, tenantField string, tenantID string) (*T, error)

func (*TursoTable[T]) List

func (t *TursoTable[T]) List(ctx context.Context) ([]T, error)

func (*TursoTable[T]) ListPaginated added in v0.14.0

func (t *TursoTable[T]) ListPaginated(ctx context.Context, limit, offset int) ([]T, error)

func (*TursoTable[T]) ListScoped added in v0.10.0

func (t *TursoTable[T]) ListScoped(ctx context.Context, tenantField string, tenantID string) ([]T, error)

func (*TursoTable[T]) ListScopedPaginated added in v0.14.0

func (t *TursoTable[T]) ListScopedPaginated(ctx context.Context, tenantField, tenantID string, limit, offset int) ([]T, error)

func (*TursoTable[T]) QueryKeyset added in v0.6.0

func (t *TursoTable[T]) QueryKeyset(ctx context.Context, cursor string, size int, orderBy string, where map[string]any) ([]T, string, error)

func (*TursoTable[T]) Update

func (t *TursoTable[T]) Update(ctx context.Context, id any, patch map[string]any) (*T, error)

func (*TursoTable[T]) UpdateScoped added in v0.10.0

func (t *TursoTable[T]) UpdateScoped(ctx context.Context, id any, patch map[string]any, tenantField string, tenantID string) (*T, error)

Jump to

Keyboard shortcuts

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