Example
Type Parameters
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 |
Constructors
Constructor
new Store<Defined in: src/data/store.ts:79 Creates a new Store instanceTSchema
,TMethods
>(name
,options
):Store
<TSchema
,TMethods
>
Parameters
Parameter | Type | Description |
---|---|---|
name | string | The collection name in MongoDB |
options | { indexes : IndexDescription []; methods? : TMethods ; schema : TSchema ; searchIndexes? : SearchIndexDescription []; } | 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 |
options.searchIndexes? | SearchIndexDescription [] | MongoDB Atlas Search |
Returns
Store
<TSchema
, TMethods
>
Properties
Property | Modifier | Type | Defined in |
---|---|---|---|
Doc | readonly | EnhancedOmit <InferDocumentType <TSchema >, "_id" > & object & TMethods | src/data/store.ts:63 |
Methods
aggregate()
aggregate(Defined in: src/data/store.ts:377 Aggregates documents using MongoDB’s aggregation frameworkpipeline
,options?
):AggregationCursor
<Document
>
Parameters
Parameter | Type | Description |
---|---|---|
pipeline | Document [] | The aggregation pipeline |
options? | AggregateOptions | Optional options |
Returns
AggregationCursor
<Document
>
The aggregation cursor
bulkWrite()
bulkWrite(Defined in: src/data/store.ts:387 Performs a bulk write operation on the collectionoperations
):Promise
<BulkWriteResult
>
Parameters
Parameter | Type | Description |
---|---|---|
operations | AnyBulkWriteOperation <InferDocumentType <TSchema >>[] | The operations to perform |
Returns
Promise
<BulkWriteResult
>
The result of the bulk write operation
countDocuments()
countDocuments(Defined in: src/data/store.ts:266 Counts the number of documents that match a queryquery
):Promise
<number
>
Parameters
Parameter | Type | Description |
---|---|---|
query | Filter <InferDocumentType <TSchema >> | The query to filter documents |
Returns
Promise
<number
>
The number of documents that match the query
deleteMany()
deleteMany(Defined in: src/data/store.ts:366 Deletes multiple documentsselector
):Promise
<DeleteResult
>
Parameters
Parameter | Type | Description |
---|---|---|
selector | Filter <InferDocumentType <TSchema >> | The selector to find the documents to delete |
Returns
Promise
<DeleteResult
>
The result of the delete operation
deleteOne()
deleteOne(Defined in: src/data/store.ts:356 Deletes a single documentselector
):Promise
<DeleteResult
>
Parameters
Parameter | Type | Description |
---|---|---|
selector | Filter <InferDocumentType <TSchema >> | The selector to find the document to delete |
Returns
Promise
<DeleteResult
>
The result of the delete operation
fetch()
fetch(Defined in: src/data/store.ts:277 Fetches multiple documents, equivalent to Node.js MongoDB driver’squery
,options?
):Promise
<EnhancedOmit
<InferDocumentType
<TSchema
>,"_id"
> &object
&TMethods
[]>
find
and toArray
methods combined.
Parameters
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 | - |
Returns
Promise
<EnhancedOmit
<InferDocumentType
<TSchema
>, "_id"
> & object
& TMethods
[]>
The documents
findById()
findById(Defined in: src/data/store.ts:240 Fetches a single document by its IDid
):Promise
<null
|EnhancedOmit
<InferDocumentType
<TSchema
>,"_id"
> &object
&TMethods
>
Parameters
Parameter | Type | Description |
---|---|---|
id | string | ObjectId | The ID of the document to find |
Returns
Promise
<null
| EnhancedOmit
<InferDocumentType
<TSchema
>, "_id"
> & object
& TMethods
>
The document, or null if not found
findOne()
findOne(Defined in: src/data/store.ts:199query
,options?
):Promise
<null
|EnhancedOmit
<InferDocumentType
<TSchema
>,"_id"
> &object
&TMethods
>
Parameters
Parameter | Type |
---|---|
query | Filter <InferDocumentType <TSchema >> |
options? | FindOptions <Document > |
Returns
Promise
<null
| EnhancedOmit
<InferDocumentType
<TSchema
>, "_id"
> & object
& TMethods
>
getDatabase()
getDatabase(): Db
Defined in: src/data/store.ts:396
Returns the raw MongoDB database instance for advanced operations
Returns
Db
The MongoDB database instance
Throws
Error if the store is not provisionedgetName()
getName(): string
Defined in: src/data/store.ts:99
Returns
string
insertMany()
insertMany(Defined in: src/data/store.ts:298 Inserts multiple documentsdocuments
):Promise
<InsertManyResult
<Document
>>
Parameters
Parameter | Type | Description |
---|---|---|
documents | OptionalUnlessRequiredId <InferDocumentType <TSchema >>[] | The documents to insert |
Returns
Promise
<InsertManyResult
<Document
>>
The result of the insert operation
insertOne()
insertOne(Defined in: src/data/store.ts:288 Inserts a single documentdocument
):Promise
<InsertOneResult
<Document
>>
Parameters
Parameter | Type | Description |
---|---|---|
document | OptionalUnlessRequiredId <InferDocumentType <TSchema >> | The document to insert |
Returns
Promise
<InsertOneResult
<Document
>>
The result of the insert operation
rawCollection()
rawCollection():Defined in: src/data/store.ts:405 Returns the raw MongoDB collection instance for advanced operationsCollection
<InferDocumentType
<TSchema
>>
Returns
Collection
<InferDocumentType
<TSchema
>>
The MongoDB collection instance
Throws
Error if the store is not provisionedrenameFrom()
renameFrom(Defined in: src/data/store.ts:414 Renames an existing collection to this store’s name, used for migrationsoldName
,options?
):Promise
<void
>
Parameters
Parameter | Type | Description |
---|---|---|
oldName | string | The 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 existsrequireById()
requireById(Defined in: src/data/store.ts:252 Fetches a single document by its ID, or throws an error if not foundid
,errorHandler?
):Promise
<EnhancedOmit
<InferDocumentType
<TSchema
>,"_id"
> &object
&TMethods
>
Parameters
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 |
Returns
Promise
<EnhancedOmit
<InferDocumentType
<TSchema
>, "_id"
> & object
& TMethods
>
The document
requireOne()
requireOne(Defined in: src/data/store.ts:207query
,options?
,errorHandler?
):Promise
<EnhancedOmit
<InferDocumentType
<TSchema
>,"_id"
> &object
&TMethods
>
Parameters
Parameter | Type |
---|---|
query | Filter <InferDocumentType <TSchema >> |
options? | FindOptions <Document > |
errorHandler? | () => Error |
Returns
Promise
<EnhancedOmit
<InferDocumentType
<TSchema
>, "_id"
> & object
& TMethods
>
updateMany()
updateMany(Defined in: src/data/store.ts:331 Updates multiple documentsselector
,update
,options?
):Promise
<UpdateResult
<Document
>>
Parameters
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 | - |
Returns
Promise
<UpdateResult
<Document
>>
The result of the update operation
updateOne()
updateOne(Defined in: src/data/store.ts:309 Updates a single documentselector
,update
):Promise
<UpdateResult
<Document
>>
Parameters
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 |
Returns
Promise
<UpdateResult
<Document
>>
The result of the update operation
upsertMany()
upsertMany(Defined in: src/data/store.ts:346 Updates multiple documents, or inserts them if they don’t existselector
,update
):Promise
<UpdateResult
<Document
>>
Parameters
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 |
Returns
Promise
<UpdateResult
<Document
>>
The result of the update operation
upsertOne()
upsertOne(Defined in: src/data/store.ts:320 Updates a single document, or inserts it if it doesn’t existselector
,update
):Promise
<UpdateResult
<Document
>>
Parameters
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 |
Returns
Promise
<UpdateResult
<Document
>>
The result of the update operation