@mikro-orm/knex 6.0.0-dev.63 → 6.0.0-dev.65

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.
@@ -4,8 +4,8 @@ import { EntityRepository } from '@mikro-orm/core';
4
4
  import type { SqlEntityManager } from './SqlEntityManager';
5
5
  import type { QueryBuilder } from './query';
6
6
  export declare class SqlEntityRepository<T extends object> extends EntityRepository<T> {
7
- protected readonly _em: SqlEntityManager;
8
- constructor(_em: SqlEntityManager, entityName: EntityName<T>);
7
+ protected readonly em: SqlEntityManager;
8
+ constructor(em: SqlEntityManager, entityName: EntityName<T>);
9
9
  /**
10
10
  * Creates a QueryBuilder instance
11
11
  */
@@ -22,8 +22,4 @@ export declare class SqlEntityRepository<T extends object> extends EntityReposit
22
22
  * @inheritDoc
23
23
  */
24
24
  getEntityManager(): SqlEntityManager;
25
- /**
26
- * @inheritDoc
27
- */
28
- protected get em(): SqlEntityManager;
29
25
  }
@@ -3,9 +3,9 @@ Object.defineProperty(exports, "__esModule", { value: true });
3
3
  exports.SqlEntityRepository = void 0;
4
4
  const core_1 = require("@mikro-orm/core");
5
5
  class SqlEntityRepository extends core_1.EntityRepository {
6
- constructor(_em, entityName) {
7
- super(_em, entityName);
8
- this._em = _em;
6
+ constructor(em, entityName) {
7
+ super(em, entityName);
8
+ this.em = em;
9
9
  }
10
10
  /**
11
11
  * Creates a QueryBuilder instance
@@ -23,19 +23,13 @@ class SqlEntityRepository extends core_1.EntityRepository {
23
23
  * Returns configured knex instance.
24
24
  */
25
25
  getKnex(type) {
26
- return this.getEntityManager().getConnection(type).getKnex();
26
+ return this.getEntityManager().getKnex();
27
27
  }
28
28
  /**
29
29
  * @inheritDoc
30
30
  */
31
31
  getEntityManager() {
32
- return this._em;
33
- }
34
- /**
35
- * @inheritDoc
36
- */
37
- get em() {
38
- return this._em;
32
+ return this.em;
39
33
  }
40
34
  }
41
35
  exports.SqlEntityRepository = SqlEntityRepository;
package/package.json CHANGED
@@ -1,6 +1,6 @@
1
1
  {
2
2
  "name": "@mikro-orm/knex",
3
- "version": "6.0.0-dev.63",
3
+ "version": "6.0.0-dev.65",
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": "^5.7.2"
67
67
  },
68
68
  "peerDependencies": {
69
- "@mikro-orm/core": "~6.0.0-dev.63"
69
+ "@mikro-orm/core": "~6.0.0-dev.65"
70
70
  }
71
71
  }
