@tachybase/actions 1.6.17 → 1.6.19

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 +52 -69
  2. package/package.json +5 -5
@@ -33,7 +33,6 @@ __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");
37
36
  var import_constants = require("../constants");
38
37
  var import_utils2 = require("../utils");
39
38
  function totalPage(total, pageSize) {
@@ -111,80 +110,51 @@ async function listWithPagination(ctx) {
111
110
  });
112
111
  let filterTreeData = [];
113
112
  let filterTreeCount = 0;
113
+ let hasFilteredTree = false;
114
114
  if (ctx.action.params.tree && options.filter) {
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
- }
115
+ const parentKey = ((_a = collection.treeParentField) == null ? void 0 : _a.foreignKey) || "parentId";
119
116
  const params = Object.values(options.filter).flat()[0] || {};
120
- let dataIds = [];
121
117
  if (Object.entries(params).length) {
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);
171
- }
172
- const ids = [...new Set(allDataIds)];
173
- const where = {
174
- id: {
175
- [import_sequelize.Op.in]: ids
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);
176
138
  }
177
- };
139
+ }
140
+ const ids = [...visible.keys()];
141
+ const requiredFields = ["id", parentKey];
142
+ const requestedFields = Array.isArray(options.fields) ? options.fields : void 0;
178
143
  const [rows2, count2] = await repository.findAndCount({
179
- filter: where,
180
- appends: options.appends || []
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
181
150
  });
182
151
  const _data = rows2.map((item) => item.dataValues);
183
- const father = _data.filter((parent) => parent.parentId === null);
152
+ const visibleIds = new Set(_data.map((row) => row.id));
153
+ const father = _data.filter((parent) => parent[parentKey] == null || !visibleIds.has(parent[parentKey]));
184
154
  const transTreeData = /* @__PURE__ */ __name((father2, allRows) => {
185
155
  father2.forEach((parent, index) => {
186
156
  var _a2;
187
- const children = allRows.filter((child) => child.parentId === parent.id);
157
+ const children = allRows.filter((child) => child[parentKey] === parent.id);
188
158
  const i = index.toString();
189
159
  parent.__index = parent.father || parent.father === "0" ? parent.father + ".children." + i : i;
190
160
  if (children == null ? void 0 : children.length) {
@@ -202,14 +172,26 @@ async function listWithPagination(ctx) {
202
172
  });
203
173
  }, "transTreeData");
204
174
  transTreeData(father, _data);
205
- filterTreeData = father;
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);
206
188
  filterTreeCount = father.length;
207
189
  }
208
190
  }
209
- const [rows, count] = await repository.findAndCount(options);
191
+ const [rows, count] = hasFilteredTree ? [filterTreeData, filterTreeCount] : await repository.findAndCount(options);
210
192
  ctx.body = {
211
- count: filterTreeData.length ? filterTreeCount : count,
212
- rows: filterTreeData.length ? filterTreeData : rows,
193
+ count: hasFilteredTree ? filterTreeCount : count,
194
+ rows: hasFilteredTree ? filterTreeData : rows,
213
195
  page: Number(page),
214
196
  pageSize: Number(pageSize),
215
197
  totalPage: totalPage(count, pageSize)
@@ -234,6 +216,7 @@ async function list(ctx, next) {
234
216
  await next();
235
217
  }
236
218
  __name(list, "list");
219
+ Object.assign(list, { supportsScopedTreeRead: true });
237
220
  // Annotate the CommonJS export names for ESM import in node:
238
221
  0 && (module.exports = {
239
222
  list
package/package.json CHANGED
@@ -1,6 +1,6 @@
1
1
  {
2
2
  "name": "@tachybase/actions",
3
- "version": "1.6.17",
3
+ "version": "1.6.19",
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/database": "1.6.17",
22
- "@tachybase/utils": "1.6.17",
23
- "@tachybase/cache": "1.6.17",
24
- "@tachybase/resourcer": "1.6.17"
21
+ "@tachybase/database": "1.6.19",
22
+ "@tachybase/cache": "1.6.19",
23
+ "@tachybase/resourcer": "1.6.19",
24
+ "@tachybase/utils": "1.6.19"
25
25
  },
26
26
  "devDependencies": {
27
27
  "@types/koa": "^2.15.0",