data

package
v1.1.1 Latest Latest
Warning

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

Go to latest
Published: Feb 20, 2026 License: MIT Imports: 13 Imported by: 0

Documentation

Index

Constants

This section is empty.

Variables

View Source
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

func IsValidIdentifier(name string) bool

IsValidIdentifier checks if a string is a valid SQL identifier

func ListenDB

func ListenDB(ctx context.Context, databaseURL string, broker *realtime.Broker)

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

type DB struct {
	Pool *pgxpool.Pool
}

DB wraps the PostgreSQL connection pool

func Connect

func Connect(ctx context.Context, databaseURL string) (*DB, error)

Connect establishes a connection pool to PostgreSQL

func (*DB) AddColumn added in v1.1.1

func (db *DB) AddColumn(ctx context.Context, tableName string, field FieldSchema) (string, error)

AddColumn adds a new column to an existing table

func (*DB) BulkInsertAuditLogs added in v1.1.1

func (db *DB) BulkInsertAuditLogs(ctx context.Context, logs []AuditLog) error

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) Close

func (db *DB) Close()

Close gracefully closes the database connection pool

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

func (db *DB) DeleteTable(ctx context.Context, tableName string) error

DeleteTable drops an existing table

func (*DB) EnableRLS added in v1.1.1

func (db *DB) EnableRLS(ctx context.Context, tx pgx.Tx, tableName string) error

EnableRLS enables Row Level Security on a 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

func (db *DB) GetTableColumns(ctx context.Context, tableName string) (map[string]bool, error)

GetTableColumns returns a map of column names for a specific table

func (*DB) GetTableSchema

func (db *DB) GetTableSchema(ctx context.Context, tableName string) ([]FieldSchema, error)

GetTableSchema fetches the schema of a table from information_schema

func (*DB) HasColumn added in v1.1.1

func (db *DB) HasColumn(ctx context.Context, tableName, columnName string) bool

HasColumn checks if a specific table has a specific column

func (*DB) Health

func (db *DB) Health(ctx context.Context) error

Health checks if the database connection is healthy

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

func (db *DB) InsertAuditLog(ctx context.Context, log AuditLog) error

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

func (db *DB) ListSchemas(ctx context.Context) ([]string, error)

ListSchemas returns a list of all schema names in the database

func (*DB) ListTables

func (db *DB) ListTables(ctx context.Context) ([]string, error)

ListTables returns a list of table names in the public schema

func (*DB) RunMigrations

func (db *DB) RunMigrations(ctx context.Context) error

func (*DB) UpdateRecord added in v1.1.1

func (db *DB) UpdateRecord(ctx context.Context, collectionName, id string, data map[string]any, ownerField, ownerID string) error

UpdateRecord updates a record, respecting RLS

func (*DB) WithTransactionAndRLS added in v1.1.1

func (db *DB) WithTransactionAndRLS(ctx context.Context, fn func(tx pgx.Tx) error) error

WithTransactionAndRLS wraps a query in a transaction that injects RLS context. If RLS context is found in the context, it's injected automatically.

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

type ListRecordsResult struct {
	Data  []map[string]any
	Total int64
}

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

type RLSContext struct {
	UserID  string
	Email   string
	Roles   []string
	IsAdmin bool
}

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

type TableRelationship added in v1.1.1

type TableRelationship struct {
	FromTable string `json:"from_table"`
	FromCol   string `json:"from_col"`
	ToTable   string `json:"to_table"`
	ToCol     string `json:"to_col"`
}

Jump to

Keyboard shortcuts

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