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
1async loadMyDrone() {
2 const drone = await this.droneModel.loadDroneByName("myDrone");
3 return drone;
4}
module that logs to stdout is declared by default
1async initialize() {
2 this.logger.log("drone module is successfully initialized");
3}
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
1const drone = await this.getDrone("ObjectId");
load the document with the corresponding id, and return null if it does not exist
1const drone = await this.loadDrone("ObjectId");
load the documents with the corresponding ids, and return an array of documents or nulls
1const drones = await this.loadDroneMany(["ObjectId", "ObjectId"]);
create a document with the data of db.ModelInput
1const drone = await this.createDrone({ name: "myDrone" });
update the document with the corresponding id, and return the updated document
1const drone = await this.updateDrone("ObjectId", { name: "newName" });
soft-delete the document with the corresponding id, and return the deleted document
1const 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 }
1const drones = await this.listByName("myDrone"); // db.Drone[]
return a list of document ids that match the query condition
1const 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 }
1const drone = await this.findByName("myDrone"); // db.Drone | null
return a document id that matches the query condition, or null if it does not exist
1const droneId = await this.findIdByName("myDrone"); // string | null
return a document that matches the query condition, or throw an error if it does not exist
1const 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
1const droneId = await this.pickIdByName("myDrone"); // string
return true if a document that matches the query condition exists, or false if it does not exist
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
aggregate the ModelInsight of the documents that match the query condition
1const droneInsight = await this.insightByName("myDrone"); // cnst.DroneInsight
1async _preCreate(data: DataInputOf<db.DroneInput, db.Drone>):
2Promise<DataInputOf<db.DroneInput, db.Drone>> {
3 if (data.name === 'myDrone') throw new Error('No more myDrone');
4 return data;
5}
1@Service('DroneService')
2export class DroneService extends DbService(db.droneDb) {
3 #myCustomField = "this is my custom field";
4
5 async myCustomMethod(arg: string) {
6 this.logger.info("this is my custom method");
7 return arg;
8 }
9}
1import type * as srv from '../srv';
2
3@Service('DroneService')
4export class DroneService extends DbService(db.droneDb) {
5 @Srv() missionService: srv.MissionService;
6
7 async applyMission(missionId: string) {
8 const mission = await this.missionService.getMission(missionId);
9 await drone.setMission(missionId).save();
10 return drone;
11 }
12}
1import { Logger } from '@core/common';
2import axios, { type AxiosInstance } from 'axios';
3
4export class MyApi {
5 url: string;
6 #api: AxiosInstance;
7 #logger = new Logger('MyApi');
8
9 constructor(url: string) {
10 this.url = url;
11 this.#api = axios.create({ baseURL: url, timeout: 5000 });
12 }
13
14 async getSomething() {
15 try {
16 const { data } = await this.#api.get('/something');
17 return data;
18 } catch (e) {
19 this.#logger.warn("Failed to get from MyApi: " + e.message);
20 return null;
21 }
22 }
23}
1export * from './myApi';
1// import ...
2import { MyApi } from '@<APP_NAME>/nest';
3
4export const registerDroneModule = (url?: string) => {
5 const myApi = new MyApi(url ?? "http://localhost");
6 return databaseModuleOf(
7 {
8 constant: cnst.droneCnst,
9 database: db.droneDb,
10 signal: DroneSignal,
11 service: DroneService,
12 uses: { myApi }, // inject variable here
13 },
14 allSrvs
15 );
16};
1@Service('DroneService')
2export class DroneService extends DbService(db.droneDb) {
3 @Use() myApi: MyApi;
4
5 async getSomething(){
6 const something = await this.myApi.getSomething();
7 return something;
8 }
9}
1@Service('DroneService')
2export class DroneService extends DbService(db.droneDb) {
3 @Websocket() websocket: Websocket<DroneSignal>;
4
5 async publishDroneTmtc() {
6 this.websocket.droneTmtcUpdated(drone.id, drone.tmtc);
7 }
8}
1@Service('DroneService')
2export class DroneService extends DbService(db.droneDb) {
3 @Queue() droneQueue: Queue<DroneSignal>;
4
5 async queueSomething(droneId: string) {
6 const job = await this.droneQueue.queueSomething(droneId);
7 return job;
8 }
9}
1import { Try } from '@core/server';
2
3@Service("DroneService")
4export class DroneService extends DbService(db.droneDb) {
5
6 @Try()
7 async trySomething() {
8 throw new Error("Some Error")
9 }
10
11 async doStuff() {
12 // ...
13 await trySomething(); // error is not thrown
14 // ...
15 }
16}
1import { Transaction } from '@core/server';
2import type * as srv from '../srv';
3
4@Service('DroneService')
5export class DroneService extends DbService(db.droneDb) {
6 @Srv() missionService: srv.MissionService;
7
8 @Transaction()
9 async applyMission(missionId: string) {
10 const mission = await this.missionService.getMission(missionId);
11 await drone.setMission(missionId).save();
12 return drone;
13 }
14}