Skip to main content
API Reference / modelence / server / Store Defined in: packages/modelence/src/data/store.ts:484 The Store class provides a type-safe interface for MongoDB collections with built-in schema validation and helper methods.

Example

Type Parameters

Constructors

Constructor

new Store<TSchema, TMethods>(name, options): Store<TSchema, TMethods>
Defined in: packages/modelence/src/data/store.ts:520 Creates a new Store instance

Parameters

Returns

Store<TSchema, TMethods>

Properties

Methods

aggregate()

aggregate(pipeline, options?): AggregationCursor<Document>
Defined in: packages/modelence/src/data/store.ts:1352 Aggregates documents using MongoDB’s aggregation framework

Parameters

Returns

AggregationCursor<Document> The aggregation cursor

bulkWrite()

bulkWrite(operations): Promise<BulkWriteResult>
Defined in: packages/modelence/src/data/store.ts:1362 Performs a bulk write operation on the collection

Parameters

Returns

Promise<BulkWriteResult> The result of the bulk write operation

countDocuments()

countDocuments(query): Promise<number>
Defined in: packages/modelence/src/data/store.ts:996 Counts the number of documents that match a query

Parameters

Returns

Promise<number> The number of documents that match the query

create()

create(document, options?): Promise<EnhancedOmit<InferDocumentType<TSchema>, "_id"> & object & TMethods>
Defined in: packages/modelence/src/data/store.ts:1065 Inserts a single document and returns the inserted document with its generated _id and any helper methods applied. Unlike insertOne, which only returns the insert result metadata, this method returns the full inserted document — useful when you need to immediately use the newly created record (e.g. returning it from an API handler).

Example

Parameters

Returns

Promise<EnhancedOmit<InferDocumentType<TSchema>, "_id"> & object & TMethods> The inserted document with _id populated and methods applied

deleteMany()

deleteMany(selector, options?): Promise<DeleteResult>
Defined in: packages/modelence/src/data/store.ts:1181 Deletes multiple documents

Parameters

Returns

Promise<DeleteResult> The result of the delete operation

deleteOne()

deleteOne(selector, options?): Promise<DeleteResult>
Defined in: packages/modelence/src/data/store.ts:1168 Deletes a single document

Parameters

Returns

Promise<DeleteResult> The result of the delete operation

distinct()

distinct<K>(key, filter?, options?): Promise<Flatten<WithId<InferDocumentType<TSchema>>[K]>[]>
Defined in: packages/modelence/src/data/store.ts:1323 Returns an array of distinct values for a field across the collection

Type Parameters

Parameters

Returns

Promise<Flatten<WithId<InferDocumentType<TSchema>>[K]>[]> An array of distinct values

extend()

extend<TExtendedSchema, TExtendedMethods>(config): Store<TSchema & TExtendedSchema, PreserveMethodsForExtendedSchema<TMethods, TSchema & TExtendedSchema> & TExtendedMethods>
Defined in: packages/modelence/src/data/store.ts:623 Extends the store with additional schema fields, indexes, methods, and search indexes. Returns a new Store instance with the extended schema and updated types. Methods from the original store are preserved with updated type signatures.

Example

Type Parameters

Parameters

Returns

Store<TSchema & TExtendedSchema, PreserveMethodsForExtendedSchema<TMethods, TSchema & TExtendedSchema> & TExtendedMethods> A new Store instance with the extended schema

fetch()

fetch(query, options?): Promise<EnhancedOmit<InferDocumentType<TSchema>, "_id"> & object & TMethods[]>
Defined in: packages/modelence/src/data/store.ts:1026 Fetches multiple documents, equivalent to Node.js MongoDB driver’s find and toArray methods combined.

Example

Parameters

Returns

Promise<EnhancedOmit<InferDocumentType<TSchema>, "_id"> & object & TMethods[]> The documents

findById()

findById(id): Promise<null | EnhancedOmit<InferDocumentType<TSchema>, "_id"> & object & TMethods>
Defined in: packages/modelence/src/data/store.ts:968 Fetches a single document by its ID

Parameters

Returns

Promise<null | EnhancedOmit<InferDocumentType<TSchema>, "_id"> & object & TMethods> The document, or null if not found

findOne()

findOne(query, options?): Promise<null | EnhancedOmit<InferDocumentType<TSchema>, "_id"> & object & TMethods>
Defined in: packages/modelence/src/data/store.ts:925 Finds a single document matching the query

Example

Parameters

Returns

Promise<null | EnhancedOmit<InferDocumentType<TSchema>, "_id"> & object & TMethods> The document, or null if not found

findOneAndDelete()

findOneAndDelete(selector, options?): Promise<null | EnhancedOmit<InferDocumentType<TSchema>, "_id"> & object & TMethods>
Defined in: packages/modelence/src/data/store.ts:1262 Atomically finds a document and deletes it, returning the deleted document

Parameters

Returns

Promise<null | EnhancedOmit<InferDocumentType<TSchema>, "_id"> & object & TMethods> The deleted document, or null if not found

findOneAndReplace()

findOneAndReplace(selector, replacement, options?): Promise<null | EnhancedOmit<InferDocumentType<TSchema>, "_id"> & object & TMethods>
Defined in: packages/modelence/src/data/store.ts:1281 Atomically finds a document and replaces it, returning the document

Parameters

Returns

Promise<null | EnhancedOmit<InferDocumentType<TSchema>, "_id"> & object & TMethods> The document (before or after replacement, depending on options), or null if not found

findOneAndUpdate()

findOneAndUpdate(selector, update, options?): Promise<null | EnhancedOmit<InferDocumentType<TSchema>, "_id"> & object & TMethods>
Defined in: packages/modelence/src/data/store.ts:1196 Atomically finds a document and updates it, returning the document

