@nocobase/plugin-ui-schema-storage 0.15.0-alpha.3 → 0.15.0-alpha.5

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.
@@ -1,11 +1,11 @@
1
1
  module.exports = {
2
- "@nocobase/client": "0.15.0-alpha.3",
3
- "@nocobase/database": "0.15.0-alpha.3",
4
- "@nocobase/cache": "0.15.0-alpha.3",
5
- "@nocobase/utils": "0.15.0-alpha.3",
2
+ "@nocobase/client": "0.15.0-alpha.5",
3
+ "@nocobase/database": "0.15.0-alpha.5",
4
+ "@nocobase/cache": "0.15.0-alpha.5",
5
+ "@nocobase/utils": "0.15.0-alpha.5",
6
6
  "lodash": "4.17.21",
7
- "@nocobase/server": "0.15.0-alpha.3",
8
- "@nocobase/actions": "0.15.0-alpha.3",
9
- "@nocobase/resourcer": "0.15.0-alpha.3",
7
+ "@nocobase/server": "0.15.0-alpha.5",
8
+ "@nocobase/actions": "0.15.0-alpha.5",
9
+ "@nocobase/resourcer": "0.15.0-alpha.5",
10
10
  "@formily/json-schema": "2.2.27"
11
11
  };
@@ -22,6 +22,9 @@ interface InsertAdjacentOptions extends removeParentOptions {
22
22
  }
