Skip to content

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.

T extends Record<string, unknown>

The entity shape. Must extend Record<string, unknown>.

D = Partial<T>

The write shape (defaults to Partial<T>).

new MongoRepository<T, D>(app): MongoRepository<T, D>

Defined in: mongo-repository.ts:44

MantleApplication

MongoRepository<T, D>

protected optional _session?: ClientSession

Defined in: mongo-repository.ts:42

Session bound by withTransaction() — every driver call passes it when set.


protected readonly client: MongoClient

Defined in: mongo-repository.ts:22


abstract readonly collectionName: string

Defined in: mongo-repository.ts:26

The MongoDB collection name to target.


readonly createdAtField: string = "createdAt"

Defined in: mongo-repository.ts:31

Field written for auto-managed creation timestamps.

"createdAt"

protected readonly db: Db

Defined in: mongo-repository.ts:23


readonly fieldMap: 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.

{}

readonly timestamps: boolean = true

Defined in: mongo-repository.ts:29

When true, writes maintain createdAt/updatedAt as BSON Date fields.

true

readonly updatedAtField: string = "updatedAt"

Defined in: mongo-repository.ts:33

Field written for auto-managed update timestamps.

"updatedAt"

get protected collection(): Collection<Document>

Defined in: mongo-repository.ts:50

Escape hatch — the underlying driver Collection for native queries ($regex, aggregations, …).

Collection<Document>

count(params?): Promise<number>

Defined in: mongo-repository.ts:172

QueryParams

Promise<number>

Repository.count


deleteById(id): Promise<T>

Defined in: mongo-repository.ts:161

Id

Promise<T>

Repository.deleteById


describe(): RepositoryCapabilities

Defined in: mongo-repository.ts:54

Optional capability introspection. All

RepositoryCapabilities

adapters implement it; user repositories may omit it.

Repository.describe


findAll(params?): Promise<T[]>

Defined in: mongo-repository.ts:66

QueryParams

Promise<T[]>

Repository.findAll


findById(id): Promise<T | null>

Defined in: mongo-repository.ts:83

Id

Promise<T | null>

Repository.findById


protected fromDocument(doc): T

Defined in: mongo-repository.ts:238

Map a driver document to the entity shape: _id: ObjectId becomes id: string.

Record<string, unknown>

T


patchById(id, data): Promise<T>

Defined in: mongo-repository.ts:129

Id

D

Promise<T>

Repository.patchById


save(data): Promise<T>

Defined in: mongo-repository.ts:92

D

Promise<T>

Repository.save


saveAll(data): Promise<T[]>

Defined in: mongo-repository.ts:102

D[]

Promise<T[]>

Repository.saveAll


protected sessionOptions(): object

Defined in: mongo-repository.ts:209

object

optional session?: ClientSession


protected toDocument(data, op, now?): Record<string, unknown>

Defined in: mongo-repository.ts:245

Map boundary data to a driver document: id becomes _id, timestamps applied.

Record<string, unknown>

"create" | "update"

Date = ...

Record<string, unknown>


protected toEntityField(field): string

Defined in: mongo-repository.ts:232

Translates a document field name back to its entity field name — the inverse of toField.

string

string


protected toField(field): string

Defined in: mongo-repository.ts:227

Translates an entity field name to its document field name (fieldMap override, else identity).

string

string


protected toObjectId(id): ObjectId

Defined in: mongo-repository.ts:214

Convert a boundary Id to an ObjectId, rejecting malformed ids loudly.

Id

ObjectId


updateById(id, data): Promise<T>

Defined in: mongo-repository.ts:113

Id

D

Promise<T>

Repository.updateById


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.

R

(txRepo) => Promise<R>

Promise<R>


protected wrapError(err): Error

Defined in: mongo-repository.ts:263

unknown

Error