@nocobase/plugin-collection-tree 1.6.32 → 1.6.34

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.
@@ -8,11 +8,11 @@
8
8
  */
9
9
 
10
10
  module.exports = {
11
- "@nocobase/client": "1.6.32",
12
- "@nocobase/database": "1.6.32",
13
- "@nocobase/utils": "1.6.32",
11
+ "@nocobase/client": "1.6.34",
12
+ "@nocobase/database": "1.6.34",
13
+ "@nocobase/utils": "1.6.34",
14
14
  "lodash": "4.17.21",
15
- "@nocobase/data-source-manager": "1.6.32",
16
- "@nocobase/server": "1.6.32",
15
+ "@nocobase/data-source-manager": "1.6.34",
16
+ "@nocobase/server": "1.6.34",
17
17
  "sequelize": "6.35.2"
18
18
  };
@@ -0,0 +1,10 @@
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 { Application } from '@nocobase/server';
10
+ export default function (app: Application): void;
@@ -0,0 +1,116 @@
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 __create = Object.create;
11
+ var __defProp = Object.defineProperty;
12
+ var __getOwnPropDesc = Object.getOwnPropertyDescriptor;
13
+ var __getOwnPropNames = Object.getOwnPropertyNames;
14
+ var __getProtoOf = Object.getPrototypeOf;
15
+ var __hasOwnProp = Object.prototype.hasOwnProperty;
16
+ var __export = (target, all) => {
17
+ for (var name in all)
18
+ __defProp(target, name, { get: all[name], enumerable: true });
19
+ };
20
+ var __copyProps = (to, from, except, desc) => {
21
+ if (from && typeof from === "object" || typeof from === "function") {
22
+ for (let key of __getOwnPropNames(from))
23
+ if (!__hasOwnProp.call(to, key) && key !== except)
24
+ __defProp(to, key, { get: () => from[key], enumerable: !(desc = __getOwnPropDesc(from, key)) || desc.enumerable });
25
+ }
26
+ return to;
27
+ };
28
+ var __toESM = (mod, isNodeMode, target) => (target = mod != null ? __create(__getProtoOf(mod)) : {}, __copyProps(
29
+ // If the importer is in node compatibility mode or this is not an ESM
30
+ // file that has been converted to a CommonJS file using a Babel-
31
+ // compatible transform (i.e. "__esModule" has not been set), then set
32
+ // "default" to the CommonJS "module.exports" for node compatibility.
33
+ isNodeMode || !mod || !mod.__esModule ? __defProp(target, "default", { value: mod, enumerable: true }) : target,
34
+ mod
35
+ ));
36
+ var __toCommonJS = (mod) => __copyProps(__defProp({}, "__esModule", { value: true }), mod);
37
+ var sync_path_exports = {};
38
+ __export(sync_path_exports, {
39
+ default: () => sync_path_default
40
+ });
41
+ module.exports = __toCommonJS(sync_path_exports);
42
+ var import_lodash = __toESM(require("lodash"));
43
+ async function getTreePath(db, model, path, collection, pathCollectionName, transaction) {
44
+ if (model.get("parentId") !== null) {
45
+ const parent = await db.getRepository(collection).findOne({
46
+ filter: { id: model.get("parentId") },
47
+ transaction
48
+ });
49
+ if (parent && parent.get("parentId") !== model.get("id")) {
50
+ path = `/${parent.get("id")}${path}`;
51
+ const collectionTreePath = db.getCollection(pathCollectionName);
52
+ const nodePkColumnName = collectionTreePath.getField("nodePk").columnName();
53
+ const parentPathData = await db.getRepository(pathCollectionName).findOne({
54
+ filter: { [nodePkColumnName]: parent.get("id") },
55
+ transaction
56
+ });
57
+ const parentPath = import_lodash.default.get(parentPathData, "path", null);
58
+ if (parentPath == null) {
59
+ path = await getTreePath(db, parent, path, collection, pathCollectionName, transaction);
60
+ } else {
61
+ path = `${parentPath}/${model.get("id")}`;
62
+ }
63
+ }
64
+ }
65
+ return path;
66
+ }
67
+ function sync_path_default(app) {
68
+ app.command("tree-collection:sync-path").preload().option("-c, --collection [collection]").action(async (options) => {
69
+ const { collection: name } = options || {};
70
+ const mainData = app.pm.get("data-source-main");
71
+ mainData.setLoadFilter({
72
+ name
73
+ });
74
+ await app.emitAsync("beforeStart");
75
+ if (!name) {
76
+ throw new Error("Collection name is required");
77
+ }
78
+ const collection = app.db.getCollection(name);
79
+ if (!collection) {
80
+ throw new Error(`Collection ${name} not found`);
81
+ }
82
+ const isTree = collection.options.tree;
83
+ if (!isTree) {
84
+ throw new Error(`Collection ${name} is not a tree collection`);
85
+ }
86
+ const pathTableName = `main_${name}_path`;
87
+ const pathRepo = app.db.getRepository(pathTableName);
88
+ const nodeRepo = app.db.getRepository(name);
89
+ const chunkSize = 1e3;
90
+ await app.db.sequelize.transaction(async (transaction) => {
91
+ await pathRepo.destroy({ truncate: true, transaction });
92
+ console.log(`Truncated table ${pathTableName}`);
93
+ await nodeRepo.chunk({
94
+ chunkSize,
95
+ callback: async (records, options2) => {
96
+ const toInsert = [];
97
+ for (const record of records) {
98
+ const id = record.get("id");
99
+ let path = `/${id}`;
100
+ path = await getTreePath(app.db, record, path, name, pathTableName, transaction);
101
+ toInsert.push({
102
+ nodePk: id,
103
+ path,
104
+ rootPk: path.split("/")[1]
105
+ });
106
+ }
107
+ if (toInsert.length > 0) {
108
+ await app.db.getModel(pathTableName).bulkCreate(toInsert, { transaction });
109
+ console.log(`Inserted ${toInsert.length} records into ${pathTableName}`);
110
+ }
111
+ },
112
+ transaction
113
+ });
114
+ });
115
+ });
116
+ }
@@ -119,7 +119,7 @@ class PluginCollectionTreeServer extends import_server.Plugin {
119
119
  });
