@mikro-orm/knex 6.1.10-dev.2 → 6.1.10-dev.3

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.
package/package.json CHANGED
@@ -1,6 +1,6 @@
1
1
  {
2
2
  "name": "@mikro-orm/knex",
3
- "version": "6.1.10-dev.2",
3
+ "version": "6.1.10-dev.3",
4
4
  "description": "TypeScript ORM for Node.js based on Data Mapper, Unit of Work and Identity Map patterns. Supports MongoDB, MySQL, PostgreSQL and SQLite databases as well as usage with vanilla JavaScript.",
5
5
  "main": "index.js",
6
6
  "module": "index.mjs",
@@ -66,6 +66,6 @@
66
66
  "@mikro-orm/core": "^6.1.9"
67
67
  },
68
68
  "peerDependencies": {
69
- "@mikro-orm/core": "6.1.10-dev.2"
69
+ "@mikro-orm/core": "6.1.10-dev.3"
70
70
  }
71
71
  }
@@ -16,9 +16,23 @@ export declare class DatabaseSchema {
16
16
  getTables(): DatabaseTable[];
17
17
  getTable(name: string): DatabaseTable | undefined;
18
18
  hasTable(name: string): boolean;
19
- setNativeEnums(nativeEnums: Dictionary<unknown[]>): void;
20
- getNativeEnums(): Dictionary<unknown[]>;
19
+ setNativeEnums(nativeEnums: Dictionary<{
20
+ name: string;
21
+ schema?: string;
22
+ items: string[];
23
+ }>): void;
24
+ getNativeEnums(): Dictionary<{
25
+ name: string;
26
+ schema?: string;
27
+ items: string[];
28
+ }>;
29
+ getNativeEnum(name: string): {
30
+ name: string;
31
+ schema?: string;
32
+ items: string[];
33
+ };
21
34
  hasNamespace(namespace: string): boolean;
35
+ hasNativeEnum(name: string): boolean;
22
36
  getNamespaces(): string[];
23
37
  static create(connection: AbstractSqlConnection, platform: AbstractSqlPlatform, config: Configuration, schemaName?: string, schemas?: string[]): Promise<DatabaseSchema>;
24
38
  static fromMetadata(metadata: EntityMetadata[], platform: AbstractSqlPlatform, config: Configuration, schemaName?: string): DatabaseSchema;
@@ -43,9 +43,15 @@ class DatabaseSchema {
43
43
  getNativeEnums() {
44
44
  return this.nativeEnums;
45
45
  }
46
+ getNativeEnum(name) {
47
+ return this.nativeEnums[name];
48
+ }
46
49
  hasNamespace(namespace) {
47
50
  return this.namespaces.has(namespace);
48
51
  }
52
+ hasNativeEnum(name) {
53
+ return name in this.nativeEnums;
54
+ }
49
55
  getNamespaces() {
50
56
  return [...this.namespaces];
51
57
  }
@@ -63,9 +69,20 @@ class DatabaseSchema {
63
69
  const schema = new DatabaseSchema(platform, schemaName ?? config.get('schema'));
64
70
  const nativeEnums = {};
65
71
  for (const meta of metadata) {
66
- meta.props
67
- .filter(prop => prop.nativeEnumName)
68
- .forEach(prop => nativeEnums[prop.nativeEnumName] = prop.items?.map(val => '' + val) ?? []);
72
+ for (const prop of meta.props) {
73
+ if (prop.nativeEnumName) {
74
+ let key = prop.nativeEnumName;
75
+ const s = meta.schema ?? schema.name;
76
+ if (s && s !== platform.getDefaultSchemaName()) {
77
+ key = s + '.' + key;
78
+ }
79
+ nativeEnums[key] = {
80
+ name: prop.nativeEnumName,
81
+ schema: meta.schema ?? schema.name,
82
+ items: prop.items?.map(val => '' + val) ?? [],
83
+ };
84
+ }
85
+ }
69
86
  }
70
87
  schema.setNativeEnums(nativeEnums);
71
88
  for (const meta of metadata) {
@@ -13,7 +13,11 @@ export declare class DatabaseTable {
13
13
  private indexes;
14
14
  private checks;
15
15
  private foreignKeys;
16
- nativeEnums: Dictionary<unknown[]>;
16
+ nativeEnums: Dictionary<{
17
+ name: string;
18
+ schema?: string;
19
+ items: string[];
20
+ }>;
17
21
  comment?: string;
18
22
  constructor(platform: AbstractSqlPlatform, name: string, schema?: string | undefined);
19
23
  getColumns(): Column[];
@@ -23,7 +23,17 @@ class SchemaComparator {
23
23
  * stored in toSchema.
24
24
  */
25
25
  compare(fromSchema, toSchema, inverseDiff) {
26
- const diff = { newTables: {}, removedTables: {}, changedTables: {}, orphanedForeignKeys: [], newNamespaces: new Set(), removedNamespaces: new Set(), fromSchema };
26
+ const diff = {
27
+ newTables: {},
28
+ removedTables: {},
29
+ changedTables: {},
30
+ orphanedForeignKeys: [],
31
+ newNativeEnums: [],
32
+ removedNativeEnums: [],
33
+ newNamespaces: new Set(),
34
+ removedNamespaces: new Set(),
35
+ fromSchema,
36
+ };
27
37
  const foreignKeysToTable = {};
28
38
  for (const namespace of toSchema.getNamespaces()) {
29
39
  if (fromSchema.hasNamespace(namespace) || namespace === this.platform.getDefaultSchemaName()) {
@@ -37,6 +47,18 @@ class SchemaComparator {
37
47
  }
38
48
  diff.removedNamespaces.add(namespace);
39
49
  }
50
+ for (const nativeEnum of Object.keys(toSchema.getNativeEnums())) {
51
+ if (fromSchema.hasNativeEnum(nativeEnum)) {
52
+ continue;
53
+ }
54
+ diff.newNativeEnums.push(toSchema.getNativeEnum(nativeEnum));
55
+ }
56
+ for (const nativeEnum of Object.keys(fromSchema.getNativeEnums())) {
57
+ if (toSchema.hasNativeEnum(nativeEnum)) {
58
+ continue;
59
+ }
60
+ diff.removedNativeEnums.push(fromSchema.getNativeEnum(nativeEnum));
61
+ }
40
62
  for (const table of toSchema.getTables()) {
41
63
  const tableName = table.getShortestName();
42
64
  if (!fromSchema.hasTable(tableName)) {
@@ -68,7 +68,7 @@ class SqlSchemaGenerator extends core_1.AbstractSchemaGenerator {
68
68
  continue;
69
69
  }
70
70
  created.push(enumName);
71
- const sql = this.helper.getCreateNativeEnumSQL(enumName, enumOptions, options.schema ?? this.config.get('schema'));
71
+ const sql = this.helper.getCreateNativeEnumSQL(enumName, enumOptions.items, options.schema ?? this.config.get('schema'));
72
72
  ret += await this.dump(this.knex.schema.raw(sql), '\n');
73
73
  }
74
74
  }
@@ -180,7 +180,6 @@ class SqlSchemaGenerator extends core_1.AbstractSchemaGenerator {
180
180
  const wildcardSchemaTables = Object.values(this.metadata.getAll()).filter(meta => meta.schema === '*').map(meta => meta.tableName);
181
181
  fromSchema.prune(options.schema, wildcardSchemaTables);
182
182
  toSchema.prune(options.schema, wildcardSchemaTables);
183
- toSchema.setNativeEnums(fromSchema.getNativeEnums());
184
183
  return { fromSchema, toSchema };
185
184
  }
186
185
  async diffToSQL(schemaDiff, options) {
@@ -191,6 +190,12 @@ class SqlSchemaGenerator extends core_1.AbstractSchemaGenerator {
191
190
  ret += await this.dump(this.knex.schema.createSchemaIfNotExists(newNamespace));
192
191
  }
193
192
  }
193
+ if (this.platform.supportsNativeEnums()) {
194
+ for (const newNativeEnum of schemaDiff.newNativeEnums) {
195
+ const sql = this.helper.getCreateNativeEnumSQL(newNativeEnum.name, newNativeEnum.items, newNativeEnum.schema ?? options.schema ?? this.config.get('schema'));
196
+ ret += await this.dump(this.knex.schema.raw(sql), '\n');
197
+ }
198
+ }
194
199
  if (!options.safe) {
195
200
  for (const orphanedForeignKey of schemaDiff.orphanedForeignKeys) {
196
201
  const [schemaName, tableName] = this.splitTableName(orphanedForeignKey.localTableName);
@@ -227,6 +232,12 @@ class SqlSchemaGenerator extends core_1.AbstractSchemaGenerator {
227
232
  ret += await this.dump(builder);
228
233
  }
229
234
  }
235
+ if (!options.safe && this.platform.supportsNativeEnums()) {
236
+ for (const removedNativeEnum of schemaDiff.removedNativeEnums) {
237
+ const sql = this.helper.getDropNativeEnumSQL(removedNativeEnum.name, removedNativeEnum.schema);
238
+ ret += await this.dump(this.knex.schema.raw(sql), '\n');
239
+ }
240
+ }
230
241
  if (options.dropTables && !options.safe) {
231
242
  for (const removedNamespace of schemaDiff.removedNamespaces) {
232
243
  ret += await this.dump(this.knex.schema.dropSchema(removedNamespace));
@@ -296,22 +307,16 @@ class SqlSchemaGenerator extends core_1.AbstractSchemaGenerator {
296
307
  const ret = [];
297
308
  const [schemaName, tableName] = this.splitTableName(diff.name);
298
309
  if (this.platform.supportsNativeEnums()) {
299
- const createdNativeEnums = [];
300
310
  const changedNativeEnums = [];
301
- for (const { column, changedProperties, fromColumn } of Object.values(diff.changedColumns)) {
311
+ for (const { column, changedProperties } of Object.values(diff.changedColumns)) {
302
312
  if (!column.nativeEnumName) {
303
313
  continue;
304
314
  }
305
- if (changedProperties.has('enumItems') && column.nativeEnumName in diff.fromTable.nativeEnums) {
306
- changedNativeEnums.push([column.nativeEnumName, column.enumItems, diff.fromTable.getColumn(column.name).enumItems]);
307
- }
308
- else if (changedProperties.has('type') && !(column.nativeEnumName in diff.fromTable.nativeEnums)) {
309
- createdNativeEnums.push([column.nativeEnumName, column.enumItems]);
315
+ const key = schemaName && schemaName !== this.platform.getDefaultSchemaName() ? schemaName + '.' + column.nativeEnumName : column.nativeEnumName;
316
+ if (changedProperties.has('enumItems') && key in diff.fromTable.nativeEnums) {
317
+ changedNativeEnums.push([column.nativeEnumName, column.enumItems, diff.fromTable.nativeEnums[key].items]);
310
318
  }
311
319
  }
312
- core_1.Utils.removeDuplicates(createdNativeEnums).forEach(([enumName, items]) => {
313
- ret.push(this.knex.schema.raw(this.helper.getCreateNativeEnumSQL(enumName, items, schemaName)));
314
- });
315
320
  core_1.Utils.removeDuplicates(changedNativeEnums).forEach(([enumName, itemsNew, itemsOld]) => {
316
321
  // postgres allows only adding new items, the values are case insensitive
317
322
  itemsOld = itemsOld.map(v => v.toLowerCase());
package/typings.d.ts CHANGED
@@ -107,10 +107,19 @@ export interface TableDifference {
107
107
  }
108
108
  export interface SchemaDifference {
109
109
  newNamespaces: Set<string>;
110
+ newNativeEnums: {
111
+ name: string;
112
+ schema?: string;
113
+ items: string[];
114
+ }[];
110
115
  newTables: Dictionary<DatabaseTable>;
111
116
  changedTables: Dictionary<TableDifference>;
112
117
  removedTables: Dictionary<DatabaseTable>;
113
118
  removedNamespaces: Set<string>;
119
+ removedNativeEnums: {
120
+ name: string;
121
+ schema?: string;
122
+ }[];
114
123
  orphanedForeignKeys: ForeignKey[];
115
124
  fromSchema: DatabaseSchema;
116
125
  }