@mikro-orm/knex 7.0.0-dev.0 → 7.0.0-dev.10

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 (79) hide show
  1. package/AbstractSqlConnection.d.ts +2 -2
  2. package/AbstractSqlConnection.js +36 -31
  3. package/AbstractSqlDriver.d.ts +14 -12
  4. package/AbstractSqlDriver.js +198 -186
  5. package/AbstractSqlPlatform.d.ts +4 -4
  6. package/AbstractSqlPlatform.js +17 -21
  7. package/PivotCollectionPersister.d.ts +1 -1
  8. package/PivotCollectionPersister.js +5 -8
  9. package/SqlEntityManager.d.ts +4 -3
  10. package/SqlEntityManager.js +2 -6
  11. package/SqlEntityRepository.d.ts +2 -2
  12. package/SqlEntityRepository.js +2 -6
  13. package/dialects/index.d.ts +4 -4
  14. package/dialects/index.js +4 -20
  15. package/dialects/mssql/MsSqlNativeQueryBuilder.d.ts +1 -1
  16. package/dialects/mssql/MsSqlNativeQueryBuilder.js +29 -29
  17. package/dialects/mssql/index.d.ts +1 -1
  18. package/dialects/mssql/index.js +1 -17
  19. package/dialects/mysql/MySqlExceptionConverter.js +16 -19
  20. package/dialects/mysql/MySqlNativeQueryBuilder.d.ts +1 -1
  21. package/dialects/mysql/MySqlNativeQueryBuilder.js +13 -17
  22. package/dialects/mysql/MySqlPlatform.d.ts +10 -6
  23. package/dialects/mysql/MySqlPlatform.js +30 -20
  24. package/dialects/mysql/MySqlSchemaHelper.d.ts +5 -5
  25. package/dialects/mysql/MySqlSchemaHelper.js +8 -12
  26. package/dialects/mysql/index.d.ts +4 -4
  27. package/dialects/mysql/index.js +4 -20
  28. package/dialects/postgresql/PostgreSqlNativeQueryBuilder.d.ts +1 -1
  29. package/dialects/postgresql/PostgreSqlNativeQueryBuilder.js +2 -6
  30. package/dialects/postgresql/index.d.ts +1 -1
  31. package/dialects/postgresql/index.js +1 -17
  32. package/dialects/sqlite/BaseSqliteConnection.d.ts +2 -2
  33. package/dialects/sqlite/BaseSqliteConnection.js +14 -12
  34. package/dialects/sqlite/BaseSqlitePlatform.d.ts +4 -5
  35. package/dialects/sqlite/BaseSqlitePlatform.js +11 -19
  36. package/dialects/sqlite/SqliteExceptionConverter.js +16 -19
  37. package/dialects/sqlite/SqliteNativeQueryBuilder.d.ts +1 -1
  38. package/dialects/sqlite/SqliteNativeQueryBuilder.js +2 -6
  39. package/dialects/sqlite/SqliteSchemaHelper.d.ts +5 -5
  40. package/dialects/sqlite/SqliteSchemaHelper.js +22 -26
  41. package/dialects/sqlite/index.d.ts +5 -5
  42. package/dialects/sqlite/index.js +5 -21
  43. package/index.d.ts +11 -11
  44. package/index.js +13 -34
  45. package/package.json +7 -16
  46. package/query/ArrayCriteriaNode.d.ts +2 -2
  47. package/query/ArrayCriteriaNode.js +2 -6
  48. package/query/CriteriaNode.d.ts +1 -1
  49. package/query/CriteriaNode.js +25 -33
  50. package/query/CriteriaNodeFactory.d.ts +1 -1
  51. package/query/CriteriaNodeFactory.js +17 -21
  52. package/query/NativeQueryBuilder.d.ts +2 -2
  53. package/query/NativeQueryBuilder.js +33 -37
  54. package/query/ObjectCriteriaNode.d.ts +2 -2
  55. package/query/ObjectCriteriaNode.js +43 -43
  56. package/query/QueryBuilder.d.ts +17 -15
  57. package/query/QueryBuilder.js +204 -193
  58. package/query/QueryBuilderHelper.d.ts +4 -4
  59. package/query/QueryBuilderHelper.js +86 -96
  60. package/query/ScalarCriteriaNode.d.ts +2 -2
  61. package/query/ScalarCriteriaNode.js +12 -16
  62. package/query/enums.js +4 -7
  63. package/query/index.d.ts +9 -9
  64. package/query/index.js +9 -25
  65. package/schema/DatabaseSchema.d.ts +3 -3
  66. package/schema/DatabaseSchema.js +7 -11
  67. package/schema/DatabaseTable.d.ts +3 -3
  68. package/schema/DatabaseTable.js +44 -33
  69. package/schema/SchemaComparator.d.ts +4 -4
  70. package/schema/SchemaComparator.js +15 -19
  71. package/schema/SchemaHelper.d.ts +5 -5
  72. package/schema/SchemaHelper.js +22 -26
  73. package/schema/SqlSchemaGenerator.d.ts +4 -4
  74. package/schema/SqlSchemaGenerator.js +23 -36
  75. package/schema/index.d.ts +5 -5
  76. package/schema/index.js +5 -21
  77. package/typings.d.ts +7 -4
  78. package/typings.js +1 -2
  79. package/index.mjs +0 -232
