Bỏ qua, đến nội dung

Resourcer & Actions ​

1) Core classes và trách nhiệm ​

ClassTrách nhiệm
ResourceManagerGiữ map resources, global action handlers, middleware graph (topo sort), pre-action handlers
ResourceĐại diện một resource name, map action instances, hỗ trợ only/except
ActionChứa params, handler, middlewares, merge params bằng chiến lược đặc thù

2) Route parsing (parseRequest) ​

resourcer/utils.ts parse request theo hai mode:

Mode 1: Explicit route ​

/resourcer/<resource>:<action>

Mode 2: REST-like routes ​

/:resourceName
/:resourceName/:resourceIndex
/:associatedName/:associatedIndex/:resourceName

Có biến thể theo type: single, hasOne, hasMany, belongsTo, belongsToMany.

HTTP method → action mapping ​

HTTPĐơn resourceAssociation resource
GET (no index)listlist
GET (có index)getget
POSTcreatecreate / set / add (tùy type)
PUT / PATCHupdateupdate
DELETEdestroydestroy / remove

3) Runtime middleware - ResourceManager.middleware() ​

Action.getHandlers() - thứ tự chain ​

Middleware chain

Global middleware chạy trước tất cả. Pre-action handler chạy ngay trước handler cuối. Đây là nơi ACL thường inject fixed params.

4) Cơ chế handler registration ​

APIScopeVí dụ
registerActionHandler(name, fn)Global handler cho action nameregisterActionHandler('list', listHandler)
Resource constructorInject toàn bộ global handler vào resource actionsTự động
Override localKey resource:actionregisterActionHandler('users:list', customList)
registerPreActionHandler(name, fn, topo)Pre-handler theo actionregisterPreActionHandler('create', validate)

Pre-action handler hỗ trợ key cụ thể (users:list) hoặc key chung (list). Runtime ưu tiên key cụ thể.

5) Package @digiforce-nc/actions ​

registerActions(app) nạp handlers chuẩn:

ActionHành viGhi chú
listPaged hoặc non-paged theo paginateSimple pagination mode khi row count lớn
getrepository.findOne
createrepository.create với values, whitelist, blacklistHỗ trợ updateAssociationValues
updaterepository.update
destroyrepository.destroy
set / add / removeThao tác associationCho belongsToMany, hasMany
queryAggregate (measures + dimensions)Không cho association resource

Action query

query bắt buộc có measures hoặc dimensions. Không hỗ trợ trên association resource (sourceId phải rỗng). Dùng cho dashboard/chart data.

6) Quan hệ với DataSourceManager ​

DataSource trong @digiforce-nc/data-source-manager cũng dùng ResourceManager, nhưng action mặc định được nạp bởi loadDefaultActions():

Pipeline resourcer giống nhau, nhưng bộ default action có thể khác giữa namespace server core và data-source-manager.

7) Điểm cần nhớ khi viết plugin ​

Best practices

  • Logic tiền xử lý theo topo: dùng registerPreActionHandler.
  • Action mới: dùng registerActionHandler rồi define resource/action tương ứng.
  • Không ghi đè mù params: dùng mergeParams theo strategy để không phá whitelist/filter đã có từ middleware khác.
Tình huốngNên dùngTránh
Validate input trước createregisterPreActionHandler('create', validate)Override handler create
Thêm filter bảo mậtACL fixedParams hoặc pre-handlerGhi đè ctx.action.params.filter
Custom action mớiregisterActionHandler + resource.defineGắn trực tiếp vào router Koa
Sửa response formatMiddleware after handler (topo after)Sửa handler chuẩn

Đọc tiếp ​