store

package
v0.18.0 Latest Latest
Warning

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

Go to latest
Published: Aug 17, 2026 License: MIT Imports: 16 Imported by: 0

Documentation

Overview

Package store opens the team database and scopes every query to the authenticated user. It supports two dialects:

  • PostgreSQL (team mode): row-level security is the isolation backstop. Two roles are involved — the MIGRATION role (table owner) applies schema and policies (owners bypass RLS, which the SECURITY DEFINER membership helpers rely on), and the APPLICATION role (non-owner, default "omniagent_app") the running server connects as; ENABLE ROW LEVEL SECURITY binds every policy for it. Application queries run inside AsUser or AsSystem, which set the transaction-local GUCs the policies read.
  • SQLite (personal mode): a single implicit user, so there is nothing to isolate. The RLS function/policy/grant migrations are skipped and AsUser/AsSystem are pass-throughs — the same service layer runs against both dialects unchanged.

The raw database handle is unexported so no query path can bypass scoping.

Index

Constants

View Source
const DefaultAppRole = "omniagent_app"

DefaultAppRole is the non-owner role the application connects as.

Variables

This section is empty.

Functions

func Migrate

func Migrate(ctx context.Context, cfg Config) error

Migrate applies the schema and RLS policies using the owner connection: citext extension, Ent-generated tables, then the embedded SQL files (functions, policies, grants) in name order. Everything is idempotent and serialized by an advisory lock, so concurrent restarts are safe.

Types

type Config

type Config struct {
	// AppDSN is the application connection string (non-owner role).
	AppDSN string

	// MigrateDSN is the owner connection string used only by Migrate.
	// Empty falls back to AppDSN with a warning — acceptable for dev,
	// wrong for production (the app role must not own tables).
	MigrateDSN string

	// AppRole is the application role name granted table access by the
	// migrations. Defaults to DefaultAppRole.
	AppRole string

	// Logger defaults to slog.Default().
	Logger *slog.Logger
}

Config configures the team store.

type Store

type Store struct {
	// contains filtered or unexported fields
}

Store is the dialect-aware, scoped team database handle.

func Open

func Open(ctx context.Context, cfg Config) (*Store, error)

Open connects to the team database as the application role.

func (*Store) AsSystem

func (s *Store) AsSystem(ctx context.Context, fn func(ctx context.Context, tx *ent.Tx) error) error

AsSystem runs fn inside a transaction with the system (auth layer / agent) context: full access to auth tables and agent message inserts, per policy. Use only from the auth layer and agent plumbing — never for user requests.

func (*Store) AsUser

func (s *Store) AsUser(ctx context.Context, userID uuid.UUID, superadmin bool, fn func(ctx context.Context, tx *ent.Tx) error) error

AsUser runs fn inside a transaction scoped to the given user. Every query fn performs is subject to that user's row-level security view.

func (*Store) Close

func (s *Store) Close() error

Close closes the underlying database.

Jump to

Keyboard shortcuts

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