Modules generally have business-related data structures. For example, module test-party has a business data table testParty, which contains several fields

After the module is compiled, deployed and released, the current data version of the module is recommended to be in a closed state. If module data architecture changed, an incremental data version is required. In this way, with the upgrade of the module, the data architecture inside the module will also be upgraded automatically and seamlessly

Definition of Data Version

Configure fileVersion as the current data version in the file package.json of the module

{
  "name": "egg-born-module-test-party",
  "version": "2.1.43",
  "eggBornModule": {
    "fileVersion": 4
  }
}

Backend API Routes

The following API routes need to be implemented, which are automatically called by the system when upgrading the data version

// version
{ method: 'post', path: 'version/update', controller: version, middlewares: 'inner' },
{ method: 'post', path: 'version/init', controller: version, middlewares: 'inner' },
{ method: 'post', path: 'version/test', controller: version, middlewares: 'test' },
Name Description
middlewares: ‘inner’ indicates this API route only be allowed inner access
middlewares: ‘test’ indicates this API route only be allowed in the test environment
version/update Data architecture changes unrelated to multi-instance
version/init Data architecture changes related to multi-instance, such as initializing authorization for some built-in roles
version/test Only execute in the test environment for initializing unit-test related data, such as providing initial test data or initial role authorization for the subsequent unit-tests

Difference between version/update and version/init

  1. One EggBornJS service can support multiple instances, and the instances shares the same MySQL database architecture, but the data generated dynamically is isolated from each other
  2. version/update deals with data architecture changes that are not related to multi-instance, such as tables, views, store procedures and functions, etc.
  3. version/init deals with data changes that are related to multi-instance, such as function authorizations, menu authorizations and atom authorizations, etc.

Backend Logic

src/module/test-party/backend/src/service/version.js

module.exports = app => {

  class Version extends app.Service {

    async update(options) {
      if (options.version === 1) {
        ...
      }
      // if current fileVersion=2
      if (options.version === 2) {
        ...
      }
    }

    async init(options) {
      if (options.version === 1) {
        ...
      }
      // if current fileVersion=2
      if (options.version === 2) {
        ...
      }
    }

    async test() {
    }

  }

  return Version;
};

Name Description
options.version It is only necessary to write corresponding change logic for different data versions of the module, and the system will automatically perform the corresponding codes according to the current data version