package/query/index.js CHANGED
@@ -1,25 +1,9 @@
1
- "use strict";
2
- var __createBinding = (this && this.__createBinding) || (Object.create ? (function(o, m, k, k2) {
3
- if (k2 === undefined) k2 = k;
4
- var desc = Object.getOwnPropertyDescriptor(m, k);
5
- if (!desc || ("get" in desc ? !m.__esModule : desc.writable || desc.configurable)) {
6
- desc = { enumerable: true, get: function() { return m[k]; } };
7
- }
8
- Object.defineProperty(o, k2, desc);
9
- }) : (function(o, m, k, k2) {
10
- if (k2 === undefined) k2 = k;
11
- o[k2] = m[k];
12
- }));
13
- var __exportStar = (this && this.__exportStar) || function(m, exports) {
14
- for (var p in m) if (p !== "default" && !Object.prototype.hasOwnProperty.call(exports, p)) __createBinding(exports, m, p);
15
- };
16
- Object.defineProperty(exports, "__esModule", { value: true });
17
- __exportStar(require("./enums"), exports);
18
- __exportStar(require("./QueryBuilderHelper"), exports);
19
- __exportStar(require("./QueryBuilder"), exports);
20
- __exportStar(require("./CriteriaNode"), exports);
21
- __exportStar(require("./ArrayCriteriaNode"), exports);
22
- __exportStar(require("./ObjectCriteriaNode"), exports);
23
- __exportStar(require("./ScalarCriteriaNode"), exports);
24
- __exportStar(require("./CriteriaNodeFactory"), exports);
25
- __exportStar(require("./NativeQueryBuilder"), exports);
1
+ export * from './enums.js';
2
+ export * from './QueryBuilderHelper.js';
3
+ export * from './QueryBuilder.js';
4
+ export * from './CriteriaNode.js';
5
+ export * from './ArrayCriteriaNode.js';
6
+ export * from './ObjectCriteriaNode.js';
7
+ export * from './ScalarCriteriaNode.js';
8
+ export * from './CriteriaNodeFactory.js';
9
+ export * from './NativeQueryBuilder.js';
@@ -1,7 +1,7 @@
1
1
  import { type Configuration, type Dictionary, type EntityMetadata } from '@mikro-orm/core';
2
- import { DatabaseTable } from './DatabaseTable';
3
- import type { AbstractSqlConnection } from '../AbstractSqlConnection';
4
- import type { AbstractSqlPlatform } from '../AbstractSqlPlatform';
2
+ import { DatabaseTable } from './DatabaseTable.js';
3
+ import type { AbstractSqlConnection } from '../AbstractSqlConnection.js';
4
+ import type { AbstractSqlPlatform } from '../AbstractSqlPlatform.js';
5
5
  /**
6
6
  * @internal
7
7
  */
@@ -1,12 +1,9 @@
1
- "use strict";
2
- Object.defineProperty(exports, "__esModule", { value: true });
3
- exports.DatabaseSchema = void 0;
4
- const core_1 = require("@mikro-orm/core");
5
- const DatabaseTable_1 = require("./DatabaseTable");
1
+ import { ReferenceKind } from '@mikro-orm/core';
2
+ import { DatabaseTable } from './DatabaseTable.js';
6
3
  /**
7
4
  * @internal
8
5
  */
