field | Description | Example |
---|---|---|
<MODEL_NAME>Model | database model declared in model.document.ts |
|
logger | module that logs to stdout is declared by default |
|
database model declared in model.document.ts
async loadMyDrone() {
const drone = await this.droneModel.loadDroneByName("myDrone");
return drone;
}
module that logs to stdout is declared by default
async onModuleInit() {
this.logger.log("drone module is successfully initialized");
}
method | Description | Example |
---|---|---|
getModel(id: string) | load the document with the corresponding id, and return an error if it does not exist |
|
loadModel(id: string) | load the document with the corresponding id, and return null if it does not exist |
|
loadModelMany(ids: string[]) | load the documents with the corresponding ids, and return an array of documents or nulls |
|
createModel(data: db.ModelInput) | create a document with the data of db.ModelInput |
|
updateModel(id: string, data: db.ModelInput) | update the document with the corresponding id, and return the updated document |
|
removeModel(id: string) | soft-delete the document with the corresponding id, and return the deleted document |
|
load the document with the corresponding id, and return an error if it does not exist
const drone = await this.getDrone("ObjectId");
load the document with the corresponding id, and return null if it does not exist
const drone = await this.loadDrone("ObjectId");
load the documents with the corresponding ids, and return an array of documents or nulls
const drones = await this.loadDroneMany(["ObjectId", "ObjectId"]);
create a document with the data of db.ModelInput
const drone = await this.createDrone({ name: "myDrone" });
update the document with the corresponding id, and return the updated document
const drone = await this.updateDrone("ObjectId", { name: "newName" });
soft-delete the document with the corresponding id, and return the deleted document
const drone = await this.removeDrone("ObjectId"); // drone.status = "inactive"
method | Description | Example |
---|---|---|
list<Query_KEY>(...queryArgs: QueryArgs, option?: ListQueryOption) | return a 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 a list of document ids that match the query condition |
|
find<Query_KEY>(...queryArgs: QueryArgs, option?: FindQueryOption) | return a document that matches the query condition, or null if it does not exist FindQueryOption = { skip?: number, sort?: Sort, sample?: boolean } |
|
findId<Query_KEY>(...queryArgs: QueryArgs, option?: FindQueryOption) | return a document id that matches the query condition, or null if it does not exist |
|
pick<Query_KEY>(...queryArgs: QueryArgs, option?: FindQueryOption) | return a document that matches the query condition, or throw an error if it does not exist |
|
pickId<Query_KEY>(...queryArgs: QueryArgs, option?: FindQueryOption) | return a document id that matches the query condition, or throw an error if it does not exist |
|
exists<Query_KEY>(...queryArgs: QueryArgs) | return true if a document that matches the query condition exists, or false if it does not exist |
|
count<Query_KEY>(...queryArgs: QueryArgs) | return the number of documents that match the query condition |
|
insight<Query_KEY>(...queryArgs: QueryArgs) | aggregate the ModelInsight of the documents that match the query condition |
|
return a 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 a list of document ids that match the query condition
const droneIds = await this.listIdsByName("myDrone"); // string[]
return a document that matches the query condition, or null if it does not exist FindQueryOption = { skip?: number, sort?: Sort, sample?: boolean }
const drone = await this.findByName("myDrone"); // db.Drone | null
return a document id that matches the query condition, or null if it does not exist
const droneId = await this.findIdByName("myDrone"); // string | null
return a document that matches the query condition, or throw an error if it does not exist
const drone = await this.pickByName("myDrone"); // db.Drone
return a document id that matches the query condition, or throw an error if it does not exist
const droneId = await this.pickIdByName("myDrone"); // string
return true if a document that matches the query condition exists, or false if it does not exist
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
aggregate the ModelInsight of the documents that match the query condition
const droneInsight = await this.insightByName("myDrone"); // cnst.DroneInsight
async _preCreate(data: DataInputOf<db.DroneInput, db.Drone>):
Promise<DataInputOf<db.DroneInput, db.Drone>> {
if (data.name === 'myDrone') throw new Error('No more myDrone');
return data;
}
@Service('DroneService')
export class DroneService extends DbService(db.droneDb) {
#myCustomField = "this is my custom field";
async myCustomMethod(arg: string) {
this.logger.info("this is my custom method");
return arg;
}
}
import type * as srv from '../srv';
@Service('DroneService')
export class DroneService extends DbService(db.droneDb) {
@Srv() missionService: srv.MissionService;
async applyMission(missionId: string) {
const mission = await this.missionService.getMission(missionId);
await drone.setMission(missionId).save();
return drone;
}
}
import { Logger } from '@core/common';
import axios, { type AxiosInstance } from 'axios';
export class MyApi {
url: string;
#api: AxiosInstance;
#logger = new Logger('MyApi');
constructor(url: string) {
this.url = url;
this.#api = axios.create({ baseURL: url, timeout: 5000 });
}
async getSomething() {
try {
const { data } = await this.#api.get('/something');
return data;
} catch (e) {
this.#logger.warn("Failed to get from MyApi: " + e.message);
return null;
}
}
}
export * from './myApi';
// import ...
import { MyApi } from '@<APP_NAME>/nest';
export const registerDroneModule = (url?: string) => {
const myApi = new MyApi(url ?? "http://localhost");
return databaseModuleOf(
{
constant: cnst.droneCnst,
database: db.droneDb,
signal: DroneSignal,
service: DroneService,
uses: { myApi }, // inject variable here
},
allSrvs
);
};
@Service('DroneService')
export class DroneService extends DbService(db.droneDb) {
@Use() myApi: MyApi;
async getSomething(){
const something = await this.myApi.getSomething();
return something;
}
}
@Service('DroneService')
export class DroneService extends DbService(db.droneDb) {
@Websocket() websocket: Websocket<DroneSignal>;
async publishDroneTmtc() {
this.websocket.droneTmtcUpdated(drone.id, drone.tmtc);
}
}
@Service('DroneService')
export class DroneService extends DbService(db.droneDb) {
@Queue() droneQueue: Queue<DroneSignal>;
async queueSomething(droneId: string) {
const job = await this.droneQueue.queueSomething(droneId);
return job;
}
}
import { Cron, Interval } from '@core/server';
@Service("DroneService")
export class DroneService extends DbService(db.droneDb) {
@Interval(1000) // ms
async doSomeIntervalJob() {
this.logger.log("interval job done");
}
@Cron("* * * * * *")
async doSomeCronJob() {
this.logger.log("cron job done");
}
}
import { Try } from '@core/server';
@Service("DroneService")
export class DroneService extends DbService(db.droneDb) {
@Try()
async trySomething() {
throw new Error("Some Error")
}
async doStuff() {
// ...
await trySomething(); // error is not thrown
// ...
}
}
import { Transaction } from '@core/server';
import type * as srv from '../srv';
@Service('DroneService')
export class DroneService extends DbService(db.droneDb) {
@Srv() missionService: srv.MissionService;
@Transaction()
async applyMission(missionId: string) {
const mission = await this.missionService.getMission(missionId);
await drone.setMission(missionId).save();
return drone;
}
}