@tachybase/actions 1.6.23 → 1.6.25

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.
Files changed (2) hide show
  1. package/lib/actions/list.js +69 -52
  2. package/package.json +5 -5
@@ -33,6 +33,7 @@ __export(list_exports, {
33
33
  module.exports = __toCommonJS(list_exports);
34
34
  var import_node_querystring = __toESM(require("node:querystring"));
35
35
  var import_utils = require("@tachybase/utils");
36
+ var import_sequelize = require("sequelize");
36
37
  var import_constants = require("../constants");
37
38
  var import_utils2 = require("../utils");
38
39
  function totalPage(total, pageSize) {
@@ -110,51 +111,80 @@ async function listWithPagination(ctx) {
110
111
  });
111
112
  let filterTreeData = [];
112
113
  let filterTreeCount = 0;
113
- let hasFilteredTree = false;
114
114
  if (ctx.action.params.tree && options.filter) {
115
- const parentKey = ((_a = collection.treeParentField) == null ? void 0 : _a.foreignKey) || "parentId";
115
+ let foreignKey = ((_c = (_b = collection.treeParentField) == null ? void 0 : _b.collection.model.rawAttributes[(_a = collection.treeParentField) == null ? void 0 : _a.foreignKey]) == null ? void 0 : _c.field) || "parentId";
116
+ if (!ctx.db.isMySQLCompatibleDialect()) {
117
+ foreignKey = `"${foreignKey}"`;
118
+ }
116
119
  const params = Object.values(options.filter).flat()[0] || {};
120
+ let dataIds = [];
117
121
  if (Object.entries(params).length) {
118
- hasFilteredTree = true;
119
- const readScope = await ((_b = ctx.getTreeReadScope) == null ? void 0 : _b.call(ctx, collection));
120
- const scopeFilter = (readScope == null ? void 0 : readScope.filter) || {};
121
- const scopedFilter = /* @__PURE__ */ __name((filter) => ({ $and: [scopeFilter, filter] }), "scopedFilter");
122
- const matches = await repository.find({ filter: scopedFilter(params), context: ctx });
123
- const visible = new Map(matches.map((item) => [item.id, item]));
124
- let ancestorIds = matches.map((item) => item.get(parentKey)).filter((id) => id != null);
125
- let descendantIds = matches.map((item) => item.id);
126
- while (ancestorIds.length || descendantIds.length) {
127
- const branches = [];
128
- if (ancestorIds.length) branches.push({ id: { $in: ancestorIds } });
129
- if (descendantIds.length) branches.push({ [parentKey]: { $in: descendantIds } });
130
- const adjacent = await repository.find({ filter: scopedFilter({ $or: branches }), context: ctx });
131
- ancestorIds = [];
132
- descendantIds = [];
133
- for (const row of adjacent) {
134
- if (visible.has(row.id)) continue;
135
- visible.set(row.id, row);
136
- if (row.get(parentKey) != null) ancestorIds.push(row.get(parentKey));
137
- descendantIds.push(row.id);
138
- }
122
+ const getParent = /* @__PURE__ */ __name(async (filter = {}) => {
123
+ const data = await repository.find({
124
+ filter
125
+ });
126
+ dataIds = data.map((item) => item.id);
127
+ }, "getParent");
128
+ await getParent(params);
129
+ const allDataIds = [];
130
+ for (const dataId of dataIds) {
131
+ const query = `
132
+ WITH RECURSIVE tree1 AS (
133
+ SELECT id, ${foreignKey}
134
+ FROM ${collection.model.getTableName()}
135
+ WHERE id = :dataId
136
+
137
+ UNION ALL
138
+
139
+ SELECT p.id, p.${foreignKey}
140
+ FROM tree1 up
141
+ JOIN ${collection.model.getTableName()} p ON up.${foreignKey} = p.id
142
+ ),
143
+ tree2 AS (
144
+ SELECT id, ${foreignKey}
145
+ FROM ${collection.model.getTableName()}
146
+ WHERE id = :dataId
147
+
148
+ UNION ALL
149
+
150
+ SELECT p.id, p.${foreignKey}
151
+ FROM tree2 down
152
+ JOIN ${collection.model.getTableName()} p ON down.id = p.${foreignKey}
153
+ )
154
+ SELECT DISTINCT *
155
+ FROM (
156
+ SELECT *
157
+ FROM tree1
158
+ UNION ALL
159
+ SELECT *
160
+ FROM tree2
161
+ ) AS formData;`;
162
+ const filterTreeDatas = await ctx.db.sequelize.query(query, {
163
+ replacements: {
164
+ dataId
165
+ },
166
+ type: import_sequelize.QueryTypes.SELECT
167
+ });
168
+ const newRows = filterTreeDatas;
169
+ const filterIds = newRows.map((item) => item.id);
170
+ allDataIds.push(...filterIds);
139
171
  }
140
- const ids = [...visible.keys()];
141
- const requiredFields = ["id", parentKey];
142
- const requestedFields = Array.isArray(options.fields) ? options.fields : void 0;
172
+ const ids = [...new Set(allDataIds)];
173
+ const where = {
174
+ id: {
175
+ [import_sequelize.Op.in]: ids
176
+ }
177
+ };
143
178
  const [rows2, count2] = await repository.findAndCount({
144
- filter: scopedFilter({ id: { $in: ids } }),
145
- fields: requestedFields !== void 0 ? [.../* @__PURE__ */ new Set([...requestedFields, ...requiredFields])] : void 0,
146
- except: (_c = options.except) == null ? void 0 : _c.filter((field) => !requiredFields.includes(field)),
147
- appends: options.appends || [],
148
- sort: options.sort,
149
- context: ctx
179
+ filter: where,
180
+ appends: options.appends || []
150
181
  });
151
182
  const _data = rows2.map((item) => item.dataValues);
152
- const visibleIds = new Set(_data.map((row) => row.id));
153
- const father = _data.filter((parent) => parent[parentKey] == null || !visibleIds.has(parent[parentKey]));
183
+ const father = _data.filter((parent) => parent.parentId === null);
154
184
  const transTreeData = /* @__PURE__ */ __name((father2, allRows) => {
155
185
  father2.forEach((parent, index) => {
156
186
  var _a2;
157
- const children = allRows.filter((child) => child[parentKey] === parent.id);
187
+ const children = allRows.filter((child) => child.parentId === parent.id);
158
188
  const i = index.toString();
159
189
  parent.__index = parent.father || parent.father === "0" ? parent.father + ".children." + i : i;
160
190
  if (children == null ? void 0 : children.length) {
@@ -172,26 +202,14 @@ async function listWithPagination(ctx) {
172
202
  });
173
203
  }, "transTreeData");
174
204
  transTreeData(father, _data);
175
- const stripInternalFields = /* @__PURE__ */ __name((nodes) => {
176
- var _a2, _b2;
177
- for (const row of nodes) {
178
- for (const field of requiredFields) {
179
- if (requestedFields !== void 0 && !requestedFields.includes(field) || ((_a2 = options.except) == null ? void 0 : _a2.includes(field))) {
180
- delete row[field];
181
- }
182
- }
183
- if ((_b2 = row.children) == null ? void 0 : _b2.length) stripInternalFields(row.children);
184
- }
185
- }, "stripInternalFields");
186
- stripInternalFields(father);
187
- filterTreeData = father.slice(options.offset || 0, (options.offset || 0) + options.limit);
205
+ filterTreeData = father;
188
206
  filterTreeCount = father.length;
189
207
  }
190
208
  }
191
- const [rows, count] = hasFilteredTree ? [filterTreeData, filterTreeCount] : await repository.findAndCount(options);
209
+ const [rows, count] = await repository.findAndCount(options);
192
210
  ctx.body = {
193
- count: hasFilteredTree ? filterTreeCount : count,
194
- rows: hasFilteredTree ? filterTreeData : rows,
211
+ count: filterTreeData.length ? filterTreeCount : count,
212
+ rows: filterTreeData.length ? filterTreeData : rows,
195
213
  page: Number(page),
196
214
  pageSize: Number(pageSize),
197
215
  totalPage: totalPage(count, pageSize)
@@ -216,7 +234,6 @@ async function list(ctx, next) {
216
234
  await next();
217
235
  }
218
236
  __name(list, "list");
219
- Object.assign(list, { supportsScopedTreeRead: true });
220
237
  // Annotate the CommonJS export names for ESM import in node:
221
238
  0 && (module.exports = {
222
239
  list
package/package.json CHANGED
@@ -1,6 +1,6 @@
1
1
  {
2
2
  "name": "@tachybase/actions",
3
- "version": "1.6.23",
3
+ "version": "1.6.25",
4
4
  "description": "",
5
5
  "homepage": "https://github.com/tegojs/tego#readme",
6
6
  "bugs": {
@@ -18,10 +18,10 @@
18
18
  "koa": "^2.16.2",
19
19
  "lodash": "4.17.21",
20
20
  "sequelize": "^6.37.7",
21
- "@tachybase/cache": "1.6.23",
22
- "@tachybase/utils": "1.6.23",
23
- "@tachybase/database": "1.6.23",
24
- "@tachybase/resourcer": "1.6.23"
21
+ "@tachybase/utils": "1.6.25",
22
+ "@tachybase/resourcer": "1.6.25",
23
+ "@tachybase/cache": "1.6.25",
24
+ "@tachybase/database": "1.6.25"
25
25
  },
26
26
  "devDependencies": {
27
27
  "@types/koa": "^2.15.0",