@nocobase/plugin-ui-schema-storage 1.2.12-alpha → 1.3.0-alpha.20240710084543

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 (56) hide show
  1. package/package.json +2 -2
  2. package/dist/client/index.d.ts +0 -13
  3. package/dist/client/index.js +0 -10
  4. package/dist/externalVersion.js +0 -20
  5. package/dist/index.d.ts +0 -11
  6. package/dist/index.js +0 -39
  7. package/dist/server/actions/ui-schema-action.d.ts +0 -27
  8. package/dist/server/actions/ui-schema-action.js +0 -140
  9. package/dist/server/collections/uiSchemaServerHooks.d.ts +0 -11
  10. package/dist/server/collections/uiSchemaServerHooks.js +0 -58
  11. package/dist/server/collections/uiSchemaTemplates.d.ts +0 -10
  12. package/dist/server/collections/uiSchemaTemplates.js +0 -75
  13. package/dist/server/collections/uiSchemaTreePath.d.ts +0 -11
  14. package/dist/server/collections/uiSchemaTreePath.js +0 -73
  15. package/dist/server/collections/uiSchemas.d.ts +0 -11
  16. package/dist/server/collections/uiSchemas.js +0 -62
  17. package/dist/server/dao/ui_schema_node_dao.d.ts +0 -26
  18. package/dist/server/dao/ui_schema_node_dao.js +0 -24
  19. package/dist/server/helper.d.ts +0 -8
  20. package/dist/server/helper.js +0 -9
  21. package/dist/server/index.d.ts +0 -12
  22. package/dist/server/index.js +0 -41
  23. package/dist/server/migrations/20230330214649-filter-form-block.d.ts +0 -13
  24. package/dist/server/migrations/20230330214649-filter-form-block.js +0 -58
  25. package/dist/server/migrations/20230509221649-association-select.d.ts +0 -13
  26. package/dist/server/migrations/20230509221649-association-select.js +0 -71
  27. package/dist/server/migrations/20230509235247-record-picker.d.ts +0 -16
  28. package/dist/server/migrations/20230509235247-record-picker.js +0 -121
  29. package/dist/server/migrations/20230510235247-form-field.d.ts +0 -13
  30. package/dist/server/migrations/20230510235247-form-field.js +0 -100
  31. package/dist/server/migrations/20230522231231-association-field.d.ts +0 -13
  32. package/dist/server/migrations/20230522231231-association-field.js +0 -74
  33. package/dist/server/migrations/20231015125000-support-filter-blocks-in-select-record-drawer.d.ts +0 -13
  34. package/dist/server/migrations/20231015125000-support-filter-blocks-in-select-record-drawer.js +0 -55
  35. package/dist/server/model.d.ts +0 -14
  36. package/dist/server/model.js +0 -42
  37. package/dist/server/repository.d.ts +0 -101
  38. package/dist/server/repository.js +0 -980
  39. package/dist/server/server-hooks/hooks/bind-menu-to-role.d.ts +0 -13
  40. package/dist/server/server-hooks/hooks/bind-menu-to-role.js +0 -49
  41. package/dist/server/server-hooks/hooks/factory.d.ts +0 -14
  42. package/dist/server/server-hooks/hooks/factory.js +0 -42
  43. package/dist/server/server-hooks/hooks/index.d.ts +0 -14
  44. package/dist/server/server-hooks/hooks/index.js +0 -45
  45. package/dist/server/server-hooks/hooks/remove-parents-if-no-children.d.ts +0 -14
  46. package/dist/server/server-hooks/hooks/remove-parents-if-no-children.js +0 -44
  47. package/dist/server/server-hooks/hooks/remove-schema.d.ts +0 -14
  48. package/dist/server/server-hooks/hooks/remove-schema.js +0 -51
  49. package/dist/server/server-hooks/index.d.ts +0 -33
  50. package/dist/server/server-hooks/index.js +0 -174
  51. package/dist/server/server-hooks/model.d.ts +0 -11
  52. package/dist/server/server-hooks/model.js +0 -38
  53. package/dist/server/server.d.ts +0 -17
  54. package/dist/server/server.js +0 -117
  55. package/dist/swagger/index.d.ts +0 -334
  56. package/dist/swagger/index.js +0 -363
