Documentation
¶
Overview ¶
Package database provides database abstraction for SQLite and PostgreSQL.
Index ¶
Constants ¶
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 ¶
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.
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 ¶
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 ¶
NewRLSDriver wraps an Ent SQL driver with RLS tenant context support.