Bỏ qua, đến nội dung

Tham chiếu kỹ thuật

Snapshot JSON Field

Thuộc tínhGiá trị
Field typesnapshot
DB column typeJSON
targetFieldField relation để chụp giá trị
appendsMảng association names cần eager load
TriggerafterCreateWithAssociations

Cột JSON lưu toàn bộ dữ liệu target record đã serialize, bao gồm cả appends.

SnapshotField — afterCreateWithAssociations

Hook chụp snapshot chạy sau khi bản ghi và tất cả association đã được tạo:

Bản ghi mới INSERT + associations
  → afterCreateWithAssociations trigger
  → Tìm tất cả snapshot fields trong collection
  → Với mỗi snapshot field:
    → Đọc targetField (relation) → lấy target record ID
    → Query target record + eager load appends
    → Serialize toàn bộ thành JSON object
    → UPDATE bản ghi SET snapshotField = JSON

Chi tiết quá trình chụp

targetField = 'product', appends = ['category', 'images']

→ record.productId = 5
→ SELECT * FROM products WHERE id = 5
   INCLUDE category, images
→ result = {
    id: 5,
    name: "iPhone 15",
    price: 25000000,
    category: { id: 1, name: "Điện thoại" },
    images: [{ url: "img1.jpg" }, { url: "img2.jpg" }]
  }
→ Lưu JSON vào cột snapshot

collectionsHistory / fieldsHistory — Schema Evolution

Hai collection phụ trợ theo dõi lịch sử thay đổi schema:

collectionsHistory

CộtKiểuMô tả
nameSTRINGTên collection
schemaJSONSchema tại thời điểm ghi
createdAtDATEThời điểm ghi lịch sử

fieldsHistory

CộtKiểuMô tả
collectionNameSTRINGTên collection chứa field
nameSTRINGTên field
schemaJSONSchema field tại thời điểm ghi
createdAtDATEThời điểm ghi lịch sử

Tại sao cần schema history?

Khi xem snapshot cũ, collection gốc có thể đã thay đổi (thêm/xóa field, đổi tên). Schema history giúp client render snapshot đúng cấu trúc tại thời điểm chụp — không bị ảnh hưởng bởi schema hiện tại.

Server Hooks

HookSự kiệnHành động
afterCreateWithAssociationsTạo bản ghi + associationsChụp snapshot từ targetField
collections.afterCreateTạo collectionGhi schema vào collectionsHistory
collections.afterUpdateSửa collectionGhi schema mới vào collectionsHistory
fields.afterCreateTạo fieldGhi schema vào fieldsHistory
fields.afterUpdateSửa fieldGhi schema mới vào fieldsHistory

ACL

PermissionMô tả
collectionsHistory:listCho phép đọc lịch sử schema (client cần để render snapshot)

Client — SnapshotFieldProvider

Provider cung cấp context cho snapshot rendering:

SnapshotFieldProvider
  → Đọc snapshot JSON value từ record
  → Fetch schema history (collectionsHistory, fieldsHistory)
  → Dựng schema component tree từ historical schema
  → Render snapshot data dạng read-only fields

Snapshot Block Initializers

Plugin cung cấp block initializers cho phép kéo thả snapshot block vào detail/form page:

  • Block hiển thị snapshot data giống một "mini detail" của target record.
  • Sử dụng schema từ fieldsHistory để render đúng field types.

Sơ đồ tổng quan

┌────────────┐  targetField  ┌──────────────┐   serialize   ┌──────────┐
│ New Record │ ────────────→ │ Target Record│ ────────────→ │ Snapshot │
│ (order)    │               │ + appends    │               │ (JSON)   │
└────────────┘               └──────────────┘               └──────────┘

                             ┌──────────────┐                     │
                             │ collections/ │ ◄────────── schema history
                             │ fieldsHistory│    (cho rendering)
                             └──────────────┘