1@Database.Input(() => cnst.DroneInput)
2export class DroneInput extends Input(cnst.DroneInput) {}
1@Database.Document(() => cnst.Drone)
2export class Drone extends Document(cnst.Drone) {
3 activate() {
4 this.status = 'active';
5 return this;
6 }
7 offline() {
8 this.status = 'offline';
9 return this;
10 }
11}
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 | null
return the document id that matches the query condition, return null if not found
1const droneId = await this.findIdByName("myDrone"); // string | null
return the document that matches the query condition, throw error if not found
1const drone = await this.pickByName("myDrone"); // db.Drone
return the document id that matches the query condition, throw error if not found
1const droneId = await this.pickIdByName("myDrone"); // string
return true if the document exists, false if not found
1const droneExists = await this.existsByName("myDrone"); // string | boolean
return the number of documents that match the query condition
1const droneCount = await this.countByName("myName"); // number
return 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.Drone
return the document that matches the query condition, throw error if not found
1const drone = await this.Drone.pickOne({ name: "myDrone" }); // db.Drone
return the document with the id, update the data, throw error if not found
1const drone = await this.Drone.pickAndWrite("ObjectId", { name: "newName" }); // db.Drone
return 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.Drone
sample 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.
1@Database.Middleware(() => cnst.Drone)
2export class DroneMiddleware extends Middleware(DroneModel, Drone) {
3 onSchema(schema: SchemaOf<DroneModel, Drone>) {
4 schema.pre<Drone>("save", function (next) {
5 const model = this.constructor as DroneModel["Drone"];
6 if(drone.name === "myDrone") console.log("No more myDrone name");
7 next();
8 });
9 schema.index({ name: 1, status: 1 });
10 }
11}
import type * as db from '../db'
import pattern for type reference