9
- class DatabaseSchema {
6
+ export class DatabaseSchema {
10
7
  platform;
11
8
  name;
12
9
  tables = [];
@@ -18,7 +15,7 @@ class DatabaseSchema {
18
15
  }
19
16
  addTable(name, schema, comment) {
20
17
  const namespaceName = schema ?? this.name;
21
- const table = new DatabaseTable_1.DatabaseTable(this.platform, name, namespaceName);
18
+ const table = new DatabaseTable(this.platform, name, namespaceName);
22
19
  table.nativeEnums = this.nativeEnums;
23
20
  table.comment = comment;
24
21
  this.tables.push(table);
@@ -136,15 +133,15 @@ class DatabaseSchema {
136
133
  if (prop.persist === false || (prop.columnTypes?.length ?? 0) === 0) {
137
134
  return false;
138
135
  }
139
- if (prop.kind === core_1.ReferenceKind.EMBEDDED && prop.object) {
136
+ if (prop.kind === ReferenceKind.EMBEDDED && prop.object) {
140
137
  return true;
141
138
  }
142
139
  const getRootProperty = (prop) => prop.embedded ? getRootProperty(meta.properties[prop.embedded[0]]) : prop;
143
140
  const rootProp = getRootProperty(prop);
144
- if (rootProp.kind === core_1.ReferenceKind.EMBEDDED) {
141
+ if (rootProp.kind === ReferenceKind.EMBEDDED) {
145
142
  return prop === rootProp || !rootProp.object;
146
143
  }
147
- return [core_1.ReferenceKind.SCALAR, core_1.ReferenceKind.MANY_TO_ONE].includes(prop.kind) || (prop.kind === core_1.ReferenceKind.ONE_TO_ONE && prop.owner);
144
+ return [ReferenceKind.SCALAR, ReferenceKind.MANY_TO_ONE].includes(prop.kind) || (prop.kind === ReferenceKind.ONE_TO_ONE && prop.owner);
148
145
  }
149
146
  toJSON() {
150
147
  const { platform, namespaces, ...rest } = this;
@@ -165,4 +162,3 @@ class DatabaseSchema {
165
162
  }
166
163
  }
167
164
  }
168
- exports.DatabaseSchema = DatabaseSchema;
@@ -1,7 +1,7 @@
1
1
  import { type Configuration, type DeferMode, type Dictionary, type EntityMetadata, type EntityProperty, type NamingStrategy } from '@mikro-orm/core';
2
- import type { SchemaHelper } from './SchemaHelper';
3
- import type { CheckDef, Column, ForeignKey, IndexDef } from '../typings';
4
- import type { AbstractSqlPlatform } from '../AbstractSqlPlatform';
2
+ import type { SchemaHelper } from './SchemaHelper.js';
3
+ import type { CheckDef, Column, ForeignKey, IndexDef } from '../typings.js';
4
+ import type { AbstractSqlPlatform } from '../AbstractSqlPlatform.js';
5
5
  /**
6
6
  * @internal
7
7
  */
@@ -1,11 +1,8 @@
1
- "use strict";
2
- Object.defineProperty(exports, "__esModule", { value: true });
3
- exports.DatabaseTable = void 0;
4
- const core_1 = require("@mikro-orm/core");
1
+ import { Cascade, DecimalType, EntitySchema, ReferenceKind, t, Type, UnknownType, Utils, } from '@mikro-orm/core';
5
2
  /**
6
3
  * @internal
7
4
  */
8
- class DatabaseTable {
5
+ export class DatabaseTable {
9
6
  platform;
10
7
  name;
11
8
  schema;
@@ -36,7 +33,7 @@ class DatabaseTable {
36
33
  delete this.columns[name];
37
34
  }
38
35
  getIndexes() {
39
- return core_1.Utils.removeDuplicates(this.indexes);
36
+ return Utils.removeDuplicates(this.indexes);
40
37
  }
41
38
  getChecks() {
42
39
  return this.checks;
@@ -64,9 +61,9 @@ class DatabaseTable {
64
61
  prop.fieldNames?.forEach((field, idx) => {
65
62
  const type = prop.enum ? 'enum' : prop.columnTypes[idx];
66
63
  const mappedType = this.platform.getMappedType(type);
67
- if (mappedType instanceof core_1.DecimalType) {
64
+ if (mappedType instanceof DecimalType) {
68
65
  const match = prop.columnTypes[idx].match(/\w+\((\d+), ?(\d+)\)/);
69
- /* istanbul ignore else */
66
+ /* v8 ignore next 5 */
70
67
  if (match) {
71
68
  prop.precision ??= +match[1];
72
69
  prop.scale ??= +match[2];
@@ -86,7 +83,7 @@ class DatabaseTable {
86
83
  generated: prop.generated,
87
84
  mappedType,
88
85
  unsigned: prop.unsigned && this.platform.isNumericColumn(mappedType),
89
- autoincrement: prop.autoincrement ?? (primary && prop.kind === core_1.ReferenceKind.SCALAR && this.platform.isNumericColumn(mappedType)),
86
+ autoincrement: prop.autoincrement ?? (primary && prop.kind === ReferenceKind.SCALAR && this.platform.isNumericColumn(mappedType)),
90
87
  primary,
91
88
  nullable: this.columns[field]?.nullable ?? !!prop.nullable,
92
89
  nativeEnumName: prop.nativeEnumName,
@@ -94,16 +91,19 @@ class DatabaseTable {
94
91
  precision: prop.precision,
95
92
  scale: prop.scale,
96
93
  default: prop.defaultRaw,
97
- enumItems: prop.nativeEnumName || prop.items?.every(core_1.Utils.isString) ? prop.items : undefined,
94
+ enumItems: prop.nativeEnumName || prop.items?.every(Utils.isString) ? prop.items : undefined,
98
95
  comment: prop.comment,
99
96
  extra: prop.extra,
100
97
  ignoreSchemaChanges: prop.ignoreSchemaChanges,
101
98
  };
102
99
  this.columns[field].unsigned ??= this.columns[field].autoincrement;
100
+ if (this.nativeEnums[type]) {
101
+ this.columns[field].enumItems ??= this.nativeEnums[type].items;
102
+ }
103
103
  const defaultValue = this.platform.getSchemaHelper().normalizeDefaultValue(prop.defaultRaw, prop.length);
104
104
  this.columns[field].default = defaultValue;
105
105
  });
106
- if ([core_1.ReferenceKind.MANY_TO_ONE, core_1.ReferenceKind.ONE_TO_ONE].includes(prop.kind)) {
106
+ if ([ReferenceKind.MANY_TO_ONE, ReferenceKind.ONE_TO_ONE].includes(prop.kind)) {
107
107
  const constraintName = this.getIndexName(true, prop.fieldNames, 'foreign');
108
108
  let schema = prop.targetMeta.root.schema === '*' ? this.schema : (prop.targetMeta.root.schema ?? config.get('schema', this.platform.getDefaultSchemaName()));
109
109
  if (prop.referencedTableName.includes('.')) {
@@ -116,14 +116,14 @@ class DatabaseTable {
116
116
  referencedColumnNames: prop.referencedColumnNames,
117
117
  referencedTableName: schema ? `${schema}.${prop.referencedTableName}` : prop.referencedTableName,
118
118
  };
119
- const cascade = prop.cascade.includes(core_1.Cascade.REMOVE) || prop.cascade.includes(core_1.Cascade.ALL);
119
+ const cascade = prop.cascade.includes(Cascade.REMOVE) || prop.cascade.includes(Cascade.ALL);
120
120
  if (prop.deleteRule || cascade || prop.nullable) {
121
121
  this.foreignKeys[constraintName].deleteRule = prop.deleteRule || (cascade ? 'cascade' : 'set null');
122
122
  }
123
123
  if (prop.updateRule) {
124
124
  this.foreignKeys[constraintName].updateRule = prop.updateRule || 'cascade';
125
125
  }
126
- if ((prop.cascade.includes(core_1.Cascade.PERSIST) || prop.cascade.includes(core_1.Cascade.ALL))) {
126
+ if ((prop.cascade.includes(Cascade.PERSIST) || prop.cascade.includes(Cascade.ALL))) {
127
127
  const hasCascadePath = Object.values(this.foreignKeys).some(fk => {
128
128
  return fk.constraintName !== constraintName
129
129
  && ((fk.updateRule && fk.updateRule !== 'no action') || (fk.deleteRule && fk.deleteRule !== 'no action'))
@@ -160,7 +160,7 @@ class DatabaseTable {
160
160
  }
161
161
  }
162
162
  getIndexName(value, columnNames, type) {
163
- if (core_1.Utils.isString(value)) {
163
+ if (Utils.isString(value)) {
164
164
  return value;
165
165
  }
166
166
  return this.platform.getIndexName(this.name, columnNames, type);
@@ -168,14 +168,14 @@ class DatabaseTable {
168
168
  getEntityDeclaration(namingStrategy, schemaHelper, scalarPropertiesForRelations) {
169
169
  const { fksOnColumnProps, fksOnStandaloneProps, columnFks, fkIndexes, nullableForeignKeys, skippedColumnNames, } = this.foreignKeysToProps(namingStrategy, scalarPropertiesForRelations);
170
170
  const name = namingStrategy.getEntityName(this.name, this.schema);
171
- const schema = new core_1.EntitySchema({ name, collection: this.name, schema: this.schema, comment: this.comment });
171
+ const schema = new EntitySchema({ name, collection: this.name, schema: this.schema, comment: this.comment });
172
172
  const compositeFkIndexes = {};
173
173
  const compositeFkUniques = {};
174
174
  const potentiallyUnmappedIndexes = this.indexes.filter(index => !index.primary // Skip primary index. Whether it's in use by scalar column or FK, it's already mapped.
175
- && (index.columnNames.length > 1 // All composite indexes are to be mapped to entity decorators or FK props.
175
+ && (( // Non-trivial non-composite indexes will be declared at the entity's metadata, though later outputted in the property
176
+ index.columnNames.length > 1 // All composite indexes are to be mapped to entity decorators or FK props.
176
177
  || skippedColumnNames.includes(index.columnNames[0]) // Non-composite indexes for skipped columns are to be mapped as entity decorators.
177
- || index.deferMode || index.expression // Non-trivial non-composite indexes will be declared at the entity's metadata, though later outputted in the property
178
- || !(index.columnNames[0] in columnFks) // Trivial non-composite indexes for scalar props are to be mapped to the column.
178
+ || index.deferMode || index.expression || !(index.columnNames[0] in columnFks)) // Trivial non-composite indexes for scalar props are to be mapped to the column.
179
179
  )
180
180
  // ignore indexes that don't have all column names (this can happen in sqlite where there is no way to infer this for expressions)
181
181
  && !(index.columnNames.some(col => !col) && !index.expression));
@@ -245,10 +245,10 @@ class DatabaseTable {
245
245
  }
246
246
  const meta = schema.init().meta;
247
247
  const oneToOneCandidateProperties = meta.relations
248
- .filter(prop => prop.primary && prop.kind === core_1.ReferenceKind.MANY_TO_ONE);
248
+ .filter(prop => prop.primary && prop.kind === ReferenceKind.MANY_TO_ONE);
249
249
  if (oneToOneCandidateProperties.length === 1
250
250
  && oneToOneCandidateProperties[0].fieldNames.length === (new Set(meta.getPrimaryProps().flatMap(prop => prop.fieldNames))).size) {
251
- oneToOneCandidateProperties[0].kind = core_1.ReferenceKind.ONE_TO_ONE;
251
+ oneToOneCandidateProperties[0].kind = ReferenceKind.ONE_TO_ONE;
252
252
  }
253
253
  return meta;
254
254
  }
@@ -595,9 +595,9 @@ class DatabaseTable {
595
595
  const unique = compositeFkUniques[prop] || this.indexes.find(idx => idx.columnNames[0] === column.name && !idx.composite && idx.unique && !idx.primary);
596
596
  const kind = this.getReferenceKind(fk, unique);
597
597
  const runtimeType = this.getPropertyTypeForColumn(namingStrategy, column, fk);
598
- const type = fk ? runtimeType : (core_1.Utils.keys(core_1.t).find(k => {
598
+ const type = fk ? runtimeType : (Utils.keys(t).find(k => {
599
599
  const typeInCoreMap = this.platform.getMappedType(k);
600
- return (typeInCoreMap !== core_1.Type.getType(core_1.UnknownType) || k === 'unknown') && typeInCoreMap === column.mappedType;
600
+ return (typeInCoreMap !== Type.getType(UnknownType) || k === 'unknown') && typeInCoreMap === column.mappedType;
601
601
  }) ?? runtimeType);
602
602
  const ignoreSchemaChanges = (type === 'unknown' && column.length) ? (column.extra ? ['type', 'extra'] : ['type']) : undefined;
603
603
  const defaultRaw = this.getPropertyDefaultValue(schemaHelper, column, runtimeType, true);
@@ -644,12 +644,12 @@ class DatabaseTable {
644
644
  }
645
645
  getReferenceKind(fk, unique) {
646
646
  if (fk && unique) {
647
- return core_1.ReferenceKind.ONE_TO_ONE;
647
+ return ReferenceKind.ONE_TO_ONE;
648
648
  }
649
649
  if (fk) {
650
- return core_1.ReferenceKind.MANY_TO_ONE;
650
+ return ReferenceKind.MANY_TO_ONE;
651
651
  }
652
- return core_1.ReferenceKind.SCALAR;
652
+ return ReferenceKind.SCALAR;
653
653
  }
654
654
  getPropertyName(namingStrategy, baseName, fk) {
655
655
  let field = baseName;
@@ -701,19 +701,31 @@ class DatabaseTable {
701
701
  return '' + val;
702
702
  }
703
703
  addIndex(meta, index, type) {
704
- const properties = core_1.Utils.unique(core_1.Utils.flatten(core_1.Utils.asArray(index.properties).map(prop => {
705
- const root = prop.replace(/\..+$/, '');
704
+ const properties = Utils.unique(Utils.flatten(Utils.asArray(index.properties).map(prop => {
705
+ const parts = prop.split('.');
706
+ const root = parts[0];
706
707
  if (meta.properties[prop]) {
707
708
  if (meta.properties[prop].embeddedPath) {
708
709
  return [meta.properties[prop].embeddedPath.join('.')];
709
710
  }
710
711
  return meta.properties[prop].fieldNames;
711
712
  }
713
+ const rootProp = meta.properties[root];
714
+ // inline embedded property index, we need to find the field name of the child property
715
+ if (rootProp?.embeddable && !rootProp.object && parts.length > 1) {
716
+ const expand = (p, i) => {
717
+ if (parts.length === i) {
718
+ return p.fieldNames[0];
719
+ }
720
+ return expand(p.embeddedProps[parts[i]], i + 1);
721
+ };
722
+ return [expand(rootProp, 1)];
723
+ }
712
724
  // json index, we need to rename the column only
713
- if (meta.properties[root]) {
714
- return [prop.replace(root, meta.properties[root].fieldNames[0])];
725
+ if (rootProp) {
726
+ return [prop.replace(root, rootProp.fieldNames[0])];
715
727
  }
716
- /* istanbul ignore next */
728
+ /* v8 ignore next */
717
729
  return [prop];
718
730
  })));
719
731
  if (properties.length === 0 && !index.expression) {
@@ -739,13 +751,12 @@ class DatabaseTable {
739
751
  }
740
752
  toJSON() {
741
753
  const { platform, columns, ...rest } = this;
742
- const columnsMapped = core_1.Utils.keys(columns).reduce((o, col) => {
754
+ const columnsMapped = Utils.keys(columns).reduce((o, col) => {
743
755
  const { mappedType, ...restCol } = columns[col];
744
756
  o[col] = restCol;
745
- o[col].mappedType = core_1.Utils.keys(core_1.t).find(k => core_1.t[k] === mappedType.constructor);
757
+ o[col].mappedType = Utils.keys(t).find(k => t[k] === mappedType.constructor);
746
758
  return o;
747
759
  }, {});
748
760
  return { columns: columnsMapped, ...rest };
749
761
  }
750
762
  }
751
- exports.DatabaseTable = DatabaseTable;
@@ -1,8 +1,8 @@
1
1
  import { type Dictionary } from '@mikro-orm/core';
2
- import type { Column, ForeignKey, IndexDef, SchemaDifference, TableDifference } from '../typings';
3
- import type { DatabaseSchema } from './DatabaseSchema';
4
- import type { DatabaseTable } from './DatabaseTable';
5
- import type { AbstractSqlPlatform } from '../AbstractSqlPlatform';
2
+ import type { Column, ForeignKey, IndexDef, SchemaDifference, TableDifference } from '../typings.js';
3
+ import type { DatabaseSchema } from './DatabaseSchema.js';
4
+ import type { DatabaseTable } from './DatabaseTable.js';
5
+ import type { AbstractSqlPlatform } from '../AbstractSqlPlatform.js';
6
6
  /**
7
7
  * Compares two Schemas and return an instance of SchemaDifference.
8
8
  */
@@ -1,12 +1,9 @@
1
- "use strict";
2
- Object.defineProperty(exports, "__esModule", { value: true });
3
- exports.SchemaComparator = void 0;
4
- const node_util_1 = require("node:util");
5
- const core_1 = require("@mikro-orm/core");
1
+ import { inspect } from 'node:util';
2
+ import { ArrayType, BooleanType, DateTimeType, JsonType, parseJsonSafe, Utils, } from '@mikro-orm/core';
6
3
  /**
7
4
  * Compares two Schemas and return an instance of SchemaDifference.
8
5
  */
9
- class SchemaComparator {
6
+ export class SchemaComparator {
10
7
  platform;
11
8
  helper;
12
9
  logger;
@@ -311,7 +308,7 @@ class SchemaComparator {
311
308
  const [removedColumn, addedColumn] = candidateColumns[0];
312
309
  const removedColumnName = removedColumn.name;
313
310
  const addedColumnName = addedColumn.name;
314
- /* istanbul ignore if */
311
+ /* v8 ignore next 3 */
315
312
  if (tableDifferences.renamedColumns[removedColumnName]) {
316
313
  continue;
317
314
  }
@@ -399,8 +396,8 @@ class SchemaComparator {
399
396
  let toColumnType = this.platform.normalizeColumnType(toColumn.mappedType.getColumnType(toProp, this.platform).toLowerCase(), toProp);
400
397
  const log = (msg, params) => {
401
398
  if (logging) {
402
- const copy = core_1.Utils.copy(params);
403
- core_1.Utils.dropUndefinedProperties(copy);
399
+ const copy = Utils.copy(params);
400
+ Utils.dropUndefinedProperties(copy);
404
401
  this.log(msg, copy);
405
402
  }
406
403
  };
@@ -441,8 +438,8 @@ class SchemaComparator {
441
438
  log(`'comment' changed for column ${fromTable.name}.${fromColumn.name}`, { fromColumn, toColumn });
442
439
  changedProperties.add('comment');
443
440
  }
444
- if (!(fromColumn.mappedType instanceof core_1.ArrayType) &&
445
- !(toColumn.mappedType instanceof core_1.ArrayType) &&
441
+ if (!(fromColumn.mappedType instanceof ArrayType) &&
442
+ !(toColumn.mappedType instanceof ArrayType) &&
446
443
  this.diffEnumItems(fromColumn.enumItems, toColumn.enumItems)) {
447
444
  log(`'enumItems' changed for column ${fromTable.name}.${fromColumn.name}`, { fromColumn, toColumn });
448
445
  changedProperties.add('enumItems');
@@ -521,30 +518,30 @@ class SchemaComparator {
521
518
  return simplify(expr1) !== simplify(expr2);
522
519
  }
523
520
  parseJsonDefault(defaultValue) {
524
- /* istanbul ignore next */
521
+ /* v8 ignore next 3 */
525
522
  if (!defaultValue) {
526
523
  return null;
527
524
  }
528
525
  const val = defaultValue
529
526
  .replace(/^(_\w+\\)?'(.*?)\\?'$/, '$2')
530
527
  .replace(/^\(?'(.*?)'\)?$/, '$1');
531
- return (0, core_1.parseJsonSafe)(val);
528
+ return parseJsonSafe(val);
532
529
  }
533
530
  hasSameDefaultValue(from, to) {
534
531
  if (from.default == null || from.default.toString().toLowerCase() === 'null' || from.default.toString().startsWith('nextval(')) {
535
532
  return to.default == null || to.default.toLowerCase() === 'null';
536
533
  }
537
- if (to.mappedType instanceof core_1.BooleanType) {
534
+ if (to.mappedType instanceof BooleanType) {
538
535
  const defaultValueFrom = !['0', 'false', 'f', 'n', 'no', 'off'].includes('' + from.default);
539
536
  const defaultValueTo = !['0', 'false', 'f', 'n', 'no', 'off'].includes('' + to.default);
540
537
  return defaultValueFrom === defaultValueTo;
541
538
  }
542
- if (to.mappedType instanceof core_1.JsonType) {
539
+ if (to.mappedType instanceof JsonType) {
543
540
  const defaultValueFrom = this.parseJsonDefault(from.default);
544
541
  const defaultValueTo = this.parseJsonDefault(to.default);
545
- return core_1.Utils.equals(defaultValueFrom, defaultValueTo);
542
+ return Utils.equals(defaultValueFrom, defaultValueTo);
546
543
  }
547
- if (to.mappedType instanceof core_1.DateTimeType && from.default && to.default) {
544
+ if (to.mappedType instanceof DateTimeType && from.default && to.default) {
548
545
  // normalize now/current_timestamp defaults, also remove `()` from the end of default expression
549
546
  const defaultValueFrom = from.default.toLowerCase().replace('current_timestamp', 'now').replace(/\(\)$/, '');
550
547
  const defaultValueTo = to.default.toLowerCase().replace('current_timestamp', 'now').replace(/\(\)$/, '');
@@ -574,9 +571,8 @@ class SchemaComparator {
574
571
  }
575
572
  log(message, params) {
576
573
  if (params) {
577
- message += ' ' + (0, node_util_1.inspect)(params);
574
+ message += ' ' + inspect(params);
578
575
  }
579
576
  this.logger.log('schema', message);
580
577
  }
581
578
  }
582
- exports.SchemaComparator = SchemaComparator;
@@ -1,9 +1,9 @@
1
1
  import { type Connection, type Dictionary } from '@mikro-orm/core';
2
- import type { AbstractSqlConnection } from '../AbstractSqlConnection';
3
- import type { AbstractSqlPlatform } from '../AbstractSqlPlatform';
4
- import type { CheckDef, Column, ForeignKey, IndexDef, Table, TableDifference } from '../typings';
5
- import type { DatabaseSchema } from './DatabaseSchema';
6
- import type { DatabaseTable } from './DatabaseTable';
2
+ import type { AbstractSqlConnection } from '../AbstractSqlConnection.js';
3
+ import type { AbstractSqlPlatform } from '../AbstractSqlPlatform.js';
4
+ import type { CheckDef, Column, ForeignKey, IndexDef, Table, TableDifference } from '../typings.js';
5
+ import type { DatabaseSchema } from './DatabaseSchema.js';
6
+ import type { DatabaseTable } from './DatabaseTable.js';
7
7
  export declare abstract class SchemaHelper {
8
8
  protected readonly platform: AbstractSqlPlatform;
9
9
  constructor(platform: AbstractSqlPlatform);
@@ -1,8 +1,5 @@
1
- "use strict";
2
- Object.defineProperty(exports, "__esModule", { value: true });
3
- exports.SchemaHelper = void 0;
4
- const core_1 = require("@mikro-orm/core");
5
- class SchemaHelper {
1
+ import { RawQueryFragment, Utils } from '@mikro-orm/core';
2
+ export class SchemaHelper {
6
3
  platform;
7
4
  constructor(platform) {
8
5
  this.platform = platform;
@@ -36,7 +33,7 @@ class SchemaHelper {
36
33
  }
37
34
  async getPrimaryKeys(connection, indexes = [], tableName, schemaName) {
38
35
  const pks = indexes.filter(i => i.primary).map(pk => pk.columnNames);
39
- return core_1.Utils.flatten(pks);
36
+ return Utils.flatten(pks);
40
37
  }
41
38
  inferLengthFromColumnType(type) {
42
39
  const match = type.match(/^\w+\s*(?:\(\s*(\d+)\s*\)|$)/);
@@ -77,7 +74,7 @@ class SchemaHelper {
77
74
  return `alter table ${tableReference} rename column ${oldColumnName} to ${columnName}`;
78
75
  }
79
76
  getCreateIndexSQL(tableName, index) {
80
- /* istanbul ignore next */
77
+ /* v8 ignore next 3 */
81
78
  if (index.expression) {
82
79
  return index.expression;
83
80
  }
@@ -121,7 +118,7 @@ class SchemaHelper {
121
118
  changedNativeEnums.push([column.nativeEnumName, column.enumItems, diff.fromTable.nativeEnums[key].items]);
122
119
  }
123
120
  }
124
- core_1.Utils.removeDuplicates(changedNativeEnums).forEach(([enumName, itemsNew, itemsOld]) => {
121
+ Utils.removeDuplicates(changedNativeEnums).forEach(([enumName, itemsNew, itemsOld]) => {
125
122
  // postgres allows only adding new items, the values are case insensitive
126
123
  itemsOld = itemsOld.map(v => v.toLowerCase());
127
124
  const newItems = itemsNew.filter(val => !itemsOld.includes(val.toLowerCase()));
@@ -145,7 +142,7 @@ class SchemaHelper {
145
142
  for (const check of Object.values(diff.changedChecks)) {
146
143
  ret.push(this.dropConstraint(diff.name, check.name));
147
144
  }
148
- /* istanbul ignore else */
145
+ /* v8 ignore next 3 */
149
146
  if (!safe && Object.values(diff.removedColumns).length > 0) {
150
147
  ret.push(this.getDropColumnsSQL(tableName, Object.values(diff.removedColumns), schemaName));
151
148
  }
@@ -228,7 +225,7 @@ class SchemaHelper {
228
225
  const defaultName = this.platform.getDefaultPrimaryName(table.name, pkIndex.columnNames);
229
226
  return pkIndex?.keyName !== defaultName;
230
227
  }
231
- /* istanbul ignore next */
228
+ /* v8 ignore next 3 */
232
229
  castColumn(name, type) {
233
230
  return '';
234
231
  }
@@ -259,17 +256,17 @@ class SchemaHelper {
259
256
  const columnType = column.type + (column.generated ? ` generated always as ${column.generated}` : '');
260
257
  const useDefault = column.default != null && column.default !== 'null' && !column.autoincrement;
261
258
  const col = [this.quote(column.name), columnType];
262
- core_1.Utils.runIfNotEmpty(() => col.push('unsigned'), column.unsigned && this.platform.supportsUnsigned());
263
- core_1.Utils.runIfNotEmpty(() => col.push('null'), column.nullable);
264
- core_1.Utils.runIfNotEmpty(() => col.push('not null'), !column.nullable && !column.generated);
265
- core_1.Utils.runIfNotEmpty(() => col.push('auto_increment'), column.autoincrement);
266
- core_1.Utils.runIfNotEmpty(() => col.push('unique'), column.autoincrement && !column.primary);
259
+ Utils.runIfNotEmpty(() => col.push('unsigned'), column.unsigned && this.platform.supportsUnsigned());
260
+ Utils.runIfNotEmpty(() => col.push('null'), column.nullable);
261
+ Utils.runIfNotEmpty(() => col.push('not null'), !column.nullable && !column.generated);
262
+ Utils.runIfNotEmpty(() => col.push('auto_increment'), column.autoincrement);
263
+ Utils.runIfNotEmpty(() => col.push('unique'), column.autoincrement && !column.primary);
267
264
  if (column.autoincrement && !column.generated && !compositePK && (!changedProperties || changedProperties.has('autoincrement') || changedProperties.has('type'))) {
268
- core_1.Utils.runIfNotEmpty(() => col.push('primary key'), primaryKey && column.primary);
265
+ Utils.runIfNotEmpty(() => col.push('primary key'), primaryKey && column.primary);
269
266
  }
270
- core_1.Utils.runIfNotEmpty(() => col.push(`default ${column.default}`), useDefault);
271
- core_1.Utils.runIfNotEmpty(() => col.push(column.extra), column.extra);
272
- core_1.Utils.runIfNotEmpty(() => col.push(`comment ${this.platform.quoteValue(column.comment)}`), column.comment);
267
+ Utils.runIfNotEmpty(() => col.push(`default ${column.default}`), useDefault);
268
+ Utils.runIfNotEmpty(() => col.push(column.extra), column.extra);
269
+ Utils.runIfNotEmpty(() => col.push(`comment ${this.platform.quoteValue(column.comment)}`), column.comment);
273
270
  return col.join(' ');
274
271
  }
275
272
  getPreAlterTable(tableDiff, safe) {
@@ -322,7 +319,7 @@ class SchemaHelper {
322
319
  if (defaultValue == null) {
323
320
  return defaultValue;
324
321
  }
325
- const raw = core_1.RawQueryFragment.getKnownFragment(defaultValue);
322
+ const raw = RawQueryFragment.getKnownFragment(defaultValue);
326
323
  if (raw) {
327
324
  return this.platform.formatQuery(raw.sql, raw.params);
328
325
  }
@@ -340,11 +337,11 @@ class SchemaHelper {
340
337
  getDropDatabaseSQL(name) {
341
338
  return `drop database if exists ${this.quote(name)}`;
342
339
  }
343
- /* istanbul ignore next */
340
+ /* v8 ignore next 3 */
344
341
  getCreateNamespaceSQL(name) {
345
342
  return `create schema if not exists ${this.quote(name)}`;
346
343
  }
347
- /* istanbul ignore next */
344
+ /* v8 ignore next 3 */
348
345
  getDropNamespaceSQL(name) {
349
346
  return `drop schema if exists ${this.quote(name)}`;
350
347
  }
@@ -369,13 +366,13 @@ class SchemaHelper {
369
366
  if (e instanceof Error && e.message.includes(this.getDatabaseNotExistsError(name))) {
370
367
  return false;
371
368
  }
372
- /* istanbul ignore next */
369
+ /* v8 ignore next */
373
370
  throw e;
374
371
  }
375
372
  }
376
373
  append(array, sql, pad = false) {
377
374
  const length = array.length;
378
- for (const row of core_1.Utils.asArray(sql)) {
375
+ for (const row of Utils.asArray(sql)) {
379
376
  if (!row) {
380
377
  continue;
381
378
  }
@@ -466,7 +463,7 @@ class SchemaHelper {
466
463
  getReferencedTableName(referencedTableName, schema) {
467
464
  const [schemaName, tableName] = this.splitTableName(referencedTableName);
468
465
  schema = schemaName ?? schema ?? this.platform.getConfig().get('schema');
469
- /* istanbul ignore next */
466
+ /* v8 ignore next 3 */
470
467
  if (schema && schemaName === '*') {
471
468
  return `${schema}.${referencedTableName.replace(/^\*\./, '')}`;
472
469
  }
@@ -542,4 +539,3 @@ class SchemaHelper {
542
539
  return sql;
543
540
  }
544
541
  }
545
- exports.SchemaHelper = SchemaHelper;
@@ -1,9 +1,9 @@
1
1
  import { AbstractSchemaGenerator, type ClearDatabaseOptions, type CreateSchemaOptions, type DropSchemaOptions, type EnsureDatabaseOptions, type ISchemaGenerator, type MikroORM, type Transaction, type UpdateSchemaOptions } from '@mikro-orm/core';
2
- import type { SchemaDifference } from '../typings';
3
- import { DatabaseSchema } from './DatabaseSchema';
4
- import type { AbstractSqlDriver } from '../AbstractSqlDriver';
2
+ import type { SchemaDifference } from '../typings.js';
3
+ import { DatabaseSchema } from './DatabaseSchema.js';
4
+ import type { AbstractSqlDriver } from '../AbstractSqlDriver.js';
5
5
  export declare class SqlSchemaGenerator extends AbstractSchemaGenerator<AbstractSqlDriver> implements ISchemaGenerator {
6
- protected readonly helper: import("./SchemaHelper").SchemaHelper;
6
+ protected readonly helper: import("./SchemaHelper.js").SchemaHelper;
7
7
  protected readonly options: {
8
8
  disableForeignKeys?: boolean;
9
9
  createForeignKeyConstraints?: boolean;