CabloyJS refers to all operations of business data as Atom Action
, which fall into two main categories:
Basic Action
: create、read、write、deleteCustom Action
:business-specific operations, such asreview
,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
- 1atom: {
- 2 action: {
- 3 create: 1,
- 4 read: 2,
- 5 write: 3,
- 6 delete: 4,
- 7 save: 51,
- 8 submit: 52,
- 9 custom: 100, // custom action start from custom
- 10 },
- 11 actionMeta: {
- 12 create: { title: 'Create', actionComponent: 'action' },
- 13 read: { title: 'View', actionPath: 'atom/view?atomId={{atomId}}&itemId={{itemId}}&atomClassId={{atomClassId}}&module={{module}}&atomClassName={{atomClassName}}' },
- 14 write: { title: 'Edit', actionPath: 'atom/edit?atomId={{atomId}}&itemId={{itemId}}&atomClassId={{atomClassId}}&module={{module}}&atomClassName={{atomClassName}}' },
- 15 delete: { title: 'Delete', actionComponent: 'action' },
- 16 save: { title: 'Save', actionComponent: 'action', authorize: false },
- 17 submit: { title: 'Submit', actionComponent: 'action', authorize: false },
- 18 custom: { title: 'Custom' },
- 19 },
- 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 modulea-base
can meet the needs of most scenarios. If you want to customize the logic ofbasic actions
, you only need to provide a combination ofactionModule
,actionComponent
andactionPath
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{ method: 'post', path: 'atom/create', controller: atom, middlewares: 'transaction',
- 2 meta: { right: { type: 'atom', action: 1 } },
- 3},
- 4{ method: 'post', path: 'atom/read', controller: atom,
- 5 meta: { right: { type: 'atom', action: 2 } },
- 6},
- 7{ method: 'post', path: 'atom/select', controller: atom },
- 8{ method: 'post', path: 'atom/write', controller: atom, middlewares: 'transaction',
- 9 meta: { right: { type: 'atom', action: 3 } },
- 10},
- 11{ method: 'post', path: 'atom/submit', controller: atom, middlewares: 'transaction',
- 12 meta: { right: { type: 'atom', action: 3 } },
- 13},
- 14{ method: 'post', path: 'atom/delete', controller: atom, middlewares: 'transaction',
- 15 meta: { right: { type: 'atom', action: 4 } },
- 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{ method: 'post', path: 'party/create', controller: party, middlewares: 'inner', meta: { auth: { enable: false } } },
- 2{ method: 'post', path: 'party/read', controller: party, middlewares: 'inner', meta: { auth: { enable: false } } },
- 3{ method: 'post', path: 'party/select', controller: party, middlewares: 'inner', meta: { auth: { enable: false } } },
- 4{ method: 'post', path: 'party/write', controller: party, middlewares: 'inner,validate',
- 5 meta: {
- 6 auth: { enable: false },
- 7 validate: { validator: 'party', data: 'item' },
- 8 },
- 9},
- 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 objectuser
when performing thebusiness API routes
, the global middlewareauth
is disabled here to reduce unnecessary operations
Business Logic Codes
create
The business logic of action create
generally performs the following operations:
- Create a new item of business table
- Initialize fields of
atom
(optional) - Return
atom key
src/suite-vendor/test-party/modules/test-party/backend/src/service/party.js
- 1async create({ atomClass, key, item, user }) {
- 2 // add party
- 3 const res = await this.ctx.model.party.insert({
- 4 atomId: key.atomId,
- 5 });
- 6 return { atomId: key.atomId, itemId: res.insertId };
- 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
- 1async read({ atomClass, key, item, user }) {
- 2 // read
- 3 this._getMeta(item);
- 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
- 1async select({ atomClass, options, items, user }) {
- 2 // select
- 3 for (const item of items) {
- 4 this._getMeta(item);
- 5 }
- 6}
write
The business logic of action write
generally performs the following operations:
- Modify the item’s fields of
business table
src/suite-vendor/test-party/modules/test-party/backend/src/service/party.js
- 1async write({ atomClass, key, item, user }) {
- 2 // update party
- 3 await this.ctx.model.party.update({
- 4 id: key.itemId,
- 5 personCount: item.personCount,
- 6 partyTypeId: item.partyTypeId,
- 7 });
- 8}
delete
The business logic of action delete
generally performs the following operations:
- Delete the item of
business table
src/suite-vendor/test-party/modules/test-party/backend/src/service/party.js
- 1async delete({ atomClass, key, user }) {
- 2 // delete party
- 3 await this.ctx.model.party.delete({
- 4 id: key.itemId,
- 5 });
- 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
- 1const meta = {
- 2 base: {
- 3 atoms: {
- 4 party: {
- 5 actions: {
- 6 review: {
- 7 code: 101,
- 8 title: 'Review',
- 9 flag: '1',
- 10 },
- 11 },
- 12 flags: {
- 13 1: {
- 14 title: 'Reviewing',
- 15 },
- 16 2: {
- 17 title: 'Reviewed',
- 18 },
- 19 },
- 20 },
- 21 },
- 22 },
- 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{ method: 'post', path: 'atom/action',
- 2 controller: atom,
- 3 middlewares: 'transaction',
- 4 meta: { right: { type: 'atom' } },
- 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{ method: 'post', path: 'party/action',
- 2 controller: party,
- 3 middlewares: 'inner',
- 4 meta: { auth: { enable: false } } },
Business Logic Codes
src/suite-vendor/test-party/modules/test-party/backend/src/service/party.js
- 1async action({ action, atomClass, key, user }) {
- 2 if (action === 101) {
- 3 // change flag
- 4 await this.ctx.meta.atom.flag({
- 5 key,
- 6 atom: { atomFlag: 2 },
- 7 user,
- 8 });
- 9 }
- 10}
Comments: