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-mainhệ 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 collectionsfields
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)

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