Bỏ qua, đến nội dung

@digiforce-nc/plugin-field-formula ​

Plugin cung cấp kiểu field công thức tính toán — tự động tính giá trị dựa trên các field khác trong cùng bản ghi, sử dụng engine evaluator hỗ trợ cú pháp mathjs và excel-like.

Plugin này làm gì? ​

Hãy hình dung: bạn có field price và quantity, muốn field total tự động tính price * quantity. Plugin này cung cấp field type formula với engine tính toán mạnh mẽ — hỗ trợ cả cú pháp toán học (mathjs) lẫn hàm Excel (SUM, IF, CONCAT). Kết quả tự động tính lại khi dữ liệu thay đổi.

Ba nhiệm vụ chính ​

#Nhiệm vụChi tiết
1Tính toán công thứcEngine evaluator tính giá trị dựa trên expression và field values
2Tự động recomputeHooks trigger tính lại khi bản ghi save, bulk create, hoặc field definition thay đổi
3Typed outputKết quả lưu theo dataType setting (number, string, date...)

Kiến trúc ​

Tổng quan ​

Plugin có cả server (field type, evaluator, hooks) và client (interface, expression editor). Server tính toán và lưu kết quả; client cung cấp UI soạn công thức.

Luồng tính toán khi save bản ghi ​

Luồng recompute khi field definition thay đổi ​

Evaluator engines ​

EngineCú phápVí dụ
mathjsToán học tiêu chuẩnprice * quantity, sqrt(a^2 + b^2)
excel-likeHàm dạng ExcelIF(status == "paid", total, 0), CONCAT(firstName, " ", lastName)

Field type formula ​

Thuộc tínhMô tả
expressionBiểu thức công thức (tham chiếu field qua tên)
engineEngine tính toán: mathjs hoặc formula (excel-like)
dataTypeKiểu dữ liệu cột lưu kết quả (double, string, integer...)

Hooks ​

HookSự kiệnHành động
afterSaveBản ghi được lưuTính lại formula cho bản ghi đó
bulkCreateTạo nhiều bản ghiTính formula cho tất cả bản ghi mới
fields.afterUpdateSửa field definitionRecompute toàn bộ bản ghi

Ví dụ sử dụng ​

Cấu hình field formula ​

typescript
const totalField = {
  name: 'total',
  type: 'formula',
  interface: 'formula',
  dataType: 'double',
  expression: 'price * quantity',
  engine: 'mathjs',
};

Thành phần client ​

Thành phầnMô tả
FormulaFieldInterfaceĐịnh nghĩa interface cho field formula
FormulaComponent hiển thị kết quả tính toán (read-only)
Expression EditorUI soạn công thức với autocomplete tên field

Dependencies ​

PackageVai trò
@digiforce-nc/serverServer framework (peer)
@digiforce-nc/databaseDatabase ORM — đăng ký field type
@digiforce-nc/evaluatorsEngine tính toán (mathjs, excel-like)
@digiforce-nc/utilsTiện ích chung
@digiforce-nc/clientClient framework — đăng ký component

Mục lục chi tiết ​