EggBornJS further encapsulates and extends the database operation method in EggJS, providing the following mechanisms:
- Provides the object of
ctx.db
to facilitate support fordatabase transaction
- Provides the object of
model
to facilitate support formulti-instance
andsoft deletion
ctx.db
EggJS provides database connection object on app
, while EggBornJS provides database connection object on ctx
, which facilitates maintaining the same database connection object in the context environment when database transaction
is started
Here, let’s first look at how to accomplish routine operations such as create, update, delete, select
by ctx.db
The database operation method provided by
ctx.db
is basically the same as EggJS app.mysql
In the module test-party
, there is a data table testParty
. Now we perform the CRUD
operations of testParty
in the controller method corresponding to the API route of kitchen-sink/guide/echo6
src/suite-vendor/test-party/modules/test-party/backend/src/controller/kitchen-sink/guide.js
- 1async echo6() {
- 2 // testParty: insert/udpate/delete/get
- 3
- 4 // insert
- 5 const res = await this.ctx.db.insert('testParty', {
- 6 iid: this.ctx.instance.id,
- 7 deleted: 0,
- 8 personCount: 3,
- 9 });
- 10 const id = res.insertId;
- 11 // update
- 12 await this.ctx.db.update('testParty', {
- 13 id,
- 14 personCount: 5,
- 15 });
- 16 // get
- 17 const item = await this.ctx.db.get('testParty', {
- 18 id,
- 19 });
- 20 // delete
- 21 await this.ctx.db.delete('testParty', {
- 22 id,
- 23 });
- 24 // ok
- 25 this.ctx.success(item);
- 26}
model
The object of model
further encapsulates ctx.db
, which facilitates support for multi-instance
and soft deletion
multi-instance
:Automatically injectctx.instance.id
into the parameters ofctx.db
related methodssoft deletion
:Automatically change thedelete
method ofmodel
toupdate
method, and change thedeleted
field of data table from0
to1
Definition of Model
src/suite-vendor/test-party/modules/test-party/backend/src/model/party.js
- 1module.exports = app => {
- 2 class Party extends app.meta.Model {
- 3 constructor(ctx) {
- 4 super(ctx, { table: 'testParty', options: { disableDeleted: false } });
- 5 }
- 6 }
- 7 return Party;
- 8};
Name | Default | Description |
---|---|---|
table | The name of the data table or view corresponding to the model object | |
disableDeleted | false | Disable Soft Deletion feature |
Reference Model
In order to support module compilation
, all objects need explicit require
, and model object is no exception
src/suite-vendor/test-party/modules/test-party/backend/src/models.js
- 1const party = require('./model/party.js');
- 2
- 3module.exports = app => {
- 4 const models = {
- 5 party,
- 6 };
- 7 return models;
- 8};
Usage of Model
Let’s see how to use model
to perform the same CRUD
operations
- 1async echo7() {
- 2 // testParty: insert/udpate/delete/get
- 3
- 4 // insert
- 5 const res = await this.ctx.model.party.insert({ personCount: 3 });
- 6 const id = res.insertId;
- 7 // update
- 8 await this.ctx.model.party.update({ id, personCount: 6 });
- 9 // get
- 10 const item = await this.ctx.model.party.get({ id });
- 11 // delete
- 12 await this.ctx.model.party.delete({ id });
- 13 // ok
- 14 this.ctx.success(item);
- 15}
Comments: