API Reference / modelence / server / Store

Defined in: src/data/store.ts:50

The Store class provides a type-safe interface for MongoDB collections with built-in schema validation and helper methods.

Example

const dbTodos = new Store('todos', {
  schema: {
    title: schema.string(),
    completed: schema.boolean(),
    dueDate: schema.date().optional(),
    userId: schema.userId(),
  },
  methods: {
    isOverdue() {
      return this.dueDate < new Date();
    }
  }
});

Type Parameters

Type ParameterDescription
TSchema extends ModelSchemaThe document schema type
TMethods extends Record<string, (this, …args) => any>Custom methods that will be added to documents

Constructors

Constructor

new Store<TSchema, TMethods>(name, options): Store<TSchema, TMethods>

Defined in: src/data/store.ts:76

Creates a new Store instance

Parameters

ParameterTypeDescription
namestringThe collection name in MongoDB
options{ indexes: IndexDescription[]; methods?: TMethods; schema: TSchema; }Store configuration
options.indexesIndexDescription[]MongoDB indexes to create
options.methods?TMethodsCustom methods to add to documents
options.schemaTSchemaDocument schema using Modelence schema types

Returns

Store<TSchema, TMethods>

Properties

PropertyModifierTypeDefined in
DocreadonlyEnhancedOmit<InferDocumentType<TSchema>, "_id"> & object & TMethodssrc/data/store.ts:61

Methods

aggregate()

aggregate(pipeline, options?): AggregationCursor<Document>

Defined in: src/data/store.ts:346

Aggregates documents using MongoDB’s aggregation framework

Parameters

ParameterTypeDescription
pipelineDocument[]The aggregation pipeline
options?AggregateOptionsOptional options

Returns

AggregationCursor<Document>

The aggregation cursor


bulkWrite()

bulkWrite(operations): Promise<BulkWriteResult>

Defined in: src/data/store.ts:356

Performs a bulk write operation on the collection

Parameters

ParameterTypeDescription
operationsAnyBulkWriteOperation<InferDocumentType<TSchema>>[]The operations to perform

Returns

Promise<BulkWriteResult>

The result of the bulk write operation


countDocuments()

countDocuments(query): Promise<number>

Defined in: src/data/store.ts:235

Counts the number of documents that match a query

Parameters

ParameterTypeDescription
queryFilter<InferDocumentType<TSchema>>The query to filter documents

Returns

Promise<number>

The number of documents that match the query


deleteMany()

deleteMany(selector): Promise<DeleteResult>

Defined in: src/data/store.ts:335

Deletes multiple documents

Parameters

ParameterTypeDescription
selectorFilter<InferDocumentType<TSchema>>The selector to find the documents to delete

Returns

Promise<DeleteResult>

The result of the delete operation


deleteOne()

deleteOne(selector): Promise<DeleteResult>

Defined in: src/data/store.ts:325

Deletes a single document

Parameters

ParameterTypeDescription
selectorFilter<InferDocumentType<TSchema>>The selector to find the document to delete

Returns

Promise<DeleteResult>

The result of the delete operation


fetch()

fetch(query, options?): Promise<EnhancedOmit<InferDocumentType<TSchema>, "_id"> & object & TMethods[]>

Defined in: src/data/store.ts:246

Fetches multiple documents, equivalent to Node.js MongoDB driver’s find and toArray methods combined.

Parameters

ParameterTypeDescription
queryFilter<InferDocumentType<TSchema>>The query to filter documents
options?{ limit?: number; skip?: number; sort?: Document; }Options
options.limit?number-
options.skip?number-
options.sort?Document-

Returns

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

The documents


findById()

findById(id): Promise<null | EnhancedOmit<InferDocumentType<TSchema>, "_id"> & object & TMethods>

Defined in: src/data/store.ts:209

Fetches a single document by its ID

Parameters

ParameterTypeDescription
idstring | ObjectIdThe ID of the document to find

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: src/data/store.ts:168

Parameters

ParameterType
queryFilter<InferDocumentType<TSchema>>
options?FindOptions<Document>

Returns

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


getDatabase()

