Cabloy 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 Cabloy 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 that provided by EggJS

In the module test-todo, there is a data table testTodo. We are still doing CRUD of testTodo in the controller method corresponding to the API route of test/echo

async echo() {
  // insert
  const res = await this.ctx.db.insert('testTodo', {
    iid: this.ctx.instance.id,
    deleted: 0,
    atomId: 0,
    description: 'test',
  });
  const id = res.insertId;
  // update
  await this.ctx.db.update('testTodo', {
    id,
    description: 'test2',
  });
  // get
  const item = await this.ctx.db.get('testTodo', {
    id,
  });
  // delete
  await this.ctx.db.delete('testTodo', {
    id,
  });
  // response
  this.ctx.success(`${item.id}:${item.description}`);
}

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-todo/backend/src/model/todo.js

module.exports = app => {
  class Todo extends app.meta.Model {
    constructor(ctx) {
      super(ctx, { table: 'testTodo', options: { disableDeleted: false } });
    }
  }
  return Todo;
};
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

As mentioned earlier, in order to support the compilation feature of business module, all objects need explicit require, and model object is no exception

src/module/test-todo/backend/src/models.js

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

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

Usage of Model

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

async echo() {
  // insert
  const res = await this.ctx.model.todo.insert({
    atomId: 0,
    description: 'test',
  });
  const id = res.insertId;
  // update
  await this.ctx.model.todo.update({
    id,
    description: 'test2',
  });
  // get
  const item = await this.ctx.model.todo.get({
    id,
  });
  // delete
  await this.ctx.model.todo.delete({
    id,
  });
  // response
  this.ctx.success(`${item.id}:${item.description}`);
}