API reference
Public types and functions in v0.5.0.
Module path: github.com/anukool23/dbswitch
Package dbswitch
Schema types
type ColumnType int
const (
TypeUUID ColumnType = iota
TypeText
TypeBool
TypeInt
TypeTimestamp
)
type DefaultValue int
const (
DefaultNone DefaultValue = iota
DefaultGenerateUUID
DefaultCurrentTime
DefaultFalse
DefaultTrue
)
type Column struct {
Name string
Type ColumnType
PrimaryKey bool
Unique bool
NotNull bool
Default DefaultValue
}
type Table struct {
Name string
Columns []Column
}Store
// Implemented by *postgres.DB, *mongo.Store, and *dynamodb.Store.
type Store interface {
CreateTable(ctx context.Context, t Table) error
Insert(ctx context.Context, table string, data map[string]any) error
FindOne(ctx context.Context, table string, where map[string]any) (map[string]any, error)
Find(ctx context.Context, table string, where map[string]any) ([]map[string]any, error)
List(ctx context.Context, table string, opts ListOptions) ([]map[string]any, error)
Count(ctx context.Context, table string, filter map[string]any) (int64, error)
Update(ctx context.Context, table string, set, where map[string]any) (int64, error)
Delete(ctx context.Context, table string, where map[string]any) (int64, error)
Close()
}List options
type SortDirection int
const (
Ascending SortDirection = iota // default
Descending
)
type ListOptions struct {
Filter map[string]any // equality conditions, ANDed
SortBy string // field to order by ("" = unordered)
SortDir SortDirection
Limit int // 0 = no limit
Offset int // 0 = no skip; page-based pagination
After any // cursor: rows past this SortBy value
}See Listing & pagination.
Dialect
type Dialect interface {
Placeholder(n int) string
QuoteIdentifier(name string) string
ColumnTypeSQL(t ColumnType) string
DefaultSQL(d DefaultValue) string
}SQL backends only — see Query builders.
Errors
var ErrNotFound = errors.New("dbswitch: no rows found")
var ErrDuplicate = errors.New("dbswitch: unique constraint violation")
type DuplicateError struct {
Constraint string
}
func (e *DuplicateError) Error() string
func (e *DuplicateError) Is(target error) bool // matches ErrDuplicateQuery builders
func BuildCreateTable(d Dialect, t Table) string
func BuildInsert(d Dialect, table string, data map[string]any) (string, []any)
func BuildSelect(d Dialect, table string, where map[string]any) (string, []any)
func BuildUpdate(d Dialect, table string, set, where map[string]any) (string, []any, error)
func BuildDelete(d Dialect, table string, where map[string]any) (string, []any, error)
func BuildList(d Dialect, table string, opts ListOptions) (string, []any)
func BuildCount(d Dialect, table string, filter map[string]any) (string, []any)Package dbswitch/postgres
func Open(ctx context.Context, dsn string) (*DB, error)
type DB struct{ /* ... */ }
func (db *DB) Close()
func (db *DB) CreateTable(ctx context.Context, t dbswitch.Table) error
func (db *DB) Insert(ctx context.Context, table string, data map[string]any) error
func (db *DB) FindOne(ctx context.Context, table string, where map[string]any) (map[string]any, error)
func (db *DB) Find(ctx context.Context, table string, where map[string]any) ([]map[string]any, error)
func (db *DB) List(ctx context.Context, table string, opts dbswitch.ListOptions) ([]map[string]any, error)
func (db *DB) Count(ctx context.Context, table string, filter map[string]any) (int64, error)
func (db *DB) Update(ctx context.Context, table string, set, where map[string]any) (int64, error)
func (db *DB) Delete(ctx context.Context, table string, where map[string]any) (int64, error)
type Dialect struct{} // implements dbswitch.DialectPackage dbswitch/mongo
// Note the extra dbName argument (vs postgres.Open's single DSN).
func Open(ctx context.Context, uri, dbName string) (*Store, error)
type Store struct{ /* ... */ } // implements dbswitch.Store
func (s *Store) Close()
func (s *Store) CreateTable(ctx context.Context, t dbswitch.Table) error
func (s *Store) Insert(ctx context.Context, table string, data map[string]any) error
func (s *Store) FindOne(ctx context.Context, table string, where map[string]any) (map[string]any, error)
func (s *Store) Find(ctx context.Context, table string, where map[string]any) ([]map[string]any, error)
func (s *Store) List(ctx context.Context, table string, opts dbswitch.ListOptions) ([]map[string]any, error)
func (s *Store) Count(ctx context.Context, table string, filter map[string]any) (int64, error)
func (s *Store) Update(ctx context.Context, table string, set, where map[string]any) (int64, error)
func (s *Store) Delete(ctx context.Context, table string, where map[string]any) (int64, error)Package dbswitch/dynamodb
// optFns customize the underlying AWS SDK client — e.g. a custom endpoint for
// DynamoDB Local. Credentials/region come from the default AWS config chain.
func Open(ctx context.Context, optFns ...func(*dynamodbsdk.Options)) (*Store, error)
type Store struct{ /* ... */ } // implements dbswitch.Store
func (s *Store) Close() // no-op — no persistent connection to release
func (s *Store) CreateTable(ctx context.Context, t dbswitch.Table) error
func (s *Store) Insert(ctx context.Context, table string, data map[string]any) error
func (s *Store) FindOne(ctx context.Context, table string, where map[string]any) (map[string]any, error)
func (s *Store) Find(ctx context.Context, table string, where map[string]any) ([]map[string]any, error)
func (s *Store) List(ctx context.Context, table string, opts dbswitch.ListOptions) ([]map[string]any, error)
func (s *Store) Count(ctx context.Context, table string, filter map[string]any) (int64, error)
func (s *Store) Update(ctx context.Context, table string, set, where map[string]any) (int64, error)
func (s *Store) Delete(ctx context.Context, table string, where map[string]any) (int64, error)dynamodbsdk above is github.com/aws/aws-sdk-go-v2/service/dynamodb — see
Backends for a full custom-endpoint example.
Full generated reference: pkg.go.dev/github.com/anukool23/dbswitch.