@nocobase/plugin-excel-formula-field
Version:
English | [中文](./README.zh-CN.md)
114 lines (113 loc) • 3.67 kB
JavaScript
var __defProp = Object.defineProperty;
var __getOwnPropDesc = Object.getOwnPropertyDescriptor;
var __getOwnPropNames = Object.getOwnPropertyNames;
var __hasOwnProp = Object.prototype.hasOwnProperty;
var __export = (target, all) => {
for (var name in all)
__defProp(target, name, { get: all[name], enumerable: true });
};
var __copyProps = (to, from, except, desc) => {
if (from && typeof from === "object" || typeof from === "function") {
for (let key of __getOwnPropNames(from))
if (!__hasOwnProp.call(to, key) && key !== except)
__defProp(to, key, { get: () => from[key], enumerable: !(desc = __getOwnPropDesc(from, key)) || desc.enumerable });
}
return to;
};
var __toCommonJS = (mod) => __copyProps(__defProp({}, "__esModule", { value: true }), mod);
var excel_formula_field_exports = {};
__export(excel_formula_field_exports, {
ExcelFormulaField: () => ExcelFormulaField
});
module.exports = __toCommonJS(excel_formula_field_exports);
var import_database = require("@nocobase/database");
var import_evaluate = require("../utils/evaluate");
class ExcelFormulaField extends import_database.Field {
get dataType() {
const { dataType } = this.options;
return dataType === "string" ? import_database.DataTypes.STRING : import_database.DataTypes.DOUBLE;
}
calculate(expression, scope) {
try {
return (0, import_evaluate.evaluate)(expression, scope);
} catch {
}
return null;
}
initFieldData = async ({ transaction }) => {
const { expression, name } = this.options;
const records = await this.collection.repository.find({
order: [this.collection.model.primaryKeyAttribute],
transaction
});
for (const record of records) {
const scope = record.toJSON();
const result = this.calculate(expression, scope);
if (result) {
await record.update(
{
[name]: result
},
{
transaction,
silent: true,
hooks: false
}
);
}
}
};
calculateField = async (instance) => {
const { expression, name } = this.options;
const scope = instance.toJSON();
const result = this.calculate(expression, scope);
if (result) {
instance.set(name, result);
}
};
updateFieldData = async (instance, { transaction }) => {
if (this.collection.name === instance.collectionName && instance.name === this.options.name) {
this.options = Object.assign(this.options, instance.options);
const { name, expression } = this.options;
const records = await this.collection.repository.find({
order: [this.collection.model.primaryKeyAttribute],
transaction
});
for (const record of records) {
const scope = record.toJSON();
const result = this.calculate(expression, scope);
await record.update(
{
[name]: result
},
{
transaction,
silent: true,
hooks: false
}
);
}
}
};
bind() {
super.bind();
this.on("afterSync", this.initFieldData);
this.database.on("fields.afterUpdate", this.updateFieldData);
this.on("beforeSave", this.calculateField);
}
unbind() {
super.unbind();
this.off("beforeSave", this.calculateField);
this.database.off("fields.afterUpdate", this.updateFieldData);
this.off("afterSync", this.initFieldData);
}
toSequelize() {
const opts = super.toSequelize();
delete opts.dataType;
return opts;
}
}
// Annotate the CommonJS export names for ESM import in node:
0 && (module.exports = {
ExcelFormulaField
});