@mikro-orm/knex 6.1.13-dev.9 → 6.2.0
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/AbstractSqlConnection.js +8 -2
- package/AbstractSqlDriver.d.ts +5 -5
- package/AbstractSqlDriver.js +21 -10
- package/MonkeyPatchable.d.ts +5 -0
- package/MonkeyPatchable.js +15 -0
- package/README.md +17 -11
- package/SqlEntityManager.d.ts +1 -1
- package/SqlEntityManager.js +1 -2
- package/dialects/index.d.ts +4 -0
- package/dialects/index.js +20 -0
- package/dialects/mssql/MsSqlColumnCompiler.d.ts +4 -0
- package/dialects/mssql/MsSqlColumnCompiler.js +10 -0
- package/dialects/mssql/MsSqlKnexDialect.d.ts +6 -0
- package/dialects/mssql/MsSqlKnexDialect.js +22 -0
- package/dialects/mssql/MsSqlQueryCompiler.d.ts +12 -0
- package/dialects/mssql/MsSqlQueryCompiler.js +94 -0
- package/dialects/mssql/MsSqlTableCompiler.d.ts +9 -0
- package/dialects/mssql/MsSqlTableCompiler.js +40 -0
- package/dialects/mssql/index.d.ts +1 -0
- package/dialects/mssql/index.js +17 -0
- package/dialects/mysql/MariaDbKnexDialect.d.ts +6 -0
- package/dialects/mysql/MariaDbKnexDialect.js +16 -0
- package/dialects/mysql/MySqlColumnCompiler.d.ts +9 -0
- package/dialects/mysql/MySqlColumnCompiler.js +19 -0
- package/dialects/mysql/MySqlConnection.d.ts +8 -0
- package/dialects/mysql/MySqlConnection.js +43 -0
- package/dialects/mysql/MySqlExceptionConverter.d.ts +9 -0
- package/dialects/mysql/MySqlExceptionConverter.js +83 -0
- package/dialects/mysql/MySqlKnexDialect.d.ts +5 -0
- package/dialects/mysql/MySqlKnexDialect.js +21 -0
- package/dialects/mysql/MySqlPlatform.d.ts +31 -0
- package/dialects/mysql/MySqlPlatform.js +88 -0
- package/dialects/mysql/MySqlQueryCompiler.d.ts +5 -0
- package/dialects/mysql/MySqlQueryCompiler.js +23 -0
- package/dialects/mysql/MySqlSchemaHelper.d.ts +43 -0
- package/dialects/mysql/MySqlSchemaHelper.js +297 -0
- package/dialects/mysql/index.d.ts +6 -0
- package/dialects/mysql/index.js +22 -0
- package/dialects/postgresql/PostgreSqlKnexDialect.d.ts +6 -0
- package/dialects/postgresql/PostgreSqlKnexDialect.js +19 -0
- package/dialects/postgresql/PostgreSqlTableCompiler.d.ts +11 -0
- package/dialects/postgresql/PostgreSqlTableCompiler.js +89 -0
- package/dialects/postgresql/index.d.ts +1 -0
- package/dialects/postgresql/index.js +17 -0
- package/dialects/sqlite/BaseSqliteConnection.d.ts +10 -0
- package/dialects/sqlite/BaseSqliteConnection.js +56 -0
- package/dialects/sqlite/BaseSqlitePlatform.d.ts +49 -0
- package/dialects/sqlite/BaseSqlitePlatform.js +78 -0
- package/dialects/sqlite/BaseSqliteSchemaHelper.d.ts +27 -0
- package/dialects/sqlite/BaseSqliteSchemaHelper.js +172 -0
- package/dialects/sqlite/BetterSqliteKnexDialect.d.ts +5 -0
- package/dialects/sqlite/BetterSqliteKnexDialect.js +15 -0
- package/dialects/sqlite/LibSqlKnexDialect.d.ts +6 -0
- package/dialects/sqlite/LibSqlKnexDialect.js +18 -0
- package/dialects/sqlite/SqliteKnexDialect.d.ts +8 -0
- package/dialects/sqlite/SqliteKnexDialect.js +67 -0
- package/dialects/sqlite/SqliteTableCompiler.d.ts +5 -0
- package/dialects/sqlite/SqliteTableCompiler.js +45 -0
- package/dialects/sqlite/index.d.ts +7 -0
- package/dialects/sqlite/index.js +23 -0
- package/index.d.ts +1 -0
- package/index.js +1 -0
- package/index.mjs +16 -0
- package/package.json +3 -3
- package/query/QueryBuilder.d.ts +59 -41
- package/query/QueryBuilder.js +61 -23
- package/query/QueryBuilderHelper.js +8 -3
- package/schema/DatabaseTable.js +16 -4
- package/schema/SchemaComparator.js +13 -3
- package/schema/SchemaHelper.d.ts +5 -1
- package/schema/SchemaHelper.js +19 -2
- package/schema/SqlSchemaGenerator.d.ts +12 -4
- package/schema/SqlSchemaGenerator.js +51 -29
package/schema/SchemaHelper.js
CHANGED
|
@@ -29,6 +29,9 @@ class SchemaHelper {
|
|
|
29
29
|
const pks = indexes.filter(i => i.primary).map(pk => pk.columnNames);
|
|
30
30
|
return core_1.Utils.flatten(pks);
|
|
31
31
|
}
|
|
32
|
+
inferLengthFromColumnType(type) {
|
|
33
|
+
return undefined;
|
|
34
|
+
}
|
|
32
35
|
async getForeignKeys(connection, tableName, schemaName) {
|
|
33
36
|
const fks = await connection.execute(this.getForeignKeysSQL(tableName, schemaName));
|
|
34
37
|
return this.mapForeignKeys(fks, tableName, schemaName);
|
|
@@ -99,6 +102,11 @@ class SchemaHelper {
|
|
|
99
102
|
getRenameIndexSQL(tableName, index, oldIndexName) {
|
|
100
103
|
return [this.getDropIndexSQL(tableName, { ...index, keyName: oldIndexName }), this.getCreateIndexSQL(tableName, index)].join(';\n');
|
|
101
104
|
}
|
|
105
|
+
getDropColumnsSQL(tableName, columns, schemaName) {
|
|
106
|
+
const name = this.platform.quoteIdentifier((schemaName && schemaName !== this.platform.getDefaultSchemaName() ? schemaName + '.' : '') + tableName);
|
|
107
|
+
const drops = columns.map(column => `drop column ${this.platform.quoteIdentifier(column.name)}`).join(', ');
|
|
108
|
+
return `alter table ${name} ${drops}`;
|
|
109
|
+
}
|
|
102
110
|
hasNonDefaultPrimaryKeyName(table) {
|
|
103
111
|
const pkIndex = table.getPrimaryKey();
|
|
104
112
|
if (!pkIndex || !this.platform.supportsCustomPrimaryKeyNames()) {
|
|
@@ -221,10 +229,19 @@ class SchemaHelper {
|
|
|
221
229
|
return norm[0].replace('(?)', length != null ? `(${length})` : '');
|
|
222
230
|
}
|
|
223
231
|
getCreateDatabaseSQL(name) {
|
|
224
|
-
|
|
232
|
+
// two line breaks to force separate execution
|
|
233
|
+
return `create database ${name};\n\nuse ${name}`;
|
|
225
234
|
}
|
|
226
235
|
getDropDatabaseSQL(name) {
|
|
227
|
-
return `drop database if exists ${name}`;
|
|
236
|
+
return `drop database if exists ${this.platform.quoteIdentifier(name)}`;
|
|
237
|
+
}
|
|
238
|
+
/* istanbul ignore next */
|
|
239
|
+
getCreateNamespaceSQL(name) {
|
|
240
|
+
return `create schema if not exists ${this.platform.quoteIdentifier(name)}`;
|
|
241
|
+
}
|
|
242
|
+
/* istanbul ignore next */
|
|
243
|
+
getDropNamespaceSQL(name) {
|
|
244
|
+
return `drop schema if exists ${this.platform.quoteIdentifier(name)}`;
|
|
228
245
|
}
|
|
229
246
|
getDatabaseExistsSQL(name) {
|
|
230
247
|
return `select 1 from information_schema.schemata where schema_name = '${name}'`;
|
|
@@ -1,10 +1,15 @@
|
|
|
1
|
-
import { AbstractSchemaGenerator, type
|
|
1
|
+
import { AbstractSchemaGenerator, type ClearDatabaseOptions, type CreateSchemaOptions, type DropSchemaOptions, type EnsureDatabaseOptions, type ISchemaGenerator, type MikroORM, type Transaction, type UpdateSchemaOptions } from '@mikro-orm/core';
|
|
2
2
|
import type { SchemaDifference } from '../typings';
|
|
3
3
|
import { DatabaseSchema } from './DatabaseSchema';
|
|
4
4
|
import type { AbstractSqlDriver } from '../AbstractSqlDriver';
|
|
5
5
|
export declare class SqlSchemaGenerator extends AbstractSchemaGenerator<AbstractSqlDriver> implements ISchemaGenerator {
|
|
6
|
-
|
|
7
|
-
|
|
6
|
+
protected readonly helper: import("./SchemaHelper").SchemaHelper;
|
|
7
|
+
protected readonly options: {
|
|
8
|
+
disableForeignKeys?: boolean | undefined;
|
|
9
|
+
createForeignKeyConstraints?: boolean | undefined;
|
|
10
|
+
ignoreSchema?: string[] | undefined;
|
|
11
|
+
managementDbName?: string | undefined;
|
|
12
|
+
};
|
|
8
13
|
protected lastEnsuredDatabase?: string;
|
|
9
14
|
static register(orm: MikroORM): void;
|
|
10
15
|
createSchema(options?: CreateSchemaOptions): Promise<void>;
|
|
@@ -15,6 +20,8 @@ export declare class SqlSchemaGenerator extends AbstractSchemaGenerator<Abstract
|
|
|
15
20
|
getTargetSchema(schema?: string): DatabaseSchema;
|
|
16
21
|
getCreateSchemaSQL(options?: CreateSchemaOptions): Promise<string>;
|
|
17
22
|
dropSchema(options?: DropSchemaOptions): Promise<void>;
|
|
23
|
+
createNamespace(name: string): Promise<void>;
|
|
24
|
+
dropNamespace(name: string): Promise<void>;
|
|
18
25
|
clearDatabase(options?: ClearDatabaseOptions): Promise<void>;
|
|
19
26
|
getDropSchemaSQL(options?: Omit<DropSchemaOptions, 'dropDb'>): Promise<string>;
|
|
20
27
|
private getSchemaName;
|
|
@@ -43,10 +50,11 @@ export declare class SqlSchemaGenerator extends AbstractSchemaGenerator<Abstract
|
|
|
43
50
|
/**
|
|
44
51
|
* creates new database and connects to it
|
|
45
52
|
*/
|
|
46
|
-
createDatabase(name
|
|
53
|
+
createDatabase(name?: string): Promise<void>;
|
|
47
54
|
dropDatabase(name?: string): Promise<void>;
|
|
48
55
|
execute(sql: string, options?: {
|
|
49
56
|
wrap?: boolean;
|
|
57
|
+
ctx?: Transaction;
|
|
50
58
|
}): Promise<void>;
|
|
51
59
|
private wrapSchema;
|
|
52
60
|
private createSchemaBuilder;
|
|
@@ -58,7 +58,8 @@ class SqlSchemaGenerator extends core_1.AbstractSchemaGenerator {
|
|
|
58
58
|
if (namespace === this.platform.getDefaultSchemaName()) {
|
|
59
59
|
continue;
|
|
60
60
|
}
|
|
61
|
-
|
|
61
|
+
const sql = this.helper.getCreateNamespaceSQL(namespace);
|
|
62
|
+
ret += await this.dump(this.knex.schema.raw(sql), '\n');
|
|
62
63
|
}
|
|
63
64
|
if (this.platform.supportsNativeEnums()) {
|
|
64
65
|
const created = [];
|
|
@@ -88,6 +89,14 @@ class SqlSchemaGenerator extends core_1.AbstractSchemaGenerator {
|
|
|
88
89
|
const sql = await this.getDropSchemaSQL(options);
|
|
89
90
|
await this.execute(sql);
|
|
90
91
|
}
|
|
92
|
+
async createNamespace(name) {
|
|
93
|
+
const sql = await this.helper.getCreateNamespaceSQL(name);
|
|
94
|
+
await this.execute(sql);
|
|
95
|
+
}
|
|
96
|
+
async dropNamespace(name) {
|
|
97
|
+
const sql = await this.helper.getDropNamespaceSQL(name);
|
|
98
|
+
await this.execute(sql);
|
|
99
|
+
}
|
|
91
100
|
async clearDatabase(options) {
|
|
92
101
|
// truncate by default, so no value is considered as true
|
|
93
102
|
/* istanbul ignore if */
|
|
@@ -101,12 +110,7 @@ class SqlSchemaGenerator extends core_1.AbstractSchemaGenerator {
|
|
|
101
110
|
.truncate();
|
|
102
111
|
}
|
|
103
112
|
await this.execute(this.helper.enableForeignKeysSQL());
|
|
104
|
-
|
|
105
|
-
const allowGlobalContext = this.config.get('allowGlobalContext');
|
|
106
|
-
this.config.set('allowGlobalContext', true);
|
|
107
|
-
this.em.clear();
|
|
108
|
-
this.config.set('allowGlobalContext', allowGlobalContext);
|
|
109
|
-
}
|
|
113
|
+
this.clearIdentityMap();
|
|
110
114
|
}
|
|
111
115
|
async getDropSchemaSQL(options = {}) {
|
|
112
116
|
await this.ensureDatabase();
|
|
@@ -118,7 +122,7 @@ class SqlSchemaGenerator extends core_1.AbstractSchemaGenerator {
|
|
|
118
122
|
// remove FKs explicitly if we can't use cascading statement and we don't disable FK checks (we need this for circular relations)
|
|
119
123
|
for (const meta of metadata) {
|
|
120
124
|
const table = schema.getTable(meta.tableName);
|
|
121
|
-
if (!this.platform.usesCascadeStatement() && table && !wrap) {
|
|
125
|
+
if (!this.platform.usesCascadeStatement() && table && (!wrap || options.dropForeignKeys)) {
|
|
122
126
|
for (const fk of Object.values(table.getForeignKeys())) {
|
|
123
127
|
const builder = this.createSchemaBuilder(table.schema).alterTable(table.name, tbl => {
|
|
124
128
|
tbl.dropForeign(fk.columnNames, fk.constraintName);
|
|
@@ -186,8 +190,8 @@ class SqlSchemaGenerator extends core_1.AbstractSchemaGenerator {
|
|
|
186
190
|
let ret = '';
|
|
187
191
|
if (this.platform.supportsSchemas()) {
|
|
188
192
|
for (const newNamespace of schemaDiff.newNamespaces) {
|
|
189
|
-
|
|
190
|
-
ret += await this.dump(this.knex.schema.
|
|
193
|
+
const sql = this.helper.getCreateNamespaceSQL(newNamespace);
|
|
194
|
+
ret += await this.dump(this.knex.schema.raw(sql), '\n');
|
|
191
195
|
}
|
|
192
196
|
}
|
|
193
197
|
if (this.platform.supportsNativeEnums()) {
|
|
@@ -240,7 +244,8 @@ class SqlSchemaGenerator extends core_1.AbstractSchemaGenerator {
|
|
|
240
244
|
}
|
|
241
245
|
if (options.dropTables && !options.safe) {
|
|
242
246
|
for (const removedNamespace of schemaDiff.removedNamespaces) {
|
|
243
|
-
|
|
247
|
+
const sql = this.helper.getDropNamespaceSQL(removedNamespace);
|
|
248
|
+
ret += await this.dump(this.knex.schema.raw(sql), '\n');
|
|
244
249
|
}
|
|
245
250
|
}
|
|
246
251
|
return this.wrapSchema(ret, options);
|
|
@@ -266,11 +271,13 @@ class SqlSchemaGenerator extends core_1.AbstractSchemaGenerator {
|
|
|
266
271
|
.references(foreignKey.referencedColumnNames)
|
|
267
272
|
.inTable(this.getReferencedTableName(foreignKey.referencedTableName, schema))
|
|
268
273
|
.withKeyName(foreignKey.constraintName);
|
|
269
|
-
if (foreignKey.
|
|
270
|
-
|
|
271
|
-
|
|
272
|
-
|
|
273
|
-
|
|
274
|
+
if (foreignKey.localTableName !== foreignKey.referencedTableName || this.platform.supportsMultipleCascadePaths()) {
|
|
275
|
+
if (foreignKey.updateRule) {
|
|
276
|
+
builder.onUpdate(foreignKey.updateRule);
|
|
277
|
+
}
|
|
278
|
+
if (foreignKey.deleteRule) {
|
|
279
|
+
builder.onDelete(foreignKey.deleteRule);
|
|
280
|
+
}
|
|
274
281
|
}
|
|
275
282
|
if (foreignKey.deferMode) {
|
|
276
283
|
builder.deferrable(foreignKey.deferMode);
|
|
@@ -341,10 +348,8 @@ class SqlSchemaGenerator extends core_1.AbstractSchemaGenerator {
|
|
|
341
348
|
this.dropCheck(table, check);
|
|
342
349
|
}
|
|
343
350
|
/* istanbul ignore else */
|
|
344
|
-
if (!safe) {
|
|
345
|
-
|
|
346
|
-
this.helper.pushTableQuery(table, `alter table ${this.platform.quoteIdentifier(tableName)} drop column ${this.platform.quoteIdentifier(column.name)}`);
|
|
347
|
-
}
|
|
351
|
+
if (!safe && Object.values(diff.removedColumns).length > 0) {
|
|
352
|
+
this.helper.pushTableQuery(table, this.helper.getDropColumnsSQL(tableName, Object.values(diff.removedColumns), schemaName));
|
|
348
353
|
}
|
|
349
354
|
}));
|
|
350
355
|
ret.push(this.createSchemaBuilder(schemaName).alterTable(tableName, table => {
|
|
@@ -368,8 +373,10 @@ class SqlSchemaGenerator extends core_1.AbstractSchemaGenerator {
|
|
|
368
373
|
if (changedProperties.size === 1 && changedProperties.has('enumItems') && column.nativeEnumName) {
|
|
369
374
|
continue;
|
|
370
375
|
}
|
|
371
|
-
const col = this.helper.createTableColumn(table, column, diff.fromTable, changedProperties, true)
|
|
372
|
-
|
|
376
|
+
const col = this.helper.createTableColumn(table, column, diff.fromTable, changedProperties, true)?.alter();
|
|
377
|
+
if (col) {
|
|
378
|
+
this.helper.configureColumn(column, col, this.knex, changedProperties);
|
|
379
|
+
}
|
|
373
380
|
}
|
|
374
381
|
for (const { column } of Object.values(diff.changedColumns).filter(diff => diff.changedProperties.has('autoincrement'))) {
|
|
375
382
|
this.helper.pushTableQuery(table, this.helper.getAlterColumnAutoincrement(tableName, column, schemaName));
|
|
@@ -421,9 +428,10 @@ class SqlSchemaGenerator extends core_1.AbstractSchemaGenerator {
|
|
|
421
428
|
* creates new database and connects to it
|
|
422
429
|
*/
|
|
423
430
|
async createDatabase(name) {
|
|
431
|
+
name ??= this.config.get('dbName');
|
|
424
432
|
const sql = this.helper.getCreateDatabaseSQL('' + this.knex.ref(name));
|
|
425
433
|
if (sql) {
|
|
426
|
-
await this.
|
|
434
|
+
await this.execute(sql);
|
|
427
435
|
}
|
|
428
436
|
this.config.set('dbName', name);
|
|
429
437
|
await this.driver.reconnect();
|
|
@@ -432,20 +440,34 @@ class SqlSchemaGenerator extends core_1.AbstractSchemaGenerator {
|
|
|
432
440
|
name ??= this.config.get('dbName');
|
|
433
441
|
this.config.set('dbName', this.helper.getManagementDbName());
|
|
434
442
|
await this.driver.reconnect();
|
|
435
|
-
await this.
|
|
443
|
+
await this.execute(this.helper.getDropDatabaseSQL(name));
|
|
436
444
|
}
|
|
437
445
|
async execute(sql, options = {}) {
|
|
438
446
|
options.wrap ??= false;
|
|
439
|
-
const lines = this.wrapSchema(sql, options).split('\n')
|
|
440
|
-
|
|
447
|
+
const lines = this.wrapSchema(sql, options).split('\n');
|
|
448
|
+
const groups = [];
|
|
449
|
+
let i = 0;
|
|
450
|
+
for (const line of lines) {
|
|
451
|
+
if (line.trim() === '') {
|
|
452
|
+
if (groups[i]?.length > 0) {
|
|
453
|
+
i++;
|
|
454
|
+
}
|
|
455
|
+
continue;
|
|
456
|
+
}
|
|
457
|
+
groups[i] ??= [];
|
|
458
|
+
groups[i].push(line.trim());
|
|
459
|
+
}
|
|
460
|
+
if (groups.length === 0) {
|
|
441
461
|
return;
|
|
442
462
|
}
|
|
443
463
|
if (this.platform.supportsMultipleStatements()) {
|
|
444
|
-
const
|
|
445
|
-
|
|
464
|
+
for (const group of groups) {
|
|
465
|
+
const query = group.join('\n');
|
|
466
|
+
await this.driver.execute(query);
|
|
467
|
+
}
|
|
446
468
|
return;
|
|
447
469
|
}
|
|
448
|
-
await core_1.Utils.runSerial(
|
|
470
|
+
await core_1.Utils.runSerial(groups.flat(), line => this.driver.execute(line));
|
|
449
471
|
}
|
|
450
472
|
wrapSchema(sql, options) {
|
|
451
473
|
options.wrap ??= this.options.disableForeignKeys;
|