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.
Type Parameter | Description |
---|---|
TSchema extends ModelSchema | The document schema type |
TMethods extends Record <string , (this , …args ) => any > | Custom methods that will be added to documents |
new Store<
TSchema
,TMethods
>(name
,options
):Store
<TSchema
,TMethods
>
Defined in: src/data/store.ts:76
Creates a new Store instance
Parameter | Type | Description |
---|---|---|
name | string | The collection name in MongoDB |
options | { indexes : IndexDescription []; methods? : TMethods ; schema : TSchema ; } | Store configuration |
options.indexes | IndexDescription [] | MongoDB indexes to create |
options.methods? | TMethods | Custom methods to add to documents |
options.schema | TSchema | Document schema using Modelence schema types |
Store
<TSchema
, TMethods
>
Property | Modifier | Type | Defined in |
---|---|---|---|
Doc | readonly | EnhancedOmit <InferDocumentType <TSchema >, "_id" > & object & TMethods | src/data/store.ts:61 |
aggregate(
pipeline
,options?
):AggregationCursor
<Document
>
Defined in: src/data/store.ts:346
Aggregates documents using MongoDB’s aggregation framework
Parameter | Type | Description |
---|---|---|
pipeline | Document [] | The aggregation pipeline |
options? | AggregateOptions | Optional options |
AggregationCursor
<Document
>
The aggregation cursor
bulkWrite(
operations
):Promise
<BulkWriteResult
>
Defined in: src/data/store.ts:356
Performs a bulk write operation on the collection
Parameter | Type | Description |
---|---|---|
operations | AnyBulkWriteOperation <InferDocumentType <TSchema >>[] | The operations to perform |
Promise
<BulkWriteResult
>
The result of the bulk write operation
countDocuments(
query
):Promise
<number
>
Defined in: src/data/store.ts:235
Counts the number of documents that match a query
Parameter | Type | Description |
---|---|---|
query | Filter <InferDocumentType <TSchema >> | The query to filter documents |
Promise
<number
>
The number of documents that match the query
deleteMany(
selector
):Promise
<DeleteResult
>
Defined in: src/data/store.ts:335
Deletes multiple documents
Parameter | Type | Description |
---|---|---|
selector | Filter <InferDocumentType <TSchema >> | The selector to find the documents to delete |
Promise
<DeleteResult
>
The result of the delete operation
deleteOne(
selector
):Promise
<DeleteResult
>
Defined in: src/data/store.ts:325
Deletes a single document
Parameter | Type | Description |
---|---|---|
selector | Filter <InferDocumentType <TSchema >> | The selector to find the document to delete |
Promise
<DeleteResult
>
The result of the delete operation
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.
Parameter | Type | Description |
---|---|---|
query | Filter <InferDocumentType <TSchema >> | The query to filter documents |
options? | { limit? : number ; skip? : number ; sort? : Document ; } | Options |
options.limit? | number | - |
options.skip? | number | - |
options.sort? | Document | - |
Promise
<EnhancedOmit
<InferDocumentType
<TSchema
>, "_id"
> & object
& TMethods
[]>
The documents
findById(
id
):Promise
<null
|EnhancedOmit
<InferDocumentType
<TSchema
>,"_id"
> &object
&TMethods
>
Defined in: src/data/store.ts:209
Fetches a single document by its ID
Parameter | Type | Description |
---|---|---|
id | string | ObjectId | The ID of the document to find |
Promise
<null
| EnhancedOmit
<InferDocumentType
<TSchema
>, "_id"
> & object
& TMethods
>
The document, or null if not found
findOne(
query
,options?
):Promise
<null
|EnhancedOmit
<InferDocumentType
<TSchema
>,"_id"
> &object
&TMethods
>
Defined in: src/data/store.ts:168
Parameter | Type |
---|---|
query | Filter <InferDocumentType <TSchema >> |
options? | FindOptions <Document > |
Promise
<null
| EnhancedOmit
<InferDocumentType
<TSchema
>, "_id"
> & object
& TMethods
>
getDatabase():
Db
Defined in: src/data/store.ts:365
Returns the raw MongoDB database instance for advanced operations
Db
The MongoDB database instance
Error if the store is not provisioned
getName():
string
Defined in: src/data/store.ts:93
string
insertMany(
documents
):Promise
<InsertManyResult
<Document
>>
Defined in: src/data/store.ts:267
Inserts multiple documents
Parameter | Type | Description |
---|---|---|
documents | OptionalUnlessRequiredId <InferDocumentType <TSchema >>[] | The documents to insert |
Promise
<InsertManyResult
<Document
>>
The result of the insert operation
insertOne(
document
):Promise
<InsertOneResult
<Document
>>
Defined in: src/data/store.ts:257
Inserts a single document
Parameter | Type | Description |
---|---|---|
document | OptionalUnlessRequiredId <InferDocumentType <TSchema >> | The document to insert |
Promise
<InsertOneResult
<Document
>>
The result of the insert operation
rawCollection():
Collection
<InferDocumentType
<TSchema
>>
Defined in: src/data/store.ts:374
Returns the raw MongoDB collection instance for advanced operations
Collection
<InferDocumentType
<TSchema
>>
The MongoDB collection instance
Error if the store is not provisioned
renameFrom(
oldName
,options?
):Promise
<void
>
Defined in: src/data/store.ts:383
Renames an existing collection to this store’s name, used for migrations
Parameter | Type | Description |
---|---|---|
oldName | string | The previous name of the collection |
options? | { session? : ClientSession ; } | - |
options.session? | ClientSession | - |
Promise
<void
>
Error if the old collection doesn’t exist or if this store’s collection already exists
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
Parameter | Type | Description |
---|---|---|
id | string | ObjectId | The ID of the document to find |
errorHandler? | () => Error | Optional error handler to return a custom error if the document is not found |
Promise
<EnhancedOmit
<InferDocumentType
<TSchema
>, "_id"
> & object
& TMethods
>
The document
requireOne(
query
,options?
,errorHandler?
):Promise
<EnhancedOmit
<InferDocumentType
<TSchema
>,"_id"
> &object
&TMethods
>
Defined in: src/data/store.ts:176
Parameter | Type |
---|---|
query | Filter <InferDocumentType <TSchema >> |
options? | FindOptions <Document > |
errorHandler? | () => Error |
Promise
<EnhancedOmit
<InferDocumentType
<TSchema
>, "_id"
> & object
& TMethods
>
updateMany(
selector
,update
,options?
):Promise
<UpdateResult
<Document
>>
Defined in: src/data/store.ts:300
Updates multiple documents
Parameter | Type | Description |
---|---|---|
selector | Filter <InferDocumentType <TSchema >> | The selector to find the documents to update |
update | UpdateFilter <InferDocumentType <TSchema >> | The MongoDB modifier to apply to the documents |
options? | { session? : ClientSession ; } | - |
options.session? | ClientSession | - |
Promise
<UpdateResult
<Document
>>
The result of the update operation
updateOne(
selector
,update
):Promise
<UpdateResult
<Document
>>
Defined in: src/data/store.ts:278
Updates a single document
Parameter | Type | Description |
---|---|---|
selector | string | ObjectId | Filter <InferDocumentType <TSchema >> | The selector to find the document to update |
update | UpdateFilter <InferDocumentType <TSchema >> | The update to apply to the document |
Promise
<UpdateResult
<Document
>>
The result of the update operation
upsertMany(
selector
,update
):Promise
<UpdateResult
<Document
>>
Defined in: src/data/store.ts:315
Updates multiple documents, or inserts them if they don’t exist
Parameter | Type | Description |
---|---|---|
selector | Filter <InferDocumentType <TSchema >> | The selector to find the documents to update |
update | UpdateFilter <InferDocumentType <TSchema >> | The MongoDB modifier to apply to the documents |
Promise
<UpdateResult
<Document
>>
The result of the update operation
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
Parameter | Type | Description |
---|---|---|
selector | string | ObjectId | Filter <InferDocumentType <TSchema >> | The selector to find the document to update |
update | UpdateFilter <InferDocumentType <TSchema >> | The MongoDB modifier to apply to the document |
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.
Type Parameter | Description |
---|---|
TSchema extends ModelSchema | The document schema type |
TMethods extends Record <string , (this , …args ) => any > | Custom methods that will be added to documents |
new Store<
TSchema
,TMethods
>(name
,options
):Store
<TSchema
,TMethods
>
Defined in: src/data/store.ts:76
Creates a new Store instance
Parameter | Type | Description |
---|---|---|
name | string | The collection name in MongoDB |
options | { indexes : IndexDescription []; methods? : TMethods ; schema : TSchema ; } | Store configuration |
options.indexes | IndexDescription [] | MongoDB indexes to create |
options.methods? | TMethods | Custom methods to add to documents |
options.schema | TSchema | Document schema using Modelence schema types |
Store
<TSchema
, TMethods
>
Property | Modifier | Type | Defined in |
---|---|---|---|
Doc | readonly | EnhancedOmit <InferDocumentType <TSchema >, "_id" > & object & TMethods | src/data/store.ts:61 |
aggregate(
pipeline
,options?
):AggregationCursor
<Document
>
Defined in: src/data/store.ts:346
Aggregates documents using MongoDB’s aggregation framework
Parameter | Type | Description |
---|---|---|
pipeline | Document [] | The aggregation pipeline |
options? | AggregateOptions | Optional options |
AggregationCursor
<Document
>
The aggregation cursor
bulkWrite(
operations
):Promise
<BulkWriteResult
>
Defined in: src/data/store.ts:356
Performs a bulk write operation on the collection
Parameter | Type | Description |
---|---|---|
operations | AnyBulkWriteOperation <InferDocumentType <TSchema >>[] | The operations to perform |
Promise
<BulkWriteResult
>
The result of the bulk write operation
countDocuments(
query
):Promise
<number
>
Defined in: src/data/store.ts:235
Counts the number of documents that match a query
Parameter | Type | Description |
---|---|---|
query | Filter <InferDocumentType <TSchema >> | The query to filter documents |
Promise
<number
>
The number of documents that match the query
deleteMany(
selector
):Promise
<DeleteResult
>
Defined in: src/data/store.ts:335
Deletes multiple documents
Parameter | Type | Description |
---|---|---|
selector | Filter <InferDocumentType <TSchema >> | The selector to find the documents to delete |
Promise
<DeleteResult
>
The result of the delete operation
deleteOne(
selector
):Promise
<DeleteResult
>
Defined in: src/data/store.ts:325
Deletes a single document
Parameter | Type | Description |
---|---|---|
selector | Filter <InferDocumentType <TSchema >> | The selector to find the document to delete |
Promise
<DeleteResult
>
The result of the delete operation
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.
Parameter | Type | Description |
---|---|---|
query | Filter <InferDocumentType <TSchema >> | The query to filter documents |
options? | { limit? : number ; skip? : number ; sort? : Document ; } | Options |
options.limit? | number | - |
options.skip? | number | - |
options.sort? | Document | - |
Promise
<EnhancedOmit
<InferDocumentType
<TSchema
>, "_id"
> & object
& TMethods
[]>
The documents
findById(
id
):Promise
<null
|EnhancedOmit
<InferDocumentType
<TSchema
>,"_id"
> &object
&TMethods
>
Defined in: src/data/store.ts:209
Fetches a single document by its ID
Parameter | Type | Description |
---|---|---|
id | string | ObjectId | The ID of the document to find |
Promise
<null
| EnhancedOmit
<InferDocumentType
<TSchema
>, "_id"
> & object
& TMethods
>
The document, or null if not found
findOne(
query
,options?
):Promise
<null
|EnhancedOmit
<InferDocumentType
<TSchema
>,"_id"
> &object
&TMethods
>
Defined in: src/data/store.ts:168
Parameter | Type |
---|---|
query | Filter <InferDocumentType <TSchema >> |
options? | FindOptions <Document > |
Promise
<null
| EnhancedOmit
<InferDocumentType
<TSchema
>, "_id"
> & object
& TMethods
>
getDatabase():
Db
Defined in: src/data/store.ts:365
Returns the raw MongoDB database instance for advanced operations
Db
The MongoDB database instance
Error if the store is not provisioned
getName():
string
Defined in: src/data/store.ts:93
string
insertMany(
documents
):Promise
<InsertManyResult
<Document
>>
Defined in: src/data/store.ts:267
Inserts multiple documents
Parameter | Type | Description |
---|---|---|
documents | OptionalUnlessRequiredId <InferDocumentType <TSchema >>[] | The documents to insert |
Promise
<InsertManyResult
<Document
>>
The result of the insert operation
insertOne(
document
):Promise
<InsertOneResult
<Document
>>
Defined in: src/data/store.ts:257
Inserts a single document
Parameter | Type | Description |
---|---|---|
document | OptionalUnlessRequiredId <InferDocumentType <TSchema >> | The document to insert |
Promise
<InsertOneResult
<Document
>>
The result of the insert operation
rawCollection():
Collection
<InferDocumentType
<TSchema
>>
Defined in: src/data/store.ts:374
Returns the raw MongoDB collection instance for advanced operations
Collection
<InferDocumentType
<TSchema
>>
The MongoDB collection instance
Error if the store is not provisioned
renameFrom(
oldName
,options?
):Promise
<void
>
Defined in: src/data/store.ts:383
Renames an existing collection to this store’s name, used for migrations
Parameter | Type | Description |
---|---|---|
oldName | string | The previous name of the collection |
options? | { session? : ClientSession ; } | - |
options.session? | ClientSession | - |
Promise
<void
>
Error if the old collection doesn’t exist or if this store’s collection already exists
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
Parameter | Type | Description |
---|---|---|
id | string | ObjectId | The ID of the document to find |
errorHandler? | () => Error | Optional error handler to return a custom error if the document is not found |
Promise
<EnhancedOmit
<InferDocumentType
<TSchema
>, "_id"
> & object
& TMethods
>
The document
requireOne(
query
,options?
,errorHandler?
):Promise
<EnhancedOmit
<InferDocumentType
<TSchema
>,"_id"
> &object
&TMethods
>
Defined in: src/data/store.ts:176
Parameter | Type |
---|---|
query | Filter <InferDocumentType <TSchema >> |
options? | FindOptions <Document > |
errorHandler? | () => Error |
Promise
<EnhancedOmit
<InferDocumentType
<TSchema
>, "_id"
> & object
& TMethods
>
updateMany(
selector
,update
,options?
):Promise
<UpdateResult
<Document
>>
Defined in: src/data/store.ts:300
Updates multiple documents
Parameter | Type | Description |
---|---|---|
selector | Filter <InferDocumentType <TSchema >> | The selector to find the documents to update |
update | UpdateFilter <InferDocumentType <TSchema >> | The MongoDB modifier to apply to the documents |
options? | { session? : ClientSession ; } | - |
options.session? | ClientSession | - |
Promise
<UpdateResult
<Document
>>
The result of the update operation
updateOne(
selector
,update
):Promise
<UpdateResult
<Document
>>
Defined in: src/data/store.ts:278
Updates a single document
Parameter | Type | Description |
---|---|---|
selector | string | ObjectId | Filter <InferDocumentType <TSchema >> | The selector to find the document to update |
update | UpdateFilter <InferDocumentType <TSchema >> | The update to apply to the document |
Promise
<UpdateResult
<Document
>>
The result of the update operation
upsertMany(
selector
,update
):Promise
<UpdateResult
<Document
>>
Defined in: src/data/store.ts:315
Updates multiple documents, or inserts them if they don’t exist
Parameter | Type | Description |
---|---|---|
selector | Filter <InferDocumentType <TSchema >> | The selector to find the documents to update |
update | UpdateFilter <InferDocumentType <TSchema >> | The MongoDB modifier to apply to the documents |
Promise
<UpdateResult
<Document
>>
The result of the update operation
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
Parameter | Type | Description |
---|---|---|
selector | string | ObjectId | Filter <InferDocumentType <TSchema >> | The selector to find the document to update |
update | UpdateFilter <InferDocumentType <TSchema >> | The MongoDB modifier to apply to the document |
Promise
<UpdateResult
<Document
>>
The result of the update operation