Documentation Index
Fetch the complete documentation index at: https://mintlify.com/abejarano/ts-mongo-criteria/llms.txt
Use this file to discover all available pages before exploring further.
MongoRepository<T> is the abstract base class at the heart of @abejarano/ts-mongodb-criteria. It wires together MongoCriteriaConverter, MongoClientFactory, and MongoTransaction so that every concrete repository you write automatically gets paginated listing, criteria-based filtering, upsert-by-ID persistence, and optional transactional support with zero boilerplate.
Class overview
T must extend AggregateRoot. The constructor receives an AggregateRootClass<T> — the class reference (not an instance) — so the repository can call T.fromPrimitives() when hydrating documents retrieved from MongoDB.
Abstract methods
These two methods must be implemented in every concrete repository subclass.collectionName(): string
Returns the name of the MongoDB collection this repository manages.
ensureIndexes runs only once per collection per process.
ensureIndexes(collection: Collection): Promise<void>
Called once per process (guarded by the index registry) the first time the collection is accessed. Use it to declare any indexes your queries rely on.
Collection from the MongoDB driver so that all native createIndex options are available.
Public methods
one
Finds the first document matching filter and returns it hydrated as T, or null when no document matches.
A MongoDB query filter object (plain POJO). For example
{ email: "alice@example.com" }.An optional transaction context obtained from
MongoTransaction.run(). When provided the underlying findOne is bound to that session.Promise<T | null>
_id field is automatically mapped to id (as a string) when calling fromPrimitives.
many
Finds all documents matching filter and returns them as an array of hydrated T instances.
A MongoDB query filter object. Returns all documents in the collection when passed an empty object
{}.Optional transaction context. The underlying cursor is opened with the associated session when provided.
Promise<T[]>
list
Converts a Criteria object into a MongoDB query (via MongoCriteriaConverter) and returns a paginated Paginate<D> result.
Encapsulates filters, sort order, page number, and page size. Build one with
new Criteria(filters, order, limit, page).Field names to remove from each returned document via a MongoDB projection. Defaults to
[] (no projection). _id is always excluded from the mapped result regardless of this parameter.Optional transaction context applied to both the cursor and the subsequent
countDocuments call.Promise<Paginate<D>>
upsert
Persists an entity to MongoDB. If a document with the same _id already exists it is updated; otherwise a new document is inserted. The entity’s toPrimitives() result is spread into a $set operation.
The aggregate root instance to persist.
entity.getId() must return a valid 24-character hex ObjectId string.Optional transaction context. The underlying
updateOne is bound to the session when provided.Promise<void>
toPrimitives() may return a plain object or a Promise that resolves to one — both forms are handled.
delete
Deletes all documents matching filter using the MongoDB driver’s deleteMany.
A MongoDB query filter. All matching documents are removed.
Optional native MongoDB
DeleteOptions (e.g. { comment: "…" }). Merged with the session when a transaction is active.Optional transaction context.
Promise<void>
Protected helpers
These methods are available inside subclass implementations but are not part of the publicIRepository contract.
collection<U>()
Returns the raw MongoDB Collection<U> for the current repository. Automatically triggers ensureIndexesOnce() on the first call so that indexes are always present before any query runs.
Returns Promise<Collection<U>>
Use this inside custom query methods that go beyond what one / many / list provide:
updateOne
Runs a native MongoDB updateOne with { upsert: true } on the collection. Useful when you need fine-grained control (e.g. $push, $inc) that upsert does not expose.
The filter identifying the target document.
An update document (e.g.
{ $set: { … } }) or an aggregation pipeline array.Optional transaction context.
Promise<void>
IRepository<T> interface
MongoRepository<T> satisfies the IRepository<T> interface exported by the library. Use it to declare dependency types so that application code is decoupled from the concrete repository class and can be unit-tested with mocks.
Index registry
MongoRepository maintains a process-wide static indexRegistry = new Set<string>(). When collection() is called, it invokes ensureIndexesOnce(), which:
- Looks up
collectionName()in the set. - If absent, calls your
ensureIndexes(collection)implementation and adds the name to the set. - If already present, skips the call entirely.