Documentation
¶
Index ¶
- Variables
- func BuildCreateTableSQL(tableName string, schema []FieldSchema) (string, error)
- func IsValidIdentifier(name string) bool
- func ListenDB(ctx context.Context, databaseURL string, broker *realtime.Broker)
- func NewContext(ctx context.Context, rls RLSContext) context.Context
- type AuditLog
- type DB
- func (db *DB) AddColumn(ctx context.Context, tableName string, field FieldSchema) (string, error)
- func (db *DB) BulkInsertAuditLogs(ctx context.Context, logs []AuditLog) error
- func (db *DB) BulkInsertRecord(ctx context.Context, collectionName string, records []map[string]any) error
- func (db *DB) Close()
- func (db *DB) CreatePolicy(ctx context.Context, tx pgx.Tx, tableName, policyName, rule string) error
- func (db *DB) DeleteColumn(ctx context.Context, tableName string, columnName string) (string, error)
- func (db *DB) DeleteRecord(ctx context.Context, collectionName, id string, ownerField, ownerID string) error
- func (db *DB) DeleteTable(ctx context.Context, tableName string) error
- func (db *DB) EnableRLS(ctx context.Context, tx pgx.Tx, tableName string) error
- func (db *DB) GetDatabaseSchema(ctx context.Context) (*DatabaseSchema, error)
- func (db *DB) GetRecord(ctx context.Context, collectionName, id string, ownerField, ownerID string) (map[string]any, error)
- func (db *DB) GetTableColumns(ctx context.Context, tableName string) (map[string]bool, error)
- func (db *DB) GetTableSchema(ctx context.Context, tableName string) ([]FieldSchema, error)
- func (db *DB) HasColumn(ctx context.Context, tableName, columnName string) bool
- func (db *DB) Health(ctx context.Context) error
- func (db *DB) InjectUserContext(ctx context.Context, rls RLSContext) error
- func (db *DB) InsertAuditLog(ctx context.Context, log AuditLog) error
- func (db *DB) InsertRecord(ctx context.Context, collectionName string, data map[string]any) (string, error)
- func (db *DB) ListRecords(ctx context.Context, collectionName string, filters map[string][]string, ...) (*ListRecordsResult, error)
- func (db *DB) ListSchemas(ctx context.Context) ([]string, error)
- func (db *DB) ListTables(ctx context.Context) ([]string, error)
- func (db *DB) RunMigrations(ctx context.Context) error
- func (db *DB) UpdateRecord(ctx context.Context, collectionName, id string, data map[string]any, ...) error
- func (db *DB) WithTransactionAndRLS(ctx context.Context, fn func(tx pgx.Tx) error) error
- type DatabaseSchema
- type EmbeddedDB
- type FieldSchema
- type ListRecordsResult
- type QueryBuilder
- func (qb *QueryBuilder) BuildCount() (string, []any)
- func (qb *QueryBuilder) BuildSelect() (string, []any)
- func (qb *QueryBuilder) Order(orderBy string) *QueryBuilder
- func (qb *QueryBuilder) Paginate(limit, offset int) *QueryBuilder
- func (qb *QueryBuilder) Where(column, operator string, value any) *QueryBuilder
- type RLSContext
- type TableDefinition
- type TableRelationship
Constants ¶
This section is empty.
Variables ¶
var TypeMapping = map[string]string{
"int2": "INT2",
"int4": "INT4",
"int8": "INT8",
"float4": "FLOAT4",
"float8": "FLOAT8",
"numeric": "NUMERIC",
"json": "JSON",
"jsonb": "JSONB",
"text": "TEXT",
"varchar": "VARCHAR",
"uuid": "UUID",
"date": "DATE",
"time": "TIME",
"timetz": "TIMETZ",
"timestamp": "TIMESTAMP",
"timestamptz": "TIMESTAMPTZ",
"bool": "BOOL",
"boolean": "BOOLEAN",
"bytea": "BYTEA",
"inet": "INET",
"cidr": "CIDR",
"macaddr": "MACADDR",
"interval": "INTERVAL",
"money": "MONEY",
"text_array": "TEXT[]",
"int_array": "INT4[]",
"number": "INT4",
"integer": "INT4",
"string": "TEXT",
}
TypeMapping maps OzyBase types to PostgreSQL types
Functions ¶
func BuildCreateTableSQL ¶
func BuildCreateTableSQL(tableName string, schema []FieldSchema) (string, error)
BuildCreateTableSQL generates a CREATE TABLE statement from a schema definition
func IsValidIdentifier ¶ added in v1.1.1
IsValidIdentifier checks if a string is a valid SQL identifier
func ListenDB ¶
ListenDB connects to Postgres using a dedicated connection and listens for notifications
func NewContext ¶ added in v1.1.1
func NewContext(ctx context.Context, rls RLSContext) context.Context
NewContext returns a new context with the RLS information
Types ¶
type AuditLog ¶ added in v1.1.1
type AuditLog struct {
UserID *string
IP string
Method string
Path string
Status int
Latency int64
Country string
City string
UserAgent string
CreatedAt time.Time
}
AuditLog represents a simplified log entry for database persistence
type DB ¶
DB wraps the PostgreSQL connection pool
func (*DB) BulkInsertAuditLogs ¶ added in v1.1.1
BulkInsertAuditLogs efficiently inserts multiple logs using CopyFrom
func (*DB) BulkInsertRecord ¶ added in v1.1.1
func (db *DB) BulkInsertRecord(ctx context.Context, collectionName string, records []map[string]any) error
BulkInsertRecord inserts multiple records using high-performance pgx.CopyFrom
func (*DB) CreatePolicy ¶ added in v1.1.1
func (db *DB) CreatePolicy(ctx context.Context, tx pgx.Tx, tableName, policyName, rule string) error
CreatePolicy creates a simple RLS policy. For now, it target the 'auth.uid() = [column]' pattern.
func (*DB) DeleteColumn ¶ added in v1.1.1
func (db *DB) DeleteColumn(ctx context.Context, tableName string, columnName string) (string, error)
DeleteColumn removes a column from an existing table
func (*DB) DeleteRecord ¶ added in v1.1.1
func (db *DB) DeleteRecord(ctx context.Context, collectionName, id string, ownerField, ownerID string) error
DeleteRecord soft-deletes a record, respecting RLS
func (*DB) DeleteTable ¶ added in v1.1.1
DeleteTable drops an existing table
func (*DB) GetDatabaseSchema ¶ added in v1.1.1
func (db *DB) GetDatabaseSchema(ctx context.Context) (*DatabaseSchema, error)
GetDatabaseSchema fetches the full schema for visualization
func (*DB) GetRecord ¶
func (db *DB) GetRecord(ctx context.Context, collectionName, id string, ownerField, ownerID string) (map[string]any, error)
GetRecord fetches a single record, respecting RLS
func (*DB) GetTableColumns ¶ added in v1.1.1
GetTableColumns returns a map of column names for a specific table
func (*DB) GetTableSchema ¶
GetTableSchema fetches the schema of a table from information_schema
func (*DB) InjectUserContext ¶ added in v1.1.1
func (db *DB) InjectUserContext(ctx context.Context, rls RLSContext) error
InjectUserContext sets local variables in the current transaction for RLS policies to use.
func (*DB) InsertAuditLog ¶ added in v1.1.1
InsertAuditLog inserts a single audit log
func (*DB) InsertRecord ¶
func (db *DB) InsertRecord(ctx context.Context, collectionName string, data map[string]any) (string, error)
InsertRecord inserts a record into a dynamic collection table
func (*DB) ListRecords ¶
func (db *DB) ListRecords(ctx context.Context, collectionName string, filters map[string][]string, orderBy string, limit, offset int) (*ListRecordsResult, error)
ListRecords fetches all records with filters and sorting, respecting RLS if configured in DB. This implementation uses a structured QueryBuilder for improved maintainability.
func (*DB) ListSchemas ¶ added in v1.1.1
ListSchemas returns a list of all schema names in the database
func (*DB) ListTables ¶
ListTables returns a list of table names in the public schema
type DatabaseSchema ¶ added in v1.1.1
type DatabaseSchema struct {
Tables []TableDefinition `json:"tables"`
Relationships []TableRelationship `json:"relationships"`
}
DatabaseSchema represents the full schema of the database
type EmbeddedDB ¶ added in v1.1.1
type EmbeddedDB struct {
// contains filtered or unexported fields
}
EmbeddedDB handles the lifecycle of an embedded PostgreSQL instance
func NewEmbeddedDB ¶ added in v1.1.1
func NewEmbeddedDB() *EmbeddedDB
NewEmbeddedDB creates a new embedded PostgreSQL instance configuration
func (*EmbeddedDB) GetConnectionString ¶ added in v1.1.1
func (e *EmbeddedDB) GetConnectionString() string
GetConnectionString returns the DSN for the embedded instance
func (*EmbeddedDB) Start ¶ added in v1.1.1
func (e *EmbeddedDB) Start() error
Start initializes and starts the embedded PostgreSQL engine
func (*EmbeddedDB) Stop ¶ added in v1.1.1
func (e *EmbeddedDB) Stop() error
Stop gracefully shuts down the embedded PostgreSQL engine
type FieldSchema ¶
type FieldSchema struct {
Name string `json:"name"`
Type string `json:"type"`
Required bool `json:"required,omitempty"`
Unique bool `json:"unique,omitempty"`
IsPrimary bool `json:"is_primary,omitempty"`
Default any `json:"default,omitempty"`
References string `json:"references,omitempty"` // format: "table.column"
}
FieldSchema represents a single field in a collection schema
type ListRecordsResult ¶ added in v1.1.1
ListRecordsResult encapsulates the output of a paginated list operation
type QueryBuilder ¶ added in v1.1.1
type QueryBuilder struct {
// contains filtered or unexported fields
}
QueryBuilder provides a structured and safe way to build SQL queries
func NewQueryBuilder ¶ added in v1.1.1
func NewQueryBuilder(tableName string) *QueryBuilder
NewQueryBuilder initializes a builder for a specific table
func (*QueryBuilder) BuildCount ¶ added in v1.1.1
func (qb *QueryBuilder) BuildCount() (string, []any)
BuildCount generates a count query based on the current filters
func (*QueryBuilder) BuildSelect ¶ added in v1.1.1
func (qb *QueryBuilder) BuildSelect() (string, []any)
BuildSelect generates the final SELECT query and its arguments
func (*QueryBuilder) Order ¶ added in v1.1.1
func (qb *QueryBuilder) Order(orderBy string) *QueryBuilder
Order sets the sorting rule
func (*QueryBuilder) Paginate ¶ added in v1.1.1
func (qb *QueryBuilder) Paginate(limit, offset int) *QueryBuilder
Paginate sets limit and offset
func (*QueryBuilder) Where ¶ added in v1.1.1
func (qb *QueryBuilder) Where(column, operator string, value any) *QueryBuilder
Where adds a refined filter to the query
type RLSContext ¶ added in v1.1.1
RLSContext holds security-related information to be injected into Postgres
func FromContext ¶ added in v1.1.1
func FromContext(ctx context.Context) (RLSContext, bool)
FromContext retrieves RLS information from the context
type TableDefinition ¶ added in v1.1.1
type TableDefinition struct {
Name string `json:"name"`
IsSystem bool `json:"is_system"`
Columns []FieldSchema `json:"columns"`
}