Giao diện
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
| Operation | Mô tả |
|---|---|
getJsonSchema | Lấy schema tree từ UID gốc — cache enabled |
getProperties | Chỉ lấy children trực tiếp (depth = 1) — cache enabled |
insert | Chèn schema vào vị trí trong cây (tạo node + closure paths) |
remove | Xóa node + cascade tất cả descendants |
patch | Merge partial update vào schema hiện có |
batchPatch | Update nhiều node cùng lúc — atomic transaction |
insertAdjacent | Chèn ở vị trí cụ thể: beforeBegin, afterBegin, beforeEnd, afterEnd |
clearAncestor | Detach node khỏi cây (xóa ancestor links, giữ node) |
clearCache | Xóa cache cho UID hoặc toàn bộ |
saveAsTemplate | Snapshot subtree thành template tái sử dụng |
Cache layer
getJsonSchema và getProperties đều có cache. Cache key là UID của node gốc. Invalidation xảy ra khi:
insert,remove,patch,batchPatchtrên bất kỳ node trong subtree.insertAdjacentthay đổi children.clearAncestordetach 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
schemalà magic 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
schemacolumn.
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 type | Thời điểm | Ví dụ |
|---|---|---|
onCollectionDestroy | Collection bị xóa | Xóa schema gắn với collection |
onCollectionFieldDestroy | Field bị xóa | Xóa column schema |
onSelfCreate | Schema node được tạo | Gán role cho button schema |
onSelfSave | Schema node được lưu | Đồng bộ i18n |
onSelfMove | Schema node di chuyển | Cập nhật references |
onSelfDestroy | Schema node bị xóa | Cleanup tài nguyên |
Built-in hooks
| Hook | Mô tả |
|---|---|
removeSchema | Xóa schema khi collection/field bị destroy |
bindMenuToRole | Khi tạo menu schema, tự động gán cho roles |
removeParentsIfNoChildren | Xó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.