db

package
v0.1.0 Latest Latest
Warning

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

Go to latest
Published: Jul 1, 2026 License: MIT Imports: 14 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 MySQLOpen

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

func NewPool

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

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 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
}

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]) Create

func (t *MySQLTable[T]) Create(ctx context.Context, entity *T) 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]) Get

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

func (*MySQLTable[T]) List

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

func (*MySQLTable[T]) Update

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

type PoolConfig

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

func DefaultPoolConfig

func DefaultPoolConfig() PoolConfig

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]) Delete

func (t *Table[T]) Delete(ctx context.Context, id any) 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]) 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]) PrimaryKey

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

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]) Upsert

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

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]) Create

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

func (*TursoTable[T]) Delete

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

func (*TursoTable[T]) Get

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

func (*TursoTable[T]) List

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

func (*TursoTable[T]) Update

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

Jump to

Keyboard shortcuts

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