What is Settings

Module a-settings provides the Settings feature, which can display the Settings management page of each module in the front end, so as to facilitate the unified configuration

Because the Settings uses the form validation mechanism, you only need to provide the JSON Schema related to the Settings

Settings Types

Each module can provide two Settings:

  1. User Settings:user related settings
  2. Instance Settings:instance related settings

Definition of Schema and Validation

src/module/test-party/backend/src/meta.js

(Be omitted)

Definition of Settings

Specify validator for user settings and instance settings respectively in meta of the module

src/module/test-party/backend/src/meta.js

settings: {
  user: {
    validator: 'userTest',
  },
  instance: {
    validator: 'instanceTest',
  },
},
Name Description
settings.user user settings
settings.user.validator validator for user settings
settings.instance instance settings
settings.instance.validator validator for instance settings

Default Values

Specify the default values for user settings and instance settings respectively in config of the module

src/module/test-party/backend/src/config/config.js

// settings
config.settings = {
  instance: {
    groupInfo: {
      slogan: '',
    },
  },
  user: {
    groupInfo: {
      username: 'zhennann',
    },
    groupExtra: {
      panelExtra: {
        groupInfo: {
          mobile: '123',
          sex: 1,
          language: 'en-us',
        },
      },
    },
  },
};

Usage of Settings

  1. Page at Frontend: In the frontend, you can directly manage the settings of each module through the Settings menu

  2. API at Backend: In the backend, module a-settings injects object settings into ctx.meta through middleware mechanism`

src/module/test-party/backend/src/controller/test/feat/settings.js

Api: User Settings

getUser

Get the value of a field

let value = await this.ctx.meta.settings.getUser({ 
  name: '/groupInfo/username'
});
Name Description
name field name in JSON Pointer format

loadSettingsUser

Get the complete value of settings

let data = await this.ctx.meta.settings.loadSettingsUser();
let value = data.groupInfo.username;

saveSettingsUser

Save the complete value of settings

data.groupInfo.username = 'zhennann';
await this.ctx.meta.settings.saveSettingsUser({ data });

Api: Instance Settings

getInstance

Get the value of a field

let value = await this.ctx.meta.settings.getInstance({ 
  name: '/groupInfo/slogan'
});
Name Description
name field name in JSON Pointer format

loadSettingsInstance

Get the complete value of settings

let data = await this.ctx.meta.settings.loadSettingsInstance();
let value = data.groupInfo.slogan;

saveSettingsInstance

Save the complete value of settings

data.groupInfo.slogan = 'Less is more, while more is less';
await this.ctx.meta.settings.saveSettingsInstance({ data });