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 ¶
- func EnsureCollection(ctx context.Context, db *mongo.Database, name string, ...) error
- func InTx(ctx context.Context) bool
- type Options
- type Store
- func (s *Store) Accounts() store.AccountStore
- func (s *Store) Audit() store.AuditStore
- func (s *Store) Close() error
- func (s *Store) Database() *mongo.Database
- func (s *Store) Init(ctx context.Context) error
- func (s *Store) Invites() store.InviteStore
- func (s *Store) OAuth() store.OAuthStore
- func (s *Store) Passkeys() store.PasskeyStore
- func (s *Store) People() store.PersonStore
- func (s *Store) Sessions() store.SessionStore
- func (s *Store) Tx(ctx context.Context, fn func(ctx context.Context) error) error
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.
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 (*Store) Accounts ¶
func (s *Store) Accounts() store.AccountStore
Accounts returns the account store.
func (*Store) Close ¶
Close disconnects from Mongo when New made the client; a shared client (Options.DB) stays connected.
func (*Store) 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) Invites ¶
func (s *Store) Invites() store.InviteStore
Invites returns the invite store.
func (*Store) Passkeys ¶
func (s *Store) Passkeys() store.PasskeyStore
Passkeys returns the passkey store.
func (*Store) Sessions ¶
func (s *Store) Sessions() store.SessionStore
Sessions returns the session store.
func (*Store) Tx ¶
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.