@mikro-orm/knex 6.0.0-dev.64 → 6.0.0-dev.66
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 +2 -2
- package/query/QueryBuilderHelper.js +2 -6
- package/schema/DatabaseSchema.d.ts +4 -1
- package/schema/DatabaseSchema.js +19 -3
- package/schema/DatabaseTable.d.ts +1 -0
- package/schema/DatabaseTable.js +4 -2
- package/schema/SchemaHelper.d.ts +3 -1
- package/schema/SchemaHelper.js +7 -1
- package/schema/SqlSchemaGenerator.d.ts +1 -1
- package/schema/SqlSchemaGenerator.js +28 -3
- package/typings.d.ts +1 -0
package/package.json
CHANGED
|
@@ -1,6 +1,6 @@
|
|
|
1
1
|
{
|
|
2
2
|
"name": "@mikro-orm/knex",
|
|
3
|
-
"version": "6.0.0-dev.
|
|
3
|
+
"version": "6.0.0-dev.66",
|
|
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.
|
|
69
|
+
"@mikro-orm/core": "~6.0.0-dev.66"
|
|
70
70
|
}
|
|
71
71
|
}
|
|
@@ -22,6 +22,7 @@ class QueryBuilderHelper {
|
|
|
22
22
|
if (core_1.Utils.isRawSql(field)) {
|
|
23
23
|
return this.knex.raw(field.sql, field.params);
|
|
24
24
|
}
|
|
25
|
+
/* istanbul ignore next */
|
|
25
26
|
if (typeof field !== 'string') {
|
|
26
27
|
return field;
|
|
27
28
|
}
|
|
@@ -516,12 +517,7 @@ class QueryBuilderHelper {
|
|
|
516
517
|
if (fieldName instanceof core_1.RawQueryFragment) {
|
|
517
518
|
return fieldName.sql;
|
|
518
519
|
}
|
|
519
|
-
|
|
520
|
-
ret = fieldName;
|
|
521
|
-
}
|
|
522
|
-
else {
|
|
523
|
-
ret = alias + fieldName;
|
|
524
|
-
}
|
|
520
|
+
ret = alias + fieldName;
|
|
525
521
|
}
|
|
526
522
|
else {
|
|
527
523
|
const [a, ...rest] = field.split('.');
|
|
@@ -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;
|
package/schema/DatabaseSchema.js
CHANGED
|
@@ -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[];
|
package/schema/DatabaseTable.js
CHANGED
|
@@ -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
|
|
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,
|
package/schema/SchemaHelper.d.ts
CHANGED
|
@@ -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
|
-
|
|
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;
|
package/schema/SchemaHelper.js
CHANGED
|
@@ -46,7 +46,13 @@ class SchemaHelper {
|
|
|
46
46
|
async getEnumDefinitions(connection, checks, tableName, schemaName) {
|
|
47
47
|
return {};
|
|
48
48
|
}
|
|
49
|
-
|
|
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 {
|
|
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
|
|
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
|
|
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
|
/**
|
|
@@ -367,7 +392,7 @@ class SqlSchemaGenerator extends core_1.AbstractSchemaGenerator {
|
|
|
367
392
|
}
|
|
368
393
|
await core_1.Utils.runSerial(lines, line => this.driver.execute(line));
|
|
369
394
|
}
|
|
370
|
-
wrapSchema(sql, options
|
|
395
|
+
wrapSchema(sql, options) {
|
|
371
396
|
options.wrap ??= this.options.disableForeignKeys;
|
|
372
397
|
if (!options.wrap || sql.trim() === '') {
|
|
373
398
|
return sql;
|