@Database.Input(() => cnst.DroneInput)
export class DroneInput extends Input(cnst.DroneInput) {}
@Database.Document(() => cnst.Drone)
export class Drone extends Document(cnst.Drone) {
activate() {
this.status = 'active';
return this;
}
offline() {
this.status = 'offline';
return this;
}
}
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
await drone.refresh();
set the data of the document
drone.set({ name: "newName" });
save the document to the database (create or update)
await 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
const drone = await this.getDrone("ObjectId");
load the document with the id, return null if not found
const drone = await this.loadDrone("ObjectId");
load the documents with the ids, return null if not found
const drones = await this.loadDroneMany(["ObjectId", "ObjectId"]);
create the document with the data
const drone = await this.createDrone({ name: "myDrone" });
update the document with the id and the data
const drone = await this.updateDrone("ObjectId", { name: "newName" });
soft-delete the document with the id
const 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 }
const drones = await this.listByName("myDrone"); // db.Drone[]
return the list of document ids that match the query condition
const 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 }
const drone = await this.findByName("myDrone"); // db.Drone | null
return the document id that matches the query condition, return null if not found
const droneId = await this.findIdByName("myDrone"); // string | null
return the document that matches the query condition, throw error if not found
const drone = await this.pickByName("myDrone"); // db.Drone
return the document id that matches the query condition, throw error if not found
const droneId = await this.pickIdByName("myDrone"); // string
return true if the document exists, false if not found
const droneExists = await this.existsByName("myDrone"); // string | boolean
return the number of documents that match the query condition
const droneCount = await this.countByName("myName"); // number
return the ModelInsight of the documents that match the query condition
const 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 |
|
addSummary | increment the value of the specific key in the summary model |
|
moveSummary | increment/decrement the value of the specific key in the summary model |
|
subSummary | decrement the value of the specific key in the summary model |
|
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
const drone = await this.Drone.pickById("ObjectId"); // db.Drone
return the document that matches the query condition, throw error if not found
const drone = await this.Drone.pickOne({ name: "myDrone" }); // db.Drone
return the document with the id, update the data, throw error if not found
const 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
const drone = await this.Drone.pickOneAndWrite({ name: "myDrone" }, { name: "newName" }); // db.Drone
increment the value of the specific key in the summary model
await this.Drone.addSummary("active", 1);
increment/decrement the value of the specific key in the summary model
await this.Drone.moveSummary("active", "offline", 1);
decrement the value of the specific key in the summary model
await this.Drone.subSummary("active", 1);
sample the documents that match the query condition
const drones = await this.Drone.sample({ name: "myDrone" }, 5); // db.Drone[];
sample the document that matches the query condition, return null if not found
const 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
@Loader.ByField('name') droneNameLoader: Loader<string, Drone>;
async loadDroneByName(name: string) {
const drone = await this.droneNameLoader.load("myDrone");
return drone; // db.Drone | null
}
dataloader that loads multiple documents by a specific field value
@Loader.ByArrayField("name") droneNameArrayLoader: Loader<string, Drone[]>;
async loadDroneArrayByName(name: string) {
const drones = await this.droneNameArrayLoader.load("myDrone");
return drones; // db.Drone[] | null
}
dataloader that loads a document by a query condition
@Loader.ByQuery(["name"]) droneNameQueryLoader: Loader<{ name: string }, Drone>;
async loadDroneWithQueryByName(name: string) {
const drone = await this.droneNameQueryLoader.load({ name: "myDrone" });
return drone; // db.Drone | null
}
const drone = await this.droneLoader.load("ObjectId"); // db.Drone | null
const 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.
@Database.Middleware(() => cnst.Drone)
export class DroneMiddleware extends Middleware(DroneModel, Drone) {
onSchema(schema: SchemaOf<DroneModel, Drone>) {
schema.pre<Drone>("save", function (next) {
const model = this.constructor as DroneModel["Drone"];
if(drone.name === "myDrone") console.log("No more myDrone name");
next();
});
schema.index({ name: 1, status: 1 });
}
}
import type * as db from '../db'
import pattern for type reference