EggBornJS further encapsulates and extends the database operation method in EggJS, providing the following mechanisms:

  1. Provides the object of ctx.db to facilitate support for database transaction
  2. Provides the object of model to facilitate support for multi-instance and soft 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

  1. 1async echo6() {
  2. 2 // testParty: insert/udpate/delete/get
  3. 3
  4. 4 // insert
  5. 5 const res = await this.ctx.db.insert('testParty', {
  6. 6 iid: this.ctx.instance.id,
  7. 7 deleted: 0,
  8. 8 personCount: 3,
  9. 9 });
  10. 10 const id = res.insertId;
  11. 11 // update
  12. 12 await this.ctx.db.update('testParty', {
  13. 13 id,
  14. 14 personCount: 5,
  15. 15 });
  16. 16 // get
  17. 17 const item = await this.ctx.db.get('testParty', {
  18. 18 id,
  19. 19 });
  20. 20 // delete
  21. 21 await this.ctx.db.delete('testParty', {
  22. 22 id,
  23. 23 });
  24. 24 // ok
  25. 25 this.ctx.success(item);
  26. 26}

model

The object of model further encapsulates ctx.db, which facilitates support for multi-instance and soft deletion

  1. multi-instance:Automatically inject ctx.instance.id into the parameters of ctx.db related methods
  2. soft deletion:Automatically change the delete method of model to update method, and change the deleted field of data table from 0 to 1

Definition of Model

src/suite-vendor/test-party/modules/test-party/backend/src/model/party.js

  1. 1module.exports = app => {
  2. 2 class Party extends app.meta.Model {
  3. 3 constructor(ctx) {
  4. 4 super(ctx, { table: 'testParty', options: { disableDeleted: false } });
  5. 5 }
  6. 6 }
  7. 7 return Party;
  8. 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

  1. 1const party = require('./model/party.js');
  2. 2
  3. 3module.exports = app => {
  4. 4 const models = {
  5. 5 party,
  6. 6 };
  7. 7 return models;
  8. 8};

Usage of Model

Let’s see how to use model to perform the same CRUD operations

  1. 1async echo7() {
  2. 2 // testParty: insert/udpate/delete/get
  3. 3
  4. 4 // insert
  5. 5 const res = await this.ctx.model.party.insert({ personCount: 3 });
  6. 6 const id = res.insertId;
  7. 7 // update
  8. 8 await this.ctx.model.party.update({ id, personCount: 6 });
  9. 9 // get
  10. 10 const item = await this.ctx.model.party.get({ id });
  11. 11 // delete
  12. 12 await this.ctx.model.party.delete({ id });
  13. 13 // ok
  14. 14 this.ctx.success(item);
  15. 15}