getDatabase(): Db

Defined in: src/data/store.ts:365

Returns the raw MongoDB database instance for advanced operations

Returns

Db

The MongoDB database instance

Throws

Error if the store is not provisioned


getName()

getName(): string

Defined in: src/data/store.ts:93

Returns

string


insertMany()

insertMany(documents): Promise<InsertManyResult<Document>>

Defined in: src/data/store.ts:267

Inserts multiple documents

Parameters

ParameterTypeDescription
documentsOptionalUnlessRequiredId<InferDocumentType<TSchema>>[]The documents to insert

Returns

Promise<InsertManyResult<Document>>

The result of the insert operation


insertOne()

insertOne(document): Promise<InsertOneResult<Document>>

Defined in: src/data/store.ts:257

Inserts a single document

Parameters

ParameterTypeDescription
documentOptionalUnlessRequiredId<InferDocumentType<TSchema>>The document to insert

Returns

Promise<InsertOneResult<Document>>

The result of the insert operation


rawCollection()

rawCollection(): Collection<InferDocumentType<TSchema>>

Defined in: src/data/store.ts:374

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: src/data/store.ts:383

Renames an existing collection to this store’s name, used for migrations

Parameters

ParameterTypeDescription
oldNamestringThe previous name of the collection
options?{ session?: ClientSession; }-
options.session?ClientSession-

Returns

Promise<void>

Throws

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


requireById()

requireById(id, errorHandler?): Promise<EnhancedOmit<InferDocumentType<TSchema>, "_id"> & object & TMethods>

Defined in: src/data/store.ts:221

Fetches a single document by its ID, or throws an error if not found

Parameters

ParameterTypeDescription
idstring | ObjectIdThe ID of the document to find
errorHandler?() => ErrorOptional error handler to return a custom error if the document is not found

Returns

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

The document


requireOne()

requireOne(query, options?, errorHandler?): Promise<EnhancedOmit<InferDocumentType<TSchema>, "_id"> & object & TMethods>

Defined in: src/data/store.ts:176

Parameters

ParameterType
queryFilter<InferDocumentType<TSchema>>
options?FindOptions<Document>
errorHandler?() => Error

Returns

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


updateMany()

updateMany(selector, update, options?): Promise<UpdateResult<Document>>

Defined in: src/data/store.ts:300

Updates multiple documents

Parameters

ParameterTypeDescription
selectorFilter<InferDocumentType<TSchema>>The selector to find the documents to update
updateUpdateFilter<InferDocumentType<TSchema>>The MongoDB modifier to apply to the documents
options?{ session?: ClientSession; }-
options.session?ClientSession-

Returns

Promise<UpdateResult<Document>>

The result of the update operation


updateOne()

updateOne(selector, update): Promise<UpdateResult<Document>>

Defined in: src/data/store.ts:278

Updates a single document

Parameters

ParameterTypeDescription
selectorstring | ObjectId | Filter<InferDocumentType<TSchema>>The selector to find the document to update
updateUpdateFilter<InferDocumentType<TSchema>>The update to apply to the document

Returns

Promise<UpdateResult<Document>>

The result of the update operation


upsertMany()

upsertMany(selector, update): Promise<UpdateResult<Document>>

Defined in: src/data/store.ts:315

Updates multiple documents, or inserts them if they don’t exist

Parameters

ParameterTypeDescription
selectorFilter<InferDocumentType<TSchema>>The selector to find the documents to update
updateUpdateFilter<InferDocumentType<TSchema>>The MongoDB modifier to apply to the documents

Returns

Promise<UpdateResult<Document>>

The result of the update operation


upsertOne()

upsertOne(selector, update): Promise<UpdateResult<Document>>

Defined in: src/data/store.ts:289

Updates a single document, or inserts it if it doesn’t exist

Parameters

ParameterTypeDescription
selectorstring | ObjectId | Filter<InferDocumentType<TSchema>>The selector to find the document to update
updateUpdateFilter<InferDocumentType<TSchema>>The MongoDB modifier to apply to the document

Returns

Promise<UpdateResult<Document>>

The result of the update operation

API Reference / modelence / server / Store

Defined in: src/data/store.ts:50

The Store class provides a type-safe interface for MongoDB collections with built-in schema validation and helper methods.

Example

const dbTodos = new Store('todos', {
  schema: {
    title: schema.string(),
    completed: schema.boolean(),
    dueDate: schema.date().optional(),
    userId: schema.userId(),
  },
  methods: {
    isOverdue() {
      return this.dueDate < new Date();
    }
  }
});

Type Parameters

Type ParameterDescription
TSchema extends ModelSchemaThe document schema type
TMethods extends Record<string, (this, …args) => any>Custom methods that will be added to documents

Constructors

Constructor

new Store<TSchema, TMethods>(name, options): Store<TSchema, TMethods>

Defined in: src/data/store.ts:76

Creates a new Store instance

Parameters

ParameterTypeDescription
namestringThe collection name in MongoDB
options{ indexes: IndexDescription[]; methods?: TMethods; schema: TSchema; }Store configuration
options.indexesIndexDescription[]MongoDB indexes to create
options.methods?TMethodsCustom methods to add to documents
options.schemaTSchemaDocument schema using Modelence schema types

Returns

Store<TSchema, TMethods>

Properties

PropertyModifierTypeDefined in
DocreadonlyEnhancedOmit<InferDocumentType<TSchema>, "_id"> & object & TMethodssrc/data/store.ts:61

Methods

aggregate()

aggregate(pipeline, options?): AggregationCursor<Document>

Defined in: src/data/store.ts:346

Aggregates documents using MongoDB’s aggregation framework

Parameters

ParameterTypeDescription
pipelineDocument[]The aggregation pipeline
options?AggregateOptionsOptional options

Returns

AggregationCursor<Document>

The aggregation cursor


bulkWrite()

bulkWrite(operations): Promise<BulkWriteResult>

Defined in: src/data/store.ts:356

Performs a bulk write operation on the collection

Parameters

ParameterTypeDescription
operationsAnyBulkWriteOperation<InferDocumentType<TSchema>>[]The operations to perform

Returns

Promise<BulkWriteResult>

The result of the bulk write operation


countDocuments()

countDocuments(query): Promise<number>

Defined in: src/data/store.ts:235

Counts the number of documents that match a query

Parameters

ParameterTypeDescription
queryFilter<InferDocumentType<TSchema>>The query to filter documents

Returns

Promise<number>

The number of documents that match the query


deleteMany()

deleteMany(selector): Promise<DeleteResult>

Defined in: src/data/store.ts:335

Deletes multiple documents

Parameters

ParameterTypeDescription
selectorFilter<InferDocumentType<TSchema>>The selector to find the documents to delete

Returns

Promise<DeleteResult>

The result of the delete operation


deleteOne()

deleteOne(selector): Promise<DeleteResult>

Defined in: src/data/store.ts:325

Deletes a single document

Parameters

ParameterTypeDescription
selectorFilter<InferDocumentType<TSchema>>The selector to find the document to delete

Returns

Promise<DeleteResult>

The result of the delete operation


fetch()

fetch(query, options?): Promise<EnhancedOmit<InferDocumentType<TSchema>, "_id"> & object & TMethods[]>

Defined in: src/data/store.ts:246

Fetches multiple documents, equivalent to Node.js MongoDB driver’s find and toArray methods combined.

Parameters

ParameterTypeDescription
queryFilter<InferDocumentType<TSchema>>The query to filter documents
options?{ limit?: number; skip?: number; sort?: Document; }Options
options.limit?number-
options.skip?number-
options.sort?Document-

Returns

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

The documents


findById()

findById(id): Promise<null | EnhancedOmit<InferDocumentType<TSchema>, "_id"> & object & TMethods>

Defined in: src/data/store.ts:209

Fetches a single document by its ID

Parameters

ParameterTypeDescription
idstring | ObjectIdThe ID of the document to find

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: src/data/store.ts:168

Parameters

ParameterType
queryFilter<InferDocumentType<TSchema>>
options?FindOptions<Document>

Returns

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


getDatabase()

