mongostore

package
v0.1.0 Latest Latest
Warning

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

Go to latest
Published: Nov 24, 2026 License: MIT Imports: 13 Imported by: ? modules, 0 packages

The first number is the count of modules that contain a package that imports this one. The module count is a better indicator of popularity than the package count. A module with 100 importing packages is counted only once instead of 100 times, reflecting the fact that the choice to use the package was likely made only once, not 100 times.
A '?' means that the value hasn't been computed yet.

Documentation

Overview

Package mongostore implements store.Store on MongoDB. It is Family Hub's internal/store moved here, and it reads and writes Family Hub's live database unchanged: the same collections, documents, _id forms, indexes and TTLs (docs/PLAN.md §4.2.1).

The store assumes a replica set, so Tx and the delete cascades run in real transactions. Mongo keeps times with millisecond precision, so a time read back is the time written, truncated to the millisecond and in UTC.

Transactions travel in the context: inside Tx the context carries the *mongo.Session (mongo.NewSessionContext), so any collection call an application makes with that context, on the same client, joins the transaction. An application keeps its own collections in the same database (Database) and creates them with EnsureCollection.

Conventions shared by every sub-store:

  • a write to one entity that does not exist returns core.ErrNotFound, except deletes of sessions and the bulk operations (DeleteByPerson, RevokeFamily, ...), which are idempotent;
  • inserting an id that exists returns core.ErrConflict, except the Put methods, which replace;
  • Revoke* methods are idempotent: the first revocation time wins.

Index

Constants

This section is empty.

Variables

This section is empty.

Functions

func EnsureCollection

func EnsureCollection(ctx context.Context, db *mongo.Database, name string, indexes ...mongo.IndexModel) error

EnsureCollection creates the collection name in db, if it does not exist, and the indexes. Creating an index that exists with the same keys and options is a no-op, so it is safe at every start; an index whose name exists with other options is an error. Collections must exist before a transaction writes to them.

func InTx

func InTx(ctx context.Context) bool

InTx reports whether ctx carries a session with a running transaction, as the context inside Tx does.

Types

type Options

type Options struct {
	// URI is the connection string. It carries credentials, so it is never logged and never part of an error.
	URI string
	// Database is the database name inside Mongo; required with URI.
	Database string
	// DB is an existing database whose client the application owns and shares (Family Hub). Close leaves that
	// client connected, and AppName, ConnectTimeout and OpTimeout do not apply: the client's own settings do.
	DB *mongo.Database
	// AppName is the client name the server logs; default "mcp-gatekeeper".
	AppName string
	// ConnectTimeout bounds finding the server, both at start and for every operation that waits for one; default 10s.
	ConnectTimeout time.Duration
	// OpTimeout bounds one operation, or one whole transaction, whose context has no deadline of its own; default 30s.
	OpTimeout time.Duration
	// Cascades run inside the transaction of an account or person delete, after the store's own deletes.
	Cascades []store.Cascade
	// Logger receives the migration log; nil discards it.
	Logger *slog.Logger
}

Options configures a Store. Give either URI and Database, or DB.

type Store

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

Store is store.Store on MongoDB.

func New

func New(o Options) (*Store, error)

New prepares a Store without contacting the server; call Init before use.

func Open

func Open(ctx context.Context, o Options) (*Store, error)

Open is New followed by Init; on an Init error the client New made is closed.

func (*Store) Accounts

func (s *Store) Accounts() store.AccountStore

Accounts returns the account store.

func (*Store) Audit

func (s *Store) Audit() store.AuditStore

Audit returns the audit store.

func (*Store) Close

func (s *Store) Close() error

Close disconnects from Mongo when New made the client; a shared client (Options.DB) stays connected.

func (*Store) Database

func (s *Store) Database() *mongo.Database

Database returns the database the store works in, for an application's own collections. Calls made on it with a context from Tx join the transaction.

func (*Store) Init

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

Init checks the server answers and applies the migrations.

func (*Store) Invites

func (s *Store) Invites() store.InviteStore

Invites returns the invite store.

func (*Store) OAuth

func (s *Store) OAuth() store.OAuthStore

OAuth returns the OAuth store.

func (*Store) Passkeys

func (s *Store) Passkeys() store.PasskeyStore

Passkeys returns the passkey store.

func (*Store) People

func (s *Store) People() store.PersonStore

People returns the person store.

func (*Store) Sessions

func (s *Store) Sessions() store.SessionStore

Sessions returns the session store.

func (*Store) Tx

func (s *Store) Tx(ctx context.Context, fn func(ctx context.Context) error) error

Tx runs fn in one transaction; an error from fn rolls it back and is returned. The context fn gets carries the session. When ctx already carries a running transaction of this store's client, fn joins it (there are no savepoints: an error a nested fn returns fails the outer transaction only if the outer fn returns it too).

The driver may run fn again after a transient transaction error (a write conflict, a primary change), so fn and every Cascade must keep their side effects inside the transaction.

Jump to

Keyboard shortcuts

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