@nocobase/database 3.0.0-alpha.5 → 3.0.0-alpha.7

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.
@@ -136,6 +136,8 @@ export declare class Collection<TModelAttributes extends {} = any, TCreationAttr
136
136
  setSortable(sortable: any): void;
137
137
  updateField(name: string, options: FieldOptions): void;
138
138
  private normalizeFieldName;
139
+ private findAttributeByIndexField;
140
+ private normalizeIndexFields;
139
141
  addIndex(index: string | string[] | {
140
142
  fields: string[];
141
143
  unique?: boolean;
package/lib/collection.js CHANGED
@@ -607,6 +607,20 @@ const _Collection = class _Collection extends import_events.EventEmitter {
607
607
  }
608
608
  return Array.isArray(val) ? val.map((v) => (0, import_utils.snakeCase)(v)) : (0, import_utils.snakeCase)(val);
609
609
  }
610
+ findAttributeByIndexField(field) {
611
+ var _a;
612
+ const attributes = this.model.getAttributes();
613
+ const normalizedField = this.normalizeFieldName(field);
614
+ return (_a = Object.entries(attributes).find(([name, attribute]) => {
615
+ return this.normalizeFieldName(name) === normalizedField || attribute.field === normalizedField;
616
+ })) == null ? void 0 : _a[1];
617
+ }
618
+ normalizeIndexFields(fields) {
619
+ return fields.map((field) => {
620
+ const attribute = this.findAttributeByIndexField(field);
621
+ return (attribute == null ? void 0 : attribute.field) || this.normalizeFieldName(field);
622
+ });
623
+ }
610
624
  addIndex(index) {
611
625
  if (!index) {
612
626
  return;
@@ -628,19 +642,22 @@ const _Collection = class _Collection extends import_events.EventEmitter {
628
642
  indexItem = index;
629
643
  indexName = index.fields;
630
644
  }
631
- if (import_lodash.default.isEqual(this.model.primaryKeyAttributes, indexName)) {
645
+ const normalizedIndexName = this.normalizeIndexFields(indexName);
646
+ const normalizedPrimaryKeyAttributes = this.normalizeIndexFields(this.model.primaryKeyAttributes);
647
+ if (import_lodash.default.isEqual(normalizedPrimaryKeyAttributes, normalizedIndexName)) {
632
648
  return;
633
649
  }
634
- const name = this.model.primaryKeyAttributes.join(",");
635
- if (name.startsWith(`${indexName.join(",")},`)) {
650
+ const name = normalizedPrimaryKeyAttributes.join(",");
651
+ if (name.startsWith(`${normalizedIndexName.join(",")},`)) {
636
652
  return;
637
653
  }
638
654
  for (const item of indexes) {
639
- if (import_lodash.default.isEqual(this.normalizeFieldName(item.fields), this.normalizeFieldName(indexName))) {
655
+ const normalizedFields = this.normalizeIndexFields(item.fields);
656
+ if (import_lodash.default.isEqual(normalizedFields, normalizedIndexName)) {
640
657
  return;
641
658
  }
642
- const name2 = item.fields.join(",");
643
- if (name2.startsWith(`${indexName.join(",")},`)) {
659
+ const name2 = normalizedFields.join(",");
660
+ if (name2.startsWith(`${normalizedIndexName.join(",")},`)) {
644
661
  return;
645
662
  }
646
663
  }
@@ -675,6 +692,7 @@ const _Collection = class _Collection extends import_events.EventEmitter {
675
692
  const attributes = {};
676
693
  for (const [name, field] of Object.entries(this.model.getAttributes())) {
677
694
  attributes[this.normalizeFieldName(name)] = field;
695
+ attributes[field.field] = field;
678
696
  }
679
697
  this.model._indexes = import_lodash.default.uniqBy(
680
698
  indexes.filter((item) => {
@@ -50,6 +50,23 @@ const pushAttribute = /* @__PURE__ */ __name((node, attribute) => {
50
50
  node.attributes.push(attribute);
51
51
  }
52
52
  }, "pushAttribute");
53
+ const getRootModelOrderFields = /* @__PURE__ */ __name((node) => {
54
+ if (!import_lodash.default.isArray(node.order)) {
55
+ return [];
56
+ }
57
+ const fields = [];
58
+ for (const orderItem of node.order) {
59
+ if (!import_lodash.default.isArray(orderItem) || orderItem.length !== 2 || !import_lodash.default.isString(orderItem[0])) {
60
+ continue;
61
+ }
62
+ const fieldName = orderItem[0];
63
+ const attribute = node.model.rawAttributes[fieldName] || Object.values(node.model.rawAttributes).find((item) => item.field === fieldName);
64
+ if (attribute) {
65
+ fields.push(attribute.field || fieldName);
66
+ }
67
+ }
68
+ return fields;
69
+ }, "getRootModelOrderFields");
53
70
  const EagerLoadingNodeProto = {
54
71
  afterBuild(db) {
55
72
  const collection = db.modelCollection.get(this.model);
@@ -218,11 +235,15 @@ const _EagerLoadingTree = class _EagerLoadingTree {
218
235
  if (!primaryKeyField) {
219
236
  throw new Error(`Model ${node.model.name} does not have primary key`);
220
237
  }
238
+ const group = [`${node.model.name}.${primaryKeyField}`];
239
+ if (this.db.inDialect("mssql") || this.db.isMySQLCompatibleDialect()) {
240
+ group.push(...getRootModelOrderFields(node).map((field) => `${node.model.name}.${field}`));
241
+ }
221
242
  const ids2 = (await node.model.findAll({
222
243
  ...this.rootQueryOptions,
223
244
  includeIgnoreAttributes: false,
224
245
  attributes: [primaryKeyField],
225
- group: `${node.model.name}.${primaryKeyField}`,
246
+ group: import_lodash.default.uniq(group),
226
247
  transaction,
227
248
  include: (0, import_utils.processIncludes)(includeForFilter, node.model),
228
249
  raw: true
@@ -35,6 +35,10 @@ export declare class SyncRunner {
35
35
  isParentColumn(columnName: string, options: any): Promise<boolean>;
36
36
  removeUnusedColumns(columns: any, options: any): Promise<void>;
37
37
  findAttributeByColumnName(columnName: string): any;
38
+ private getIndexFields;
39
+ private indexExists;
40
+ private deduplicateIndexes;
41
+ private filterExistingIndexesBeforeSync;
38
42
  performSync(options: any): Promise<any>;
39
43
  handleZeroColumnModel(options: any): Promise<void>;
40
44
  handleSchema(options: any): Promise<void>;
@@ -286,8 +286,52 @@ const _SyncRunner = class _SyncRunner {
286
286
  return attribute.field == columnName;
287
287
  });
288
288
  }
289
+ getIndexFields(index) {
290
+ return (index.fields || []).map((field) => {
291
+ if (typeof field === "string") {
292
+ return field;
293
+ }
294
+ return field.attribute || field.name;
295
+ });
296
+ }
297
+ indexExists(modelIndex, existingIndexes) {
298
+ const modelIndexFields = this.getIndexFields(modelIndex);
299
+ return existingIndexes.some((existingIndex) => {
300
+ if (modelIndex.name && existingIndex.name === modelIndex.name) {
301
+ return true;
302
+ }
303
+ return Boolean(modelIndex.unique) === Boolean(existingIndex.unique) && JSON.stringify(modelIndexFields) === JSON.stringify(this.getIndexFields(existingIndex));
304
+ });
305
+ }
306
+ deduplicateIndexes(indexes) {
307
+ return indexes.filter((index, indexPosition) => {
308
+ return indexes.findIndex((item) => this.indexExists(index, [item])) === indexPosition;
309
+ });
310
+ }
311
+ async filterExistingIndexesBeforeSync(options) {
312
+ const indexes = this.model["_indexes"];
313
+ if (!(indexes == null ? void 0 : indexes.length)) {
314
+ return;
315
+ }
316
+ const deduplicatedIndexes = this.deduplicateIndexes(indexes);
317
+ this.model["_indexes"] = deduplicatedIndexes;
318
+ try {
319
+ const existingIndexes = await this.queryInterface.showIndex(this.tableName, options);
320
+ this.model["_indexes"] = deduplicatedIndexes.filter((index) => !this.indexExists(index, existingIndexes));
321
+ } catch (error) {
322
+ }
323
+ }
289
324
  async performSync(options) {
290
- return this.collection.isInherited() ? await import_inherited_sync_runner.InheritedSyncRunner.syncInheritModel(this.model, options) : await import_sequelize.Model.sync.call(this.model, options);
325
+ if (this.collection.isInherited()) {
326
+ return await import_inherited_sync_runner.InheritedSyncRunner.syncInheritModel(this.model, options);
327
+ }
328
+ const indexes = this.model["_indexes"];
329
+ try {
330
+ await this.filterExistingIndexesBeforeSync(options);
331
+ return await import_sequelize.Model.sync.call(this.model, options);
332
+ } finally {
333
+ this.model["_indexes"] = indexes;
334
+ }
291
335
  }
292
336
  async handleZeroColumnModel(options) {
293
337
  if (Object.keys(this.model.tableAttributes).length === 0) {
package/package.json CHANGED
@@ -1,13 +1,13 @@
1
1
  {
2
2
  "name": "@nocobase/database",
3
- "version": "3.0.0-alpha.5",
3
+ "version": "3.0.0-alpha.7",
4
4
  "description": "",
5
5
  "main": "./lib/index.js",
6
6
  "types": "./lib/index.d.ts",
7
7
  "license": "Apache-2.0",
8
8
  "dependencies": {
9
- "@nocobase/logger": "3.0.0-alpha.5",
10
- "@nocobase/utils": "3.0.0-alpha.5",
9
+ "@nocobase/logger": "3.0.0-alpha.7",
10
+ "@nocobase/utils": "3.0.0-alpha.7",
11
11
  "async-mutex": "^0.3.2",
12
12
  "chalk": "^4.1.1",
13
13
  "cron-parser": "4.4.0",
@@ -38,5 +38,5 @@
38
38
  "url": "git+https://github.com/nocobase/nocobase.git",
39
39
  "directory": "packages/database"
40
40
  },
41
- "gitHead": "82c7ed1cdb2f247c190582e34ee37f154cac0968"
41
+ "gitHead": "19cd28c710f3b3c614ebe2fd7d5a415d527ee559"
42
42
  }