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/module/test-party/backend/src/controller/kitchen-sink/guide.js

async echo6() {
  // testParty: insert/udpate/delete/get

  // insert
  const res = await this.ctx.db.insert('testParty', {
    iid: this.ctx.instance.id,
    deleted: 0,
    personCount: 3,
  });
  const id = res.insertId;
  // update
  await this.ctx.db.update('testParty', {
    id,
    personCount: 5,
  });
  // get
  const item = await this.ctx.db.get('testParty', {
    id,
  });
  // delete
  await this.ctx.db.delete('testParty', {
    id,
  });
  // ok
  this.ctx.success(item);
}

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/module/test-party/backend/src/model/party.js

module.exports = app => {
  class Party extends app.meta.Model {
    constructor(ctx) {
      super(ctx, { table: 'testParty', options: { disableDeleted: false } });
    }
  }
  return Party;
};
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/module/test-party/backend/src/models.js

const party = require('./model/party.js');

module.exports = app => {
  const models = {
    party,
  };
  return models;
};

Usage of Model

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

async echo7() {
  // testParty: insert/udpate/delete/get

  // insert
  const res = await this.ctx.model.party.insert({ personCount: 3 });
  const id = res.insertId;
  // update
  await this.ctx.model.party.update({ id, personCount: 6 });
  // get
  const item = await this.ctx.model.party.get({ id });
  // delete
  await this.ctx.model.party.delete({ id });
  // ok
  this.ctx.success(item);
}