1export class DroneInput extends Input(cnst.DroneInput) {}1export class Drone extends Document(cnst.Drone) {
2 activate() {
3 this.status = 'active';
4 return this;
5 }
6 offline() {
7 this.status = 'offline';
8 return this;
9 }
10}| method | Description | Example |
|---|---|---|
| refresh | refresh the data of the document from the database | |
| set | set the data of the document | |
| save | save the document to the database (create or update) | |
refresh the data of the document from the database
1await drone.refresh();set the data of the document
1drone.set({ name: "newName" });save the document to the database (create or update)
1await drone.save();| method | Description | Example |
|---|---|---|
| getModel(id: string) | load the document with the id, return null if not found | |
| loadModel(id: string) | load the document with the id, return null if not found | |
| loadModelMany(ids: string[]) | load the documents with the ids, return null if not found | |
| createModel(data: db.ModelInput) | create the document with the data | |
| updateModel(id: string, data: db.ModelInput) | update the document with the id and the data | |
| removeModel(id: string) | soft-delete the document with the id | |
load the document with the id, return null if not found
1const drone = await this.getDrone("ObjectId");load the document with the id, return null if not found
1const drone = await this.loadDrone("ObjectId");load the documents with the ids, return null if not found
1const drones = await this.loadDroneMany(["ObjectId", "ObjectId"]);create the document with the data
1const drone = await this.createDrone({ name: "myDrone" });update the document with the id and the data
1const drone = await this.updateDrone("ObjectId", { name: "newName" });soft-delete the document with the id
1const drone = await this.removeDrone("ObjectId"); // drone.status = "inactive"| method | Description | Example |
|---|---|---|
| list<Query_KEY>(...queryArgs: QueryArgs, option?: ListQueryOption) | return the list of documents that match the query condition ListQueryOption = { skip?: number, limit?: number, sort?: Sort, sample?: boolean } | |
| listIds<Query_KEY>(...queryArgs: QueryArgs, option?: ListQueryOption) | return the list of document ids that match the query condition | |
| find<Query_KEY>(...queryArgs: QueryArgs, option?: FindQueryOption) | return the document that matches the query condition, return null if not found FindQueryOption = { skip?: number, sort?: Sort, sample?: boolean } | |
| findId<Query_KEY>(...queryArgs: QueryArgs, option?: FindQueryOption) | return the document id that matches the query condition, return null if not found | |
| pick<Query_KEY>(...queryArgs: QueryArgs, option?: FindQueryOption) | return the document that matches the query condition, throw error if not found | |
| pickId<Query_KEY>(...queryArgs: QueryArgs, option?: FindQueryOption) | return the document id that matches the query condition, throw error if not found | |
| exists<Query_KEY>(...queryArgs: QueryArgs) | return true if the document exists, false if not found | |
| count<Query_KEY>(...queryArgs: QueryArgs) | return the number of documents that match the query condition | |
| insight<Query_KEY>(...queryArgs: QueryArgs) | return the ModelInsight of the documents that match the query condition | |
return the list of documents that match the query condition ListQueryOption = { skip?: number, limit?: number, sort?: Sort, sample?: boolean }
1const drones = await this.listByName("myDrone"); // db.Drone[]return the list of document ids that match the query condition
1const droneIds = await this.listIdsByName("myDrone"); // string[]return the document that matches the query condition, return null if not found FindQueryOption = { skip?: number, sort?: Sort, sample?: boolean }
1const drone = await this.findByName("myDrone"); // db.Drone | nullreturn the document id that matches the query condition, return null if not found
1const droneId = await this.findIdByName("myDrone"); // string | nullreturn the document that matches the query condition, throw error if not found
1const drone = await this.pickByName("myDrone"); // db.Dronereturn the document id that matches the query condition, throw error if not found
1const droneId = await this.pickIdByName("myDrone"); // stringreturn true if the document exists, false if not found
1const droneExists = await this.existsByName("myDrone"); // string | booleanreturn the number of documents that match the query condition
1const droneCount = await this.countByName("myName"); // numberreturn the ModelInsight of the documents that match the query condition
1const droneInsight = await this.insightByName("myDrone"); // cnst.DroneInsight| method | Description | Example |
|---|---|---|
| pickById | return the document with the id, throw error if not found | |
| pickOne | return the document that matches the query condition, throw error if not found | |
| pickAndWrite | return the document with the id, update the data, throw error if not found | |
| pickOneAndWrite | return the document that matches the query condition, update the data, throw error if not found | |
| sample | sample the documents that match the query condition | |
| sampleOne | sample the document that matches the query condition, return null if not found | |
return the document with the id, throw error if not found
1const drone = await this.Drone.pickById("ObjectId"); // db.Dronereturn the document that matches the query condition, throw error if not found
1const drone = await this.Drone.pickOne({ name: "myDrone" }); // db.Dronereturn the document with the id, update the data, throw error if not found
1const drone = await this.Drone.pickAndWrite("ObjectId", { name: "newName" }); // db.Dronereturn the document that matches the query condition, update the data, throw error if not found
1const drone = await this.Drone.pickOneAndWrite({ name: "myDrone" }, { name: "newName" }); // db.Dronesample the documents that match the query condition
1const drones = await this.Drone.sample({ name: "myDrone" }, 5); // db.Drone[];sample the document that matches the query condition, return null if not found
1const drone = await this.Drone.sampleOne({ name: "myDrone" }); // db.Drone | null| decorator | Description | Example |
|---|---|---|
| @Loader.ByField | dataloader that loads a single document by a specific field value | |
| @Loader.ByArrayField | dataloader that loads multiple documents by a specific field value | |
| @Loader.ByQuery | dataloader that loads a document by a query condition | |
dataloader that loads a single document by a specific field value
1@Loader.ByField('name') droneNameLoader: Loader<string, Drone>;
2
3async loadDroneByName(name: string) {
4 const drone = await this.droneNameLoader.load("myDrone");
5 return drone; // db.Drone | null
6}dataloader that loads multiple documents by a specific field value
1@Loader.ByArrayField("name") droneNameArrayLoader: Loader<string, Drone[]>;
2
3async loadDroneArrayByName(name: string) {
4 const drones = await this.droneNameArrayLoader.load("myDrone");
5 return drones; // db.Drone[] | null
6}dataloader that loads a document by a query condition
1@Loader.ByQuery(["name"]) droneNameQueryLoader: Loader<{ name: string }, Drone>;
2
3async loadDroneWithQueryByName(name: string) {
4 const drone = await this.droneNameQueryLoader.load({ name: "myDrone" });
5 return drone; // db.Drone | null
6}1const drone = await this.droneLoader.load("ObjectId"); // db.Drone | null
2const drones = await this.droneLoader.loadMany(["ObjectId", "ObjectId"]); // db.Drone[]Query performance can be significantly improved through Schema Indexing in mongoDB.
You can set the logic for data consistency before calling the document.save() function.
1export class DroneMiddleware extends Middleware(DroneModel, Drone) {
2 onSchema(schema: SchemaOf<DroneModel, Drone>) {
3 schema.pre<Drone>("save", function (next) {
4 const model = this.constructor as DroneModel["Drone"];
5 if(drone.name === "myDrone") console.log("No more myDrone name");
6 next();
7 });
8 schema.index({ name: 1, status: 1 });
9 }
10}import type * as db from '../db'import pattern for type reference