db

package module
v1.2.0 Latest Latest
Warning

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

Go to latest
Published: Aug 22, 2025 License: Apache-2.0 Imports: 4 Imported by: 3

Documentation

Index

Constants

This section is empty.

Variables

This section is empty.

Functions

func FindIterator

func FindIterator[T any](
	c context.Context,
	instance Instance,
	errBuilder func() error,
	proto T,
	query Query,
) iter.Seq2[T, error]

FindIterator executes the given query and returns an iterator over results decoded into type T. Errors are wrapped in the iterator and surfaced during iteration.

func FindOne

func FindOne[T any](
	c context.Context,
	instance Instance,
	errBuilder func() error,
	proto T,
	query Query,
) (res_ T, found_ bool, err_ error)

FindOne executes the given query using the provided database instance and attempts to decode a single result into type T. Returns the decoded result, a flag indicating if a result was found, and an error if one occurred.

func WithTx

func WithTx[In any, Out any](
	getRepo func(c context.Context) Instance,
	cb func(c context.Context, in In) (Out, error),
) func(c context.Context, in In) (Out, error)

WithTx wraps a function that requires transactional execution, passing input and returning output. Begins a transaction, executes the callback with the transaction context, and commits or rolls back based on the result.

func WithTxNoInput

func WithTxNoInput[Out any](
	getRepo func(c context.Context) Instance,
	cb func(c context.Context) (Out, error),
) func(c context.Context) (Out, error)

WithTxNoInput wraps a function that requires transactional execution without input. Begins a transaction, executes the callback with the transaction context, and commits or rolls back based on the result.

Types

type Builder

type Builder interface {
	// NewSimpleInstance returns a direct, non-pooled database instance based on the given URI.
	NewSimpleInstance(c context.Context, uri string) (Instance, error)

	// NewPoolInstance returns a connection-pooled database instance.
	NewPoolInstance(c context.Context, uri string) (Instance, error)

	// GetPlatform retrieves the platform DB instance from context.
	GetPlatform(c context.Context) Instance

	// SetPlatform stores the platform DB instance into context.
	SetPlatform(c context.Context, instance Instance) context.Context

	// GetOrganization retrieves the organization DB instance from context.
	GetOrganization(c context.Context) Instance

	// SetOrganization stores the organization DB instance into context.
	SetOrganization(c context.Context, instance Instance) context.Context
}

Builder defines constructors and accessors for platform and organization-specific DB instances. Also provides factory methods for basic and pooled connection setups.

type ExecResult

type ExecResult interface {
	// RowsAffected returns the number of rows modified by the operation.
	RowsAffected() int64
}

ExecResult represents the result of a write operation (INSERT/UPDATE/DELETE).

type FS

type FS interface {
	fs.ReadDirFS
	fs.ReadFileFS
}

FS represents a virtual filesystem used for loading SQL migration files. Implements both ReadDirFS and ReadFileFS.

type Instance

type Instance interface {
	// IsValid returns true if the instance is initialized and usable.
	IsValid() bool

	// MigrateUp applies the given migration sources to the database.
	MigrateUp(c context.Context, fs ...FS) error

	// Begin starts a new transaction and returns a new context containing it.
	Begin(c context.Context) (context.Context, error)

	// Rollback rolls back the current transaction in context.
	Rollback(c context.Context) error

	// Commit commits the current transaction in context.
	Commit(c context.Context) error

	// Detach returns a new context with the transaction removed (e.g., for sub-contexts).
	Detach(c context.Context) context.Context

	// Close shuts down the instance and releases resources.
	Close(c context.Context) error

	// Exec executes a generic query and returns the result with affected rows.
	Exec(
		c context.Context,
		query Query,
	) (ExecResult, error)

	// FindOne executes a SELECT query and decodes the first row into proto.
	// Returns found = false if no rows match.
	FindOne(
		c context.Context,
		errBuilder func() error,
		proto any,
		query Query,
	) (res_ any, found_ bool, err_ error)

	// FindIterator executes a SELECT query and returns an iterator of results.
	FindIterator(
		c context.Context,
		errBuilder func() error,
		proto any,
		query Query,
	) iter.Seq2[any, error]
}

Instance abstracts a database connection or transaction. Supports basic operations, transaction lifecycle, and generic querying.

type MigrationClient

type MigrationClient interface {
	// Add registers a migration source (filesystem) for the given plugin.
	Add(pluginID string, fs FS)

	// AddGetter registers a getter migration source (filesystem) for the given plugin.
	AddGetter(
		pluginID string,
		getter func(c context.Context) FS,
	)

	// GetSystemOnly returns migrations registered by system-level plugins only.
	GetSystemOnly(c context.Context) []FS

	// Get returns all registered migrations from all plugins, including tenant-specific ones.
	Get(c context.Context) []FS
}

MigrationClient allows plugins to register and retrieve migration sources (FS).

type Query

type Query interface {
	// ToSql generates the final SQL query string and argument list.
	ToSql() (sql_ string, args_ []any, err_ error)
}

Query defines an interface for objects that can be translated into SQL + arguments.

Jump to

Keyboard shortcuts

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