CabloyJS refers to all operations of business data as Atom Action, which fall into two main categories:

  1. Basic Action: create、read、write、delete
  2. Custom Action:business-specific operations, such as review, publish, etc.

CabloyJS provides a set of basic API routes, encapsulates atom actions, so that middleware such as database transaction, validator', right` can be configured uniformly

Business module only need to provide business API routes, which will be called by basic API routes when appropriate

Basic Actions

Definition

Module a-base provides the generic logic of basic actions, which is defined as follows:

a-base/backend/src/config/constants.js

  1. 1atom: {
  2. 2 action: {
  3. 3 create: 1,
  4. 4 read: 2,
  5. 5 write: 3,
  6. 6 delete: 4,
  7. 7 save: 51,
  8. 8 submit: 52,
  9. 9 custom: 100, // custom action start from custom
  10. 10 },
  11. 11 actionMeta: {
  12. 12 create: { title: 'Create', actionComponent: 'action' },
  13. 13 read: { title: 'View', actionPath: 'atom/view?atomId={{atomId}}&itemId={{itemId}}&atomClassId={{atomClassId}}&module={{module}}&atomClassName={{atomClassName}}' },
  14. 14 write: { title: 'Edit', actionPath: 'atom/edit?atomId={{atomId}}&itemId={{itemId}}&atomClassId={{atomClassId}}&module={{module}}&atomClassName={{atomClassName}}' },
  15. 15 delete: { title: 'Delete', actionComponent: 'action' },
  16. 16 save: { title: 'Save', actionComponent: 'action', authorize: false },
  17. 17 submit: { title: 'Submit', actionComponent: 'action', authorize: false },
  18. 18 custom: { title: 'Custom' },
  19. 19 },
  20. 20},
Name Description
title action’s title
actionModule The module name of the frontend component, which is the current module by default
actionComponent frontend component
actionPath frontend page component route
authorize Whether a separate authorization is required. Because save and submit are auxiliary actions of write, no separate authorization is required
custom action placeholder, custom actions start from 101

The general logic of basic actions provided by module a-base can meet the needs of most scenarios. If you want to customize the logic of basic actions, you only need to provide a combination of actionModule, actionComponent and actionPath

Basic API Route

Module a-base provides a set of basic API routes, so that middlewares such as database transaction, validator', right` can be configured uniformly

a-base/backend/src/routes.js

  1. 1{ method: 'post', path: 'atom/create', controller: atom, middlewares: 'transaction',
  2. 2 meta: { right: { type: 'atom', action: 1 } },
  3. 3},
  4. 4{ method: 'post', path: 'atom/read', controller: atom,
  5. 5 meta: { right: { type: 'atom', action: 2 } },
  6. 6},
  7. 7{ method: 'post', path: 'atom/select', controller: atom },
  8. 8{ method: 'post', path: 'atom/write', controller: atom, middlewares: 'transaction',
  9. 9 meta: { right: { type: 'atom', action: 3 } },
  10. 10},
  11. 11{ method: 'post', path: 'atom/submit', controller: atom, middlewares: 'transaction',
  12. 12 meta: { right: { type: 'atom', action: 3 } },
  13. 13},
  14. 14{ method: 'post', path: 'atom/delete', controller: atom, middlewares: 'transaction',
  15. 15 meta: { right: { type: 'atom', action: 4 } },
  16. 16},

Business API Route

Business module only need to provide business API routes, which will be called by basic API routes when appropriate

For example, the business API routes of module test-party is as follows:

modules/test-party/backend/src/routes.js

  1. 1{ method: 'post', path: 'party/create', controller: party, middlewares: 'inner', meta: { auth: { enable: false } } },
  2. 2{ method: 'post', path: 'party/read', controller: party, middlewares: 'inner', meta: { auth: { enable: false } } },
  3. 3{ method: 'post', path: 'party/select', controller: party, middlewares: 'inner', meta: { auth: { enable: false } } },
  4. 4{ method: 'post', path: 'party/write', controller: party, middlewares: 'inner,validate',
  5. 5 meta: {
  6. 6 auth: { enable: false },
  7. 7 validate: { validator: 'party', data: 'item' },
  8. 8 },
  9. 9},
  10. 10{ method: 'post', path: 'party/delete', controller: party, middlewares: 'inner', meta: { auth: { enable: false } } },