@@ -1,980 +0,0 @@
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 __decorateClass = (decorators, target, key, kind) => {
38
- var result = kind > 1 ? void 0 : kind ? __getOwnPropDesc(target, key) : target;
39
- for (var i = decorators.length - 1, decorator; i >= 0; i--)
40
- if (decorator = decorators[i])
41
- result = (kind ? decorator(target, key, result) : decorator(result)) || result;
42
- if (kind && result)
43
- __defProp(target, key, result);
44
- return result;
45
- };
46
- var repository_exports = {};
47
- __export(repository_exports, {
48
- UiSchemaRepository: () => UiSchemaRepository,
49
- default: () => repository_default
50
- });
51
- module.exports = __toCommonJS(repository_exports);
52
- var import_database = require("@nocobase/database");
53
- var import_utils = require("@nocobase/utils");
54
- var import_lodash = __toESM(require("lodash"));
55
- const nodeKeys = ["properties", "definitions", "patternProperties", "additionalProperties", "items"];
56
- function transaction(transactionAbleArgPosition) {
57
- return (target, propertyKey, descriptor) => {
58
- const originalMethod = descriptor.value;
59
- descriptor.value = async function(...args) {
60
- if (!import_lodash.default.isNumber(transactionAbleArgPosition)) {
61
- transactionAbleArgPosition = originalMethod.length - 1;
62
- }
63
- let transaction2 = import_lodash.default.get(args, [transactionAbleArgPosition, "transaction"]);
64
- let handleTransaction = false;
65
- if (!transaction2) {
66
- transaction2 = await this.database.sequelize.transaction();
67
- handleTransaction = true;
68
- import_lodash.default.set(args, transactionAbleArgPosition, {
69
- ...import_lodash.default.get(args, transactionAbleArgPosition, {}),
70
- transaction: transaction2
71
- });
72
- }
73
- if (handleTransaction) {
74
- try {
75
- const results = await originalMethod.apply(this, args);
76
- await transaction2.commit();
77
- return results;
78
- } catch (e) {
79
- await transaction2.rollback();
80
- throw e;
81
- }
82
- } else {
83
- return await originalMethod.apply(this, args);
84
- }
85
- };
86
- return descriptor;
87
- };
88
- }
89
- const _UiSchemaRepository = class _UiSchemaRepository extends import_database.Repository {
90
- cache;
91
- get uiSchemasTableName() {
92
- return this.tableNameAdapter(this.model.tableName);
93
- }
94
- get uiSchemaTreePathTableName() {
95
- const model = this.database.getCollection("uiSchemaTreePath").model;
96
- return this.tableNameAdapter(model.tableName);
97
- }
98
- static schemaToSingleNodes(schema, carry = [], childOptions = null) {
99
- const node = import_lodash.default.cloneDeep(
100
- import_lodash.default.isString(schema) ? {
101
- "x-uid": schema
102
- } : schema
103
- );
104
- if (!import_lodash.default.get(node, "name")) {
105
- node.name = (0, import_utils.uid)();
106
- }
107
- if (!import_lodash.default.get(node, "x-uid")) {
108
- node["x-uid"] = (0, import_utils.uid)();
109
- }
110
- if (childOptions) {
111
- node.childOptions = childOptions;
112
- }
113
- carry.push(node);
114
- for (const nodeKey of nodeKeys) {
115
- const nodeProperty = import_lodash.default.get(node, nodeKey);
116
- const childNodeChildOptions = {
117
- parentUid: node["x-uid"],
118
- parentPath: [node["x-uid"], ...import_lodash.default.get(childOptions, "parentPath", [])],
119
- type: nodeKey
120
- };
121
- if (nodeKey === "items" && nodeProperty) {
122
- const handleItems = import_lodash.default.isArray(nodeProperty) ? nodeProperty : [nodeProperty];
123
- for (const [i, item] of handleItems.entries()) {
124
- carry = this.schemaToSingleNodes(item, carry, { ...childNodeChildOptions, sort: i + 1 });
125
- }
126
- } else if (import_lodash.default.isPlainObject(nodeProperty)) {
127
- const subNodeNames = import_lodash.default.keys(import_lodash.default.get(node, nodeKey));
128
- delete node[nodeKey];
129
- for (const [i, subNodeName] of subNodeNames.entries()) {
130
- const subSchema = {
131
- name: subNodeName,
132
- ...import_lodash.default.get(nodeProperty, subNodeName)
133
- };
134
- carry = this.schemaToSingleNodes(subSchema, carry, { ...childNodeChildOptions, sort: i + 1 });
135
- }
136
- }
137
- }
138
- return carry;
139
- }
140
- // if you need to handle cache in repo method, so you must set cache first
141
- setCache(cache) {
142
- this.cache = cache;
143
- }
144
- /**
145
- * clear cache with xUid which in uiSchemaTreePath's Path
146
- * @param {string} xUid
147
- * @param {Transaction} transaction
148
- * @returns {Promise<void>}
149
- */
150
- async clearXUidPathCache(xUid, transaction2) {
151
- if (!this.cache || !xUid) {
152
- return;
153
- }
154
- const uiSchemaNodes = await this.database.getRepository("uiSchemaTreePath").find({
155
- filter: {
156
- descendant: xUid
157
- },
158
- transaction: transaction2
159
- });
160
- for (const uiSchemaNode of uiSchemaNodes) {
161
- await this.cache.del(`p_${uiSchemaNode["ancestor"]}`);
162
- await this.cache.del(`s_${uiSchemaNode["ancestor"]}`);
163
- }
164
- }
165
- tableNameAdapter(tableName) {
166
- if (this.database.sequelize.getDialect() === "postgres") {
167
- return `"${this.database.options.schema || "public"}"."${tableName}"`;
168
- }
169
- return tableName;
170
- }
171
- sqlAdapter(sql) {
172
- if (this.database.isMySQLCompatibleDialect()) {
173
- return import_lodash.default.replace(sql, /"/g, "`");
174
- }
175
- return sql;
176
- }
177
- async getProperties(uid2, options = {}) {
178
- if ((options == null ? void 0 : options.readFromCache) && this.cache) {
179
- return this.cache.wrap(`p_${uid2}`, () => {
180
- return this.doGetProperties(uid2, options);
181
- });
182
- }
183
- return this.doGetProperties(uid2, options);
184
- }
185
- async getParentJsonSchema(uid2, options = {}) {
186
- const parentUid = await this.findParentUid(uid2, options.transaction);
187
- if (!parentUid) {
188
- return null;
189
- }
190
- return this.getJsonSchema(parentUid, options);
191
- }
192
- async getParentProperty(uid2, options = {}) {
193
- const parentUid = await this.findParentUid(uid2, options.transaction);
194
- if (!parentUid) {
195
- return null;
196
- }
197
- return this.getJsonSchema(parentUid, options);
198
- }
199
- async getJsonSchema(uid2, options) {
200
- if ((options == null ? void 0 : options.readFromCache) && this.cache) {
201
- return this.cache.wrap(`s_${uid2}`, () => {
202
- return this.doGetJsonSchema(uid2, options);
203
- });
204
- }
205
- return this.doGetJsonSchema(uid2, options);
206
- }
207
- nodesToSchema(nodes, rootUid) {
208
- const nodeAttributeSanitize = (node) => {
209
- const schema = {
210
- ...this.ignoreSchemaProperties(import_lodash.default.isPlainObject(node.schema) ? node.schema : JSON.parse(node.schema)),
211
- ...import_lodash.default.pick(node, [...nodeKeys, "name"]),
212
- ["x-uid"]: node["x-uid"],
213
- ["x-async"]: !!node.async
214
- };
215
- if (import_lodash.default.isNumber(node.sort)) {
216
- schema["x-index"] = node.sort;
217
- }
218
- return schema;
219
- };
220
- const buildTree = (rootNode) => {
221
- const children = nodes.filter((node) => node.parent == rootNode["x-uid"]);
222
- if (children.length > 0) {
223
- const childrenGroupByType = import_lodash.default.groupBy(children, "type");
224
- for (const childType of Object.keys(childrenGroupByType)) {
225
- const properties = childrenGroupByType[childType].map((child) => buildTree(child)).sort((a, b) => a["x-index"] - b["x-index"]);
226
- rootNode[childType] = childType == "items" ? properties.length == 1 ? properties[0] : properties : properties.reduce((carry, item) => {
227
- carry[item.name] = item;
228
- delete item["name"];
229
- return carry;
230
- }, {});
231
- }
232
- }
233
- return nodeAttributeSanitize(rootNode);
234
- };
235
- return buildTree(nodes.find((node) => node["x-uid"] == rootUid));
236
- }
237
- async clearAncestor(uid2, options) {
238
- await this.clearXUidPathCache(uid2, options == null ? void 0 : options.transaction);
239
- const db = this.database;
240
- const treeTable = this.uiSchemaTreePathTableName;
241
- await db.sequelize.query(
242
- `DELETE
243
- FROM ${treeTable}
244
- WHERE descendant IN
245
- (SELECT descendant FROM (SELECT descendant FROM ${treeTable} WHERE ancestor = :uid) as descendantTable)
246
- AND ancestor IN (SELECT ancestor
247
- FROM (SELECT ancestor FROM ${treeTable} WHERE descendant = :uid AND ancestor != descendant) as ancestorTable)
248
- `,
249
- {
250
- type: "DELETE",
251
- replacements: {
252
- uid: uid2
253
- },
254
- transaction: options.transaction
255
- }
256
- );
257
- }
258
- async patch(newSchema, options) {
259
- const { transaction: transaction2 } = options;
260
- const rootUid = newSchema["x-uid"];
261
- await this.clearXUidPathCache(rootUid, transaction2);
262
- if (!newSchema["properties"]) {
263
- const s = await this.model.findByPk(rootUid, { transaction: transaction2 });
264
- s.set("schema", { ...s.toJSON(), ...newSchema });
265
- await s.save({ transaction: transaction2, hooks: false });
266
- if (newSchema["x-server-hooks"]) {
267
- await this.database.emitAsync(`${this.collection.name}.afterSave`, s, options);
268
- }
269
- return;
270
- }
271
- const oldTree = await this.getJsonSchema(rootUid, { transaction: transaction2 });
272
- const traverSchemaTree = async (schema, path = []) => {
273
- const node = schema;
274
- const oldNode = path.length == 0 ? oldTree : import_lodash.default.get(oldTree, path);
275
- const oldNodeUid = oldNode["x-uid"];
276
- await this.updateNode(oldNodeUid, node, transaction2);
277
- const properties = node.properties;
278
- if (import_lodash.default.isPlainObject(properties)) {
279
- for (const name of Object.keys(properties)) {
280
- await traverSchemaTree(properties[name], [...path, "properties", name]);
281
- }
282
- }
283
- };
284
- await traverSchemaTree(newSchema);
285
- }
286
- async batchPatch(schemas, options) {
287
- const { transaction: transaction2 } = options;
288
- for (const schema of schemas) {
289
- await this.patch(schema, { ...options, transaction: transaction2 });
290
- }
291
- }
292
- async removeEmptyParents(options) {
293
- const { transaction: transaction2, uid: uid2, breakRemoveOn } = options;
294
- await this.clearXUidPathCache(uid2, transaction2);
295
- const removeParent = async (nodeUid) => {
296
- const parent = await this.isSingleChild(nodeUid, transaction2);
297
- if (parent && !this.breakOnMatched(parent, breakRemoveOn)) {
298
- await removeParent(parent.get("x-uid"));
299
- } else {
300
- await this.remove(nodeUid, { transaction: transaction2 });
301
- }
302
- };
303
- await removeParent(uid2);
304
- }
305
- async recursivelyRemoveIfNoChildren(options) {
306
- const { uid: uid2, transaction: transaction2, breakRemoveOn } = options;
307
- await this.clearXUidPathCache(uid2, transaction2);
308
- const removeLeafNode = async (nodeUid) => {
309
- const isLeafNode = await this.isLeafNode(nodeUid, transaction2);
310
- if (isLeafNode) {
311
- const { parentUid, schema } = await this.findNodeSchemaWithParent(nodeUid, transaction2);
312
- if (this.breakOnMatched(schema, breakRemoveOn)) {
313
- return;
314
- } else {
315
- await this.remove(nodeUid, {
316
- transaction: transaction2
317
- });
318
- await removeLeafNode(parentUid);
319
- }
320
- }
321
- };
322
- await removeLeafNode(uid2);
323
- }
324
- async remove(uid2, options) {
325
- const { transaction: transaction2 } = options;
326
- await this.clearXUidPathCache(uid2, transaction2);
327
- if (options == null ? void 0 : options.removeParentsIfNoChildren) {
328
- await this.removeEmptyParents({ transaction: transaction2, uid: uid2, breakRemoveOn: options.breakRemoveOn });
329
- return;
330
- }
331
- await this.database.sequelize.query(
332
- this.sqlAdapter(`DELETE FROM ${this.uiSchemasTableName} WHERE "x-uid" IN (
333
- SELECT descendant FROM ${this.uiSchemaTreePathTableName} WHERE ancestor = :uid
334
- )`),
335
- {
336
- replacements: {
337
- uid: uid2
338
- },
339
- transaction: transaction2
340
- }
341
- );
342
- await this.database.sequelize.query(
343
- ` DELETE FROM ${this.uiSchemaTreePathTableName}
344
- WHERE descendant IN (
345
- select descendant FROM
346
- (SELECT descendant
347
- FROM ${this.uiSchemaTreePathTableName}
348
- WHERE ancestor = :uid)as descendantTable) `,
349
- {
350
- replacements: {
351
- uid: uid2
352
- },
353
- transaction: transaction2
354
- }
355
- );
356
- }
357
- async insertAdjacent(position, target, schema, options) {
358
- const { transaction: transaction2 } = options;
359
- await this.clearXUidPathCache(schema["x-uid"], transaction2);
360
- if (options.wrap) {
361
- const wrapSchemaNodes = await this.insertNewSchema(options.wrap, {
362
- transaction: transaction2,
363
- returnNode: true
364
- });
365
- const lastWrapNode = wrapSchemaNodes[wrapSchemaNodes.length - 1];
366
- await this.insertAdjacent("afterBegin", lastWrapNode["x-uid"], schema, import_lodash.default.omit(options, "wrap"));
367
- schema = wrapSchemaNodes[0]["x-uid"];
368
- options.removeParentsIfNoChildren = false;
369
- } else {
370
- const schemaExists = await this.schemaExists(schema, { transaction: transaction2 });
371
- if (schemaExists) {
372
- schema = import_lodash.default.isString(schema) ? schema : schema["x-uid"];
373
- } else {
374
- const insertedSchema = await this.insertNewSchema(schema, {
375
- transaction: transaction2,
376
- returnNode: true
377
- });
378
- schema = insertedSchema[0]["x-uid"];
379
- }
380
- }
381
- const result = await this[`insert${import_lodash.default.upperFirst(position)}`](target, schema, options);
382
- await this.clearXUidPathCache(result["x-uid"], transaction2);
383
- return result;
384
- }
385
- async duplicate(uid2, options) {
386
- const s = await this.getJsonSchema(uid2, { ...options, includeAsyncNode: true });
387
- if (!(s == null ? void 0 : s["x-uid"])) {
388
- return null;
389
- }
390
- this.regenerateUid(s);
391
- return this.insert(s, options);
392
- }
393
- async insert(schema, options) {
394
- const nodes = _UiSchemaRepository.schemaToSingleNodes(schema);
395
- const insertedNodes = await this.insertNodes(nodes, options);
396
- const result = await this.getJsonSchema(insertedNodes[0].get("x-uid"), {
397
- transaction: options == null ? void 0 : options.transaction
398
- });
399
- await this.clearXUidPathCache(result["x-uid"], options == null ? void 0 : options.transaction);
400
- return result;
401
- }
402
- async insertNewSchema(schema, options) {
403
- const { transaction: transaction2 } = options;
404
- const nodes = _UiSchemaRepository.schemaToSingleNodes(schema);
405
- await this.database.sequelize.query(
406
- this.sqlAdapter(
407
- `INSERT INTO ${this.uiSchemasTableName} ("x-uid", "name", "schema") VALUES ${nodes.map((n) => "(?)").join(",")};`
408
- ),
409
- {
410
- replacements: import_lodash.default.cloneDeep(nodes).map((node) => {
411
- const { uid: uid2, name } = this.prepareSingleNodeForInsert(node);
412
- return [uid2, name, JSON.stringify(node)];
413
- }),
414
- type: "insert",
415
- transaction: transaction2
416
- }
417
- );
418
- const treePathData = import_lodash.default.cloneDeep(nodes).reduce((carry, item) => {
419
- const { uid: uid2, childOptions, async } = this.prepareSingleNodeForInsert(item);
420
- return [
421
- ...carry,
422
- // self reference
423
- [uid2, uid2, 0, (childOptions == null ? void 0 : childOptions.type) || null, async, null],
424
- // parent references
425
- ...import_lodash.default.get(childOptions, "parentPath", []).map((parentUid, index) => {
426
- return [parentUid, uid2, index + 1, null, null, childOptions.sort];
427
- })
428
- ];
429
- }, []);
430
- await this.database.sequelize.query(
431
- this.sqlAdapter(
432
- `INSERT INTO ${this.uiSchemaTreePathTableName} (ancestor, descendant, depth, type, async, sort) VALUES ${treePathData.map((item) => "(?)").join(",")}`
433
- ),
434
- {
435
- replacements: treePathData,
436
- type: "insert",
437
- transaction: transaction2
438
- }
439
- );
440
- const rootNode = nodes[0];
441
- if (rootNode["x-server-hooks"]) {
442
- const rootModel = await this.findOne({ filter: { "x-uid": rootNode["x-uid"] }, transaction: transaction2 });
443
- await this.database.emitAsync(`${this.collection.name}.afterCreateWithAssociations`, rootModel, options);
444
- await this.database.emitAsync(`${this.collection.name}.afterSave`, rootModel, options);
445
- }
446
- if (options == null ? void 0 : options.returnNode) {
447
- return nodes;
448
- }
449
- const result = await this.getJsonSchema(nodes[0]["x-uid"], {
450
- transaction: transaction2
451
- });
452
- await this.clearXUidPathCache(result["x-uid"], transaction2);
453
- return result;
454
- }
455
- async insertSingleNode(schema, options) {
456
- const { transaction: transaction2 } = options;
457
- const db = this.database;
458
- const { uid: uid2, name, async, childOptions } = this.prepareSingleNodeForInsert(schema);
459
- let savedNode;
460
- const existsNode = await this.findOne({
461
- filter: {
462
- "x-uid": uid2
463
- },
464
- transaction: transaction2
465
- });
466
- const treeTable = this.uiSchemaTreePathTableName;
467
- if (existsNode) {
468
- savedNode = existsNode;
469
- } else {
470
- savedNode = await this.insertSchemaRecord(name, uid2, schema, transaction2);
471
- }
472
- if (childOptions) {
473
- const oldParentUid = await this.findParentUid(uid2, transaction2);
474
- const parentUid = childOptions.parentUid;
475
- const childrenCount = await this.childrenCount(uid2, transaction2);
476
- const isTree = childrenCount > 0;
477
- if (isTree) {
478
- await this.clearAncestor(uid2, { transaction: transaction2 });
479
- await db.sequelize.query(
480
- `INSERT INTO ${treeTable} (ancestor, descendant, depth)
481
- SELECT supertree.ancestor, subtree.descendant, supertree.depth + subtree.depth + 1
482
- FROM ${treeTable} AS supertree
483
- CROSS JOIN ${treeTable} AS subtree
484
- WHERE supertree.descendant = :parentUid
485
- AND subtree.ancestor = :uid;`,
486
- {
487
- type: "INSERT",
488
- replacements: {
489
- uid: uid2,
490
- parentUid
491
- },
492
- transaction: transaction2
493
- }
494
- );
495
- }
496
- await db.sequelize.query(
497
- `UPDATE ${treeTable} SET type = :type WHERE depth = 0 AND ancestor = :uid AND descendant = :uid`,
498
- {
499
- type: "update",
500
- transaction: transaction2,
501
- replacements: {
502
- type: childOptions.type,
503
- uid: uid2
504
- }
505
- }
506
- );
507
- if (!isTree) {
508
- if (existsNode) {
509
- await db.sequelize.query(`DELETE FROM ${treeTable} WHERE descendant = :uid AND ancestor != descendant`, {
510
- type: "DELETE",
511
- replacements: {
512
- uid: uid2
513
- },
514
- transaction: transaction2
515
- });
516
- }
517
- await db.sequelize.query(
518
- `INSERT INTO ${treeTable} (ancestor, descendant, depth)
519
- SELECT t.ancestor, :modelKey, depth + 1 FROM ${treeTable} AS t WHERE t.descendant = :modelParentKey `,
520
- {
521
- type: "INSERT",
522
- transaction: transaction2,
523
- replacements: {
524
- modelKey: savedNode.get("x-uid"),
525
- modelParentKey: parentUid
526
- }
527
- }
528
- );
529
- }
530
- if (!existsNode) {
531
- await db.sequelize.query(
532
- `INSERT INTO ${treeTable}(ancestor, descendant, depth, type, async) VALUES (:modelKey, :modelKey, 0, :type, :async )`,
533
- {
534
- type: "INSERT",
535
- replacements: {
536
- modelKey: savedNode.get("x-uid"),
537
- type: childOptions.type,
538
- async
539
- },
540
- transaction: transaction2
541
- }
542
- );
543
- }
544
- const nodePosition = childOptions.position || "last";
545
- let sort;
546
- if (nodePosition === "first") {
547
- sort = 1;
548
- let updateSql2 = `UPDATE ${treeTable} as TreeTable
549
- SET sort = TreeTable.sort + 1
550
- FROM ${treeTable} as NodeInfo
551
- WHERE NodeInfo.descendant = TreeTable.descendant and NodeInfo.depth = 0
552
- AND TreeTable.depth = 1 AND TreeTable.ancestor = :ancestor and NodeInfo.type = :type`;
553
- if (this.database.isMySQLCompatibleDialect()) {
554
- updateSql2 = `UPDATE ${treeTable} as TreeTable
555
- JOIN ${treeTable} as NodeInfo ON (NodeInfo.descendant = TreeTable.descendant and NodeInfo.depth = 0)
556
- SET TreeTable.sort = TreeTable.sort + 1
557
- WHERE TreeTable.depth = 1 AND TreeTable.ancestor = :ancestor and NodeInfo.type = :type`;
558
- }
559
- await db.sequelize.query(updateSql2, {
560
- replacements: {
561
- ancestor: childOptions.parentUid,
562
- type: childOptions.type
563
- },
564
- transaction: transaction2
565
- });
566
- }
567
- if (nodePosition === "last") {
568
- const maxSort = await db.sequelize.query(
569
- `SELECT ${this.database.sequelize.getDialect() === "postgres" ? "coalesce" : "ifnull"}(MAX(TreeTable.sort), 0) as maxsort FROM ${treeTable} as TreeTable
570
- LEFT JOIN ${treeTable} as NodeInfo
571
- ON NodeInfo.descendant = TreeTable.descendant and NodeInfo.depth = 0
572
- WHERE TreeTable.depth = 1 AND TreeTable.ancestor = :ancestor and NodeInfo.type = :type`,
573
- {
574
- type: "SELECT",
575
- replacements: {
576
- ancestor: childOptions.parentUid,
577
- type: childOptions.type
578
- },
579
- transaction: transaction2
580
- }
581
- );
582
- sort = parseInt(maxSort[0]["maxsort"]) + 1;
583
- }
584
- if (import_lodash.default.isPlainObject(nodePosition)) {
585
- const targetPosition = nodePosition;
586
- const target = targetPosition.target;
587
- const targetSort = await db.sequelize.query(
588
- `SELECT TreeTable.sort as sort FROM ${treeTable} as TreeTable
589
- LEFT JOIN ${treeTable} as NodeInfo
590
- ON NodeInfo.descendant = TreeTable.descendant and NodeInfo.depth = 0 WHERE TreeTable.depth = 1 AND TreeTable.ancestor = :ancestor AND TreeTable.descendant = :descendant and NodeInfo.type = :type`,
591
- {
592
- type: "SELECT",
593
- replacements: {
594
- ancestor: childOptions.parentUid,
595
- descendant: target,
596
- type: childOptions.type
597
- },
598
- transaction: transaction2
599
- }
600
- );
601
- sort = targetSort[0].sort;
602
- if (targetPosition.type == "after") {
603
- sort += 1;
604
- }
605
- let updateSql2 = `UPDATE ${treeTable} as TreeTable
606
- SET sort = TreeTable.sort + 1
607
- FROM ${treeTable} as NodeInfo
608
- WHERE NodeInfo.descendant = TreeTable.descendant
609
- and NodeInfo.depth = 0
610
- AND TreeTable.depth = 1
611
- AND TreeTable.ancestor = :ancestor
612
- and TreeTable.sort >= :sort
613
- and NodeInfo.type = :type`;
614
- if (this.database.isMySQLCompatibleDialect()) {
615
- updateSql2 = `UPDATE ${treeTable} as TreeTable
616
- JOIN ${treeTable} as NodeInfo ON (NodeInfo.descendant = TreeTable.descendant and NodeInfo.depth = 0)
617
- SET TreeTable.sort = TreeTable.sort + 1
618
- WHERE TreeTable.depth = 1 AND TreeTable.ancestor = :ancestor and TreeTable.sort >= :sort and NodeInfo.type = :type`;
619
- }
620
- await db.sequelize.query(updateSql2, {
621
- replacements: {
622
- ancestor: childOptions.parentUid,
623
- sort,
624
- type: childOptions.type
625
- },
626
- transaction: transaction2
627
- });
628
- }
629
- const updateSql = `UPDATE ${treeTable} SET sort = :sort WHERE depth = 1 AND ancestor = :ancestor AND descendant = :descendant`;
630
- await db.sequelize.query(updateSql, {
631
- type: "UPDATE",
632
- replacements: {
633
- ancestor: childOptions.parentUid,
634
- sort,
635
- descendant: uid2
636
- },
637
- transaction: transaction2
638
- });
639
- if (oldParentUid !== null && oldParentUid !== parentUid) {
640
- await this.database.emitAsync("uiSchemaMove", savedNode, {
641
- transaction: transaction2,
642
- oldParentUid,
643
- parentUid
644
- });
645
- if (options.removeParentsIfNoChildren) {
646
- await this.recursivelyRemoveIfNoChildren({
647
- transaction: transaction2,
648
- uid: oldParentUid,
649
- breakRemoveOn: options.breakRemoveOn
650
- });
651
- }
652
- }
653
- } else {
654
- await db.sequelize.query(
655
- `INSERT INTO ${treeTable}(ancestor, descendant, depth, async) VALUES (:modelKey, :modelKey, 0, :async )`,
656
- {
657
- type: "INSERT",
658
- replacements: {
659
- modelKey: savedNode.get("x-uid"),
660
- async
661
- },
662
- transaction: transaction2
663
- }
664
- );
665
- }
666
- await this.clearXUidPathCache(uid2, transaction2);
667
- return savedNode;
668
- }
669
- async updateNode(uid2, schema, transaction2) {
670
- const nodeModel = await this.findOne({
671
- filter: {
672
- "x-uid": uid2
673
- }
674
- });
675
- await nodeModel.update(
676
- {
677
- schema: {
678
- ...nodeModel.get("schema"),
679
- ...import_lodash.default.omit(schema, ["x-async", "name", "x-uid", "properties"])
680
- }
681
- },
682
- {
683
- hooks: false,
684
- transaction: transaction2
685
- }
686
- );
687
- if (schema["x-server-hooks"]) {
688
- await this.database.emitAsync(`${this.collection.name}.afterSave`, nodeModel, { transaction: transaction2 });
689
- }
690
- }
691
- async childrenCount(uid2, transaction2) {
692
- const db = this.database;
693
- const countResult = await db.sequelize.query(
694
- `SELECT COUNT(*) as count FROM ${this.uiSchemaTreePathTableName} where ancestor = :ancestor and depth = 1`,
695
- {
696
- replacements: {
697
- ancestor: uid2
698
- },
699
- type: "SELECT",
700
- transaction: transaction2
701
- }
702
- );
703
- return parseInt(countResult[0]["count"]);
704
- }
705
- async isLeafNode(uid2, transaction2) {
706
- const childrenCount = await this.childrenCount(uid2, transaction2);
707
- return childrenCount === 0;
708
- }
709
- async findParentUid(uid2, transaction2) {
710
- const parent = await this.database.getRepository("uiSchemaTreePath").findOne({
711
- filter: {
712
- descendant: uid2,
713
- depth: 1
714
- },
715
- transaction: transaction2
716
- });
717
- return parent ? parent.get("ancestor") : null;
718
- }
719
- async findNodeSchemaWithParent(uid2, transaction2) {
720
- const schema = await this.database.getRepository("uiSchemas").findOne({
721
- filter: {
722
- "x-uid": uid2
723
- },
724
- transaction: transaction2
725
- });
726
- return {
727
- parentUid: await this.findParentUid(uid2, transaction2),
728
- schema
729
- };
730
- }
731
- async isSingleChild(uid2, transaction2) {
732
- const db = this.database;
733
- const parent = await this.findParentUid(uid2, transaction2);
734
- if (!parent) {
735
- return null;
736
- }
737
- const parentChildrenCount = await this.childrenCount(parent, transaction2);
738
- if (parentChildrenCount == 1) {
739
- const schema = await db.getRepository("uiSchemas").findOne({
740
- filter: {
741
- "x-uid": parent
742
- },
743
- transaction: transaction2
744
- });
745
- return schema;
746
- }
747
- return null;
748
- }
749
- async insertBeside(targetUid, schema, side, options) {
750
- const { transaction: transaction2 } = options;
751
- const targetParent = await this.findParentUid(targetUid, transaction2);
752
- const db = this.database;
753
- const treeTable = this.uiSchemaTreePathTableName;
754
- const typeQuery = await db.sequelize.query(`SELECT type from ${treeTable} WHERE ancestor = :uid AND depth = 0;`, {
755
- type: "SELECT",
756
- replacements: {
757
- uid: targetUid
758
- },
759
- transaction: transaction2
760
- });
761
- const nodes = _UiSchemaRepository.schemaToSingleNodes(schema);
762
- const rootNode = nodes[0];
763
- rootNode.childOptions = {
764
- parentUid: targetParent,
765
- type: typeQuery[0]["type"],
766
- position: {
767
- type: side,
768
- target: targetUid
769
- }
770
- };
771
- const insertedNodes = await this.insertNodes(nodes, options);
772
- return await this.getJsonSchema(insertedNodes[0].get("x-uid"), {
773
- transaction: transaction2
774
- });
775
- }
776
- async insertInner(targetUid, schema, position, options) {
777
- const { transaction: transaction2 } = options;
778
- const nodes = _UiSchemaRepository.schemaToSingleNodes(schema);
779
- const rootNode = nodes[0];
780
- rootNode.childOptions = {
781
- parentUid: targetUid,
782
- type: import_lodash.default.get(schema, "x-node-type", "properties"),
783
- position
784
- };
785
- const insertedNodes = await this.insertNodes(nodes, options);
786
- return await this.getJsonSchema(insertedNodes[0].get("x-uid"), {
787
- transaction: transaction2
788
- });
789
- }
790
- async insertAfterBegin(targetUid, schema, options) {
791
- return await this.insertInner(targetUid, schema, "first", options);
792
- }
793
- async insertBeforeEnd(targetUid, schema, options) {
794
- return await this.insertInner(targetUid, schema, "last", options);
795
- }
796
- async insertBeforeBegin(targetUid, schema, options) {
797
- return await this.insertBeside(targetUid, schema, "before", options);
798
- }
799
- async insertAfterEnd(targetUid, schema, options) {
800
- return await this.insertBeside(targetUid, schema, "after", options);
801
- }
802
- async insertNodes(nodes, options) {
803
- const { transaction: transaction2 } = options;
804
- const insertedNodes = [];
805
- for (const node of nodes) {
806
- insertedNodes.push(
807
- await this.insertSingleNode(node, {
808
- ...options,
809
- transaction: transaction2
810
- })
811
- );
812
- }
813
- return insertedNodes;
814
- }
815
- async doGetProperties(uid2, options = {}) {
816
- const { transaction: transaction2 } = options;
817
- const db = this.database;
818
- const rawSql = `
819
- SELECT "SchemaTable"."x-uid" as "x-uid", "SchemaTable"."name" as "name", "SchemaTable"."schema" as "schema",
820
- TreePath.depth as depth,
821
- NodeInfo.type as type, NodeInfo.async as async, ParentPath.ancestor as parent, ParentPath.sort as sort
822
- FROM ${this.uiSchemaTreePathTableName} as TreePath
823
- LEFT JOIN ${this.uiSchemasTableName} as "SchemaTable" ON "SchemaTable"."x-uid" = TreePath.descendant
824
- LEFT JOIN ${this.uiSchemaTreePathTableName} as NodeInfo ON NodeInfo.descendant = "SchemaTable"."x-uid" and NodeInfo.descendant = NodeInfo.ancestor and NodeInfo.depth = 0
825
- LEFT JOIN ${this.uiSchemaTreePathTableName} as ParentPath ON (ParentPath.descendant = "SchemaTable"."x-uid" AND ParentPath.depth = 1)
826
- WHERE TreePath.ancestor = :ancestor AND (NodeInfo.async = false or TreePath.depth <= 1)`;
827
- const nodes = await db.sequelize.query(this.sqlAdapter(rawSql), {
828
- replacements: {
829
- ancestor: uid2
830
- },
831
- transaction: transaction2
832
- });
833
- if (nodes[0].length == 0) {
834
- return {};
835
- }
836
- const schema = this.nodesToSchema(nodes[0], uid2);
837
- return import_lodash.default.pick(schema, ["type", "properties"]);
838
- }
839
- async doGetJsonSchema(uid2, options) {
840
- const db = this.database;
841
- const treeTable = this.uiSchemaTreePathTableName;
842
- const rawSql = `
843
- SELECT "SchemaTable"."x-uid" as "x-uid", "SchemaTable"."name" as name, "SchemaTable"."schema" as "schema" ,
844
- TreePath.depth as depth,
845
- NodeInfo.type as type, NodeInfo.async as async, ParentPath.ancestor as parent, ParentPath.sort as sort
846
- FROM ${treeTable} as TreePath
847
- LEFT JOIN ${this.uiSchemasTableName} as "SchemaTable" ON "SchemaTable"."x-uid" = TreePath.descendant
848
- LEFT JOIN ${treeTable} as NodeInfo ON NodeInfo.descendant = "SchemaTable"."x-uid" and NodeInfo.descendant = NodeInfo.ancestor and NodeInfo.depth = 0
849
- LEFT JOIN ${treeTable} as ParentPath ON (ParentPath.descendant = "SchemaTable"."x-uid" AND ParentPath.depth = 1)
850
- WHERE TreePath.ancestor = :ancestor ${(options == null ? void 0 : options.includeAsyncNode) ? "" : "AND (NodeInfo.async != true or TreePath.depth = 0)"}
851
- `;
852
- const nodes = await db.sequelize.query(this.sqlAdapter(rawSql), {
853
- replacements: {
854
- ancestor: uid2
855
- },
856
- transaction: options == null ? void 0 : options.transaction
857
- });
858
- if (nodes[0].length == 0) {
859
- return {};
860
- }
861
- return this.nodesToSchema(nodes[0], uid2);
862
- }
863
- ignoreSchemaProperties(schemaProperties) {
864
- return import_lodash.default.omit(schemaProperties, nodeKeys);
865
- }
866
- breakOnMatched(schemaInstance, breakRemoveOn) {
867
- if (!breakRemoveOn) {
868
- return false;
869
- }
870
- for (const key of Object.keys(breakRemoveOn)) {
871
- const instanceValue = schemaInstance.get(key);
872
- const breakRemoveOnValue = breakRemoveOn[key];
873
- if (instanceValue !== breakRemoveOnValue) {
874
- return false;
875
- }
876
- }
877
- return true;
878
- }
879
- async schemaExists(schema, options) {
880
- if (import_lodash.default.isObject(schema) && !schema["x-uid"]) {
881
- return false;
882
- }
883
- const { transaction: transaction2 } = options;
884
- const result = await this.database.sequelize.query(
885
- this.sqlAdapter(`select "x-uid" from ${this.uiSchemasTableName} where "x-uid" = :uid`),
886
- {
887
- type: "SELECT",
888
- replacements: {
889
- uid: import_lodash.default.isString(schema) ? schema : schema["x-uid"]
890
- },
891
- transaction: transaction2
892
- }
893
- );
894
- return result.length > 0;
895
- }
896
- regenerateUid(s) {
897
- s["x-uid"] = (0, import_utils.uid)();
898
- Object.keys(s.properties || {}).forEach((key) => {
899
- this.regenerateUid(s.properties[key]);
900
- });
901
- }
902
- async insertSchemaRecord(name, uid2, schema, transaction2) {
903
- const serverHooks = schema["x-server-hooks"] || [];
904
- const node = await this.create({
905
- values: {
906
- name,
907
- ["x-uid"]: uid2,
908
- schema,
909
- serverHooks
910
- },
911
- transaction: transaction2,
912
- context: {
913
- disableInsertHook: true
914
- }
915
- });
916
- return node;
917
- }
918
- prepareSingleNodeForInsert(schema) {
919
- const uid2 = schema["x-uid"];
920
- const name = schema["name"];
921
- const async = import_lodash.default.get(schema, "x-async", false);
922
- const childOptions = schema["childOptions"];
923
- delete schema["x-uid"];
924
- delete schema["x-async"];
925
- delete schema["name"];
926
- delete schema["childOptions"];
927
- return { uid: uid2, name, async, childOptions };
928
- }
929
- };
930
- __decorateClass([
931
- transaction()
932
- ], _UiSchemaRepository.prototype, "clearAncestor", 1);
933
- __decorateClass([
934
- transaction()
935
- ], _UiSchemaRepository.prototype, "patch", 1);
936
- __decorateClass([
937
- transaction()
938
- ], _UiSchemaRepository.prototype, "batchPatch", 1);
939
- __decorateClass([
940
- transaction()
941
- ], _UiSchemaRepository.prototype, "remove", 1);
942
- __decorateClass([
943
- transaction()
944
- ], _UiSchemaRepository.prototype, "insertAdjacent", 1);
945
- __decorateClass([
946
- transaction()
947
- ], _UiSchemaRepository.prototype, "duplicate", 1);
948
- __decorateClass([
949
- transaction()
950
- ], _UiSchemaRepository.prototype, "insert", 1);
951
- __decorateClass([
952
- transaction()
953
- ], _UiSchemaRepository.prototype, "insertNewSchema", 1);
954
- __decorateClass([
955
- transaction()
956
- ], _UiSchemaRepository.prototype, "insertBeside", 1);
957
- __decorateClass([
958
- transaction()
959
- ], _UiSchemaRepository.prototype, "insertInner", 1);
960
- __decorateClass([
961
- transaction()
962
- ], _UiSchemaRepository.prototype, "insertAfterBegin", 1);
963
- __decorateClass([
964
- transaction()
965
- ], _UiSchemaRepository.prototype, "insertBeforeEnd", 1);
966
- __decorateClass([
967
- transaction()
968
- ], _UiSchemaRepository.prototype, "insertBeforeBegin", 1);
969
- __decorateClass([
970
- transaction()
971
- ], _UiSchemaRepository.prototype, "insertAfterEnd", 1);
972
- __decorateClass([
973
- transaction()
974
- ], _UiSchemaRepository.prototype, "insertNodes", 1);
975
- let UiSchemaRepository = _UiSchemaRepository;
976
- var repository_default = UiSchemaRepository;
977
- // Annotate the CommonJS export names for ESM import in node:
978
- 0 && (module.exports = {
979
- UiSchemaRepository
980
- });