Simpleapp auto generate lot of backend source codes. Most of it maintain by generator and developer shouldn't modify it to avoid future compatibility issue.
To make life easy, this document only explain the backend file which maintain by developer, which is
service class.
It is tiny class for developer customize the behavior of every document. Most of the time you no need to change it unless you want something beyond CRUD process. Usually, we modify this file for 2 purpose:
- add your before/after hook. such as beforeCreate, afterCreate, beforeSearch, afterSearch and etc.
- add your own execution methods, which can bind to jsonschema
additionalApisproperty
Example better than 100 words, you may refer:
- src/simpleapp/services/branch.service:
- it define autorunning number into
branchIdbefore create new record - after new branch created, it auto generate suitable document number formats. which use own customize executable
generateDefaultDocNumbers*appuseris compulsory parameter which store current user context, we always carry this variable in backend. we can obtain current user, currentXorg, database transactions and etc from this context
- src/simpleapp/services/docno.service:
- there is manual defined execution
listDocFormats. - System maintain jsonschema
Docnoformat, defined below api setting bind tolistDocFormats
{
"x-simpleapp-config":{
additionalApis:[{
"action":"listDocFormats",
"entryPoint":"/listdocformats/:doctype",
"requiredRole":["User"],
"method":'get',
"execute":"listDocFormats",
"description":"get list of document format for 1 doctype"
} ],
...
},
}- Once
additionalApisproperties defined properly, the controller, and frontend client automatically support the api. - Controller expect parameter
doctypeexists in this methods, cause entrypoint declare that appuseralways first parameter, and 2nd parameter will base on entrypoint.
- src/simpleapp/services/user.service:
- after specifc user deleted, it will remove all existing user permission in that tenant
- src/simpleapp/services/autoinc.service:
*it provide autoincreament number for tenantId/orgId/branchId with execution
generateNextNo
{
"x-simpleapp-config":{
additionalApis:[{
action:"generateNextNo",
entrypoint:"gennextno/:collection/:field",
requiredrole:["User"],
method: 'get',
execute:"generateNextNo",
description:"Get next no and trigger increase nextno"
}],
}
}- same as
docno, there is api bind to it. however there is 2 parameters inentrypoint. mean we will have 3 parameters in executionsappuser,collection,field
Backend build on top of nestjs, if you role is backend developer you may spend time to learn nestjs from official website. Such as
- controller
- service class
- mongoose + model
- injection
- modules
Sometimes we may create additional API which is not related to jsonschema, in that case you may create under src. You may refer example: src/profile
Once you familiar wit nestjs you may refer below source codes to understand what is running behind the scene:
src/simpleapp/generate
apischemas: schema for openapicommons: many system dictionaries, providers, controlscontrollers: api controllersdefaults: default value for every schemasjsonschemas: polished version of jsonschemasmodels: mongoose schemaprocessor: parent class of service class, define something base on schema and we don't want you to changesharelibs: store formula generate from project folder, use byprocessorauto calculation.types: define all types convert from json schema, also store some system types