Why disable the global middleware auth?

  • Since the basic API rotues will pass in the object user when performing the business API routes, the global middleware auth is disabled here to reduce unnecessary operations

Business Logic Codes

create

The business logic of action create generally performs the following operations:

  1. Create a new item of business table
  2. Initialize fields of atom (optional)
  3. Return atom key

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

  1. 1async create({ atomClass, key, item, user }) {
  2. 2 // add party
  3. 3 const res = await this.ctx.model.party.insert({
  4. 4 atomId: key.atomId,
  5. 5 });
  6. 6 return { atomId: key.atomId, itemId: res.insertId };
  7. 7}

read

Generally, it is not necessary to add business logic codes for action read, because the object item has retrieved the data of table aAtom and business table

If you want to attach data other than the table aAtom and business table, you can directly operate the object item here

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

  1. 1async read({ atomClass, key, item, user }) {
  2. 2 // read
  3. 3 this._getMeta(item);
  4. 4}

select

Generally, it is not necessary to add business logic codes for action select, because the object items has retrieved the data of table aAtom and business table

If you want to attach data other than the table aAtom and business table, you can directly operate the object items here

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

  1. 1async select({ atomClass, options, items, user }) {
  2. 2 // select
  3. 3 for (const item of items) {
  4. 4 this._getMeta(item);
  5. 5 }
  6. 6}

write

The business logic of action write generally performs the following operations:

  1. Modify the item’s fields of business table

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

  1. 1async write({ atomClass, key, item, user }) {
  2. 2 // update party
  3. 3 await this.ctx.model.party.update({
  4. 4 id: key.itemId,
  5. 5 personCount: item.personCount,
  6. 6 partyTypeId: item.partyTypeId,
  7. 7 });
  8. 8}

delete

The business logic of action delete generally performs the following operations:

  1. Delete the item of business table

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

  1. 1async delete({ atomClass, key, user }) {
  2. 2 // delete party
  3. 3 await this.ctx.model.party.delete({
  4. 4 id: key.itemId,
  5. 5 });
  6. 6}

Custom Actions

You can implement business-specific operations through custom actions, such as review, publish, etc.

Definition of Custom Action

For example, add a action of review for the atom class party. The configuration of custom action is as follows:

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

  1. 1const meta = {
  2. 2 base: {
  3. 3 atoms: {
  4. 4 party: {
  5. 5 actions: {
  6. 6 review: {
  7. 7 code: 101,
  8. 8 title: 'Review',
  9. 9 flag: '1',
  10. 10 },
  11. 11 },
  12. 12 flags: {
  13. 13 1: {
  14. 14 title: 'Reviewing',
  15. 15 },
  16. 16 2: {
  17. 17 title: 'Reviewed',
  18. 18 },
  19. 19 },
  20. 20 },
  21. 21 },
  22. 22 },
  23. 23};
Name Description
review.code action’s code
review.title action’s title
review.flag action’s flag
flags definition of flags

Basic API Route

The core module a-base implements the unified distribution of custom actions

a-base/backend/src/routes.js

  1. 1{ method: 'post', path: 'atom/action',
  2. 2 controller: atom,
  3. 3 middlewares: 'transaction',
  4. 4 meta: { right: { type: 'atom' } },
  5. 5},

Business API Route

Business module only need to provide business API route, so as to perform business logic codes for the corresponding custom actions

For example, the business API route of module test-party is as follows:

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

  1. 1{ method: 'post', path: 'party/action',
  2. 2 controller: party,
  3. 3 middlewares: 'inner',
  4. 4 meta: { auth: { enable: false } } },

Business Logic Codes

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

  1. 1async action({ action, atomClass, key, user }) {
  2. 2 if (action === 101) {
  3. 3 // change flag
  4. 4 await this.ctx.meta.atom.flag({
  5. 5 key,
  6. 6 atom: { atomFlag: 2 },
  7. 7 user,
  8. 8 });
  9. 9 }
  10. 10}