database

package
v0.5.0 Latest Latest
Warning

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

Go to latest
Published: Mar 28, 2026 License: MIT Imports: 9 Imported by: 0

Documentation

Overview

Package database provides database abstraction for SQLite and PostgreSQL.

Index

Constants

View Source
const (
	// EnableRLSAgents enables RLS on the agents table.
	EnableRLSAgents = `ALTER TABLE agents ENABLE ROW LEVEL SECURITY`

	// CreatePolicyAgents creates a tenant isolation policy on agents.
	CreatePolicyAgents = `` /* 203-byte string literal not displayed */

	// EnableRLSEvents enables RLS on the events table.
	EnableRLSEvents = `ALTER TABLE events ENABLE ROW LEVEL SECURITY`

	// CreatePolicyEvents creates a tenant isolation policy on events.
	CreatePolicyEvents = `` /* 203-byte string literal not displayed */

)

RLS policy SQL statements for PostgreSQL.

Variables

This section is empty.

Functions

func ApplyRLSPolicies

func ApplyRLSPolicies(ctx context.Context, db *sql.DB) error

ApplyRLSPolicies applies Row-Level Security policies to PostgreSQL tables. This should be called after schema migration. The db parameter should be the underlying *sql.DB from the Ent client.

func DropRLSPolicies

func DropRLSPolicies(ctx context.Context, db *sql.DB) error

DropRLSPolicies removes Row-Level Security policies from PostgreSQL tables. This can be useful for testing or migration rollback.

Types

type Config

type Config struct {
	// Driver is the database driver type ("sqlite" or "postgres").
	Driver DriverType

	// DSN is the data source name (connection string).
	// For SQLite: file path or :memory:
	// For Postgres: postgres://user:pass@host:port/dbname?sslmode=disable
	DSN string

	// MultiTenant enables multi-tenancy mode (requires tenant_id per request).
	MultiTenant bool

	// UseRLS enables PostgreSQL Row-Level Security (PostgreSQL only).
	// When enabled, RLS policies are applied after schema creation.
	UseRLS bool
}

Config holds database configuration.

type DriverType

type DriverType string

DriverType identifies the database driver.

const (
	// DriverSQLite uses modernc.org/sqlite (pure Go).
	DriverSQLite DriverType = "sqlite"
	// DriverPostgres uses lib/pq for PostgreSQL.
	DriverPostgres DriverType = "postgres"
)

type OpenResult

type OpenResult struct {
	// Client is the Ent client.
	Client *ent.Client

	// DB is the underlying *sql.DB for direct database operations.
	// Used for applying RLS policies on PostgreSQL.
	DB *sql.DB
}

OpenResult contains the result of opening a database connection.

func Open

func Open(cfg Config) (*OpenResult, error)

Open creates an Ent client for the configured database.

type RLSDriver

type RLSDriver struct {
	*entsql.Driver
}

RLSDriver wraps an Ent SQL driver to set the tenant context on each transaction. This enables PostgreSQL Row-Level Security by setting app.current_tenant.

func NewRLSDriver

func NewRLSDriver(drv *entsql.Driver) *RLSDriver

NewRLSDriver wraps an Ent SQL driver with RLS tenant context support.

func (*RLSDriver) Tx

func (d *RLSDriver) Tx(ctx context.Context) (dialect.Tx, error)

Tx starts a new transaction and sets the tenant context using SET LOCAL.

type RLSTx

type RLSTx struct {
	*entsql.Tx
	// contains filtered or unexported fields
}

RLSTx wraps an Ent transaction to set tenant context on first operation.

func (*RLSTx) Exec

func (tx *RLSTx) Exec(ctx context.Context, query string, args, v any) error

Exec executes a query with tenant context.

func (*RLSTx) Query

func (tx *RLSTx) Query(ctx context.Context, query string, args, v any) error

Query executes a query with tenant context.

Jump to

Keyboard shortcuts

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