Skip to Content
gentlemans-stack
ReferenceConnectors

mongodb-connector

Source-only Mongoose connection adapter. createMongoDBConnector(logger, config) opens and closes a Mongoose Connection, exposes it to domain repositories, and reports normalized connection health.

API

This package owns only the MongoDB/Mongoose dependency set.

  • createMongoDBConnector (config: MongoDBConfig): owns the MongoDB connection lifecycle.
  • ./transaction: createRunInTransaction (type RunInTransaction).
  • ./mongo-rate-limit-provider: createMongoRateLimitProvider (config: MongoRateLimitProviderConfig), ensureRateLimitIndexes, getRateLimitModel; types RateLimitDocument, RateLimitMongooseModel.

Rate-limit provider

createMongoRateLimitProvider(connection, { now }) (@gentlemans-stack/mongodb-connector/mongo-rate-limit-provider) is the MongoDB store for rate-limit. Each hit is one atomic findOneAndUpdate, and a TTL index on expiresAt removes old windows. Call ensureRateLimitIndexes(connection) at startup so the TTL index exists before traffic arrives.

Transactions

createRunInTransaction(connection) (@gentlemans-stack/mongodb-connector/transaction) returns runInTransaction(work), which runs work(dbSession) in one session.withTransaction: pass dbSession to every write that must commit or roll back together. work can run again on a transient error, so write only through dbSession. Transactions need a replica set; on a standalone database it throws an error saying so and writes nothing. Tests use MongoMemoryReplSet. Local infra (LOCAL_INFRA) runs MongoDB as a single-node replica set, so transactions work from the host too.

Commands

pnpm nx run mongodb-connector:typecheck   # type-check the package
pnpm nx run mongodb-connector:test        # run the Vitest suite

On this page