23
23
  export declare class UiSchemaRepository extends Repository {
24
24
  cache: Cache;
25
+ get uiSchemasTableName(): any;
26
+ get uiSchemaTreePathTableName(): any;
27
+ static schemaToSingleNodes(schema: any, carry?: SchemaNode[], childOptions?: ChildOptions): SchemaNode[];
25
28
  setCache(cache: Cache): void;
26
29
  /**
27
30
  * clear cache with xUid which in uiSchemaTreePath's Path
@@ -31,18 +34,12 @@ export declare class UiSchemaRepository extends Repository {
31
34
  */
32
35
  clearXUidPathCache(xUid: string, transaction: Transaction): Promise<void>;
33
36
  tableNameAdapter(tableName: any): any;
34
- get uiSchemasTableName(): any;
35
- get uiSchemaTreePathTableName(): any;
36
37
  sqlAdapter(sql: string): string;
37
- static schemaToSingleNodes(schema: any, carry?: SchemaNode[], childOptions?: ChildOptions): SchemaNode[];
38
38
  getProperties(uid: string, options?: GetPropertiesOptions): Promise<Partial<{
39
39
  "x-uid": any;
40
40
  "x-async": boolean;
41
41
  }>>;
42
- private doGetProperties;
43
- private doGetJsonSchema;
44
42
  getJsonSchema(uid: string, options?: GetJsonSchemaOptions): Promise<any>;
45
- private ignoreSchemaProperties;
46
43
  nodesToSchema(nodes: any, rootUid: any): {
47
44
  "x-uid": any;
48
45
  "x-async": boolean;
@@ -50,42 +47,45 @@ export declare class UiSchemaRepository extends Repository {
50
47
  clearAncestor(uid: string, options?: Transactionable): Promise<void>;
51
48
  patch(newSchema: any, options?: any): Promise<void>;
52
49
  batchPatch(schemas: any[], options?: any): Promise<void>;
53
- protected updateNode(uid: string, schema: any, transaction?: Transaction): Promise<void>;
54
- protected childrenCount(uid: any, transaction: any): Promise<number>;
55
- protected isLeafNode(uid: any, transaction: any): Promise<boolean>;
56
- protected findParentUid(uid: any, transaction?: any): Promise<string>;
57
- protected findNodeSchemaWithParent(uid: any, transaction: any): Promise<{
58
- parentUid: string;
59
- schema: any;
60
- }>;
61
- protected isSingleChild(uid: any, transaction: any): Promise<any>;
62
50
  removeEmptyParents(options: Transactionable & {
63
51
  uid: string;
64
52
  breakRemoveOn?: BreakRemoveOnType;
65
53
  }): Promise<void>;
66
- private breakOnMatched;
67
54
  recursivelyRemoveIfNoChildren(options: Transactionable & {
68
55
  uid: string;
69
56
  breakRemoveOn?: BreakRemoveOnType;
70
57
  }): Promise<void>;
71
58
  remove(uid: string, options?: Transactionable & removeParentOptions): Promise<void>;
59
+ insertAdjacent(position: 'beforeBegin' | 'afterBegin' | 'beforeEnd' | 'afterEnd', target: string, schema: any, options?: InsertAdjacentOptions): Promise<any>;
60
+ duplicate(uid: string, options?: Transactionable): Promise<any>;
61
+ insert(schema: any, options?: Transactionable): Promise<any>;
62
+ insertNewSchema(schema: any, options?: Transactionable & {
63
+ returnNode?: boolean;
64
+ }): Promise<any>;
65
+ insertSingleNode(schema: SchemaNode, options: Transactionable & removeParentOptions): Promise<any>;
66
+ protected updateNode(uid: string, schema: any, transaction?: Transaction): Promise<void>;
67
+ protected childrenCount(uid: any, transaction: any): Promise<number>;
68
+ protected isLeafNode(uid: any, transaction: any): Promise<boolean>;
69
+ protected findParentUid(uid: any, transaction?: any): Promise<string>;
70
+ protected findNodeSchemaWithParent(uid: any, transaction: any): Promise<{
71
+ parentUid: string;
72
+ schema: any;
73
+ }>;
74
+ protected isSingleChild(uid: any, transaction: any): Promise<any>;
72
75
  protected insertBeside(targetUid: string, schema: any, side: 'before' | 'after', options?: InsertAdjacentOptions): Promise<any>;
73
76
  protected insertInner(targetUid: string, schema: any, position: 'first' | 'last', options?: InsertAdjacentOptions): Promise<any>;
74
- private schemaExists;
75
- insertAdjacent(position: 'beforeBegin' | 'afterBegin' | 'beforeEnd' | 'afterEnd', target: string, schema: any, options?: InsertAdjacentOptions): Promise<any>;
76
77
  protected insertAfterBegin(targetUid: string, schema: any, options?: InsertAdjacentOptions): Promise<any>;
77
78
  protected insertBeforeEnd(targetUid: string, schema: any, options?: InsertAdjacentOptions): Promise<any>;
78
79
  protected insertBeforeBegin(targetUid: string, schema: any, options?: InsertAdjacentOptions): Promise<any>;
79
80
  protected insertAfterEnd(targetUid: string, schema: any, options?: InsertAdjacentOptions): Promise<any>;
80
81
  protected insertNodes(nodes: SchemaNode[], options?: Transactionable): Promise<any[]>;
82
+ private doGetProperties;
83
+ private doGetJsonSchema;
84
+ private ignoreSchemaProperties;
85
+ private breakOnMatched;
86
+ private schemaExists;
81
87
  private regenerateUid;
82
- duplicate(uid: string, options?: Transactionable): Promise<any>;
83
- insert(schema: any, options?: Transactionable): Promise<any>;
84
- insertNewSchema(schema: any, options?: Transactionable & {
85
- returnNode?: boolean;
86
- }): Promise<any>;
87
88
  private insertSchemaRecord;
88
89
  private prepareSingleNodeForInsert;
89
- insertSingleNode(schema: SchemaNode, options: Transactionable & removeParentOptions): Promise<any>;
90
90
  }
91
91
  export default UiSchemaRepository;
@@ -79,37 +79,6 @@ function transaction(transactionAbleArgPosition) {
79
79
  }
80
80
  const _UiSchemaRepository = class _UiSchemaRepository extends import_database.Repository {
81
81
  cache;
82
- // if you need to handle cache in repo method, so you must set cache first
83
- setCache(cache) {
84
- this.cache = cache;
85
- }
86
- /**
87
- * clear cache with xUid which in uiSchemaTreePath's Path
88
- * @param {string} xUid
89
- * @param {Transaction} transaction
90
- * @returns {Promise<void>}
91
- */
92
- async clearXUidPathCache(xUid, transaction2) {
93
- if (!this.cache || !xUid) {
94
- return;
95
- }
96
- const uiSchemaNodes = await this.database.getRepository("uiSchemaTreePath").find({
97
- filter: {
98
- descendant: xUid
99
- },
100
- transaction: transaction2
101
- });
102
- for (const uiSchemaNode of uiSchemaNodes) {
103
- await this.cache.del(`p_${uiSchemaNode["ancestor"]}`);
104
- await this.cache.del(`s_${uiSchemaNode["ancestor"]}`);
105
- }
106
- }
107
- tableNameAdapter(tableName) {
108
- if (this.database.sequelize.getDialect() === "postgres") {
109
- return `"${this.database.options.schema || "public"}"."${tableName}"`;
110
- }
111
- return tableName;
112
- }
113
82
  get uiSchemasTableName() {
114
83
  return this.tableNameAdapter(this.model.tableName);
115
84
  }
@@ -117,12 +86,6 @@ const _UiSchemaRepository = class _UiSchemaRepository extends import_database.Re
117
86
  const model = this.database.getCollection("uiSchemaTreePath").model;
118
87
  return this.tableNameAdapter(model.tableName);
119
88
  }
120
- sqlAdapter(sql) {
121
- if (this.database.sequelize.getDialect() === "mysql") {
122
- return import_lodash.default.replace(sql, /"/g, "`");
123
- }
124
- return sql;
125
- }
126
89
  static schemaToSingleNodes(schema, carry = [], childOptions = null) {
127
90
  const node = import_lodash.default.cloneDeep(
128
91
  import_lodash.default.isString(schema) ? {
@@ -165,61 +128,50 @@ const _UiSchemaRepository = class _UiSchemaRepository extends import_database.Re
165
128
  }
166
129
  return carry;
167
130
  }
168
- async getProperties(uid2, options = {}) {
169
- if ((options == null ? void 0 : options.readFromCache) && this.cache) {
170
- return this.cache.wrap(`p_${uid2}`, () => {
171
- return this.doGetProperties(uid2, options);
172
- });
173
- }
174
- return this.doGetProperties(uid2, options);
131
+ // if you need to handle cache in repo method, so you must set cache first
132
+ setCache(cache) {
133
+ this.cache = cache;
175
134
  }
176
- async doGetProperties(uid2, options = {}) {
177
- const { transaction: transaction2 } = options;
178
- const db = this.database;
179
- const rawSql = `
180
- SELECT "SchemaTable"."x-uid" as "x-uid", "SchemaTable"."name" as "name", "SchemaTable"."schema" as "schema",
181
- TreePath.depth as depth,
182
- NodeInfo.type as type, NodeInfo.async as async, ParentPath.ancestor as parent, ParentPath.sort as sort
183
- FROM ${this.uiSchemaTreePathTableName} as TreePath
184
- LEFT JOIN ${this.uiSchemasTableName} as "SchemaTable" ON "SchemaTable"."x-uid" = TreePath.descendant
185
- LEFT JOIN ${this.uiSchemaTreePathTableName} as NodeInfo ON NodeInfo.descendant = "SchemaTable"."x-uid" and NodeInfo.descendant = NodeInfo.ancestor and NodeInfo.depth = 0
186
- LEFT JOIN ${this.uiSchemaTreePathTableName} as ParentPath ON (ParentPath.descendant = "SchemaTable"."x-uid" AND ParentPath.depth = 1)
187
- WHERE TreePath.ancestor = :ancestor AND (NodeInfo.async = false or TreePath.depth = 1)`;
188
- const nodes = await db.sequelize.query(this.sqlAdapter(rawSql), {
189
- replacements: {
190
- ancestor: uid2
135
+ /**
136
+ * clear cache with xUid which in uiSchemaTreePath's Path
137
+ * @param {string} xUid
138
+ * @param {Transaction} transaction
139
+ * @returns {Promise<void>}
140
+ */
141
+ async clearXUidPathCache(xUid, transaction2) {
142
+ if (!this.cache || !xUid) {
143
+ return;
144
+ }
145
+ const uiSchemaNodes = await this.database.getRepository("uiSchemaTreePath").find({
146
+ filter: {
147
+ descendant: xUid
191
148
  },
192
149
  transaction: transaction2
193
150
  });
194
- if (nodes[0].length == 0) {
195
- return {};
151
+ for (const uiSchemaNode of uiSchemaNodes) {
152
+ await this.cache.del(`p_${uiSchemaNode["ancestor"]}`);
153
+ await this.cache.del(`s_${uiSchemaNode["ancestor"]}`);
196
154
  }
197
- const schema = this.nodesToSchema(nodes[0], uid2);
198
- return import_lodash.default.pick(schema, ["type", "properties"]);
199
155
  }
200
- async doGetJsonSchema(uid2, options) {
201
- const db = this.database;
202
- const treeTable = this.uiSchemaTreePathTableName;
203
- const rawSql = `
204
- SELECT "SchemaTable"."x-uid" as "x-uid", "SchemaTable"."name" as name, "SchemaTable"."schema" as "schema" ,
205
- TreePath.depth as depth,
206
- NodeInfo.type as type, NodeInfo.async as async, ParentPath.ancestor as parent, ParentPath.sort as sort
207
- FROM ${treeTable} as TreePath
208
- LEFT JOIN ${this.uiSchemasTableName} as "SchemaTable" ON "SchemaTable"."x-uid" = TreePath.descendant
209
- LEFT JOIN ${treeTable} as NodeInfo ON NodeInfo.descendant = "SchemaTable"."x-uid" and NodeInfo.descendant = NodeInfo.ancestor and NodeInfo.depth = 0
210
- LEFT JOIN ${treeTable} as ParentPath ON (ParentPath.descendant = "SchemaTable"."x-uid" AND ParentPath.depth = 1)
211
- WHERE TreePath.ancestor = :ancestor ${(options == null ? void 0 : options.includeAsyncNode) ? "" : "AND (NodeInfo.async != true )"}
212
- `;
213
- const nodes = await db.sequelize.query(this.sqlAdapter(rawSql), {
214
- replacements: {
215
- ancestor: uid2
216
- },
217
- transaction: options == null ? void 0 : options.transaction
218
- });
219
- if (nodes[0].length == 0) {
220
- return {};
156
+ tableNameAdapter(tableName) {
157
+ if (this.database.sequelize.getDialect() === "postgres") {
158
+ return `"${this.database.options.schema || "public"}"."${tableName}"`;
221
159
  }
222
- return this.nodesToSchema(nodes[0], uid2);
160
+ return tableName;
161
+ }
162
+ sqlAdapter(sql) {
163
+ if (this.database.isMySQLCompatibleDialect()) {
164
+ return import_lodash.default.replace(sql, /"/g, "`");
165
+ }
166
+ return sql;
167
+ }
168
+ async getProperties(uid2, options = {}) {
169
+ if ((options == null ? void 0 : options.readFromCache) && this.cache) {
170
+ return this.cache.wrap(`p_${uid2}`, () => {
171
+ return this.doGetProperties(uid2, options);
172
+ });
173
+ }
174
+ return this.doGetProperties(uid2, options);
223
175
  }
224
176
  async getJsonSchema(uid2, options) {
225
177
  if ((options == null ? void 0 : options.readFromCache) && this.cache) {
@@ -229,9 +181,6 @@ const _UiSchemaRepository = class _UiSchemaRepository extends import_database.Re
229
181
  }
230
182
  return this.doGetJsonSchema(uid2, options);
231
183
  }
232
- ignoreSchemaProperties(schemaProperties) {
233
- return import_lodash.default.omit(schemaProperties, nodeKeys);
234
- }
235
184
  nodesToSchema(nodes, rootUid) {
236
185
  const nodeAttributeSanitize = (node) => {
237
186
  const schema = {
@@ -317,86 +266,6 @@ const _UiSchemaRepository = class _UiSchemaRepository extends import_database.Re
317
266
  await this.patch(schema, { ...options, transaction: transaction2 });
318
267
  }
319
268
  }
320
- async updateNode(uid2, schema, transaction2) {
321
- const nodeModel = await this.findOne({
322
- filter: {
323
- "x-uid": uid2
324
- }
325
- });
326
- await nodeModel.update(
327
- {
328
- schema: {
329
- ...nodeModel.get("schema"),
330
- ...import_lodash.default.omit(schema, ["x-async", "name", "x-uid", "properties"])
331
- }
332
- },
333
- {
334
- hooks: false,
335
- transaction: transaction2
336
- }
337
- );
338
- if (schema["x-server-hooks"]) {
339
- await this.database.emitAsync(`${this.collection.name}.afterSave`, nodeModel, { transaction: transaction2 });
340
- }
341
- }
342
- async childrenCount(uid2, transaction2) {
343
- const db = this.database;
344
- const countResult = await db.sequelize.query(
345
- `SELECT COUNT(*) as count FROM ${this.uiSchemaTreePathTableName} where ancestor = :ancestor and depth = 1`,
346
- {
347
- replacements: {
348
- ancestor: uid2
349
- },
350
- type: "SELECT",
351
- transaction: transaction2
352
- }
353
- );
354
- return parseInt(countResult[0]["count"]);
355
- }
356
- async isLeafNode(uid2, transaction2) {
357
- const childrenCount = await this.childrenCount(uid2, transaction2);
358
- return childrenCount === 0;
359
- }
360
- async findParentUid(uid2, transaction2) {
361
- const parent = await this.database.getRepository("uiSchemaTreePath").findOne({
362
- filter: {
363
- descendant: uid2,
364
- depth: 1
365
- },
366
- transaction: transaction2
367
- });
368
- return parent ? parent.get("ancestor") : null;
369
- }
370
- async findNodeSchemaWithParent(uid2, transaction2) {
371
- const schema = await this.database.getRepository("uiSchemas").findOne({
372
- filter: {
373
- "x-uid": uid2
374
- },
375
- transaction: transaction2
376
- });
377
- return {
378
- parentUid: await this.findParentUid(uid2, transaction2),
379
- schema
380
- };
381
- }
382
- async isSingleChild(uid2, transaction2) {
383
- const db = this.database;
384
- const parent = await this.findParentUid(uid2, transaction2);
385
- if (!parent) {
386
- return null;
387
- }
388
- const parentChildrenCount = await this.childrenCount(parent, transaction2);
389
- if (parentChildrenCount == 1) {
390
- const schema = await db.getRepository("uiSchemas").findOne({
391
- filter: {
392
- "x-uid": parent
393
- },
394
- transaction: transaction2
395
- });
396
- return schema;
397
- }
398
- return null;
399
- }
400
269
  async removeEmptyParents(options) {
401
270
  const { transaction: transaction2, uid: uid2, breakRemoveOn } = options;
402
271
  await this.clearXUidPathCache(uid2, transaction2);
@@ -410,19 +279,6 @@ const _UiSchemaRepository = class _UiSchemaRepository extends import_database.Re
410
279
  };
411
280
  await removeParent(uid2);
412
281
  }
413
- breakOnMatched(schemaInstance, breakRemoveOn) {
414
- if (!breakRemoveOn) {
415
- return false;
416
- }
417
- for (const key of Object.keys(breakRemoveOn)) {
418
- const instanceValue = schemaInstance.get(key);
419
- const breakRemoveOnValue = breakRemoveOn[key];
420
- if (instanceValue !== breakRemoveOnValue) {
421
- return false;
422
- }
423
- }
424
- return true;
425
- }
426
282
  async recursivelyRemoveIfNoChildren(options) {
427
283
  const { uid: uid2, transaction: transaction2, breakRemoveOn } = options;
428
284
  await this.clearXUidPathCache(uid2, transaction2);
@@ -475,123 +331,34 @@ const _UiSchemaRepository = class _UiSchemaRepository extends import_database.Re
475
331
  }
476
332
  );
477
333
  }
478
- async insertBeside(targetUid, schema, side, options) {
334
+ async insertAdjacent(position, target, schema, options) {
479
335
  const { transaction: transaction2 } = options;
480
- const targetParent = await this.findParentUid(targetUid, transaction2);
481
- const db = this.database;
482
- const treeTable = this.uiSchemaTreePathTableName;
483
- const typeQuery = await db.sequelize.query(`SELECT type from ${treeTable} WHERE ancestor = :uid AND depth = 0;`, {
484
- type: "SELECT",
485
- replacements: {
486
- uid: targetUid
487
- },
488
- transaction: transaction2
489
- });
490
- const nodes = _UiSchemaRepository.schemaToSingleNodes(schema);
491
- const rootNode = nodes[0];
492
- rootNode.childOptions = {
493
- parentUid: targetParent,
494
- type: typeQuery[0]["type"],
495
- position: {
496
- type: side,
497
- target: targetUid
498
- }
499
- };
500
- const insertedNodes = await this.insertNodes(nodes, options);
501
- return await this.getJsonSchema(insertedNodes[0].get("x-uid"), {
502
- transaction: transaction2
503
- });
504
- }
505
- async insertInner(targetUid, schema, position, options) {
506
- const { transaction: transaction2 } = options;
507
- const nodes = _UiSchemaRepository.schemaToSingleNodes(schema);
508
- const rootNode = nodes[0];
509
- rootNode.childOptions = {
510
- parentUid: targetUid,
511
- type: import_lodash.default.get(schema, "x-node-type", "properties"),
512
- position
513
- };
514
- const insertedNodes = await this.insertNodes(nodes, options);
515
- return await this.getJsonSchema(insertedNodes[0].get("x-uid"), {
516
- transaction: transaction2
517
- });
518
- }
519
- async schemaExists(schema, options) {
520
- if (import_lodash.default.isObject(schema) && !schema["x-uid"]) {
521
- return false;
522
- }
523
- const { transaction: transaction2 } = options;
524
- const result = await this.database.sequelize.query(
525
- this.sqlAdapter(`select "x-uid" from ${this.uiSchemasTableName} where "x-uid" = :uid`),
526
- {
527
- type: "SELECT",
528
- replacements: {
529
- uid: import_lodash.default.isString(schema) ? schema : schema["x-uid"]
530
- },
531
- transaction: transaction2
532
- }
533
- );
534
- return result.length > 0;
535
- }
536
- async insertAdjacent(position, target, schema, options) {
537
- const { transaction: transaction2 } = options;
538
- await this.clearXUidPathCache(schema["x-uid"], transaction2);
539
- if (options.wrap) {
540
- const wrapSchemaNodes = await this.insertNewSchema(options.wrap, {
541
- transaction: transaction2,
542
- returnNode: true
543
- });
544
- const lastWrapNode = wrapSchemaNodes[wrapSchemaNodes.length - 1];
545
- await this.insertAdjacent("afterBegin", lastWrapNode["x-uid"], schema, import_lodash.default.omit(options, "wrap"));
546
- schema = wrapSchemaNodes[0]["x-uid"];
547
- options.removeParentsIfNoChildren = false;
548
- } else {
549
- const schemaExists = await this.schemaExists(schema, { transaction: transaction2 });
550
- if (schemaExists) {
551
- schema = import_lodash.default.isString(schema) ? schema : schema["x-uid"];
552
- } else {
553
- const insertedSchema = await this.insertNewSchema(schema, {
554
- transaction: transaction2,
555
- returnNode: true
556
- });
557
- schema = insertedSchema[0]["x-uid"];
336
+ await this.clearXUidPathCache(schema["x-uid"], transaction2);
337
+ if (options.wrap) {
338
+ const wrapSchemaNodes = await this.insertNewSchema(options.wrap, {
339
+ transaction: transaction2,
340
+ returnNode: true
341
+ });
342
+ const lastWrapNode = wrapSchemaNodes[wrapSchemaNodes.length - 1];
343
+ await this.insertAdjacent("afterBegin", lastWrapNode["x-uid"], schema, import_lodash.default.omit(options, "wrap"));
344
+ schema = wrapSchemaNodes[0]["x-uid"];
345
+ options.removeParentsIfNoChildren = false;
346
+ } else {
347
+ const schemaExists = await this.schemaExists(schema, { transaction: transaction2 });
348
+ if (schemaExists) {
349
+ schema = import_lodash.default.isString(schema) ? schema : schema["x-uid"];
350
+ } else {
351
+ const insertedSchema = await this.insertNewSchema(schema, {
352
+ transaction: transaction2,
353
+ returnNode: true
354
+ });
355
+ schema = insertedSchema[0]["x-uid"];
558
356
  }
559
357
  }
560
358
  const result = await this[`insert${import_lodash.default.upperFirst(position)}`](target, schema, options);
561
359
  await this.clearXUidPathCache(result["x-uid"], transaction2);
562
360
  return result;
563
361
  }
564
- async insertAfterBegin(targetUid, schema, options) {
565
- return await this.insertInner(targetUid, schema, "first", options);
566
- }
567
- async insertBeforeEnd(targetUid, schema, options) {
568
- return await this.insertInner(targetUid, schema, "last", options);
569
- }
570
- async insertBeforeBegin(targetUid, schema, options) {
571
- return await this.insertBeside(targetUid, schema, "before", options);
572
- }
573
- async insertAfterEnd(targetUid, schema, options) {
574
- return await this.insertBeside(targetUid, schema, "after", options);
575
- }
576
- async insertNodes(nodes, options) {
577
- const { transaction: transaction2 } = options;
578
- const insertedNodes = [];
579
- for (const node of nodes) {
580
- insertedNodes.push(
581
- await this.insertSingleNode(node, {
582
- ...options,
583
- transaction: transaction2
584
- })
585
- );
586
- }
587
- return insertedNodes;
588
- }
589
- regenerateUid(s) {
590
- s["x-uid"] = (0, import_utils.uid)();
591
- Object.keys(s.properties || {}).forEach((key) => {
592
- this.regenerateUid(s.properties[key]);
593
- });
594
- }
595
362
  async duplicate(uid2, options) {
596
363
  const s = await this.getJsonSchema(uid2, { ...options, includeAsyncNode: true });
597
364
  this.regenerateUid(s);
@@ -659,33 +426,6 @@ const _UiSchemaRepository = class _UiSchemaRepository extends import_database.Re
659
426
  await this.clearXUidPathCache(result["x-uid"], transaction2);
660
427
  return result;
661
428
  }
662
- async insertSchemaRecord(name, uid2, schema, transaction2) {
663
- const serverHooks = schema["x-server-hooks"] || [];
664
- const node = await this.create({
665
- values: {
666
- name,
667
- ["x-uid"]: uid2,
668
- schema,
669
- serverHooks
670
- },
671
- transaction: transaction2,
672
- context: {
673
- disableInsertHook: true
674
- }
675
- });
676
- return node;
677
- }
678
- prepareSingleNodeForInsert(schema) {
679
- const uid2 = schema["x-uid"];
680
- const name = schema["name"];
681
- const async = import_lodash.default.get(schema, "x-async", false);
682
- const childOptions = schema["childOptions"];
683
- delete schema["x-uid"];
684
- delete schema["x-async"];
685
- delete schema["name"];
686
- delete schema["childOptions"];
687
- return { uid: uid2, name, async, childOptions };
688
- }
689
429
  async insertSingleNode(schema, options) {
690
430
  const { transaction: transaction2 } = options;
691
431
  const db = this.database;
@@ -784,7 +524,7 @@ const _UiSchemaRepository = class _UiSchemaRepository extends import_database.Re
784
524
  FROM ${treeTable} as NodeInfo
785
525
  WHERE NodeInfo.descendant = TreeTable.descendant and NodeInfo.depth = 0
786
526
  AND TreeTable.depth = 1 AND TreeTable.ancestor = :ancestor and NodeInfo.type = :type`;
787
- if (this.database.sequelize.getDialect() === "mysql") {
527
+ if (this.database.isMySQLCompatibleDialect()) {
788
528
  updateSql2 = `UPDATE ${treeTable} as TreeTable
789
529
  JOIN ${treeTable} as NodeInfo ON (NodeInfo.descendant = TreeTable.descendant and NodeInfo.depth = 0)
790
530
  SET TreeTable.sort = TreeTable.sort + 1
@@ -845,7 +585,7 @@ const _UiSchemaRepository = class _UiSchemaRepository extends import_database.Re
845
585
  AND TreeTable.ancestor = :ancestor
846
586
  and TreeTable.sort >= :sort
847
587
  and NodeInfo.type = :type`;
848
- if (this.database.sequelize.getDialect() === "mysql") {
588
+ if (this.database.isMySQLCompatibleDialect()) {
849
589
  updateSql2 = `UPDATE ${treeTable} as TreeTable
850
590
  JOIN ${treeTable} as NodeInfo ON (NodeInfo.descendant = TreeTable.descendant and NodeInfo.depth = 0)
851
591
  SET TreeTable.sort = TreeTable.sort + 1
@@ -900,6 +640,266 @@ WHERE TreeTable.depth = 1 AND TreeTable.ancestor = :ancestor and TreeTable.sort
900
640
  await this.clearXUidPathCache(uid2, transaction2);
901
641
  return savedNode;
902
642
  }
643
+ async updateNode(uid2, schema, transaction2) {
644
+ const nodeModel = await this.findOne({
645
+ filter: {
646
+ "x-uid": uid2
647
+ }
648
+ });
649
+ await nodeModel.update(
650
+ {
651
+ schema: {
652
+ ...nodeModel.get("schema"),
653
+ ...import_lodash.default.omit(schema, ["x-async", "name", "x-uid", "properties"])
654
+ }
655
+ },
656
+ {
657
+ hooks: false,
658
+ transaction: transaction2
659
+ }
660
+ );
661
+ if (schema["x-server-hooks"]) {
662
+ await this.database.emitAsync(`${this.collection.name}.afterSave`, nodeModel, { transaction: transaction2 });
663
+ }
664
+ }
665
+ async childrenCount(uid2, transaction2) {
666
+ const db = this.database;
667
+ const countResult = await db.sequelize.query(
668
+ `SELECT COUNT(*) as count FROM ${this.uiSchemaTreePathTableName} where ancestor = :ancestor and depth = 1`,
669
+ {
670
+ replacements: {
671
+ ancestor: uid2
672
+ },
673
+ type: "SELECT",
674
+ transaction: transaction2
675
+ }
676
+ );
677
+ return parseInt(countResult[0]["count"]);
678
+ }
679
+ async isLeafNode(uid2, transaction2) {
680
+ const childrenCount = await this.childrenCount(uid2, transaction2);
681
+ return childrenCount === 0;
682
+ }
683
+ async findParentUid(uid2, transaction2) {
684
+ const parent = await this.database.getRepository("uiSchemaTreePath").findOne({
685
+ filter: {
686
+ descendant: uid2,
687
+ depth: 1
688
+ },
689
+ transaction: transaction2
690
+ });
691
+ return parent ? parent.get("ancestor") : null;
692
+ }
693
+ async findNodeSchemaWithParent(uid2, transaction2) {
694
+ const schema = await this.database.getRepository("uiSchemas").findOne({
695
+ filter: {
696
+ "x-uid": uid2
697
+ },
698
+ transaction: transaction2
699
+ });
700
+ return {
701
+ parentUid: await this.findParentUid(uid2, transaction2),
702
+ schema
703
+ };
704
+ }
705
+ async isSingleChild(uid2, transaction2) {
706
+ const db = this.database;
707
+ const parent = await this.findParentUid(uid2, transaction2);
708
+ if (!parent) {
709
+ return null;
710
+ }
711
+ const parentChildrenCount = await this.childrenCount(parent, transaction2);
712
+ if (parentChildrenCount == 1) {
713
+ const schema = await db.getRepository("uiSchemas").findOne({
714
+ filter: {
715
+ "x-uid": parent
716
+ },
717
+ transaction: transaction2
718
+ });
719
+ return schema;
720
+ }
721
+ return null;
722
+ }
723
+ async insertBeside(targetUid, schema, side, options) {
724
+ const { transaction: transaction2 } = options;
725
+ const targetParent = await this.findParentUid(targetUid, transaction2);
726
+ const db = this.database;
727
+ const treeTable = this.uiSchemaTreePathTableName;
728
+ const typeQuery = await db.sequelize.query(`SELECT type from ${treeTable} WHERE ancestor = :uid AND depth = 0;`, {
729
+ type: "SELECT",
730
+ replacements: {
731
+ uid: targetUid
732
+ },
733
+ transaction: transaction2
734
+ });
735
+ const nodes = _UiSchemaRepository.schemaToSingleNodes(schema);
736
+ const rootNode = nodes[0];
737
+ rootNode.childOptions = {
738
+ parentUid: targetParent,
739
+ type: typeQuery[0]["type"],
740
+ position: {
741
+ type: side,
742
+ target: targetUid
743
+ }
744
+ };
745
+ const insertedNodes = await this.insertNodes(nodes, options);
746
+ return await this.getJsonSchema(insertedNodes[0].get("x-uid"), {
747
+ transaction: transaction2
748
+ });
749
+ }
750
+ async insertInner(targetUid, schema, position, options) {
751
+ const { transaction: transaction2 } = options;
752
+ const nodes = _UiSchemaRepository.schemaToSingleNodes(schema);
753
+ const rootNode = nodes[0];
754
+ rootNode.childOptions = {
755
+ parentUid: targetUid,
756
+ type: import_lodash.default.get(schema, "x-node-type", "properties"),
757
+ position
758
+ };
759
+ const insertedNodes = await this.insertNodes(nodes, options);
760
+ return await this.getJsonSchema(insertedNodes[0].get("x-uid"), {
761
+ transaction: transaction2
762
+ });
763
+ }
764
+ async insertAfterBegin(targetUid, schema, options) {
765
+ return await this.insertInner(targetUid, schema, "first", options);
766
+ }
767
+ async insertBeforeEnd(targetUid, schema, options) {
768
+ return await this.insertInner(targetUid, schema, "last", options);
769
+ }
770
+ async insertBeforeBegin(targetUid, schema, options) {
771
+ return await this.insertBeside(targetUid, schema, "before", options);
772
+ }
773
+ async insertAfterEnd(targetUid, schema, options) {
774
+ return await this.insertBeside(targetUid, schema, "after", options);
775
+ }
776
+ async insertNodes(nodes, options) {
777
+ const { transaction: transaction2 } = options;
778
+ const insertedNodes = [];
779
+ for (const node of nodes) {
780
+ insertedNodes.push(
781
+ await this.insertSingleNode(node, {
782
+ ...options,
783
+ transaction: transaction2
784
+ })
785
+ );
786
+ }
787
+ return insertedNodes;
788
+ }
789
+ async doGetProperties(uid2, options = {}) {
790
+ const { transaction: transaction2 } = options;
791
+ const db = this.database;
792
+ const rawSql = `
793
+ SELECT "SchemaTable"."x-uid" as "x-uid", "SchemaTable"."name" as "name", "SchemaTable"."schema" as "schema",
794
+ TreePath.depth as depth,
795
+ NodeInfo.type as type, NodeInfo.async as async, ParentPath.ancestor as parent, ParentPath.sort as sort
796
+ FROM ${this.uiSchemaTreePathTableName} as TreePath
797
+ LEFT JOIN ${this.uiSchemasTableName} as "SchemaTable" ON "SchemaTable"."x-uid" = TreePath.descendant
798
+ LEFT JOIN ${this.uiSchemaTreePathTableName} as NodeInfo ON NodeInfo.descendant = "SchemaTable"."x-uid" and NodeInfo.descendant = NodeInfo.ancestor and NodeInfo.depth = 0
799
+ LEFT JOIN ${this.uiSchemaTreePathTableName} as ParentPath ON (ParentPath.descendant = "SchemaTable"."x-uid" AND ParentPath.depth = 1)
800
+ WHERE TreePath.ancestor = :ancestor AND (NodeInfo.async = false or TreePath.depth = 1)`;
801
+ const nodes = await db.sequelize.query(this.sqlAdapter(rawSql), {
802
+ replacements: {
803
+ ancestor: uid2
804
+ },
805
+ transaction: transaction2
806
+ });
807
+ if (nodes[0].length == 0) {
808
+ return {};
809
+ }
810
+ const schema = this.nodesToSchema(nodes[0], uid2);
811
+ return import_lodash.default.pick(schema, ["type", "properties"]);
812
+ }
813
+ async doGetJsonSchema(uid2, options) {
814
+ const db = this.database;
815
+ const treeTable = this.uiSchemaTreePathTableName;
816
+ const rawSql = `
817
+ SELECT "SchemaTable"."x-uid" as "x-uid", "SchemaTable"."name" as name, "SchemaTable"."schema" as "schema" ,
818
+ TreePath.depth as depth,
819
+ NodeInfo.type as type, NodeInfo.async as async, ParentPath.ancestor as parent, ParentPath.sort as sort
820
+ FROM ${treeTable} as TreePath
821
+ LEFT JOIN ${this.uiSchemasTableName} as "SchemaTable" ON "SchemaTable"."x-uid" = TreePath.descendant
822
+ LEFT JOIN ${treeTable} as NodeInfo ON NodeInfo.descendant = "SchemaTable"."x-uid" and NodeInfo.descendant = NodeInfo.ancestor and NodeInfo.depth = 0
823
+ LEFT JOIN ${treeTable} as ParentPath ON (ParentPath.descendant = "SchemaTable"."x-uid" AND ParentPath.depth = 1)
824
+ WHERE TreePath.ancestor = :ancestor ${(options == null ? void 0 : options.includeAsyncNode) ? "" : "AND (NodeInfo.async != true )"}
825
+ `;
826
+ const nodes = await db.sequelize.query(this.sqlAdapter(rawSql), {
827
+ replacements: {
828
+ ancestor: uid2
829
+ },
830
+ transaction: options == null ? void 0 : options.transaction
831
+ });
832
+ if (nodes[0].length == 0) {
833
+ return {};
834
+ }
835
+ return this.nodesToSchema(nodes[0], uid2);
836
+ }
837
+ ignoreSchemaProperties(schemaProperties) {
838
+ return import_lodash.default.omit(schemaProperties, nodeKeys);
839
+ }
840
+ breakOnMatched(schemaInstance, breakRemoveOn) {
841
+ if (!breakRemoveOn) {
842
+ return false;
843
+ }
844
+ for (const key of Object.keys(breakRemoveOn)) {
845
+ const instanceValue = schemaInstance.get(key);
846
+ const breakRemoveOnValue = breakRemoveOn[key];
847
+ if (instanceValue !== breakRemoveOnValue) {
848
+ return false;
849
+ }
850
+ }
851
+ return true;
852
+ }
853
+ async schemaExists(schema, options) {
854
+ if (import_lodash.default.isObject(schema) && !schema["x-uid"]) {
855
+ return false;
856
+ }
857
+ const { transaction: transaction2 } = options;
858
+ const result = await this.database.sequelize.query(
859
+ this.sqlAdapter(`select "x-uid" from ${this.uiSchemasTableName} where "x-uid" = :uid`),
860
+ {
861
+ type: "SELECT",
862
+ replacements: {
863
+ uid: import_lodash.default.isString(schema) ? schema : schema["x-uid"]
864
+ },
865
+ transaction: transaction2
866
+ }
867
+ );
868
+ return result.length > 0;
869
+ }
870
+ regenerateUid(s) {
871
+ s["x-uid"] = (0, import_utils.uid)();
872
+ Object.keys(s.properties || {}).forEach((key) => {
873
+ this.regenerateUid(s.properties[key]);
874
+ });
875
+ }
876
+ async insertSchemaRecord(name, uid2, schema, transaction2) {
877
+ const serverHooks = schema["x-server-hooks"] || [];
878
+ const node = await this.create({
879
+ values: {
880
+ name,
881
+ ["x-uid"]: uid2,
882
+ schema,
883
+ serverHooks
884
+ },
885
+ transaction: transaction2,
886
+ context: {
887
+ disableInsertHook: true
888
+ }
889
+ });
890
+ return node;
891
+ }
892
+ prepareSingleNodeForInsert(schema) {
893
+ const uid2 = schema["x-uid"];
894
+ const name = schema["name"];
895
+ const async = import_lodash.default.get(schema, "x-async", false);
896
+ const childOptions = schema["childOptions"];
897
+ delete schema["x-uid"];
898
+ delete schema["x-async"];
899
+ delete schema["name"];
900
+ delete schema["childOptions"];
901
+ return { uid: uid2, name, async, childOptions };
902
+ }
903
903
  };
904
904
  __decorateClass([
905
905
  transaction()
@@ -915,37 +915,37 @@ __decorateClass([
915
915
  ], _UiSchemaRepository.prototype, "remove", 1);
916
916
  __decorateClass([
917
917
  transaction()
918
- ], _UiSchemaRepository.prototype, "insertBeside", 1);
918
+ ], _UiSchemaRepository.prototype, "insertAdjacent", 1);
919
919
  __decorateClass([
920
920
  transaction()
921
- ], _UiSchemaRepository.prototype, "insertInner", 1);
921
+ ], _UiSchemaRepository.prototype, "duplicate", 1);
922
922
  __decorateClass([
923
923
  transaction()
924
- ], _UiSchemaRepository.prototype, "insertAdjacent", 1);
924
+ ], _UiSchemaRepository.prototype, "insert", 1);
925
925
  __decorateClass([
926
926
  transaction()
927
- ], _UiSchemaRepository.prototype, "insertAfterBegin", 1);
927
+ ], _UiSchemaRepository.prototype, "insertNewSchema", 1);
928
928
  __decorateClass([
929
929
  transaction()
930
- ], _UiSchemaRepository.prototype, "insertBeforeEnd", 1);
930
+ ], _UiSchemaRepository.prototype, "insertBeside", 1);
931
931
  __decorateClass([
932
932
  transaction()
933
- ], _UiSchemaRepository.prototype, "insertBeforeBegin", 1);
933
+ ], _UiSchemaRepository.prototype, "insertInner", 1);
934
934
  __decorateClass([
935
935
  transaction()
936
- ], _UiSchemaRepository.prototype, "insertAfterEnd", 1);
936
+ ], _UiSchemaRepository.prototype, "insertAfterBegin", 1);
937
937
  __decorateClass([
938
938
  transaction()
939
- ], _UiSchemaRepository.prototype, "insertNodes", 1);
939
+ ], _UiSchemaRepository.prototype, "insertBeforeEnd", 1);
940
940
  __decorateClass([
941
941
  transaction()
942
- ], _UiSchemaRepository.prototype, "duplicate", 1);
942
+ ], _UiSchemaRepository.prototype, "insertBeforeBegin", 1);
943
943
  __decorateClass([
944
944
  transaction()
945
- ], _UiSchemaRepository.prototype, "insert", 1);
945
+ ], _UiSchemaRepository.prototype, "insertAfterEnd", 1);
946
946
  __decorateClass([
947
947
  transaction()
948
- ], _UiSchemaRepository.prototype, "insertNewSchema", 1);
948
+ ], _UiSchemaRepository.prototype, "insertNodes", 1);
949
949
  let UiSchemaRepository = _UiSchemaRepository;
950
950
  var repository_default = UiSchemaRepository;
951
951
  // Annotate the CommonJS export names for ESM import in node:
package/package.json CHANGED
@@ -4,7 +4,7 @@
4
4
  "displayName.zh-CN": "UI schema 存储服务",
5
5
  "description": "Provides centralized UI schema storage service",
6
6
  "description.zh-CN": "提供中心化的 UI schema 存储服务",
7
- "version": "0.15.0-alpha.3",
7
+ "version": "0.15.0-alpha.5",
8
8
  "license": "AGPL-3.0",
9
9
  "main": "./dist/server/index.js",
10
10
  "devDependencies": {
@@ -22,5 +22,5 @@
22
22
  "@nocobase/test": "0.x",
23
23
  "@nocobase/utils": "0.x"
24
24
  },
25
- "gitHead": "8f39868bd613a465efb20123094ebbc2390c1eef"
25
+ "gitHead": "4713b1a887dad4dadf27b65e3b3e2a46c6986cbf"
26
26
  }