Interface (Resource)

12003

1. Configure in the database resource table

rescource配置.png


2. Explanation of configuration fields

In the Jianghu framework, APIs can be configured directly via the project database _resource table:

Field Description
accessControlTable Data rule control table
resourceHook Pre- and post-operation service methods for the protocol (see hook)
pageId Primary name of the API
actionId Secondary name of the API. Together with pageId, it uniquely identifies an API
desc Description of the API/protocol
resourceType Protocol type.
- sql: handles simple CRUD operations
- service: use a custom service for complex logic
appDataSchema Schema validation for the appData parameter structure
resourceData Specific implementation details of the protocol
requestDemo Request demo (for developer reference only)
responseDemo Response demo (for developer reference only)
operation Operation type; e.g., softInsert, softUpdate, softDelete, select

3. resourceHook

The resourceHook allows execution of pre and post service methods around operations defined in the _resource table’s resourceData.
This enables you to run business logic before or after the database operation.

3.1 Example

  1. {
  2. "before": [{ "service": "service文件名", "serviceFunction": "service方法名" }],
  3. "after": [{ "service": "service文件名", "serviceFunction": "service方法名" }]
  4. }
  • before: executed before the SQL operation. The framework middleware httpResourceHook.js reads and calls the specified service method.
  • after: executed after the SQL operation. The framework middleware httpResourceHook.js reads and calls the specified service method.

3.2 Implementation Logic

  1. if (beforeHooks) {
  2. for (const beforeHook of beforeHooks) {
  3. const { service, serviceFunction } = beforeHook;
  4. checkServiceFunction(service, serviceFunction);
  5. await ctx.service[service][serviceFunction](ctx.request.body.appData.actionData, ctx);
  6. }
  7. }
  8. if (afterHooks) {
  9. for (const afterHook of afterHooks) {
  10. const { service, serviceFunction } = afterHook;
  11. checkServiceFunction(service, serviceFunction);
  12. await ctx.service[service][serviceFunction](ctx.request.body.appData.actionData, ctx);
  13. }
  14. }