dbservice

package
v0.7.5 Latest Latest
Warning

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

Go to latest
Published: Sep 1, 2026 License: BSD-2-Clause Imports: 8 Imported by: 0

Documentation

Overview

Package dbservice holds the one piece of db.Service setup genuinely shared across every service (apigw, verifier, ...): deciding, from cfg.Common.SQL.Backend, whether to connect to Mongo or to a SQL backend (via pkg/sqlstore, running schema migrations), and handing back whichever connection resulted. It intentionally stops there - each service's own store wiring (which collections/tables it needs, what indexes/schemas they use) stays in that service's own db package, not here.

This package sits above both pkg/model and pkg/trace (it imports both), which is safe: pkg/trace already imports pkg/model (for otel setup), so pkg/model itself cannot import pkg/trace back - but a leaf package like this one, sitting a layer above both, introduces no cycle.

Index

Constants

This section is empty.

Variables

This section is empty.

Functions

This section is empty.

Types

type Connection

type Connection struct {
	MongoClient *mongo.Client
	SQLDB       *sqlx.DB
	Dialect     sqlstore.Dialect
}

Connection is the result of Connect: exactly one of MongoClient or SQLDB is set, matching db.Service's existing MongoClient/SQLDB field pair. Dialect is nil unless SQLDB is set.

func Connect

func Connect(ctx context.Context, cfg *model.Cfg, tracer *trace.Tracer, spanName string) (*Connection, error)

Connect selects a backend from cfg.Common.SQL.Backend ("postgres" or "mariadb" connect to the corresponding relational database via pkg/sqlstore, running schema migrations at startup; anything else, including unset, keeps the default MongoDB-backed behavior) and connects to it. spanName names the tracing span opened around the whole connect call (whichever backend is selected), since each caller already gives it its own name (e.g. "apigw:db:connect" vs "verifier:db:connect") - covering only the Mongo branch would silently drop startup connect/ping/ migration work from traces whenever a deployment runs on SQL instead.

Returns an error up front if Common.HA.Enable is set alongside a SQL backend: HA-mode caching (pkg/cache's Mongo-backed AuthContext/generic caches) is wired up by each service's own cache.Service.New using this same Connection's MongoClient, which is nil when the SQL backend is selected - without this guard, that would surface much later as a nil *mongo.Client reaching pkg/cache instead of a clear startup error. Combining SQL storage with HA caching isn't supported yet.

Jump to

Keyboard shortcuts

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