@nocobase/plugin-collection-tree 2.0.0-alpha.9 → 2.0.0-beta.2

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": "2.0.0-alpha.9",
12
- "@nocobase/database": "2.0.0-alpha.9",
13
- "@nocobase/utils": "2.0.0-alpha.9",
11
+ "@nocobase/client": "2.0.0-beta.2",
12
+ "@nocobase/database": "2.0.0-beta.2",
13
+ "@nocobase/utils": "2.0.0-beta.2",
14
14
  "lodash": "4.17.21",
15
- "@nocobase/data-source-manager": "2.0.0-alpha.9",
16
- "@nocobase/server": "2.0.0-alpha.9",
15
+ "@nocobase/data-source-manager": "2.0.0-beta.2",
16
+ "@nocobase/server": "2.0.0-beta.2",
17
17
  "sequelize": "6.35.2"
18
18
  };
@@ -0,0 +1 @@
1
+ {}
@@ -54,18 +54,19 @@ class PluginCollectionTreeServer extends import_server.Plugin {
54
54
  this.app.dataSourceManager.afterAddDataSource((dataSource) => {
55
55
  const collectionManager = dataSource.collectionManager;
56
56
  if (collectionManager instanceof import_data_source_manager.SequelizeCollectionManager) {
57
- collectionManager.db.on("afterDefineCollection", (collection) => {
58
- var _a;
57
+ collectionManager.db.on("afterDefineCollection", (collection, eventOptions) => {
59
58
  if (!condition(collection.options)) {
60
59
  return;
61
60
  }
62
61
  const name = `${dataSource.name}_${collection.name}_path`;
63
- const parentForeignKey = ((_a = collection.treeParentField) == null ? void 0 : _a.foreignKey) || "parentId";
64
62
  const options = {};
65
63
  options["mainCollection"] = collection.name;
66
64
  if (collection.options.schema) {
67
65
  options["schema"] = collection.options.schema;
68
66
  }
67
+ if (eventOptions.fieldModels) {
68
+ options["fieldModels"] = eventOptions.fieldModels;
69
+ }
69
70
  this.defineTreePathCollection(name, options);
70
71
  collectionManager.db.on(`${collection.name}.afterSync`, async ({ transaction }) => {
71
72
  await this.db.getCollection(name).sync({ transaction });
@@ -85,8 +86,28 @@ class PluginCollectionTreeServer extends import_server.Plugin {
85
86
  transaction
86
87
  });
87
88
  });
89
+ this.db.on(`${collection.name}.afterBulkCreate`, async (instances, options2) => {
90
+ const { transaction } = options2;
91
+ const tk = collection.filterTargetKey;
92
+ const records = [];
93
+ for (const model of instances) {
94
+ let path = `/${model.get(tk)}`;
95
+ path = await this.getTreePath(model, path, collection, name, transaction);
96
+ const rootPk = path.split("/")[1] || null;
97
+ records.push({
98
+ nodePk: model.get(tk),
99
+ path,
100
+ rootPk
101
+ });
102
+ }
103
+ await this.app.db.getModel(name).bulkCreate(records, {
104
+ transaction
105
+ });
106
+ });
88
107
  this.db.on(`${collection.name}.afterUpdate`, async (model, options2) => {
108
+ var _a;
89
109
  const tk = collection.filterTargetKey;
110
+ const parentForeignKey = ((_a = collection.treeParentField) == null ? void 0 : _a.foreignKey) || "parentId";
90
111
  if (!(model._changed.has(tk) || model._changed.has(parentForeignKey))) {
91
112
  return;
92
113
  }
@@ -118,7 +139,9 @@ class PluginCollectionTreeServer extends import_server.Plugin {
118
139
  });
119
140
  });
120
141
  this.db.on(`${collection.name}.beforeSave`, async (model) => {
142
+ var _a;
121
143
  const tk = collection.filterTargetKey;
144
+ const parentForeignKey = ((_a = collection.treeParentField) == null ? void 0 : _a.foreignKey) || "parentId";
122
145
  if (model.get(tk) && model.get(parentForeignKey) === model.get(tk)) {
123
146
  throw new Error("Cannot set itself as the parent node");
124
147
  }
@@ -144,14 +167,21 @@ class PluginCollectionTreeServer extends import_server.Plugin {
144
167
  });
145
168
  }
146
169
  async defineTreePathCollection(name, options) {
170
+ let nodePkType = "bigInt";
171
+ if (options.fieldModels) {
172
+ const pk = options.fieldModels.find((x) => x.options.primaryKey === true);
173
+ if (pk) {
174
+ nodePkType = pk.type;
175
+ }
176
+ }
147
177
  this.db.collection({
148
178
  name,
149
179
  autoGenId: false,
150
180
  timestamps: false,
151
181
  fields: [
152
- { type: "bigInt", name: "nodePk" },
182
+ { type: nodePkType, name: "nodePk" },
153
183
  { type: "string", name: "path", length: 1024 },
154
- { type: "bigInt", name: "rootPk" }
184
+ { type: nodePkType, name: "rootPk" }
155
185
  ],
156
186
  indexes: [
157
187
  {
package/package.json CHANGED
@@ -1,9 +1,11 @@
1
1
  {
2
2
  "name": "@nocobase/plugin-collection-tree",
3
- "version": "2.0.0-alpha.9",
3
+ "version": "2.0.0-beta.2",
4
4
  "displayName": "Collection: Tree",
5
+ "displayName.ru-RU": "Древовидная Коллекция",
5
6
  "displayName.zh-CN": "数据表:树",
6
7
  "description": "Provides tree collection template",
8
+ "description.ru-RU": "Предоставляет шаблон коллекции в виде дерева. Широко используется для организации документов, каталожных разделов, управляемых задач, меню, комментариев и других сценариев, где важна вложенность и классификация.",
7
9
  "description.zh-CN": "提供树数据表模板",
8
10
  "keywords": [
9
11
  "Collections"
@@ -14,5 +16,5 @@
14
16
  "@nocobase/server": "2.x",
15
17
  "@nocobase/test": "2.x"
16
18
  },
17
- "gitHead": "4a9acf96f21a3aa35bccbd188b942595b09da0a9"
19
+ "gitHead": "b77a33ee933ae6e09d2d5dce017ca15d8552d57b"
18
20
  }