120
120
  this.db.on(`${collection.name}.beforeSave`, async (model) => {
121
121
  const tk = collection.filterTargetKey;
122
- if (model.get(parentForeignKey) === model.get(tk)) {
122
+ if (model.get(tk) && model.get(parentForeignKey) === model.get(tk)) {
123
123
  throw new Error("Cannot set itself as the parent node");
124
124
  }
125
125
  });
@@ -200,11 +200,15 @@ class PluginCollectionTreeServer extends import_server.Plugin {
200
200
  },
201
201
  transaction
202
202
  });
203
+ const basePath = pathData.get("path");
203
204
  const relatedNodes = await this.app.db.getRepository(pathCollectionName).find({
204
205
  filter: {
205
- path: {
206
- $startsWith: `${pathData.get("path")}`
207
- }
206
+ $or: [
207
+ { path: basePath },
208
+ // 自身节点
209
+ { path: { $startsWith: `${basePath}/` } }
210
+ // 确保是子节点(路径段)
211
+ ]
208
212
  },
209
213
  transaction
210
214
  });
package/package.json CHANGED
@@ -1,6 +1,6 @@
1
1
  {
2
2
  "name": "@nocobase/plugin-collection-tree",
3
- "version": "1.6.32",
3
+ "version": "1.6.34",
4
4
  "displayName": "Collection: Tree",
5
5
  "displayName.zh-CN": "数据表:树",
6
6
  "description": "Provides tree collection template",
@@ -14,5 +14,5 @@
14
14
  "@nocobase/server": "1.x",
15
15
  "@nocobase/test": "1.x"
16
16
  },
17
- "gitHead": "b546588373c844aa108d2adb42e76cc84c4b8de8"
17
+ "gitHead": "6066f121bc4fa37b3b1ad6032426295ff381ffa3"
18
18
  }