@@ -10,14 +10,17 @@ export declare class DatabaseSchema {
10
10
  readonly name: string;
11
11
  private tables;
12
12
  private namespaces;
13
+ private nativeEnums;
13
14
  constructor(platform: AbstractSqlPlatform, name: string);
14
15
  addTable(name: string, schema: string | undefined | null, comment?: string): DatabaseTable;
15
16
  getTables(): DatabaseTable[];
16
17
  getTable(name: string): DatabaseTable | undefined;
17
18
  hasTable(name: string): boolean;
19
+ setNativeEnums(nativeEnums: Dictionary<unknown[]>): void;
20
+ getNativeEnums(): Dictionary<unknown[]>;
18
21
  hasNamespace(namespace: string): boolean;
19
22
  getNamespaces(): string[];
20
- static create(connection: AbstractSqlConnection, platform: AbstractSqlPlatform, config: Configuration, schemaName?: string): Promise<DatabaseSchema>;
23
+ static create(connection: AbstractSqlConnection, platform: AbstractSqlPlatform, config: Configuration, schemaName?: string, schemas?: string[]): Promise<DatabaseSchema>;
21
24
  static fromMetadata(metadata: EntityMetadata[], platform: AbstractSqlPlatform, config: Configuration, schemaName?: string): DatabaseSchema;
22
25
  private static getSchemaName;
23
26
  private static shouldHaveColumn;
@@ -12,10 +12,12 @@ class DatabaseSchema {
12
12
  this.name = name;
13
13
  this.tables = [];
14
14
  this.namespaces = new Set();
15
+ this.nativeEnums = {}; // for postgres
15
16
  }
16
17
  addTable(name, schema, comment) {
17
18
  const namespaceName = schema ?? this.name;
18
19
  const table = new DatabaseTable_1.DatabaseTable(this.platform, name, namespaceName);
20
+ table.nativeEnums = this.nativeEnums;
19
21
  table.comment = comment;
20
22
  this.tables.push(table);
21
23
  if (namespaceName != null) {
@@ -32,24 +34,38 @@ class DatabaseSchema {
32
34
  hasTable(name) {
33
35
  return !!this.getTable(name);
34
36
  }
37
+ setNativeEnums(nativeEnums) {
38
+ this.nativeEnums = nativeEnums;
39
+ this.tables.forEach(t => t.nativeEnums = nativeEnums);
40
+ }
41
+ getNativeEnums() {
42
+ return this.nativeEnums;
43
+ }
35
44
  hasNamespace(namespace) {
36
45
  return this.namespaces.has(namespace);
37
46
  }
38
47
  getNamespaces() {
39
48
  return [...this.namespaces];
40
49
  }
41
- static async create(connection, platform, config, schemaName) {
42
- const schema = new DatabaseSchema(platform, schemaName ?? config.get('schema'));
50
+ static async create(connection, platform, config, schemaName, schemas) {
51
+ const schema = new DatabaseSchema(platform, schemaName ?? config.get('schema') ?? platform.getDefaultSchemaName());
43
52
  const allTables = await connection.execute(platform.getSchemaHelper().getListTablesSQL());
44
53
  const parts = config.get('migrations').tableName.split('.');
45
54
  const migrationsTableName = parts[1] ?? parts[0];
46
55
  const migrationsSchemaName = parts.length > 1 ? parts[0] : config.get('schema', platform.getDefaultSchemaName());
47
56
  const tables = allTables.filter(t => t.table_name !== migrationsTableName || (t.schema_name && t.schema_name !== migrationsSchemaName));
48
- await platform.getSchemaHelper().loadInformationSchema(schema, connection, tables);
57
+ await platform.getSchemaHelper().loadInformationSchema(schema, connection, tables, schemas && schemas.length > 0 ? schemas : undefined);
49
58
  return schema;
50
59
  }
51
60
  static fromMetadata(metadata, platform, config, schemaName) {
52
61
  const schema = new DatabaseSchema(platform, schemaName ?? config.get('schema'));
62
+ const nativeEnums = {};
63
+ for (const meta of metadata) {
64
+ meta.props
65
+ .filter(prop => prop.nativeEnumName)
66
+ .forEach(prop => nativeEnums[prop.nativeEnumName] = prop.items?.map(val => '' + val) ?? []);
67
+ }
68
+ schema.setNativeEnums(nativeEnums);
53
69
  for (const meta of metadata) {
54
70
  const table = schema.addTable(meta.collection, this.getSchemaName(meta, config, schemaName));
55
71
  table.comment = meta.comment;
@@ -13,6 +13,7 @@ export declare class DatabaseTable {
13
13
  private indexes;
14
14
  private checks;
15
15
  private foreignKeys;
16
+ nativeEnums: Dictionary<unknown[]>;
16
17
  comment?: string;
17
18
  constructor(platform: AbstractSqlPlatform, name: string, schema?: string | undefined);
18
19
  getColumns(): Column[];
@@ -14,6 +14,7 @@ class DatabaseTable {
14
14
  this.indexes = [];
15
15
  this.checks = [];
16
16
  this.foreignKeys = {};
17
+ this.nativeEnums = {}; // for postgres
17
18
  Object.defineProperties(this, {
18
19
  platform: { enumerable: false, writable: true },
19
20
  });
@@ -41,7 +42,7 @@ class DatabaseTable {
41
42
  const type = v.name in enums ? 'enum' : v.type;
42
43
  v.mappedType = this.platform.getMappedType(type);
43
44
  v.default = v.default?.toString().startsWith('nextval(') ? null : v.default;
44
- v.enumItems = enums[v.name] || [];
45
+ v.enumItems ??= enums[v.name] || [];
45
46
  o[v.name] = v;
46
47
  return o;
47
48
  }, {});
@@ -73,12 +74,13 @@ class DatabaseTable {
73
74
  unsigned: prop.unsigned && this.platform.isNumericColumn(mappedType),
74
75
  autoincrement: prop.autoincrement ?? primary,
75
76
  primary,
77
+ nativeEnumName: prop.nativeEnumName,
76
78
  nullable: !!prop.nullable,
77
79
  length: prop.length,
78
80
  precision: prop.precision,
79
81
  scale: prop.scale,
80
82
  default: prop.defaultRaw,
81
- enumItems: prop.items?.every(core_1.Utils.isString) ? prop.items : undefined,
83
+ enumItems: prop.nativeEnumName || prop.items?.every(core_1.Utils.isString) ? prop.items : undefined,
82
84
  comment: prop.comment,
83
85
  extra: prop.extra,
84
86
  ignoreSchemaChanges: prop.ignoreSchemaChanges,
@@ -18,7 +18,9 @@ export declare abstract class SchemaHelper {
18
18
  getForeignKeys(connection: AbstractSqlConnection, tableName: string, schemaName?: string): Promise<Dictionary>;
19
19
  protected getTableKey(t: Table): string;
20
20
  getEnumDefinitions(connection: AbstractSqlConnection, checks: CheckDef[], tableName: string, schemaName?: string): Promise<Dictionary<string[]>>;
21
- loadInformationSchema(schema: DatabaseSchema, connection: AbstractSqlConnection, tables: Table[]): Promise<void>;
21
+ getDropNativeEnumSQL(name: string, schema?: string): string;
22
+ getAlterNativeEnumSQL(name: string, schema?: string, value?: string): string;
23
+ loadInformationSchema(schema: DatabaseSchema, connection: AbstractSqlConnection, tables: Table[], schemas?: string[]): Promise<void>;
22
24
  getListTablesSQL(schemaName?: string): string;
23
25
  getRenameColumnSQL(tableName: string, oldColumnName: string, to: Column, schemaName?: string): string;
24
26
  getCreateIndexSQL(tableName: string, index: IndexDef): string;
@@ -46,7 +46,13 @@ class SchemaHelper {
46
46
  async getEnumDefinitions(connection, checks, tableName, schemaName) {
47
47
  return {};
48
48
  }
49
- async loadInformationSchema(schema, connection, tables) {
49
+ getDropNativeEnumSQL(name, schema) {
50
+ throw new Error('Not supported by given driver');
51
+ }
52
+ getAlterNativeEnumSQL(name, schema, value) {
53
+ throw new Error('Not supported by given driver');
54
+ }
55
+ async loadInformationSchema(schema, connection, tables, schemas) {
50
56
  for (const t of tables) {
51
57
  const table = schema.addTable(t.table_name, t.schema_name);
52
58
  table.comment = t.table_comment;
@@ -1,4 +1,4 @@
1
- import type { MikroORM, ISchemaGenerator } from '@mikro-orm/core';
1
+ import type { ISchemaGenerator, MikroORM } from '@mikro-orm/core';
2
2
  import { AbstractSchemaGenerator } from '@mikro-orm/core';
3
3
  import type { SchemaDifference } from '../typings';
4
4
  import { DatabaseSchema } from './DatabaseSchema';
@@ -93,7 +93,8 @@ class SqlSchemaGenerator extends core_1.AbstractSchemaGenerator {
93
93
  await this.ensureDatabase();
94
94
  const wrap = options.wrap ?? this.options.disableForeignKeys;
95
95
  const metadata = this.getOrderedMetadata(options.schema).reverse();
96
- const schema = await DatabaseSchema_1.DatabaseSchema.create(this.connection, this.platform, this.config, options.schema);
96
+ const schemas = this.getTargetSchema(options.schema).getNamespaces();
97
+ const schema = await DatabaseSchema_1.DatabaseSchema.create(this.connection, this.platform, this.config, options.schema, schemas);
97
98
  let ret = '';
98
99
  // remove FKs explicitly if we can't use cascading statement and we don't disable FK checks (we need this for circular relations)
99
100
  for (const meta of metadata) {
@@ -110,6 +111,12 @@ class SqlSchemaGenerator extends core_1.AbstractSchemaGenerator {
110
111
  for (const meta of metadata) {
111
112
  ret += await this.dump(this.dropTable(meta.collection, this.getSchemaName(meta, options)), '\n');
112
113
  }
114
+ if (this.platform.supportsNativeEnums()) {
115
+ for (const columnName of Object.keys(schema.getNativeEnums())) {
116
+ const sql = this.helper.getDropNativeEnumSQL(columnName, options.schema ?? this.config.get('schema'));
117
+ ret += await this.dump(this.knex.schema.raw(sql), '\n');
118
+ }
119
+ }
113
120
  if (options.dropMigrationsTable) {
114
121
  ret += await this.dump(this.dropTable(this.config.get('migrations').tableName, this.config.get('schema')), '\n');
115
122
  }
@@ -149,10 +156,12 @@ class SqlSchemaGenerator extends core_1.AbstractSchemaGenerator {
149
156
  options.safe ??= false;
150
157
  options.dropTables ??= true;
151
158
  const toSchema = this.getTargetSchema(options.schema);
152
- const fromSchema = options.fromSchema ?? await DatabaseSchema_1.DatabaseSchema.create(this.connection, this.platform, this.config, options.schema);
159
+ const schemas = toSchema.getNamespaces();
160
+ const fromSchema = options.fromSchema ?? await DatabaseSchema_1.DatabaseSchema.create(this.connection, this.platform, this.config, options.schema, schemas);
153
161
  const wildcardSchemaTables = Object.values(this.metadata.getAll()).filter(meta => meta.schema === '*').map(meta => meta.tableName);
154
162
  fromSchema.prune(options.schema, wildcardSchemaTables);
155
163
  toSchema.prune(options.schema, wildcardSchemaTables);
164
+ toSchema.setNativeEnums(fromSchema.getNativeEnums());
156
165
  return { fromSchema, toSchema };
157
166
  }
158
167
  async diffToSQL(schemaDiff, options) {
@@ -256,6 +265,12 @@ class SqlSchemaGenerator extends core_1.AbstractSchemaGenerator {
256
265
  alterTable(diff, safe) {
257
266
  const ret = [];
258
267
  const [schemaName, tableName] = this.splitTableName(diff.name);
268
+ const changedNativeEnums = [];
269
+ for (const { column, changedProperties } of Object.values(diff.changedColumns)) {
270
+ if (column.nativeEnumName && changedProperties.has('enumItems') && column.nativeEnumName in diff.fromTable.nativeEnums) {
271
+ changedNativeEnums.push([column.nativeEnumName, column.enumItems, diff.fromTable.getColumn(column.name).enumItems]);
272
+ }
273
+ }
259
274
  ret.push(this.createSchemaBuilder(schemaName).alterTable(tableName, table => {
260
275
  for (const index of Object.values(diff.removedIndexes)) {
261
276
  this.dropIndex(table, index);
@@ -292,6 +307,9 @@ class SqlSchemaGenerator extends core_1.AbstractSchemaGenerator {
292
307
  if (changedProperties.size === 1 && changedProperties.has('comment')) {
293
308
  continue;
294
309
  }
310
+ if (changedProperties.size === 1 && changedProperties.has('enumItems') && column.nativeEnumName) {
311
+ continue;
312
+ }
295
313
  const col = this.helper.createTableColumn(table, column, diff.fromTable, changedProperties).alter();
296
314
  this.helper.configureColumn(column, col, this.knex, changedProperties);
297
315
  }
@@ -339,6 +357,13 @@ class SqlSchemaGenerator extends core_1.AbstractSchemaGenerator {
339
357
  table.comment(comment);
340
358
  }
341
359
  }));
360
+ if (this.platform.supportsNativeEnums()) {
361
+ changedNativeEnums.forEach(([enumName, itemsNew, itemsOld]) => {
362
+ // postgres allows only adding new items
363
+ const newItems = itemsNew.filter(val => !itemsOld.includes(val));
364
+ ret.push(...newItems.map(val => this.knex.schema.raw(this.helper.getAlterNativeEnumSQL(enumName, schemaName, val))));
365
+ });
366
+ }
342
367
  return ret;
343
368
  }
344
369
  /**
package/typings.d.ts CHANGED
@@ -39,6 +39,7 @@ export interface Column {
39
39
  scale?: number;
40
40
  default?: string | null;
41
41
  comment?: string;
42
+ nativeEnumName?: string;
42
43
  enumItems?: string[];
43
44
  primary?: boolean;
44
45
  unique?: boolean;