Bỏ qua, đến nội dung

@digiforce-nc/plugin-snapshot-field

Plugin cung cấp kiểu field snapshot — chụp lại giá trị record tại thời điểm tạo, lưu dạng JSON bất biến, kết hợp theo dõi lịch sử schema qua collectionsHistoryfieldsHistory.

Plugin này làm gì?

Hãy hình dung: khi tạo đơn hàng, bạn muốn "chụp" thông tin sản phẩm (giá, tên, mô tả) tại thời điểm đặt hàng — dù sau đó sản phẩm thay đổi, đơn hàng vẫn giữ dữ liệu cũ. Plugin này tự động snapshot giá trị từ bản ghi liên kết (qua targetField) cùng các field bổ sung (appends), lưu thành JSON bất biến.

Bốn nhiệm vụ chính

#Nhiệm vụChi tiết
1Snapshot on createTự động chụp giá trị record liên kết khi tạo bản ghi mới
2Schema historyLưu lịch sử collection/field schema để hiển thị snapshot đúng cấu trúc
3Immutable dataGiá trị snapshot lưu dạng JSON, không thay đổi theo dữ liệu gốc
4Block initializersCung cấp block để hiển thị snapshot data trong giao diện

Kiến trúc

Tổng quan

Plugin có cả server (field type, hooks, history collections) và client (provider, block initializers, interface). Server chụp giá trị và quản lý lịch sử schema; client hiển thị dữ liệu snapshot.

Luồng snapshot khi tạo bản ghi

Field type snapshot

Thuộc tínhGiá trị
DB column typeJSON
targetFieldField liên kết (relation) để chụp giá trị từ đó
appendsDanh sách association cần eager load khi chụp
TriggerafterCreateWithAssociations — chụp sau khi tạo bản ghi + association

Database collections phụ trợ

CollectionMô tả
collectionsHistoryLưu snapshot schema của collection tại thời điểm tạo/sửa
fieldsHistoryLưu snapshot schema của field tại thời điểm tạo/sửa

Hai collection này giúp hệ thống biết cấu trúc data tại thời điểm snapshot được chụp — cần thiết khi collection gốc đã thay đổi schema (thêm/xóa field) nhưng snapshot vẫn cần hiển thị đúng.

Hooks

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

ACL

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

Ví dụ sử dụng

Cấu hình field snapshot

typescript
const productSnapshotField = {
  name: 'productSnapshot',
  type: 'snapshot',
  interface: 'snapshot',
  targetField: 'product',
  appends: ['category', 'images'],
};

Đọc snapshot data

typescript
const order = await repository.findOne({
  filterByTk: 1,
  fields: ['productSnapshot'],
});
// order.productSnapshot.price = 15000000 (giá tại thời điểm đặt)
// Dù product hiện tại đã đổi giá → snapshot không thay đổi

Thành phần client

Thành phầnMô tả
SnapshotFieldProviderProvider cung cấp context cho snapshot rendering
SnapshotFieldInterfaceĐịnh nghĩa interface cho field snapshot
Snapshot Block InitializersBlock để hiển thị dữ liệu snapshot trong detail/form

Dependencies

PackageVai trò
@digiforce-nc/serverServer framework — hooks, collections
@digiforce-nc/databaseDatabase ORM — field type + history collections
@digiforce-nc/clientClient framework — đăng ký component

Mục lục chi tiết