Parameters

Returns

Promise<null | EnhancedOmit<InferDocumentType<TSchema>, "_id"> & object & TMethods> The document (before or after update, depending on options), or null if not found

findOneAndUpsert()

findOneAndUpsert(selector, update, options?): Promise<UpsertResult<EnhancedOmit<InferDocumentType<TSchema>, "_id"> & object & TMethods>>
Defined in: packages/modelence/src/data/store.ts:1235 Atomic find-or-create: runs findOneAndUpdate with upsert and reports, as isNew, whether the returned document was newly inserted. The plain findOneAndUpdate deliberately hides result metadata and returns only the document, so it can’t distinguish an insert from a match. This method surfaces the driver’s lastErrorObject.upserted flag as isNew, so callers can branch on create-vs-match without a separate pre-existence read that would race with concurrent upserts. upsert defaults to true but is overridable — pass upsert: false to make this a pure find-and-report (unknown selector → { doc: null, isNew: false }). returnDocument is always 'after' and cannot be overridden.

Example

Parameters

Returns

Promise<UpsertResult<EnhancedOmit<InferDocumentType<TSchema>, "_id"> & object & TMethods>> { doc, isNew } — doc is null only when upsert: false and nothing matched; isNew is true exactly when this call inserted the doc.

getDatabase()

getDatabase(): Db
Defined in: packages/modelence/src/data/store.ts:1371 Returns the raw MongoDB database instance for advanced operations

Returns

Db The MongoDB database instance

Throws

Error if the store is not provisioned

getIndexCreationMode()

getIndexCreationMode(): IndexCreationMode
Defined in: packages/modelence/src/data/store.ts:548

Returns

IndexCreationMode

getName()

getName(): string
Defined in: packages/modelence/src/data/store.ts:544

Returns

string

insertMany()

insertMany(documents, options?): Promise<InsertManyResult<Document>>
Defined in: packages/modelence/src/data/store.ts:1085 Inserts multiple documents

Parameters

Returns

Promise<InsertManyResult<Document>> The result of the insert operation

insertOne()

insertOne(document, options?): Promise<InsertOneResult<Document>>
Defined in: packages/modelence/src/data/store.ts:1040 Inserts a single document

Parameters

Returns

Promise<InsertOneResult<Document>> The result of the insert operation

rawCollection()

rawCollection(): Collection<InferDocumentType<TSchema>>
Defined in: packages/modelence/src/data/store.ts:1380 Returns the raw MongoDB collection instance for advanced operations

Returns

Collection<InferDocumentType<TSchema>> The MongoDB collection instance

Throws

Error if the store is not provisioned

renameFrom()

renameFrom(oldName, options?): Promise<void>
Defined in: packages/modelence/src/data/store.ts:1389 Renames an existing collection to this store’s name, used for migrations

Parameters

Returns

Promise<void>

Throws

Error if the old collection doesn’t exist or if this store’s collection already exists

replaceOne()

replaceOne(selector, replacement, options?): Promise<UpdateResult<Document>>
Defined in: packages/modelence/src/data/store.ts:1302 Replaces a single document

Parameters

Returns

Promise<UpdateResult<Document>> The result of the replace operation

requireById()

requireById(id, errorHandler?): Promise<EnhancedOmit<InferDocumentType<TSchema>, "_id"> & object & TMethods>
Defined in: packages/modelence/src/data/store.ts:980 Fetches a single document by its ID, or throws an error if not found

Parameters

Returns

Promise<EnhancedOmit<InferDocumentType<TSchema>, "_id"> & object & TMethods> The document

requireOne()

requireOne(query, options?, errorHandler?): Promise<EnhancedOmit<InferDocumentType<TSchema>, "_id"> & object & TMethods>
Defined in: packages/modelence/src/data/store.ts:933

Parameters

Returns

Promise<EnhancedOmit<InferDocumentType<TSchema>, "_id"> & object & TMethods>

updateMany()

updateMany(selector, update, options?): Promise<UpdateResult<Document>>
Defined in: packages/modelence/src/data/store.ts:1132 Updates multiple documents

Parameters

Returns

Promise<UpdateResult<Document>> The result of the update operation

updateOne()

updateOne(selector, update, options?): Promise<UpdateResult<Document>>
Defined in: packages/modelence/src/data/store.ts:1099 Updates a single document

Parameters

Returns

Promise<UpdateResult<Document>> The result of the update operation

upsertMany()

upsertMany(selector, update, options?): Promise<UpdateResult<Document>>
Defined in: packages/modelence/src/data/store.ts:1151 Updates multiple documents, or inserts them if they don’t exist

Parameters

Returns

Promise<UpdateResult<Document>> The result of the update operation

upsertOne()

upsertOne(selector, update, options?): Promise<UpdateResult<Document>>
Defined in: packages/modelence/src/data/store.ts:1114 Updates a single document, or inserts it if it doesn’t exist

Parameters

Returns

Promise<UpdateResult<Document>> The result of the update operation

vectorSearch()

vectorSearch(params): Promise<AggregationCursor<Document>>
Defined in: packages/modelence/src/data/store.ts:1434 Performs a vector similarity search using MongoDB Atlas Vector Search

Example

Parameters

Returns

Promise<AggregationCursor<Document>> An aggregation cursor with search results and scores

watch()

watch(pipeline?, options?): ChangeStream
Defined in: packages/modelence/src/data/store.ts:1341 Opens a change stream on the collection to watch for real-time changes

Parameters

Returns

ChangeStream A ChangeStream instance

vectorIndex()

static vectorIndex(params): object
Defined in: packages/modelence/src/data/store.ts:1497 Creates a MongoDB Atlas Vector Search index definition

Example

Parameters

Returns

object A search index description object