@nocobase/plugin-flow-engine 2.0.0-alpha.6 → 2.0.0-alpha.60

This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
@@ -0,0 +1,80 @@
1
+ /**
2
+ * This file is part of the NocoBase (R) project.
3
+ * Copyright (c) 2020-2024 NocoBase Co., Ltd.
4
+ * Authors: NocoBase Team.
5
+ *
6
+ * This project is dual-licensed under AGPL-3.0 and NocoBase Commercial License.
7
+ * For more information, please refer to: https://www.nocobase.com/agreement.
8
+ */
9
+
10
+ var __defProp = Object.defineProperty;
11
+ var __getOwnPropDesc = Object.getOwnPropertyDescriptor;
12
+ var __getOwnPropNames = Object.getOwnPropertyNames;
13
+ var __hasOwnProp = Object.prototype.hasOwnProperty;
14
+ var __export = (target, all) => {
15
+ for (var name in all)
16
+ __defProp(target, name, { get: all[name], enumerable: true });
17
+ };
18
+ var __copyProps = (to, from, except, desc) => {
19
+ if (from && typeof from === "object" || typeof from === "function") {
20
+ for (let key of __getOwnPropNames(from))
21
+ if (!__hasOwnProp.call(to, key) && key !== except)
22
+ __defProp(to, key, { get: () => from[key], enumerable: !(desc = __getOwnPropDesc(from, key)) || desc.enumerable });
23
+ }
24
+ return to;
25
+ };
26
+ var __toCommonJS = (mod) => __copyProps(__defProp({}, "__esModule", { value: true }), mod);
27
+ var selects_exports = {};
28
+ __export(selects_exports, {
29
+ adjustSelectsForCollection: () => adjustSelectsForCollection
30
+ });
31
+ module.exports = __toCommonJS(selects_exports);
32
+ function adjustSelectsForCollection(koaCtx, dataSourceKey, collection, fields, appends) {
33
+ var _a, _b, _c, _d;
34
+ const ds = koaCtx.app.dataSourceManager.get(dataSourceKey || "main");
35
+ const cm = ds.collectionManager;
36
+ const coll = (_b = (_a = cm == null ? void 0 : cm.db) == null ? void 0 : _a.getCollection) == null ? void 0 : _b.call(_a, collection);
37
+ const assocKeys = Object.keys(((_c = coll == null ? void 0 : coll.model) == null ? void 0 : _c.associations) || {});
38
+ const rawAttrs = ((_d = coll == null ? void 0 : coll.model) == null ? void 0 : _d.rawAttributes) || {};
39
+ const toCamel = (s) => s.replace(/_([a-zA-Z0-9])/g, (_m, c) => String(c).toUpperCase());
40
+ const toSnake = (s) => s.replace(/([A-Z])/g, "_$1").toLowerCase().replace(/^_/, "");
41
+ const assocMap = /* @__PURE__ */ new Map();
42
+ for (const k of assocKeys) {
43
+ assocMap.set(k, k);
44
+ assocMap.set(toSnake(k), k);
45
+ assocMap.set(toCamel(k), k);
46
+ }
47
+ const outFields = [];
48
+ const outAppends = new Set(appends || []);
49
+ for (const f of fields || []) {
50
+ const segs = String(f).split(".").filter(Boolean);
51
+ if (!segs.length) continue;
52
+ const first = segs[0];
53
+ const assocCanonical = assocMap.get(first) || assocMap.get(toCamel(first)) || assocMap.get(toSnake(first));
54
+ if (assocCanonical) {
55
+ outAppends.add(assocCanonical);
56
+ if (segs.length === 1) {
57
+ continue;
58
+ }
59
+ outFields.push([assocCanonical, ...segs.slice(1)].join("."));
60
+ continue;
61
+ }
62
+ if (rawAttrs[first]) {
63
+ outFields.push(f);
64
+ } else if (rawAttrs[toSnake(first)]) {
65
+ outFields.push([toSnake(first), ...segs.slice(1)].join("."));
66
+ } else if (rawAttrs[toCamel(first)]) {
67
+ outFields.push([toCamel(first), ...segs.slice(1)].join("."));
68
+ } else {
69
+ continue;
70
+ }
71
+ }
72
+ return {
73
+ fields: outFields.length ? outFields : void 0,
74
+ appends: outAppends.size ? Array.from(outAppends) : void 0
75
+ };
76
+ }
77
+ // Annotate the CommonJS export names for ESM import in node:
78
+ 0 && (module.exports = {
79
+ adjustSelectsForCollection
80
+ });
@@ -0,0 +1,17 @@
1
+ /**
2
+ * This file is part of the NocoBase (R) project.
3
+ * Copyright (c) 2020-2024 NocoBase Co., Ltd.
4
+ * Authors: NocoBase Team.
5
+ *
6
+ * This project is dual-licensed under AGPL-3.0 and NocoBase Commercial License.
7
+ * For more information, please refer to: https://www.nocobase.com/agreement.
8
+ */
9
+ import type { ResourcerContext } from '@nocobase/resourcer';
10
+ import type { JSONValue } from '../template/resolver';
11
+ /**
12
+ * 预取:构建“同记录”的字段/关联并集,一次查询写入 ctx.state.__varResolveBatchCache,供后续解析复用
13
+ */
14
+ export declare function prefetchRecordsForResolve(koaCtx: ResourcerContext, items: Array<{
15
+ template: JSONValue;
16
+ contextParams?: Record<string, unknown>;
17
+ }>): Promise<void>;
@@ -0,0 +1,123 @@
1
+ /**
2
+ * This file is part of the NocoBase (R) project.
3
+ * Copyright (c) 2020-2024 NocoBase Co., Ltd.
4
+ * Authors: NocoBase Team.
5
+ *
6
+ * This project is dual-licensed under AGPL-3.0 and NocoBase Commercial License.
7
+ * For more information, please refer to: https://www.nocobase.com/agreement.
8
+ */
9
+
10
+ var __defProp = Object.defineProperty;
11
+ var __getOwnPropDesc = Object.getOwnPropertyDescriptor;
12
+ var __getOwnPropNames = Object.getOwnPropertyNames;
13
+ var __hasOwnProp = Object.prototype.hasOwnProperty;
14
+ var __export = (target, all) => {
15
+ for (var name in all)
16
+ __defProp(target, name, { get: all[name], enumerable: true });
17
+ };
18
+ var __copyProps = (to, from, except, desc) => {
19
+ if (from && typeof from === "object" || typeof from === "function") {
20
+ for (let key of __getOwnPropNames(from))
21
+ if (!__hasOwnProp.call(to, key) && key !== except)
22
+ __defProp(to, key, { get: () => from[key], enumerable: !(desc = __getOwnPropDesc(from, key)) || desc.enumerable });
23
+ }
24
+ return to;
25
+ };
26
+ var __toCommonJS = (mod) => __copyProps(__defProp({}, "__esModule", { value: true }), mod);
27
+ var utils_exports = {};
28
+ __export(utils_exports, {
29
+ prefetchRecordsForResolve: () => prefetchRecordsForResolve
30
+ });
31
+ module.exports = __toCommonJS(utils_exports);
32
+ var import_registry = require("./registry");
33
+ var import_selects = require("./selects");
34
+ async function prefetchRecordsForResolve(koaCtx, items) {
35
+ var _a, _b, _c, _d, _e, _f, _g;
36
+ try {
37
+ const log = (_b = (_a = koaCtx.app) == null ? void 0 : _a.logger) == null ? void 0 : _b.child({ module: "plugin-flow-engine", submodule: "variables.prefetch" });
38
+ const groupMap = /* @__PURE__ */ new Map();
39
+ const ensureGroup = (dataSourceKey, collection, filterByTk) => {
40
+ const groupKey = JSON.stringify({ ds: dataSourceKey, collection, tk: filterByTk });
41
+ let group = groupMap.get(groupKey);
42
+ if (!group) {
43
+ group = { dataSourceKey, collection, filterByTk, fields: /* @__PURE__ */ new Set(), appends: /* @__PURE__ */ new Set() };
44
+ groupMap.set(groupKey, group);
45
+ }
46
+ return group;
47
+ };
48
+ const normalizeNestedSeg = (segment) => /^\d+$/.test(segment) ? `[${segment}]` : segment;
49
+ for (const it of items) {
50
+ const template = (it == null ? void 0 : it.template) ?? {};
51
+ const contextParams = (it == null ? void 0 : it.contextParams) || {};
52
+ const usage = import_registry.variables.extractUsage(template);
53
+ for (const [cpKey, recordParams] of Object.entries(contextParams)) {
54
+ const parts = String(cpKey).split(".");
55
+ const varName = parts[0];
56
+ const nestedSeg = parts.slice(1).join(".");
57
+ const paths = (usage == null ? void 0 : usage[varName]) || [];
58
+ if (!paths.length) continue;
59
+ const segNorm = nestedSeg ? normalizeNestedSeg(nestedSeg) : "";
60
+ const remainders = [];
61
+ for (const p of paths) {
62
+ if (!segNorm) remainders.push(p);
63
+ else if (p === segNorm) remainders.push("");
64
+ else if (p.startsWith(`${segNorm}.`) || p.startsWith(`${segNorm}[`))
65
+ remainders.push(p.slice(segNorm.length + 1));
66
+ }
67
+ if (!remainders.length) continue;
68
+ const dataSourceKey = (recordParams == null ? void 0 : recordParams.dataSourceKey) || "main";
69
+ const collection = recordParams == null ? void 0 : recordParams.collection;
70
+ const filterByTk = recordParams == null ? void 0 : recordParams.filterByTk;
71
+ if (!collection || typeof filterByTk === "undefined") continue;
72
+ const group = ensureGroup(dataSourceKey, collection, filterByTk);
73
+ let { generatedAppends, generatedFields } = (0, import_registry.inferSelectsFromUsage)(remainders);
74
+ const fixed = (0, import_selects.adjustSelectsForCollection)(koaCtx, dataSourceKey, collection, generatedFields, generatedAppends);
75
+ generatedFields = fixed.fields;
76
+ generatedAppends = fixed.appends;
77
+ if (generatedFields == null ? void 0 : generatedFields.length) generatedFields.forEach((f) => group.fields.add(f));
78
+ if (generatedAppends == null ? void 0 : generatedAppends.length) generatedAppends.forEach((a) => group.appends.add(a));
79
+ }
80
+ }
81
+ if (!groupMap.size) return;
82
+ const stateObj = koaCtx.state;
83
+ if (stateObj && !stateObj["__varResolveBatchCache"]) {
84
+ stateObj["__varResolveBatchCache"] = /* @__PURE__ */ new Map();
85
+ }
86
+ const cache = (_c = koaCtx.state) == null ? void 0 : _c["__varResolveBatchCache"];
87
+ for (const { dataSourceKey, collection, filterByTk, fields, appends } of groupMap.values()) {
88
+ try {
89
+ const ds = koaCtx.app.dataSourceManager.get(dataSourceKey);
90
+ const cm = ds.collectionManager;
91
+ if (!(cm == null ? void 0 : cm.db)) continue;
92
+ const repo = cm.db.getRepository(collection);
93
+ const modelInfo = (_d = repo.collection) == null ? void 0 : _d.model;
94
+ const pkAttr = modelInfo == null ? void 0 : modelInfo.primaryKeyAttribute;
95
+ const pkIsValid = pkAttr && (modelInfo == null ? void 0 : modelInfo.rawAttributes) && Object.prototype.hasOwnProperty.call(modelInfo.rawAttributes, pkAttr);
96
+ if (fields.size && pkIsValid) {
97
+ fields.add(pkAttr);
98
+ }
99
+ const fld = fields.size ? Array.from(fields).sort() : void 0;
100
+ const app = appends.size ? Array.from(appends).sort() : void 0;
101
+ const rec = await repo.findOne({ filterByTk, fields: fld, appends: app });
102
+ const json = rec ? rec.toJSON() : void 0;
103
+ if (cache) {
104
+ const key = JSON.stringify({ ds: dataSourceKey, c: collection, tk: filterByTk, f: fld, a: app });
105
+ cache.set(key, json);
106
+ }
107
+ } catch (e) {
108
+ log == null ? void 0 : log.debug("[variables.resolve] prefetch query error", {
109
+ ds: dataSourceKey,
110
+ collection,
111
+ tk: filterByTk,
112
+ error: (e == null ? void 0 : e.message) || String(e)
113
+ });
114
+ }
115
+ }
116
+ } catch (e) {
117
+ (_g = (_f = (_e = koaCtx.app) == null ? void 0 : _e.logger) == null ? void 0 : _f.child({ module: "plugin-flow-engine", submodule: "variables.prefetch" })) == null ? void 0 : _g.debug("[variables.resolve] prefetch fatal error", { error: (e == null ? void 0 : e.message) || String(e) });
118
+ }
119
+ }
120
+ // Annotate the CommonJS export names for ESM import in node:
121
+ 0 && (module.exports = {
122
+ prefetchRecordsForResolve
123
+ });
package/package.json CHANGED
@@ -4,7 +4,7 @@
4
4
  "displayName.zh-CN": "前端流引擎",
5
5
  "description": "",
6
6
  "description.zh-CN": "",
7
- "version": "2.0.0-alpha.6",
7
+ "version": "2.0.0-alpha.60",
8
8
  "main": "./dist/server/index.js",
9
9
  "license": "AGPL-3.0",
10
10
  "devDependencies": {
@@ -24,5 +24,5 @@
24
24
  "@nocobase/test": "2.x",
25
25
  "@nocobase/utils": "2.x"
26
26
  },
27
- "gitHead": "276a65c0656bbace2df7593be0f53c96b7b0725b"
27
+ "gitHead": "9113d61ce85b60b7ba3d0e5ca64182d92a15ece4"
28
28
  }