Model
Note, that in diana-db/odm Model< T > is generic class
import { Model } from '@diana-db/odm';
import { userSchema, postSchema } from '../schemas';
const userModel = new Model<User>({
database: 'test',
collection: 'user',
name: 'User',
schema: userSchema,
// dianaDb: secondClient, // optional: which DianaDb instance this model uses
});
await userModel.init();
const postModel = new Model<Post>({
database: 'test',
collection: 'post',
name: 'Post',
schema: postSchema,
});
await postModel.init(); Model Options
Model Options is an object with these properties:
database:string, database name there collection will be createdcollection:string, collection name there documents will be storedname:string, Model nameschema:special object that define collection and model SchemadianaDb:optional DianaDb instance the model sends its requests through. Defaults to the instance most recently constructed before the model, so creating another client later doesn't re-route it
init()creates the collection, or reconciles a changed schema with the server. It is called lazily by every other method, is safe to call concurrently (callers share one attempt) and can be retried after a failure. Creating a collection needs write access; updating a changed schema needs manage access, and fails if the remote schema is locked.insertandupdateapply defaults and setters to a copy, so the object you pass is not modified.find,count,updateandremoveaccept one FindQuery or an array;find()andcount()without filters match every document.
Model<T> Methods
| Method | Description | Returns |
|---|---|---|
init() | Creates the collection or reconciles its schema. Called lazily by the other methods | Promise<void> |
Model.init() (static) | Initializes every constructed model | Promise<void> |
insert(data, transactionId?) | Validates and inserts a document into the collection. Applies default values where defined. | Promise<T & { _id: string }> |
insertMany(documents, transactionId?, batchSize?) | Validates and inserts many documents, one request per batch (1000 documents unless batchSize says otherwise). See Insert many documents. | Promise<(T & { _id: string })[]> |
createView(name, transforms) | Creates materialized view basing on the provided transform queries (aggregation pipelines) | Promise<{ view: string; action: 'created' }> |
removeView(name) | Removes a materialized view | Promise<{ view: string; action: 'removed' }> |
getViews() | Materialized views defined on this collection | Promise<{ [view]: { transformQueries: TransformQuery[] }}> |
find(filters?, transforms?, sort?, skip?, limit?, transactionId?) | Finds documents matching filters, with optional transform, sort and pagination. | Promise<T[]> |
max(key: keyof T | string) | Finds documents having max value of chosen field | Promise<T[]> |
min(key: keyof T | string) | Finds documents having min value of chosen field | Promise<T[]> |
distinct(key: keyof T) | Returns an array of distinct values of chosen field | Promise<any[]> |
findByView(name, filter?, sort?, skip?, limit?) | Finds documents using materialized view, defined by provided name, filter, with optional sort and pagination. | Promise<V[]> |
countByView(name, filter?) | Counts documents using materialized view, defined by provided name and filter. | Promise<number> |
count(filters?, transforms?, transactionId?) | Counts documents matching filters, optionally using transformations. | Promise<number> |
update(filters, updateData, transactionId?) | Updates documents that match the filters with the specified data. | Promise<{ found: number, modified: number }> |
remove(filters, transactionId?) | Deletes documents that match the specified filters. | Promise<{ found: number, removed: number }> |
lock() | Locks the Schema for current Model (collection) | Promise<{ collection: string, locked: boolean }> |
unlock() | Unlocks the Schema for current Model (collection) | Promise<{ collection: string, locked: boolean }> |