sqlstore

package
v0.1.0 Latest Latest
Warning

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

Go to latest
Published: Oct 13, 2026 License: MIT Imports: 16 Imported by: 0

Documentation

Overview

Package sqlstore is connectionforge's [connectionforge.Store] and [connectionforge.Leaser] on SQLite or PostgreSQL, through Ent.

The host owns the database: it opens it, passes it to New, calls Store.Migrate, and applies its tenancy policy to Store.Tables. Every operation runs in its own transaction and calls the host's TxHook first, so a session setting such as the tenant for row-level security applies to it. Queries also filter by owner in code, so isolation does not depend on the hook alone.

This package imports no database driver; the host chooses one. For SQLite, give the store a database with one open connection or immediate transactions, so that concurrent writers queue instead of failing, and with foreign keys enabled, which Ent requires to migrate a SQLite database.

Index

Constants

View Source
const (
	ConnectionTable = "connectionforge_connections"
	CredentialTable = "connectionforge_credentials"
	GrantTable      = "connectionforge_grants"
	BindingTable    = "connectionforge_bindings"
	LeaseTable      = "connectionforge_leases"
)

Table names. Every table except LeaseTable has an owner_id column.

Variables

This section is empty.

Functions

This section is empty.

Types

type Dialect

type Dialect string

Dialect is a supported database.

const (
	SQLite   Dialect = dialect.SQLite
	Postgres Dialect = dialect.Postgres
)

Supported dialects.

type Option

type Option func(*Store)

Option configures a Store.

func WithClock

func WithClock(c cf.Clock) Option

WithClock sets the clock used for leases. The default is the system clock.

func WithTxHook

func WithTxHook(h TxHook) Option

WithTxHook sets the transaction hook.

type Store

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

Store is a connectionforge store in a SQL database. It is safe for concurrent use.

func New

func New(db *sql.DB, d Dialect, opts ...Option) (*Store, error)

New returns a store over db. The caller owns db and closes it. It does not create the schema: call Store.Migrate.

func (*Store) Acquire

func (s *Store) Acquire(ctx context.Context, key string, ttl time.Duration) (cf.Lease, error)

Acquire implements cf.Leaser. An expired lease is taken over by a conditional update, and a new one is created; a unique index on the name decides between concurrent creators. Expiry uses the store's clock, so processes sharing a database should have synchronized clocks.

func (*Store) ActiveCredential

func (s *Store) ActiveCredential(ctx context.Context, owner cf.OwnerID, id string) (cf.Credential, error)

ActiveCredential implements cf.CredentialStore.

func (*Store) CreateConnection

func (s *Store) CreateConnection(ctx context.Context, in cf.Connection) (cf.Connection, error)

CreateConnection implements cf.ConnectionStore.

func (*Store) DeleteConnection

func (s *Store) DeleteConnection(ctx context.Context, owner cf.OwnerID, id string) error

DeleteConnection implements cf.ConnectionStore.

func (*Store) DestroyCredentials

func (s *Store) DestroyCredentials(ctx context.Context, owner cf.OwnerID, id string) error

DestroyCredentials implements cf.CredentialStore.

func (*Store) GetConnection

func (s *Store) GetConnection(ctx context.Context, owner cf.OwnerID, id string) (cf.Connection, error)

GetConnection implements cf.ConnectionStore.

func (*Store) ListBindings

func (s *Store) ListBindings(ctx context.Context, owner cf.OwnerID, id string) ([]cf.Binding, error)

ListBindings implements cf.BindingStore.

func (*Store) ListConnections

func (s *Store) ListConnections(ctx context.Context, owner cf.OwnerID, f cf.Filter) ([]cf.Connection, error)

ListConnections implements cf.ConnectionStore.

func (*Store) ListGrants

func (s *Store) ListGrants(ctx context.Context, owner cf.OwnerID, id string) ([]cf.Grant, error)

ListGrants implements cf.GrantStore.

func (*Store) Migrate

func (s *Store) Migrate(ctx context.Context) error

Migrate creates or updates the schema to match this version of the store. It is idempotent and runs outside the transaction hook, so the host runs it with a role allowed to change the schema.

func (*Store) PutBinding

func (s *Store) PutBinding(ctx context.Context, b cf.Binding) error

PutBinding implements cf.BindingStore.

func (*Store) PutGrant

func (s *Store) PutGrant(ctx context.Context, g cf.Grant) error

PutGrant implements cf.GrantStore.

func (*Store) RemoveBinding

func (s *Store) RemoveBinding(ctx context.Context, owner cf.OwnerID, id string, cons cf.Consumer, purpose string) error

RemoveBinding implements cf.BindingStore.

func (*Store) RemoveGrant

func (s *Store) RemoveGrant(ctx context.Context, owner cf.OwnerID, id string, p cf.Principal) error

RemoveGrant implements cf.GrantStore.

func (*Store) SwapCredential

func (s *Store) SwapCredential(ctx context.Context, expectActive int, next cf.Credential) error

SwapCredential implements cf.CredentialStore. The unique index on (owner, connection, version) decides between concurrent swaps.

func (*Store) Tables

func (s *Store) Tables() []string

Tables returns the tables that hold owned records, each with an owner_id column, for the host's tenancy policy. The lease table is not among them.

func (*Store) UpdateConnection

func (s *Store) UpdateConnection(ctx context.Context, in cf.Connection) (cf.Connection, error)

UpdateConnection implements cf.ConnectionStore.

type TxHook

type TxHook func(ctx context.Context, tx *sql.Tx) error

TxHook runs at the start of every transaction, before any query. A host uses it to set session variables, for example the tenant for row-level security. An error aborts the operation.

Directories

Path Synopsis
ent
schema
Package schema is the Ent schema of connectionforge's SQL store.
Package schema is the Ent schema of connectionforge's SQL store.

Jump to

Keyboard shortcuts

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