getDatabase(): Db

Defined in: src/data/store.ts:365

Returns the raw MongoDB database instance for advanced operations

Returns

Db

The MongoDB database instance

Throws

Error if the store is not provisioned


getName()

getName(): string

Defined in: src/data/store.ts:93

Returns

string


insertMany()

insertMany(documents): Promise<InsertManyResult<Document>>

Defined in: src/data/store.ts:267

Inserts multiple documents

Parameters

ParameterTypeDescription
documentsOptionalUnlessRequiredId<InferDocumentType<TSchema>>[]The documents to insert

Returns

Promise<InsertManyResult<Document>>

The result of the insert operation


insertOne()

insertOne(document): Promise<InsertOneResult<Document>>

Defined in: src/data/store.ts:257

Inserts a single document

Parameters

ParameterTypeDescription
documentOptionalUnlessRequiredId<InferDocumentType<TSchema>>The document to insert

Returns

Promise<InsertOneResult<Document>>

The result of the insert operation


rawCollection()

rawCollection(): Collection<InferDocumentType<TSchema>>

Defined in: src/data/store.ts:374

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: src/data/store.ts:383

Renames an existing collection to this store’s name, used for migrations

Parameters

ParameterTypeDescription
oldNamestringThe previous name of the collection
options?{ session?: ClientSession; }-
options.session?ClientSession-

Returns

Promise<void>

Throws

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


requireById()

requireById(id, errorHandler?): Promise<EnhancedOmit<InferDocumentType<TSchema>, "_id"> & object & TMethods>

Defined in: src/data/store.ts:221

Fetches a single document by its ID, or throws an error if not found

Parameters

ParameterTypeDescription
idstring | ObjectIdThe ID of the document to find
errorHandler?() => ErrorOptional error handler to return a custom error if the document is not found

Returns

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

The document


requireOne()

requireOne(query, options?, errorHandler?): Promise<EnhancedOmit<InferDocumentType<TSchema>, "_id"> & object & TMethods>

Defined in: src/data/store.ts:176

Parameters

ParameterType
queryFilter<InferDocumentType<TSchema>>
options?FindOptions<Document>
errorHandler?() => Error

Returns

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


updateMany()

updateMany(selector, update, options?): Promise<UpdateResult<Document>>

Defined in: src/data/store.ts:300

Updates multiple documents

Parameters

ParameterTypeDescription
selectorFilter<InferDocumentType<TSchema>>The selector to find the documents to update
updateUpdateFilter<InferDocumentType<TSchema>>The MongoDB modifier to apply to the documents
options?{ session?: ClientSession; }-
options.session?ClientSession-

Returns

Promise<UpdateResult<Document>>

The result of the update operation


updateOne()

updateOne(selector, update): Promise<UpdateResult<Document>>

Defined in: src/data/store.ts:278

Updates a single document

Parameters

ParameterTypeDescription
selectorstring | ObjectId | Filter<InferDocumentType<TSchema>>The selector to find the document to update
updateUpdateFilter<InferDocumentType<TSchema>>The update to apply to the document

Returns

Promise<UpdateResult<Document>>

The result of the update operation


upsertMany()

upsertMany(selector, update): Promise<UpdateResult<Document>>

Defined in: src/data/store.ts:315

Updates multiple documents, or inserts them if they don’t exist

Parameters

ParameterTypeDescription
selectorFilter<InferDocumentType<TSchema>>The selector to find the documents to update
updateUpdateFilter<InferDocumentType<TSchema>>The MongoDB modifier to apply to the documents

Returns

Promise<UpdateResult<Document>>

The result of the update operation


upsertOne()

upsertOne(selector, update): Promise<UpdateResult<Document>>

Defined in: src/data/store.ts:289

Updates a single document, or inserts it if it doesn’t exist

Parameters

ParameterTypeDescription
selectorstring | ObjectId | Filter<InferDocumentType<TSchema>>The selector to find the document to update
updateUpdateFilter<InferDocumentType<TSchema>>The MongoDB modifier to apply to the document

Returns

Promise<UpdateResult<Document>>

The result of the update operation