MongoRepository
Defined in: mongo-repository.ts:21
A Mantle Repository<T> implementation backed by a MongoDB collection, using the
official mongodb driver directly — no Mongoose, consistent with the
query-builder-not-ORM approach @mantlejs/knex takes for SQL.
Mongo’s native _id (ObjectId) is exposed as id: string (hex) at the
Repository<T> boundary — callers never see a raw ObjectId. Dot-path where keys
and $contains are supported natively; $like/$ilike/$notlike are not — use
the raw collection escape hatch with $regex for pattern matching.
Extended by
Section titled “Extended by”Type Parameters
Section titled “Type Parameters”T extends Record<string, unknown>
The entity shape. Must extend Record<string, unknown>.
D = Partial<T>
The write shape (defaults to Partial<T>).
Implements
Section titled “Implements”Repository<T,D>
Constructors
Section titled “Constructors”Constructor
Section titled “Constructor”new MongoRepository<
T,D>(app):MongoRepository<T,D>
Defined in: mongo-repository.ts:44
Parameters
Section titled “Parameters”Returns
Section titled “Returns”MongoRepository<T, D>
Properties
Section titled “Properties”_session?
Section titled “_session?”
protectedoptional_session?:ClientSession
Defined in: mongo-repository.ts:42
Session bound by withTransaction() — every driver call passes it when set.
client
Section titled “client”
protectedreadonlyclient:MongoClient
Defined in: mongo-repository.ts:22
collectionName
Section titled “collectionName”
abstractreadonlycollectionName:string
Defined in: mongo-repository.ts:26
The MongoDB collection name to target.
createdAtField
Section titled “createdAtField”
readonlycreatedAtField:string="createdAt"
Defined in: mongo-repository.ts:31
Field written for auto-managed creation timestamps.
Default
Section titled “Default”"createdAt"
protectedreadonlydb:Db
Defined in: mongo-repository.ts:23
fieldMap
Section titled “fieldMap”
readonlyfieldMap:Record<string,string> ={}
Defined in: mongo-repository.ts:39
Entity-field-to-document-field overrides, for a collection whose field names don’t
match the entity (e.g. a brownfield collection using snake_case). Does not apply to
id — Mongo’s id is always the driver’s _id.
Default
Section titled “Default”{}timestamps
Section titled “timestamps”
readonlytimestamps:boolean=true
Defined in: mongo-repository.ts:29
When true, writes maintain createdAt/updatedAt as BSON Date fields.
Default
Section titled “Default”trueupdatedAtField
Section titled “updatedAtField”
readonlyupdatedAtField:string="updatedAt"
Defined in: mongo-repository.ts:33
Field written for auto-managed update timestamps.
Default
Section titled “Default”"updatedAt"Accessors
Section titled “Accessors”collection
Section titled “collection”Get Signature
Section titled “Get Signature”get
protectedcollection():Collection<Document>
Defined in: mongo-repository.ts:50
Escape hatch — the underlying driver Collection for native queries ($regex, aggregations, …).
Returns
Section titled “Returns”Collection<Document>
Methods
Section titled “Methods”count()
Section titled “count()”count(
params?):Promise<number>
Defined in: mongo-repository.ts:172
Parameters
Section titled “Parameters”params?
Section titled “params?”Returns
Section titled “Returns”Promise<number>
Implementation of
Section titled “Implementation of”deleteById()
Section titled “deleteById()”deleteById(
id):Promise<T>
Defined in: mongo-repository.ts:161
Parameters
Section titled “Parameters”Returns
Section titled “Returns”Promise<T>
Implementation of
Section titled “Implementation of”describe()
Section titled “describe()”describe():
RepositoryCapabilities
Defined in: mongo-repository.ts:54
Optional capability introspection. All
Returns
Section titled “Returns”Mantlejs
Section titled “Mantlejs”adapters implement it; user repositories may omit it.
Implementation of
Section titled “Implementation of”findAll()
Section titled “findAll()”findAll(
params?):Promise<T[]>
Defined in: mongo-repository.ts:66
Parameters
Section titled “Parameters”params?
Section titled “params?”Returns
Section titled “Returns”Promise<T[]>
Implementation of
Section titled “Implementation of”findById()
Section titled “findById()”findById(
id):Promise<T|null>
Defined in: mongo-repository.ts:83
Parameters
Section titled “Parameters”Returns
Section titled “Returns”Promise<T | null>
Implementation of
Section titled “Implementation of”fromDocument()
Section titled “fromDocument()”
protectedfromDocument(doc):T
Defined in: mongo-repository.ts:238
Map a driver document to the entity shape: _id: ObjectId becomes id: string.
Parameters
Section titled “Parameters”Record<string, unknown>
Returns
Section titled “Returns”T
patchById()
Section titled “patchById()”patchById(
id,data):Promise<T>
Defined in: mongo-repository.ts:129
Parameters
Section titled “Parameters”D
Returns
Section titled “Returns”Promise<T>
Implementation of
Section titled “Implementation of”save()
Section titled “save()”save(
data):Promise<T>
Defined in: mongo-repository.ts:92
Parameters
Section titled “Parameters”D
Returns
Section titled “Returns”Promise<T>
Implementation of
Section titled “Implementation of”saveAll()
Section titled “saveAll()”saveAll(
data):Promise<T[]>
Defined in: mongo-repository.ts:102
Parameters
Section titled “Parameters”D[]
Returns
Section titled “Returns”Promise<T[]>
Implementation of
Section titled “Implementation of”sessionOptions()
Section titled “sessionOptions()”
protectedsessionOptions():object
Defined in: mongo-repository.ts:209
Returns
Section titled “Returns”object
session?
Section titled “session?”
optionalsession?:ClientSession
toDocument()
Section titled “toDocument()”
protectedtoDocument(data,op,now?):Record<string,unknown>
Defined in: mongo-repository.ts:245
Map boundary data to a driver document: id becomes _id, timestamps applied.
Parameters
Section titled “Parameters”Record<string, unknown>
"create" | "update"
Date = ...
Returns
Section titled “Returns”Record<string, unknown>
toEntityField()
Section titled “toEntityField()”
protectedtoEntityField(field):string
Defined in: mongo-repository.ts:232
Translates a document field name back to its entity field name — the inverse of toField.
Parameters
Section titled “Parameters”string
Returns
Section titled “Returns”string
toField()
Section titled “toField()”
protectedtoField(field):string
Defined in: mongo-repository.ts:227
Translates an entity field name to its document field name (fieldMap override, else identity).
Parameters
Section titled “Parameters”string
Returns
Section titled “Returns”string
toObjectId()
Section titled “toObjectId()”
protectedtoObjectId(id):ObjectId
Defined in: mongo-repository.ts:214
Convert a boundary Id to an ObjectId, rejecting malformed ids loudly.
Parameters
Section titled “Parameters”Returns
Section titled “Returns”ObjectId
updateById()
Section titled “updateById()”updateById(
id,data):Promise<T>
Defined in: mongo-repository.ts:113
Parameters
Section titled “Parameters”D
Returns
Section titled “Returns”Promise<T>
Implementation of
Section titled “Implementation of”withTransaction()
Section titled “withTransaction()”withTransaction<
R>(fn):Promise<R>
Defined in: mongo-repository.ts:190
Run a block of repository calls inside one MongoDB transaction. Requires the deployment to be a replica set — true for every Atlas cluster including free-tier M0; standalone servers throw the driver’s native error. The callback receives a session-bound copy of this repository; calls on the original instance are NOT part of the transaction.
Type Parameters
Section titled “Type Parameters”R
Parameters
Section titled “Parameters”(txRepo) => Promise<R>
Returns
Section titled “Returns”Promise<R>
wrapError()
Section titled “wrapError()”
protectedwrapError(err):Error
Defined in: mongo-repository.ts:263
Parameters
Section titled “Parameters”unknown
Returns
Section titled “Returns”Error