Bỏ qua, đến nội dung

Kiến trúc

Tổng quan

plugin-ui-schema-storage là kho lưu trữ trung tâm cho toàn bộ JSON schema giao diện. Plugin gồm ba thành phần chính: UiSchemaRepository (CRUD + closure table), MagicAttributeModel (biến đổi JSON linh hoạt), và ServerHooks (sự kiện schema).

UiSchemaRepository + Closure Table

Cấu trúc lưu trữ

Mỗi schema node được lưu trong bảng uiSchemas với PK = x-uid (chuỗi UID). Quan hệ cây được lưu qua closure table uiSchemaTreePath — tương tự uiWidgetTreePath của plugin-ui-core.

Các operations chính

OperationMô tả
getJsonSchemaLấy schema tree từ UID gốc — cache enabled
getPropertiesChỉ lấy children trực tiếp (depth = 1) — cache enabled
insertChèn schema vào vị trí trong cây (tạo node + closure paths)
removeXóa node + cascade tất cả descendants
patchMerge partial update vào schema hiện có
batchPatchUpdate nhiều node cùng lúc — atomic transaction
insertAdjacentChèn ở vị trí cụ thể: beforeBegin, afterBegin, beforeEnd, afterEnd
clearAncestorDetach node khỏi cây (xóa ancestor links, giữ node)
clearCacheXóa cache cho UID hoặc toàn bộ
saveAsTemplateSnapshot subtree thành template tái sử dụng

Cache layer

getJsonSchemagetProperties đều có cache. Cache key là UID của node gốc. Invalidation xảy ra khi:

  • insert, remove, patch, batchPatch trên bất kỳ node trong subtree.
  • insertAdjacent thay đổi children.
  • clearAncestor detach node.
  • Explicit clearCache.

MagicAttributeModel

UiSchemaModel kế thừa MagicAttributeModel — một model đặc biệt cho phép lưu trữ JSON linh hoạt:

  • Field schemamagic attribute: dữ liệu JSON được merge vào/tách ra từ record.
  • Khi đọc: record DB được biến đổi thành JSON schema hoàn chỉnh.
  • Khi ghi: JSON schema được tách thành fields cố định + phần dư lưu vào schema column.

getServerHooksByType

Method trên model trả về danh sách server hooks gắn với schema node, phân loại theo type (VD: onSelfCreate, onSelfDestroy).

ServerHooks

Hook types

ServerHooks cho phép đăng ký logic chạy khi schema events xảy ra:

Hook typeThời điểmVí dụ
onCollectionDestroyCollection bị xóaXóa schema gắn với collection
onCollectionFieldDestroyField bị xóaXóa column schema
onSelfCreateSchema node được tạoGán role cho button schema
onSelfSaveSchema node được lưuĐồng bộ i18n
onSelfMoveSchema node di chuyểnCập nhật references
onSelfDestroySchema node bị xóaCleanup tài nguyên

Built-in hooks

HookMô tả
removeSchemaXóa schema khi collection/field bị destroy
bindMenuToRoleKhi tạo menu schema, tự động gán cho roles
removeParentsIfNoChildrenXóa parent node nếu không còn children (cleanup container rỗng)

Luồng hook

Template engine

saveAsTemplate snapshot toàn bộ subtree thành template, lưu trong bảng uiSchemaTemplates. Template chứa:

  • key: unique key.
  • name: tên hiển thị.
  • componentName: component type chính.
  • collectionName + dataSourceKey: context collection (nếu có).
  • Liên kết belongsTo uiSchemas — trỏ về schema gốc.

Khi sử dụng template, schema tree được clone vào vị trí mới với UID sinh mới.