db

package
v0.19.0 Latest Latest
Warning

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

Go to latest
Published: Nov 20, 2026 License: AGPL-3.0 Imports: 20 Imported by: 2

Documentation

Index

Constants

View Source
const DatasourceEnvVar = "DATASOURCE"

DatasourceEnvVar is the environment variable that selects the storage backend.

Variables

View Source
var Database *mongo.Database

Database is the MongoDB database instance

View Source
var MongoClient *mongo.Client

MongoClient is the MongoDB client

View Source
var PostgresPool *pgxpool.Pool

PostgresPool is the process-wide pgx pool, set by Open once the whole open sequence has succeeded (never by connectPostgres itself, so a failed Open can't leave it pointing at a closed pool). It mirrors MongoClient/Database's package-level exposure so existing callers used to reaching into db.Database for Mongo have the equivalent for Postgres once a service opts in.

Functions

func ConnectMongo

func ConnectMongo() (*mongo.Client, error)

ConnectMongo initializes and returns a MongoDB client, publishing it as MongoClient / Database.

It panics on a missing DATABASE_NAME or a failed connect/ping, as it always has, and returns the other errors (an unreadable CA certificate). New code should go through Open, which reports all of them as errors.

func UserBSONSize

func UserBSONSize(ctx context.Context, db *mongo.Database, spec CollectionSpec, userID id.ID) (int64, error)

UserBSONSize returns the total BSON document size in bytes for documents in the given collection that match the userID on the specified field. A zero or non-ObjectID userID is an error wrapping id.ErrInvalid.

func UserBSONSizes

func UserBSONSizes(
	ctx context.Context,
	db *mongo.Database,
	specs []CollectionSpec,
	userID id.ID,
) (total int64, perCollection map[string]int64, partial bool, errMsg string, err error)

UserBSONSizes concurrently measures per-collection BSON byte usage for a user across all provided specs. Per-collection failures are captured in errMsg and set partial=true rather than aborting the whole call. A zero or non-ObjectID userID is not a per-collection failure: it is returned as err (wrapping id.ErrInvalid), never as a successful 0-byte measurement.

Types

type Backend

type Backend string

Backend identifies which storage engine a service is configured to use.

const (
	// BackendMongo is the default backend — every existing deployment,
	// which sets DatasourceEnvVar to nothing at all, keeps using Mongo
	// exactly as it always has.
	BackendMongo Backend = "mongo"
	// BackendPostgres opts a service into the PostgreSQL datastore.
	BackendPostgres Backend = "postgres"
)

func ActiveBackend

func ActiveBackend() Backend

ActiveBackend reports the backend the most recent successful Open call selected, so a service's health endpoint can report which one is live without threading a *Datastore through to the handler.

type CollectionSpec

type CollectionSpec struct {
	Name        string
	UserIDField string
}

CollectionSpec names a MongoDB collection and the field used to scope by user.

type Datastore

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

Datastore is the open handle to whichever backend Open selected.

Exactly one of the backend-specific handles (Mongo/Postgres) is ever non-nil, matching Kind() — Open opens exactly one backend and never both. Generic code (health checks, graceful shutdown) should use Kind, Ping and Close and never branch on which handle is set; a repository implemented against one specific backend calls Mongo() or Postgres() directly and can assume the other returns nil.

func Open

func Open(ctx context.Context) (*Datastore, error)

Open resolves DatasourceEnvVar and opens EXACTLY ONE backend accordingly, pinging it once before returning (except under ENV=test for Mongo, which never dials anything). Every failure — missing configuration, connect, ping — is returned as an error; Open never panics.

There is deliberately NO fallback path here, structurally, not just by convention: the switch below has one case per backend, each of which calls only that backend's own connector. If postgres is selected and its configuration is incomplete, this returns an error from the postgres case and NEVER reaches, or falls through to, the mongo case — even when Mongo's environment variables also happen to be fully present and valid. A silent fallback would let a service THINK it's running on the backend it asked for while actually still writing to the other one, which is exactly the failure mode a staged migration cannot tolerate.

func (*Datastore) Close

func (d *Datastore) Close()

Close releases the active backend's resources. Safe to call on a Datastore whose handle is nil (ENV=test).

func (*Datastore) Kind

func (d *Datastore) Kind() Backend

Kind reports which backend this Datastore is backed by. A service's health endpoint can call this to report which storage engine is live.

func (*Datastore) Mongo

func (d *Datastore) Mongo() *mongo.Database

Mongo returns the *mongo.Database handle, or nil if this Datastore isn't Mongo-backed.

func (*Datastore) Ping

func (d *Datastore) Ping(ctx context.Context) error

Ping verifies connectivity to the active backend.

func (*Datastore) Postgres

func (d *Datastore) Postgres() *pgxpool.Pool

Postgres returns the *pgxpool.Pool handle, or nil if this Datastore isn't Postgres-backed.

Jump to

Keyboard shortcuts

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