database

package
v0.7.0 Latest Latest
Warning

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

Go to latest
Published: Jun 9, 2026 License: Apache-2.0 Imports: 10 Imported by: 0

Documentation

Overview

Package database provides a public API for OPA Control Plane database operations.

This package wraps the internal database layer, exposing Bundle, Source, and Source Data CRUD operations using typed config structs. External consumers work directly with config.Bundle and config.Source types.

Example usage:

db := database.New()
db.WithAuthorizer(myAuthorizer)
if err := db.InitDBWithConfig(ctx, &config.DatabaseConfig{
    SQL: &config.SQLDatabaseConfig{Driver: "sqlite3", DSN: "file::memory:?cache=shared"},
}); err != nil {
    log.Fatal(err)
}
defer db.CloseDB()

// Upsert a bundle
bundle := &config.Bundle{Name: "my-bundle", Requirements: config.Requirements{{Source: ptr("my-source")}}}
if err := db.UpsertBundle(ctx, "admin", "default", bundle); err != nil {
    log.Fatal(err)
}

// Get a bundle
b, err := db.GetBundle(ctx, "admin", "default", "my-bundle")

Index

Constants

This section is empty.

Variables

Re-export sentinel errors.

View Source
var ErrInvalidJSON = errors.New("invalid JSON")

ErrInvalidJSON indicates the provided JSON could not be deserialized into the expected type.

Functions

This section is empty.

Types

type Database

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

Database wraps the internal database layer, providing a typed API for external consumers.

func New

func New() *Database

New creates a new Database instance with default configuration.

func NewFromDB added in v0.7.0

func NewFromDB(db *sql.DB, driver string) (*Database, error)

NewFromDB creates a Database that shares the given *sql.DB connection pool. The driver parameter must match the underlying driver: "sqlite3", "sqlite", "postgres", "pgx", "cockroachdb", or "mysql". Use this to share a connection (and transactions) with an external caller.

func (*Database) CloseDB

func (d *Database) CloseDB()

CloseDB closes the underlying database connection.

func (*Database) DB

func (d *Database) DB() *sql.DB

DB returns the underlying *sql.DB for use with migration tooling.

func (*Database) DeleteBundle

func (d *Database) DeleteBundle(ctx context.Context, principal, tenant, name string) error

DeleteBundle deletes a bundle by name.

func (*Database) DeleteSource

func (d *Database) DeleteSource(ctx context.Context, principal, tenant, name string) error

DeleteSource deletes a source by name.

func (*Database) Dialect

func (d *Database) Dialect() (string, error)

Dialect returns the SQL dialect name ("sqlite", "postgresql", "mysql", "cockroachdb").

func (*Database) GetBundle

func (d *Database) GetBundle(ctx context.Context, principal, tenant, name string) (*config.Bundle, error)

GetBundle retrieves a bundle by name.

func (*Database) GetLatestBundleStatus added in v0.7.0

func (d *Database) GetLatestBundleStatus(ctx context.Context, principal, tenant, name string) (*config.BundleStatus, error)

GetLatestBundleStatus returns the most recent status record for the given tenant and bundle across all revisions.

func (*Database) GetSource

func (d *Database) GetSource(ctx context.Context, principal, tenant, name string) (*config.Source, error)

GetSource retrieves a source by name.

func (*Database) InitDB deprecated

func (d *Database) InitDB(ctx context.Context, rawConfig []byte) error

InitDB initializes the database connection from a raw root configuration.

Deprecated: prefer InitDBWithConfig for type-safe configuration.

func (*Database) InitDBWithConfig added in v0.7.0

func (d *Database) InitDBWithConfig(ctx context.Context, cfg *config.DatabaseConfig) error

InitDBWithConfig initializes the database connection from a typed DatabaseConfig.

func (*Database) ListBundleStatuses added in v0.7.0

func (d *Database) ListBundleStatuses(ctx context.Context, principal, tenant, name, revision string, limit int) ([]*config.BundleStatus, error)

ListBundleStatuses returns bundle status records for the given tenant and bundle, ordered by bundle status id DESC.

func (*Database) ListBundles

func (d *Database) ListBundles(ctx context.Context, principal, tenant string, limit int, cursor string) ([]*config.Bundle, string, error)

ListBundles lists bundles for a tenant, returning the bundles and the next cursor.

func (*Database) ListSources

func (d *Database) ListSources(ctx context.Context, principal, tenant string, limit int, cursor string) ([]*config.Source, string, error)

ListSources lists sources for a tenant, returning the sources and the next cursor.

func (*Database) SourcesDataDelete

func (d *Database) SourcesDataDelete(ctx context.Context, sourceName, path, principal, tenant string) error

SourcesDataDelete deletes source data at the given path.

func (*Database) SourcesDataGet

func (d *Database) SourcesDataGet(ctx context.Context, sourceName, path, principal, tenant string) (any, bool, error)

SourcesDataGet retrieves source data at the given path. Returns the data, whether it was found, and any error.

func (*Database) SourcesDataPatch

func (d *Database) SourcesDataPatch(ctx context.Context, sourceName, path, principal, tenant string, patchJSON []byte) error

SourcesDataPatch applies a JSON Patch (RFC 6902) to source data at the given path. The patchJSON must be a valid JSON Patch array.

func (*Database) SourcesDataPut

func (d *Database) SourcesDataPut(ctx context.Context, sourceName, path string, data any, principal, tenant string) error

SourcesDataPut stores source data at the given path.

func (*Database) Tenants

func (d *Database) Tenants(ctx context.Context) iter.Seq2[string, error]

Tenants iterates over all tenant names in the database.

func (*Database) UpsertBundle

func (d *Database) UpsertBundle(ctx context.Context, principal, tenant string, bundle *config.Bundle) error

UpsertBundle creates or updates a bundle.

func (*Database) UpsertPrincipal

func (d *Database) UpsertPrincipal(ctx context.Context, id, role, tenant string) error

UpsertPrincipal creates or updates a principal (user/service account).

func (*Database) UpsertSource

func (d *Database) UpsertSource(ctx context.Context, principal, tenant string, source *config.Source) error

UpsertSource creates or updates a source.

func (*Database) UpsertTenantWithPrincipal added in v0.7.0

func (d *Database) UpsertTenantWithPrincipal(ctx context.Context, tenantName, principalID, role string) error

UpsertTenantWithPrincipal creates a tenant and its associated principal in a single transaction. Both inserts are idempotent.

func (*Database) UpsertTenantWithPrincipalTx added in v0.7.0

func (d *Database) UpsertTenantWithPrincipalTx(ctx context.Context, tx *sql.Tx, tenantName, principalID, role string) error

UpsertTenantWithPrincipalTx performs the tenant+principal upsert within an existing *sql.Tx. Use this when you need the operation to participate in a transaction managed by the caller.

func (*Database) WithAccessFactory

func (d *Database) WithAccessFactory(af ext_authz.AccessFactory) *Database

WithAccessFactory sets the factory for creating access descriptors.

func (*Database) WithAuthorizer

func (d *Database) WithAuthorizer(a ext_authz.Authorizer) *Database

WithAuthorizer sets the authorizer for permission checks.

Jump to

Keyboard shortcuts

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