Bỏ qua, đến nội dung

@digiforce-nc/plugin-data-source-main ​

Plugin quản lý collection và field cho data source chính (main) — cầu nối giữa metadata lưu trong DB và runtime ORM.

Plugin này làm gì? ​

Hãy hình dung database của ứng dụng như một kho hàng. plugin-data-source-main là hệ thống quản lý kệ hàng — nó biết có bao nhiêu kệ (collection), mỗi kệ có ngăn gì (field), và khi bạn thêm/sửa/xóa kệ qua giao diện, nó tự động tạo/đổi bảng vật lý trong database.

Ba nhiệm vụ chính ​

#Nhiệm vụChi tiết
1Lưu metadataLưu định nghĩa collection/field trong bảng collections và fields
2Load vào runtimeĐọc metadata → tạo Collection instance trong Database ORM (theo thứ tự dependency)
3Sync vật lýTạo/alter bảng thực trong database khi schema thay đổi

Kiến trúc ​

Tổng quan ​

Thành phầnVai trò
CollectionRepositoryLoad collection từ DB theo thứ tự topological sort (xử lý kế thừa, view, FK)
CollectionModelLoad 1 collection vào runtime, migrate bảng vật lý
FieldModelLoad field, đồng bộ unique index, default value, sort, FK
DB HooksTự động migrate khi admin thêm/sửa collection/field qua UI

Luồng tạo collection mới ​

API endpoints ​

Collection management ​

EndpointMô tả
collections:listDanh sách collection
collections:listMetaDanh sách collection đã load (với field metadata)
collections:createTạo collection mới (tự động migrate)
collections:updateCập nhật collection
collections:destroyXóa collection (cascade remove fields, FK)
collections:setFieldsBulk replace toàn bộ fields cho collection (transactional)

Modeling API (mới) ​

Module modeling/ cung cấp API khai báo (declarative) để tạo/cập nhật collection và field — phù hợp cho AI agent, migration script, hoặc công cụ tự động:

EndpointMô tả
collections:applyTạo hoặc cập nhật collection kèm fields theo khai báo (upsert semantics)
fields:applyTạo hoặc cập nhật field đơn lẻ cho collection có sẵn
modeling:inspectKiểm tra trạng thái modeling hiện tại (templates, plugins, collections)

collections:apply — declarative upsert ​

typescript
await agent.resource('collections').apply({
  values: {
    name: 'orders',
    title: 'Đơn hàng',
    template: 'general',
    fields: [
      { name: 'title', interface: 'input', type: 'string', uiSchema: { title: 'Tiêu đề' } },
      { name: 'amount', interface: 'number', type: 'float', uiSchema: { title: 'Số tiền' } },
    ],
  },
});

Nếu collection orders đã tồn tại → cập nhật; chưa có → tạo mới kèm migrate bảng vật lý.

Capability checks ​

Trước khi apply, module tự kiểm tra:

  • Plugin dependencies: VD field m2m yêu cầu plugin-field-m2m-array đã enabled.
  • Template support: view template yêu cầu DB view tồn tại trước.
  • Inheritance: inherit template yêu cầu parent collection tồn tại.

Verification ​

Sau khi apply, verifyCollectionDefinition() kiểm tra collection vừa tạo/sửa:

  • Primary key id có tồn tại không
  • Mỗi field có interface, type, uiSchema.title không
  • Trả về { valid: boolean, issues: string[] } giúp phát hiện lỗi sớm

Composite constraints ​

EndpointMô tả
collections:createCompositeConstraintTạo unique constraint trên nhiều field
collections:updateCompositeConstraintCập nhật constraint
collections:deleteCompositeConstraintXóa constraint

Database views ​

EndpointMô tả
dbViews:listDanh sách DB view chưa kết nối collection
dbViews:getInfer fields từ view
dbViews:queryQuery trực tiếp từ view (phân trang)

Main data source ​

EndpointMô tả
mainDataSource:refreshReload collection từ metadata
mainDataSource:syncFieldsĐồng bộ fields từ database vật lý

Database (metadata tables) ​

BảngChứa gìVí dụ
collectionsĐịnh nghĩa collection (name, title, inherit, options){ name: 'orders', title: 'Đơn hàng' }
fieldsĐịnh nghĩa field (name, type, interface, options){ name: 'title', type: 'string', interface: 'input' }
collectionCategoriesDanh mục phân loại collection{ name: 'business', color: 'blue' }
collectionCategoryLiên kết collection ↔ category (M2M)

Tính năng đặc biệt ​

Topological sort khi load ​

Collection có thể kế thừa nhau (inherit) hoặc có FK chéo. Plugin dùng topological sort để load đúng thứ tự:

users (không phụ thuộc) → orders (FK → users) → orderItems (FK → orders)

Hỗ trợ Database views ​

Admin có thể tạo collection từ DB view có sẵn — plugin tự infer field types từ view schema.

db2cm (database-to-collection-manager) ​

Khi collection được tạo bằng code (không qua UI), db2cm tự động import metadata vào bảng collections/fields để hiển thị trên UI.

Multi-instance sync ​

Khi chạy cluster, thay đổi collection/field trên instance A tự động sync sang instance B qua message bus.

Thành phần client ​

Thành phầnMô tả
Preset field templatesTự động thêm field mặc định khi tạo collection mới: id, createdAt, createdBy, updatedAt, updatedBy

Dependencies ​

PackageVai trò
@digiforce-nc/databaseDatabase ORM (peer)
@digiforce-nc/serverServer framework (peer)
@digiforce-nc/clientClient framework (peer)
@digiforce-nc/plugin-error-handlerXử lý lỗi field/collection
@digiforce-nc/plugin-field-sortSắp xếp field

Mục lục chi tiết ​

  • Kiến trúc — Meta-driven DDL, collection/field lifecycle, sync
  • API reference — Collections CRUD, fields CRUD, mainDataSource:refresh/syncFields
  • Database schema — ER diagram, meta tables
  • FAQ — Câu hỏi thường gặp