@mikro-orm/knex 6.0.0-dev.1 → 6.0.0-dev.100
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/{dist/AbstractSqlConnection.d.ts → AbstractSqlConnection.d.ts} +8 -4
- package/{dist/AbstractSqlConnection.js → AbstractSqlConnection.js} +35 -18
- package/AbstractSqlDriver.d.ts +66 -0
- package/{dist/AbstractSqlDriver.js → AbstractSqlDriver.js} +422 -129
- package/AbstractSqlPlatform.d.ts +22 -0
- package/{dist/AbstractSqlPlatform.js → AbstractSqlPlatform.js} +46 -19
- package/{dist/MonkeyPatchable.d.ts → MonkeyPatchable.d.ts} +1 -0
- package/{dist/MonkeyPatchable.js → MonkeyPatchable.js} +3 -0
- package/{dist/README.md → README.md} +66 -109
- package/{dist/SqlEntityManager.d.ts → SqlEntityManager.d.ts} +6 -10
- package/{dist/SqlEntityManager.js → SqlEntityManager.js} +5 -10
- package/SqlEntityRepository.d.ts +24 -0
- package/{dist/SqlEntityRepository.js → SqlEntityRepository.js} +11 -8
- package/{dist/index.d.ts → index.d.ts} +1 -0
- package/{dist/index.js → index.js} +6 -1
- package/index.mjs +208 -0
- package/package.json +20 -49
- package/query/ArrayCriteriaNode.d.ts +9 -0
- package/{dist/query → query}/CriteriaNode.d.ts +8 -8
- package/{dist/query → query}/CriteriaNode.js +23 -13
- package/query/CriteriaNodeFactory.d.ts +12 -0
- package/{dist/query → query}/CriteriaNodeFactory.js +10 -13
- package/{dist/query → query}/ObjectCriteriaNode.d.ts +3 -3
- package/{dist/query → query}/ObjectCriteriaNode.js +26 -9
- package/{dist/query → query}/QueryBuilder.d.ts +90 -34
- package/query/QueryBuilder.js +1098 -0
- package/{dist/query → query}/QueryBuilderHelper.d.ts +23 -19
- package/{dist/query → query}/QueryBuilderHelper.js +300 -176
- package/query/ScalarCriteriaNode.d.ts +10 -0
- package/{dist/query → query}/ScalarCriteriaNode.js +9 -6
- package/{dist/query → query}/enums.js +1 -1
- package/{dist/query → query}/index.js +5 -1
- package/{dist/schema → schema}/DatabaseSchema.d.ts +6 -3
- package/{dist/schema → schema}/DatabaseSchema.js +33 -23
- package/{dist/schema → schema}/DatabaseTable.d.ts +12 -10
- package/{dist/schema → schema}/DatabaseTable.js +55 -29
- package/{dist/schema → schema}/SchemaComparator.d.ts +7 -5
- package/{dist/schema → schema}/SchemaComparator.js +68 -10
- package/{dist/schema → schema}/SchemaHelper.d.ts +20 -13
- package/{dist/schema → schema}/SchemaHelper.js +56 -11
- package/{dist/schema/SchemaGenerator.d.ts → schema/SqlSchemaGenerator.d.ts} +11 -14
- package/{dist/schema/SchemaGenerator.js → schema/SqlSchemaGenerator.js} +139 -55
- package/{dist/schema → schema}/index.d.ts +1 -1
- package/{dist/schema → schema}/index.js +6 -2
- package/{dist/typings.d.ts → typings.d.ts} +27 -18
- package/CHANGELOG.md +0 -661
- package/dist/AbstractSqlDriver.d.ts +0 -55
- package/dist/AbstractSqlPlatform.d.ts +0 -21
- package/dist/LICENSE +0 -21
- package/dist/SqlEntityRepository.d.ts +0 -23
- package/dist/index.mjs +0 -26
- package/dist/package.json +0 -99
- package/dist/query/ArrayCriteriaNode.d.ts +0 -9
- package/dist/query/CriteriaNodeFactory.d.ts +0 -12
- package/dist/query/QueryBuilder.js +0 -688
- package/dist/query/ScalarCriteriaNode.d.ts +0 -9
- /package/{dist/query → query}/ArrayCriteriaNode.js +0 -0
- /package/{dist/query → query}/enums.d.ts +0 -0
- /package/{dist/query → query}/index.d.ts +0 -0
- /package/{dist/typings.js → typings.js} +0 -0
|
@@ -1,29 +1,15 @@
|
|
|
1
1
|
"use strict";
|
|
2
2
|
Object.defineProperty(exports, "__esModule", { value: true });
|
|
3
|
-
exports.SchemaGenerator = void 0;
|
|
3
|
+
exports.SchemaGenerator = exports.SqlSchemaGenerator = void 0;
|
|
4
4
|
const core_1 = require("@mikro-orm/core");
|
|
5
5
|
const DatabaseSchema_1 = require("./DatabaseSchema");
|
|
6
6
|
const SchemaComparator_1 = require("./SchemaComparator");
|
|
7
|
-
|
|
8
|
-
|
|
9
|
-
|
|
10
|
-
|
|
11
|
-
|
|
12
|
-
|
|
13
|
-
* `orm.getSchemaGenerator()` should be preferred way of getting the SchemaGenerator instance.
|
|
14
|
-
*/
|
|
15
|
-
constructor(driver) {
|
|
16
|
-
super(driver instanceof SqlEntityManager_1.SqlEntityManager ? driver.getDriver() : driver);
|
|
17
|
-
this.helper = this.platform.getSchemaHelper();
|
|
18
|
-
this.knex = this.connection.getKnex();
|
|
19
|
-
this.options = this.config.get('schemaGenerator');
|
|
20
|
-
}
|
|
21
|
-
async generate() {
|
|
22
|
-
const [dropSchema, createSchema] = await Promise.all([
|
|
23
|
-
this.getDropSchemaSQL({ wrap: false }),
|
|
24
|
-
this.getCreateSchemaSQL({ wrap: false }),
|
|
25
|
-
]);
|
|
26
|
-
return this.wrapSchema(dropSchema + createSchema);
|
|
7
|
+
class SqlSchemaGenerator extends core_1.AbstractSchemaGenerator {
|
|
8
|
+
helper = this.platform.getSchemaHelper();
|
|
9
|
+
options = this.config.get('schemaGenerator');
|
|
10
|
+
lastEnsuredDatabase;
|
|
11
|
+
static register(orm) {
|
|
12
|
+
orm.config.registerExtension('@mikro-orm/schema-generator', () => new SqlSchemaGenerator(orm.em));
|
|
27
13
|
}
|
|
28
14
|
async createSchema(options) {
|
|
29
15
|
await this.ensureDatabase();
|
|
@@ -35,7 +21,11 @@ class SchemaGenerator extends core_1.AbstractSchemaGenerator {
|
|
|
35
21
|
*/
|
|
36
22
|
async ensureDatabase() {
|
|
37
23
|
const dbName = this.config.get('dbName');
|
|
24
|
+
if (this.lastEnsuredDatabase === dbName) {
|
|
25
|
+
return true;
|
|
26
|
+
}
|
|
38
27
|
const exists = await this.helper.databaseExists(this.connection, dbName);
|
|
28
|
+
this.lastEnsuredDatabase = dbName;
|
|
39
29
|
if (!exists) {
|
|
40
30
|
this.config.set('dbName', this.helper.getManagementDbName());
|
|
41
31
|
await this.driver.reconnect();
|
|
@@ -48,7 +38,8 @@ class SchemaGenerator extends core_1.AbstractSchemaGenerator {
|
|
|
48
38
|
}
|
|
49
39
|
getTargetSchema(schema) {
|
|
50
40
|
const metadata = this.getOrderedMetadata(schema);
|
|
51
|
-
|
|
41
|
+
const schemaName = schema ?? this.config.get('schema') ?? this.platform.getDefaultSchemaName();
|
|
42
|
+
return DatabaseSchema_1.DatabaseSchema.fromMetadata(metadata, this.platform, this.config, schemaName);
|
|
52
43
|
}
|
|
53
44
|
async getCreateSchemaSQL(options = {}) {
|
|
54
45
|
const wrap = options.wrap ?? this.options.disableForeignKeys;
|
|
@@ -69,7 +60,6 @@ class SchemaGenerator extends core_1.AbstractSchemaGenerator {
|
|
|
69
60
|
return this.wrapSchema(ret, { wrap });
|
|
70
61
|
}
|
|
71
62
|
async dropSchema(options = {}) {
|
|
72
|
-
options.wrap ?? (options.wrap = true);
|
|
73
63
|
if (options.dropDb) {
|
|
74
64
|
const name = this.config.get('dbName');
|
|
75
65
|
return this.dropDatabase(name);
|
|
@@ -77,33 +67,79 @@ class SchemaGenerator extends core_1.AbstractSchemaGenerator {
|
|
|
77
67
|
const sql = await this.getDropSchemaSQL(options);
|
|
78
68
|
await this.execute(sql);
|
|
79
69
|
}
|
|
70
|
+
async clearDatabase(options) {
|
|
71
|
+
// truncate by default, so no value is considered as true
|
|
72
|
+
/* istanbul ignore if */
|
|
73
|
+
if (options?.truncate === false) {
|
|
74
|
+
return super.clearDatabase(options);
|
|
75
|
+
}
|
|
76
|
+
await this.execute(this.helper.disableForeignKeysSQL());
|
|
77
|
+
for (const meta of this.getOrderedMetadata(options?.schema).reverse()) {
|
|
78
|
+
await this.driver.createQueryBuilder(meta.className, this.em?.getTransactionContext(), 'write', false)
|
|
79
|
+
.withSchema(options?.schema)
|
|
80
|
+
.truncate();
|
|
81
|
+
}
|
|
82
|
+
await this.execute(this.helper.enableForeignKeysSQL());
|
|
83
|
+
if (this.em) {
|
|
84
|
+
const allowGlobalContext = this.config.get('allowGlobalContext');
|
|
85
|
+
this.config.set('allowGlobalContext', true);
|
|
86
|
+
this.em.clear();
|
|
87
|
+
this.config.set('allowGlobalContext', allowGlobalContext);
|
|
88
|
+
}
|
|
89
|
+
}
|
|
80
90
|
async getDropSchemaSQL(options = {}) {
|
|
91
|
+
await this.ensureDatabase();
|
|
81
92
|
const wrap = options.wrap ?? this.options.disableForeignKeys;
|
|
82
93
|
const metadata = this.getOrderedMetadata(options.schema).reverse();
|
|
94
|
+
const schemas = this.getTargetSchema(options.schema).getNamespaces();
|
|
95
|
+
const schema = await DatabaseSchema_1.DatabaseSchema.create(this.connection, this.platform, this.config, options.schema, schemas);
|
|
83
96
|
let ret = '';
|
|
97
|
+
// remove FKs explicitly if we can't use cascading statement and we don't disable FK checks (we need this for circular relations)
|
|
98
|
+
for (const meta of metadata) {
|
|
99
|
+
const table = schema.getTable(meta.tableName);
|
|
100
|
+
if (!this.platform.usesCascadeStatement() && table && !wrap) {
|
|
101
|
+
for (const fk of Object.values(table.getForeignKeys())) {
|
|
102
|
+
const builder = this.createSchemaBuilder(table.schema).alterTable(table.name, tbl => {
|
|
103
|
+
tbl.dropForeign(fk.columnNames, fk.constraintName);
|
|
104
|
+
});
|
|
105
|
+
ret += await this.dump(builder, '\n');
|
|
106
|
+
}
|
|
107
|
+
}
|
|
108
|
+
}
|
|
84
109
|
for (const meta of metadata) {
|
|
85
110
|
ret += await this.dump(this.dropTable(meta.collection, this.getSchemaName(meta, options)), '\n');
|
|
86
111
|
}
|
|
112
|
+
if (this.platform.supportsNativeEnums()) {
|
|
113
|
+
for (const columnName of Object.keys(schema.getNativeEnums())) {
|
|
114
|
+
const sql = this.helper.getDropNativeEnumSQL(columnName, options.schema ?? this.config.get('schema'));
|
|
115
|
+
ret += await this.dump(this.knex.schema.raw(sql), '\n');
|
|
116
|
+
}
|
|
117
|
+
}
|
|
87
118
|
if (options.dropMigrationsTable) {
|
|
88
119
|
ret += await this.dump(this.dropTable(this.config.get('migrations').tableName, this.config.get('schema')), '\n');
|
|
89
120
|
}
|
|
90
121
|
return this.wrapSchema(ret + '\n', { wrap });
|
|
91
122
|
}
|
|
92
123
|
getSchemaName(meta, options) {
|
|
124
|
+
const schemaName = options.schema ?? this.config.get('schema');
|
|
93
125
|
/* istanbul ignore next */
|
|
94
|
-
return meta.schema === '*' ?
|
|
126
|
+
return meta.schema && meta.schema === '*' ? schemaName : (meta.schema ?? schemaName);
|
|
95
127
|
}
|
|
96
128
|
async updateSchema(options = {}) {
|
|
97
129
|
const sql = await this.getUpdateSchemaSQL(options);
|
|
98
130
|
await this.execute(sql);
|
|
99
131
|
}
|
|
100
132
|
async getUpdateSchemaSQL(options = {}) {
|
|
133
|
+
await this.ensureDatabase();
|
|
101
134
|
const { fromSchema, toSchema } = await this.prepareSchemaForComparison(options);
|
|
102
135
|
const comparator = new SchemaComparator_1.SchemaComparator(this.platform);
|
|
103
136
|
const diffUp = comparator.compare(fromSchema, toSchema);
|
|
104
137
|
return this.diffToSQL(diffUp, options);
|
|
105
138
|
}
|
|
106
139
|
async getUpdateSchemaMigrationSQL(options = {}) {
|
|
140
|
+
if (!options.fromSchema) {
|
|
141
|
+
await this.ensureDatabase();
|
|
142
|
+
}
|
|
107
143
|
const { fromSchema, toSchema } = await this.prepareSchemaForComparison(options);
|
|
108
144
|
const comparator = new SchemaComparator_1.SchemaComparator(this.platform);
|
|
109
145
|
const diffUp = comparator.compare(fromSchema, toSchema);
|
|
@@ -114,14 +150,16 @@ class SchemaGenerator extends core_1.AbstractSchemaGenerator {
|
|
|
114
150
|
};
|
|
115
151
|
}
|
|
116
152
|
async prepareSchemaForComparison(options) {
|
|
117
|
-
options.wrap
|
|
118
|
-
options.safe
|
|
119
|
-
options.dropTables
|
|
153
|
+
options.wrap ??= this.options.disableForeignKeys;
|
|
154
|
+
options.safe ??= false;
|
|
155
|
+
options.dropTables ??= true;
|
|
120
156
|
const toSchema = this.getTargetSchema(options.schema);
|
|
121
|
-
const
|
|
157
|
+
const schemas = toSchema.getNamespaces();
|
|
158
|
+
const fromSchema = options.fromSchema ?? await DatabaseSchema_1.DatabaseSchema.create(this.connection, this.platform, this.config, options.schema, schemas);
|
|
122
159
|
const wildcardSchemaTables = Object.values(this.metadata.getAll()).filter(meta => meta.schema === '*').map(meta => meta.tableName);
|
|
123
160
|
fromSchema.prune(options.schema, wildcardSchemaTables);
|
|
124
161
|
toSchema.prune(options.schema, wildcardSchemaTables);
|
|
162
|
+
toSchema.setNativeEnums(fromSchema.getNativeEnums());
|
|
125
163
|
return { fromSchema, toSchema };
|
|
126
164
|
}
|
|
127
165
|
async diffToSQL(schemaDiff, options) {
|
|
@@ -225,6 +263,12 @@ class SchemaGenerator extends core_1.AbstractSchemaGenerator {
|
|
|
225
263
|
alterTable(diff, safe) {
|
|
226
264
|
const ret = [];
|
|
227
265
|
const [schemaName, tableName] = this.splitTableName(diff.name);
|
|
266
|
+
const changedNativeEnums = [];
|
|
267
|
+
for (const { column, changedProperties } of Object.values(diff.changedColumns)) {
|
|
268
|
+
if (column.nativeEnumName && changedProperties.has('enumItems') && column.nativeEnumName in diff.fromTable.nativeEnums) {
|
|
269
|
+
changedNativeEnums.push([column.nativeEnumName, column.enumItems, diff.fromTable.getColumn(column.name).enumItems]);
|
|
270
|
+
}
|
|
271
|
+
}
|
|
228
272
|
ret.push(this.createSchemaBuilder(schemaName).alterTable(tableName, table => {
|
|
229
273
|
for (const index of Object.values(diff.removedIndexes)) {
|
|
230
274
|
this.dropIndex(table, index);
|
|
@@ -240,7 +284,7 @@ class SchemaGenerator extends core_1.AbstractSchemaGenerator {
|
|
|
240
284
|
}
|
|
241
285
|
for (const column of Object.values(diff.addedColumns)) {
|
|
242
286
|
const col = this.helper.createTableColumn(table, column, diff.fromTable);
|
|
243
|
-
this.configureColumn(column, col);
|
|
287
|
+
this.helper.configureColumn(column, col, this.knex);
|
|
244
288
|
const foreignKey = Object.values(diff.addedForeignKeys).find(fk => fk.columnNames.length === 1 && fk.columnNames[0] === column.name);
|
|
245
289
|
if (foreignKey && this.options.createForeignKeyConstraints) {
|
|
246
290
|
delete diff.addedForeignKeys[foreignKey.constraintName];
|
|
@@ -261,20 +305,23 @@ class SchemaGenerator extends core_1.AbstractSchemaGenerator {
|
|
|
261
305
|
if (changedProperties.size === 1 && changedProperties.has('comment')) {
|
|
262
306
|
continue;
|
|
263
307
|
}
|
|
308
|
+
if (changedProperties.size === 1 && changedProperties.has('enumItems') && column.nativeEnumName) {
|
|
309
|
+
continue;
|
|
310
|
+
}
|
|
264
311
|
const col = this.helper.createTableColumn(table, column, diff.fromTable, changedProperties).alter();
|
|
265
|
-
this.configureColumn(column, col, changedProperties);
|
|
312
|
+
this.helper.configureColumn(column, col, this.knex, changedProperties);
|
|
266
313
|
}
|
|
267
314
|
for (const { column } of Object.values(diff.changedColumns).filter(diff => diff.changedProperties.has('autoincrement'))) {
|
|
268
|
-
this.helper.pushTableQuery(table, this.helper.getAlterColumnAutoincrement(tableName, column));
|
|
315
|
+
this.helper.pushTableQuery(table, this.helper.getAlterColumnAutoincrement(tableName, column, schemaName));
|
|
269
316
|
}
|
|
270
317
|
for (const { column, changedProperties } of Object.values(diff.changedColumns).filter(diff => diff.changedProperties.has('comment'))) {
|
|
271
318
|
if (['type', 'nullable', 'autoincrement', 'unsigned', 'default', 'enumItems'].some(t => changedProperties.has(t))) {
|
|
272
319
|
continue; // will be handled via knex
|
|
273
320
|
}
|
|
274
|
-
this.helper.pushTableQuery(table, this.helper.getChangeColumnCommentSQL(tableName, column));
|
|
321
|
+
this.helper.pushTableQuery(table, this.helper.getChangeColumnCommentSQL(tableName, column, schemaName));
|
|
275
322
|
}
|
|
276
323
|
for (const [oldColumnName, column] of Object.entries(diff.renamedColumns)) {
|
|
277
|
-
this.helper.pushTableQuery(table, this.helper.getRenameColumnSQL(tableName, oldColumnName, column));
|
|
324
|
+
this.helper.pushTableQuery(table, this.helper.getRenameColumnSQL(tableName, oldColumnName, column, schemaName));
|
|
278
325
|
}
|
|
279
326
|
for (const foreignKey of Object.values(diff.addedForeignKeys)) {
|
|
280
327
|
this.createForeignKey(table, foreignKey);
|
|
@@ -283,15 +330,15 @@ class SchemaGenerator extends core_1.AbstractSchemaGenerator {
|
|
|
283
330
|
this.createForeignKey(table, foreignKey);
|
|
284
331
|
}
|
|
285
332
|
for (const index of Object.values(diff.addedIndexes)) {
|
|
286
|
-
this.createIndex(table, index, diff.
|
|
333
|
+
this.createIndex(table, index, diff.toTable);
|
|
287
334
|
}
|
|
288
335
|
for (const index of Object.values(diff.changedIndexes)) {
|
|
289
|
-
this.createIndex(table, index, diff.
|
|
336
|
+
this.createIndex(table, index, diff.toTable, true);
|
|
290
337
|
}
|
|
291
338
|
for (const [oldIndexName, index] of Object.entries(diff.renamedIndexes)) {
|
|
292
339
|
if (index.unique) {
|
|
293
340
|
this.dropIndex(table, index, oldIndexName);
|
|
294
|
-
this.createIndex(table, index, diff.
|
|
341
|
+
this.createIndex(table, index, diff.toTable);
|
|
295
342
|
}
|
|
296
343
|
else {
|
|
297
344
|
this.helper.pushTableQuery(table, this.helper.getRenameIndexSQL(diff.name, index, oldIndexName));
|
|
@@ -304,9 +351,17 @@ class SchemaGenerator extends core_1.AbstractSchemaGenerator {
|
|
|
304
351
|
this.createCheck(table, check);
|
|
305
352
|
}
|
|
306
353
|
if ('changedComment' in diff) {
|
|
307
|
-
|
|
354
|
+
const comment = diff.changedComment ? this.platform.quoteValue(diff.changedComment).replace(/^'|'$/g, '') : '';
|
|
355
|
+
table.comment(comment);
|
|
308
356
|
}
|
|
309
357
|
}));
|
|
358
|
+
if (this.platform.supportsNativeEnums()) {
|
|
359
|
+
changedNativeEnums.forEach(([enumName, itemsNew, itemsOld]) => {
|
|
360
|
+
// postgres allows only adding new items
|
|
361
|
+
const newItems = itemsNew.filter(val => !itemsOld.includes(val));
|
|
362
|
+
ret.push(...newItems.map(val => this.knex.schema.raw(this.helper.getAlterNativeEnumSQL(enumName, schemaName, val))));
|
|
363
|
+
});
|
|
364
|
+
}
|
|
310
365
|
return ret;
|
|
311
366
|
}
|
|
312
367
|
/**
|
|
@@ -318,19 +373,26 @@ class SchemaGenerator extends core_1.AbstractSchemaGenerator {
|
|
|
318
373
|
await this.driver.reconnect();
|
|
319
374
|
}
|
|
320
375
|
async dropDatabase(name) {
|
|
376
|
+
name ??= this.config.get('dbName');
|
|
321
377
|
this.config.set('dbName', this.helper.getManagementDbName());
|
|
322
378
|
await this.driver.reconnect();
|
|
323
379
|
await this.driver.execute(this.helper.getDropDatabaseSQL('' + this.knex.ref(name)));
|
|
324
380
|
}
|
|
325
381
|
async execute(sql, options = {}) {
|
|
326
|
-
options.wrap
|
|
382
|
+
options.wrap ??= false;
|
|
327
383
|
const lines = this.wrapSchema(sql, options).split('\n').filter(i => i.trim());
|
|
328
|
-
|
|
329
|
-
|
|
384
|
+
if (lines.length === 0) {
|
|
385
|
+
return;
|
|
386
|
+
}
|
|
387
|
+
if (this.platform.supportsMultipleStatements()) {
|
|
388
|
+
const query = lines.join('\n');
|
|
389
|
+
await this.driver.execute(query);
|
|
390
|
+
return;
|
|
330
391
|
}
|
|
392
|
+
await core_1.Utils.runSerial(lines, line => this.driver.execute(line));
|
|
331
393
|
}
|
|
332
|
-
wrapSchema(sql, options
|
|
333
|
-
options.wrap
|
|
394
|
+
wrapSchema(sql, options) {
|
|
395
|
+
options.wrap ??= this.options.disableForeignKeys;
|
|
334
396
|
if (!options.wrap || sql.trim() === '') {
|
|
335
397
|
return sql;
|
|
336
398
|
}
|
|
@@ -350,16 +412,18 @@ class SchemaGenerator extends core_1.AbstractSchemaGenerator {
|
|
|
350
412
|
return this.createSchemaBuilder(tableDef.schema).createTable(tableDef.name, table => {
|
|
351
413
|
tableDef.getColumns().forEach(column => {
|
|
352
414
|
const col = this.helper.createTableColumn(table, column, tableDef);
|
|
353
|
-
this.configureColumn(column, col);
|
|
415
|
+
this.helper.configureColumn(column, col, this.knex);
|
|
354
416
|
});
|
|
355
417
|
for (const index of tableDef.getIndexes()) {
|
|
356
|
-
|
|
418
|
+
const createPrimary = !tableDef.getColumns().some(c => c.autoincrement && c.primary) || this.helper.hasNonDefaultPrimaryKeyName(tableDef);
|
|
419
|
+
this.createIndex(table, index, tableDef, createPrimary);
|
|
357
420
|
}
|
|
358
421
|
for (const check of tableDef.getChecks()) {
|
|
359
422
|
this.createCheck(table, check);
|
|
360
423
|
}
|
|
361
424
|
if (tableDef.comment) {
|
|
362
|
-
|
|
425
|
+
const comment = this.platform.quoteValue(tableDef.comment).replace(/^'|'$/g, '');
|
|
426
|
+
table.comment(comment);
|
|
363
427
|
}
|
|
364
428
|
if (!this.helper.supportsSchemaConstraints()) {
|
|
365
429
|
for (const fk of Object.values(tableDef.getForeignKeys())) {
|
|
@@ -369,30 +433,49 @@ class SchemaGenerator extends core_1.AbstractSchemaGenerator {
|
|
|
369
433
|
this.helper.finalizeTable(table, this.config.get('charset'), this.config.get('collate'));
|
|
370
434
|
});
|
|
371
435
|
}
|
|
372
|
-
createIndex(table, index,
|
|
436
|
+
createIndex(table, index, tableDef, createPrimary = false) {
|
|
373
437
|
if (index.primary && !createPrimary) {
|
|
374
438
|
return;
|
|
375
439
|
}
|
|
376
440
|
if (index.primary) {
|
|
377
|
-
|
|
378
|
-
const keyName = tableName.includes('.') ? tableName.split('.').pop() + '_pkey' : undefined;
|
|
441
|
+
const keyName = this.helper.hasNonDefaultPrimaryKeyName(tableDef) ? index.keyName : undefined;
|
|
379
442
|
table.primary(index.columnNames, keyName);
|
|
380
443
|
}
|
|
381
444
|
else if (index.unique) {
|
|
382
|
-
|
|
445
|
+
// JSON columns can have unique index but not unique constraint, and we need to distinguish those, so we can properly drop them
|
|
446
|
+
if (index.columnNames.some(column => column.includes('.'))) {
|
|
447
|
+
const columns = this.platform.getJsonIndexDefinition(index);
|
|
448
|
+
table.index(columns.map(column => this.knex.raw(`(${column})`)), index.keyName, { indexType: 'unique' });
|
|
449
|
+
}
|
|
450
|
+
else {
|
|
451
|
+
table.unique(index.columnNames, { indexName: index.keyName });
|
|
452
|
+
}
|
|
383
453
|
}
|
|
384
454
|
else if (index.expression) {
|
|
385
455
|
this.helper.pushTableQuery(table, index.expression);
|
|
386
456
|
}
|
|
457
|
+
else if (index.type === 'fulltext') {
|
|
458
|
+
const columns = index.columnNames.map(name => ({ name, type: tableDef.getColumn(name).type }));
|
|
459
|
+
if (this.platform.supportsCreatingFullTextIndex()) {
|
|
460
|
+
this.helper.pushTableQuery(table, this.platform.getFullTextIndexExpression(index.keyName, tableDef.schema, tableDef.name, columns));
|
|
461
|
+
}
|
|
462
|
+
}
|
|
387
463
|
else {
|
|
388
|
-
|
|
464
|
+
// JSON columns can have unique index but not unique constraint, and we need to distinguish those, so we can properly drop them
|
|
465
|
+
if (index.columnNames.some(column => column.includes('.'))) {
|
|
466
|
+
const columns = this.platform.getJsonIndexDefinition(index);
|
|
467
|
+
table.index(columns.map(column => this.knex.raw(`(${column})`)), index.keyName, index.type);
|
|
468
|
+
}
|
|
469
|
+
else {
|
|
470
|
+
table.index(index.columnNames, index.keyName, index.type);
|
|
471
|
+
}
|
|
389
472
|
}
|
|
390
473
|
}
|
|
391
474
|
dropIndex(table, index, oldIndexName = index.keyName) {
|
|
392
475
|
if (index.primary) {
|
|
393
476
|
table.dropPrimary(oldIndexName);
|
|
394
477
|
}
|
|
395
|
-
else if (index.unique) {
|
|
478
|
+
else if (index.unique && index.constraint) {
|
|
396
479
|
table.dropUnique(index.columnNames, oldIndexName);
|
|
397
480
|
}
|
|
398
481
|
else {
|
|
@@ -412,9 +495,6 @@ class SchemaGenerator extends core_1.AbstractSchemaGenerator {
|
|
|
412
495
|
}
|
|
413
496
|
return builder;
|
|
414
497
|
}
|
|
415
|
-
configureColumn(column, col, changedProperties) {
|
|
416
|
-
return this.helper.configureColumn(column, col, this.knex, changedProperties);
|
|
417
|
-
}
|
|
418
498
|
createForeignKeys(table, tableDef, schema) {
|
|
419
499
|
if (!this.helper.supportsSchemaConstraints()) {
|
|
420
500
|
return;
|
|
@@ -433,5 +513,9 @@ class SchemaGenerator extends core_1.AbstractSchemaGenerator {
|
|
|
433
513
|
const tmp = dump.replace(/pragma table_.+/ig, '').replace(/\n\n+/g, '\n').trim();
|
|
434
514
|
return tmp ? tmp + append : '';
|
|
435
515
|
}
|
|
516
|
+
get knex() {
|
|
517
|
+
return this.connection.getKnex();
|
|
518
|
+
}
|
|
436
519
|
}
|
|
437
|
-
exports.
|
|
520
|
+
exports.SqlSchemaGenerator = SqlSchemaGenerator;
|
|
521
|
+
exports.SchemaGenerator = SqlSchemaGenerator;
|
|
@@ -1,7 +1,11 @@
|
|
|
1
1
|
"use strict";
|
|
2
2
|
var __createBinding = (this && this.__createBinding) || (Object.create ? (function(o, m, k, k2) {
|
|
3
3
|
if (k2 === undefined) k2 = k;
|
|
4
|
-
Object.
|
|
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);
|
|
5
9
|
}) : (function(o, m, k, k2) {
|
|
6
10
|
if (k2 === undefined) k2 = k;
|
|
7
11
|
o[k2] = m[k];
|
|
@@ -12,6 +16,6 @@ var __exportStar = (this && this.__exportStar) || function(m, exports) {
|
|
|
12
16
|
Object.defineProperty(exports, "__esModule", { value: true });
|
|
13
17
|
__exportStar(require("./DatabaseSchema"), exports);
|
|
14
18
|
__exportStar(require("./DatabaseTable"), exports);
|
|
15
|
-
__exportStar(require("./
|
|
19
|
+
__exportStar(require("./SqlSchemaGenerator"), exports);
|
|
16
20
|
__exportStar(require("./SchemaHelper"), exports);
|
|
17
21
|
__exportStar(require("./SchemaComparator"), exports);
|
|
@@ -1,5 +1,5 @@
|
|
|
1
1
|
import type { Knex } from 'knex';
|
|
2
|
-
import type { CheckCallback, Dictionary, EntityProperty, GroupOperator, QBFilterQuery, QueryOrderMap, Type } from '@mikro-orm/core';
|
|
2
|
+
import type { CheckCallback, Dictionary, EntityProperty, GroupOperator, RawQueryFragment, QBFilterQuery, QueryOrderMap, Type } from '@mikro-orm/core';
|
|
3
3
|
import type { QueryType } from './query/enums';
|
|
4
4
|
import type { DatabaseSchema, DatabaseTable } from './schema';
|
|
5
5
|
export interface Table {
|
|
@@ -7,10 +7,11 @@ export interface Table {
|
|
|
7
7
|
schema_name?: string;
|
|
8
8
|
table_comment?: string;
|
|
9
9
|
}
|
|
10
|
-
export
|
|
10
|
+
export type KnexStringRef = Knex.Ref<string, {
|
|
11
11
|
[alias: string]: string;
|
|
12
12
|
}>;
|
|
13
|
-
|
|
13
|
+
type AnyString = string & {};
|
|
14
|
+
export type Field<T> = AnyString | keyof T | RawQueryFragment | KnexStringRef | Knex.QueryBuilder;
|
|
14
15
|
export interface JoinOptions {
|
|
15
16
|
table: string;
|
|
16
17
|
schema?: string;
|
|
@@ -24,6 +25,7 @@ export interface JoinOptions {
|
|
|
24
25
|
path?: string;
|
|
25
26
|
prop: EntityProperty;
|
|
26
27
|
cond: Dictionary;
|
|
28
|
+
cond_?: Dictionary;
|
|
27
29
|
}
|
|
28
30
|
export interface Column {
|
|
29
31
|
name: string;
|
|
@@ -37,11 +39,13 @@ export interface Column {
|
|
|
37
39
|
scale?: number;
|
|
38
40
|
default?: string | null;
|
|
39
41
|
comment?: string;
|
|
42
|
+
nativeEnumName?: string;
|
|
40
43
|
enumItems?: string[];
|
|
41
44
|
primary?: boolean;
|
|
42
45
|
unique?: boolean;
|
|
43
46
|
/** mysql only */
|
|
44
47
|
extra?: string;
|
|
48
|
+
ignoreSchemaChanges?: ('type' | 'extra')[];
|
|
45
49
|
}
|
|
46
50
|
export interface ForeignKey {
|
|
47
51
|
columnNames: string[];
|
|
@@ -52,20 +56,22 @@ export interface ForeignKey {
|
|
|
52
56
|
updateRule?: string;
|
|
53
57
|
deleteRule?: string;
|
|
54
58
|
}
|
|
55
|
-
export interface
|
|
59
|
+
export interface IndexDef {
|
|
56
60
|
columnNames: string[];
|
|
57
61
|
keyName: string;
|
|
58
62
|
unique: boolean;
|
|
63
|
+
constraint: boolean;
|
|
59
64
|
primary: boolean;
|
|
60
65
|
composite?: boolean;
|
|
61
66
|
expression?: string;
|
|
67
|
+
options?: Dictionary;
|
|
62
68
|
type?: string | Readonly<{
|
|
63
69
|
indexType?: string;
|
|
64
70
|
storageEngineIndexType?: 'hash' | 'btree';
|
|
65
71
|
predicate?: Knex.QueryBuilder;
|
|
66
72
|
}>;
|
|
67
73
|
}
|
|
68
|
-
export interface
|
|
74
|
+
export interface CheckDef<T = unknown> {
|
|
69
75
|
name: string;
|
|
70
76
|
expression: string | CheckCallback<T>;
|
|
71
77
|
definition?: string;
|
|
@@ -81,17 +87,18 @@ export interface TableDifference {
|
|
|
81
87
|
name: string;
|
|
82
88
|
changedComment?: string;
|
|
83
89
|
fromTable: DatabaseTable;
|
|
90
|
+
toTable: DatabaseTable;
|
|
84
91
|
addedColumns: Dictionary<Column>;
|
|
85
92
|
changedColumns: Dictionary<ColumnDifference>;
|
|
86
93
|
removedColumns: Dictionary<Column>;
|
|
87
94
|
renamedColumns: Dictionary<Column>;
|
|
88
|
-
addedIndexes: Dictionary<
|
|
89
|
-
changedIndexes: Dictionary<
|
|
90
|
-
removedIndexes: Dictionary<
|
|
91
|
-
renamedIndexes: Dictionary<
|
|
92
|
-
addedChecks: Dictionary<
|
|
93
|
-
changedChecks: Dictionary<
|
|
94
|
-
removedChecks: Dictionary<
|
|
95
|
+
addedIndexes: Dictionary<IndexDef>;
|
|
96
|
+
changedIndexes: Dictionary<IndexDef>;
|
|
97
|
+
removedIndexes: Dictionary<IndexDef>;
|
|
98
|
+
renamedIndexes: Dictionary<IndexDef>;
|
|
99
|
+
addedChecks: Dictionary<CheckDef>;
|
|
100
|
+
changedChecks: Dictionary<CheckDef>;
|
|
101
|
+
removedChecks: Dictionary<CheckDef>;
|
|
95
102
|
addedForeignKeys: Dictionary<ForeignKey>;
|
|
96
103
|
changedForeignKeys: Dictionary<ForeignKey>;
|
|
97
104
|
removedForeignKeys: Dictionary<ForeignKey>;
|
|
@@ -107,7 +114,7 @@ export interface SchemaDifference {
|
|
|
107
114
|
}
|
|
108
115
|
export interface IQueryBuilder<T> {
|
|
109
116
|
readonly alias: string;
|
|
110
|
-
readonly type
|
|
117
|
+
readonly type?: QueryType;
|
|
111
118
|
_fields?: Field<T>[];
|
|
112
119
|
select(fields: Field<T> | Field<T>[], distinct?: boolean): this;
|
|
113
120
|
addSelect(fields: string | string[]): this;
|
|
@@ -119,7 +126,8 @@ export interface IQueryBuilder<T> {
|
|
|
119
126
|
join(field: string, alias: string, cond?: QBFilterQuery, type?: 'leftJoin' | 'innerJoin' | 'pivotJoin', path?: string): this;
|
|
120
127
|
leftJoin(field: string, alias: string, cond?: QBFilterQuery): this;
|
|
121
128
|
joinAndSelect(field: string, alias: string, cond?: QBFilterQuery): this;
|
|
122
|
-
leftJoinAndSelect(field: string, alias: string, cond?: QBFilterQuery): this;
|
|
129
|
+
leftJoinAndSelect(field: string, alias: string, cond?: QBFilterQuery, fields?: string[]): this;
|
|
130
|
+
innerJoinAndSelect(field: string, alias: string, cond?: QBFilterQuery, fields?: string[]): this;
|
|
123
131
|
withSubQuery(subQuery: Knex.QueryBuilder, alias: string): this;
|
|
124
132
|
where(cond: QBFilterQuery<T>, operator?: keyof typeof GroupOperator): this;
|
|
125
133
|
where(cond: string, params?: any[], operator?: keyof typeof GroupOperator): this;
|
|
@@ -133,18 +141,19 @@ export interface IQueryBuilder<T> {
|
|
|
133
141
|
getAliasForJoinPath(path: string): string | undefined;
|
|
134
142
|
getNextAlias(entityName?: string): string;
|
|
135
143
|
}
|
|
136
|
-
export interface ICriteriaNode {
|
|
144
|
+
export interface ICriteriaNode<T extends object> {
|
|
137
145
|
readonly entityName: string;
|
|
138
|
-
readonly parent?: ICriteriaNode | undefined;
|
|
146
|
+
readonly parent?: ICriteriaNode<T> | undefined;
|
|
139
147
|
readonly key?: string | undefined;
|
|
140
148
|
payload: any;
|
|
141
149
|
prop?: EntityProperty;
|
|
142
150
|
index?: number;
|
|
143
|
-
process
|
|
151
|
+
process(qb: IQueryBuilder<T>, alias?: string): any;
|
|
144
152
|
shouldInline(payload: any): boolean;
|
|
145
|
-
willAutoJoin
|
|
153
|
+
willAutoJoin(qb: IQueryBuilder<T>, alias?: string): boolean;
|
|
146
154
|
shouldRename(payload: any): boolean;
|
|
147
155
|
renameFieldToPK<T>(qb: IQueryBuilder<T>): string;
|
|
148
156
|
getPath(addIndex?: boolean): string;
|
|
149
157
|
getPivotPath(path: string): string;
|
|
150
158
|
}
|
|
159
|
+
export {};
|