@mikro-orm/knex 6.0.0-dev.4 → 6.0.0-dev.40

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.
Files changed (56) hide show
  1. package/{dist/AbstractSqlConnection.d.ts → AbstractSqlConnection.d.ts} +0 -0
  2. package/{dist/AbstractSqlConnection.js → AbstractSqlConnection.js} +6 -1
  3. package/AbstractSqlDriver.d.ts +66 -0
  4. package/{dist/AbstractSqlDriver.js → AbstractSqlDriver.js} +268 -93
  5. package/{dist/AbstractSqlPlatform.d.ts → AbstractSqlPlatform.d.ts} +7 -7
  6. package/{dist/AbstractSqlPlatform.js → AbstractSqlPlatform.js} +12 -14
  7. package/{dist/MonkeyPatchable.d.ts → MonkeyPatchable.d.ts} +0 -0
  8. package/{dist/MonkeyPatchable.js → MonkeyPatchable.js} +0 -0
  9. package/{dist/README.md → README.md} +2 -2
  10. package/{dist/SqlEntityManager.d.ts → SqlEntityManager.d.ts} +5 -5
  11. package/{dist/SqlEntityManager.js → SqlEntityManager.js} +1 -2
  12. package/{dist/SqlEntityRepository.d.ts → SqlEntityRepository.d.ts} +3 -4
  13. package/{dist/SqlEntityRepository.js → SqlEntityRepository.js} +1 -2
  14. package/{dist/index.d.ts → index.d.ts} +1 -0
  15. package/{dist/index.js → index.js} +6 -1
  16. package/index.mjs +194 -0
  17. package/package.json +18 -47
  18. package/query/ArrayCriteriaNode.d.ts +9 -0
  19. package/{dist/query → query}/ArrayCriteriaNode.js +0 -0
  20. package/{dist/query → query}/CriteriaNode.d.ts +8 -8
  21. package/{dist/query → query}/CriteriaNode.js +15 -12
  22. package/query/CriteriaNodeFactory.d.ts +12 -0
  23. package/{dist/query → query}/CriteriaNodeFactory.js +10 -10
  24. package/{dist/query → query}/ObjectCriteriaNode.d.ts +3 -3
  25. package/{dist/query → query}/ObjectCriteriaNode.js +19 -6
  26. package/{dist/query → query}/QueryBuilder.d.ts +66 -24
  27. package/{dist/query → query}/QueryBuilder.js +381 -94
  28. package/{dist/query → query}/QueryBuilderHelper.d.ts +19 -11
  29. package/{dist/query → query}/QueryBuilderHelper.js +227 -116
  30. package/{dist/query → query}/ScalarCriteriaNode.d.ts +2 -2
  31. package/{dist/query → query}/ScalarCriteriaNode.js +6 -6
  32. package/{dist/query → query}/enums.d.ts +0 -0
  33. package/{dist/query → query}/enums.js +0 -0
  34. package/{dist/query → query}/index.d.ts +0 -0
  35. package/{dist/query → query}/index.js +5 -1
  36. package/{dist/schema → schema}/DatabaseSchema.d.ts +1 -1
  37. package/{dist/schema → schema}/DatabaseSchema.js +11 -19
  38. package/{dist/schema → schema}/DatabaseTable.d.ts +9 -9
  39. package/{dist/schema → schema}/DatabaseTable.js +22 -22
  40. package/{dist/schema → schema}/SchemaComparator.d.ts +7 -5
  41. package/{dist/schema → schema}/SchemaComparator.js +60 -10
  42. package/{dist/schema → schema}/SchemaHelper.d.ts +15 -10
  43. package/{dist/schema → schema}/SchemaHelper.js +46 -8
  44. package/{dist/schema/SchemaGenerator.d.ts → schema/SqlSchemaGenerator.d.ts} +10 -12
  45. package/{dist/schema/SchemaGenerator.js → schema/SqlSchemaGenerator.js} +89 -45
  46. package/{dist/schema → schema}/index.d.ts +1 -1
  47. package/{dist/schema → schema}/index.js +6 -2
  48. package/{dist/typings.d.ts → typings.d.ts} +21 -16
  49. package/{dist/typings.js → typings.js} +0 -0
  50. package/CHANGELOG.md +0 -661
  51. package/dist/AbstractSqlDriver.d.ts +0 -55
  52. package/dist/LICENSE +0 -21
  53. package/dist/index.mjs +0 -26
  54. package/dist/package.json +0 -99
  55. package/dist/query/ArrayCriteriaNode.d.ts +0 -9
  56. package/dist/query/CriteriaNodeFactory.d.ts +0 -12
@@ -24,25 +24,51 @@ class SchemaHelper {
24
24
  supportsSchemaConstraints() {
25
25
  return true;
26
26
  }
27
- async getPrimaryKeys(connection, indexes, tableName, schemaName) {
28
- const pk = indexes.find(i => i.primary);
29
- return pk ? pk.columnNames : [];
27
+ async getPrimaryKeys(connection, indexes = [], tableName, schemaName) {
28
+ const pks = indexes.filter(i => i.primary).map(pk => pk.columnNames);
29
+ return core_1.Utils.flatten(pks);
30
30
  }
31
31
  async getForeignKeys(connection, tableName, schemaName) {
32
32
  const fks = await connection.execute(this.getForeignKeysSQL(tableName, schemaName));
33
33
  return this.mapForeignKeys(fks, tableName, schemaName);
34
34
  }
35
+ getTableKey(t) {
36
+ const unquote = (str) => str.replace(/['"`]/g, '');
37
+ const parts = t.table_name.split('.');
38
+ if (parts.length > 1) {
39
+ return `${unquote(parts[0])}.${unquote(parts[1])}`;
40
+ }
41
+ if (t.schema_name) {
42
+ return `${unquote(t.schema_name)}.${unquote(t.table_name)}`;
43
+ }
44
+ return unquote(t.table_name);
45
+ }
35
46
  async getEnumDefinitions(connection, checks, tableName, schemaName) {
36
47
  return {};
37
48
  }
49
+ async loadInformationSchema(schema, connection, tables) {
50
+ for (const t of tables) {
51
+ const table = schema.addTable(t.table_name, t.schema_name);
52
+ table.comment = t.table_comment;
53
+ const cols = await this.getColumns(connection, table.name, table.schema);
54
+ const indexes = await this.getIndexes(connection, table.name, table.schema);
55
+ const checks = await this.getChecks(connection, table.name, table.schema, cols);
56
+ const pks = await this.getPrimaryKeys(connection, indexes, table.name, table.schema);
57
+ const fks = await this.getForeignKeys(connection, table.name, table.schema);
58
+ const enums = await this.getEnumDefinitions(connection, checks, table.name, table.schema);
59
+ table.init(cols, indexes, checks, pks, fks, enums);
60
+ }
61
+ }
38
62
  getListTablesSQL(schemaName) {
39
63
  throw new Error('Not supported by given driver');
40
64
  }
41
- getRenameColumnSQL(tableName, oldColumnName, to) {
65
+ getRenameColumnSQL(tableName, oldColumnName, to, schemaName) {
42
66
  tableName = this.platform.quoteIdentifier(tableName);
43
67
  oldColumnName = this.platform.quoteIdentifier(oldColumnName);
44
68
  const columnName = this.platform.quoteIdentifier(to.name);
45
- return `alter table ${tableName} rename column ${oldColumnName} to ${columnName}`;
69
+ const schemaReference = (schemaName !== undefined && schemaName !== 'public') ? ('"' + schemaName + '".') : '';
70
+ const tableReference = schemaReference + tableName;
71
+ return `alter table ${tableReference} rename column ${oldColumnName} to ${columnName}`;
46
72
  }
47
73
  getCreateIndexSQL(tableName, index) {
48
74
  /* istanbul ignore if */
@@ -59,13 +85,22 @@ class SchemaHelper {
59
85
  getRenameIndexSQL(tableName, index, oldIndexName) {
60
86
  return [this.getDropIndexSQL(tableName, { ...index, keyName: oldIndexName }), this.getCreateIndexSQL(tableName, index)].join(';\n');
61
87
  }
88
+ hasNonDefaultPrimaryKeyName(table) {
89
+ const pkIndex = table.getPrimaryKey();
90
+ if (!pkIndex || !this.platform.supportsCustomPrimaryKeyNames()) {
91
+ return false;
92
+ }
93
+ const defaultName = this.platform.getDefaultPrimaryName(table.name, pkIndex.columnNames);
94
+ return pkIndex?.keyName !== defaultName;
95
+ }
62
96
  createTableColumn(table, column, fromTable, changedProperties) {
63
97
  const compositePK = fromTable.getPrimaryKey()?.composite;
64
98
  if (column.autoincrement && !compositePK && (!changedProperties || changedProperties.has('autoincrement') || changedProperties.has('type'))) {
99
+ const primaryKey = !changedProperties && !this.hasNonDefaultPrimaryKeyName(fromTable);
65
100
  if (column.mappedType instanceof core_1.BigIntType) {
66
- return table.bigIncrements(column.name, { primaryKey: !changedProperties });
101
+ return table.bigIncrements(column.name, { primaryKey });
67
102
  }
68
- return table.increments(column.name, { primaryKey: !changedProperties });
103
+ return table.increments(column.name, { primaryKey });
69
104
  }
70
105
  if (column.mappedType instanceof core_1.EnumType && column.enumItems?.every(item => core_1.Utils.isString(item))) {
71
106
  return table.enum(column.name, column.enumItems);
@@ -100,13 +135,16 @@ class SchemaHelper {
100
135
  getChangeColumnCommentSQL(tableName, to) {
101
136
  return '';
102
137
  }
138
+ async getNamespaces(connection) {
139
+ return [];
140
+ }
103
141
  async getColumns(connection, tableName, schemaName) {
104
142
  throw new Error('Not supported by given driver');
105
143
  }
106
144
  async getIndexes(connection, tableName, schemaName) {
107
145
  throw new Error('Not supported by given driver');
108
146
  }
109
- async getChecks(connection, tableName, schemaName) {
147
+ async getChecks(connection, tableName, schemaName, columns) {
110
148
  throw new Error('Not supported by given driver');
111
149
  }
112
150
  async mapIndexes(indexes) {
@@ -1,20 +1,13 @@
1
+ import type { MikroORM, ISchemaGenerator } from '@mikro-orm/core';
1
2
  import { AbstractSchemaGenerator } from '@mikro-orm/core';
2
3
  import type { SchemaDifference } from '../typings';
3
4
  import { DatabaseSchema } from './DatabaseSchema';
4
5
  import type { AbstractSqlDriver } from '../AbstractSqlDriver';
5
- import { SqlEntityManager } from '../SqlEntityManager';
6
- /**
7
- * Should be renamed to `SqlSchemaGenerator` in v6
8
- */
9
- export declare class SchemaGenerator extends AbstractSchemaGenerator<AbstractSqlDriver> {
6
+ export declare class SqlSchemaGenerator extends AbstractSchemaGenerator<AbstractSqlDriver> implements ISchemaGenerator {
10
7
  private readonly helper;
11
- private readonly knex;
12
8
  private readonly options;
13
- /**
14
- * @deprecated For back compatibility, we also support EM as the parameter.
15
- */
16
- constructor(em: SqlEntityManager);
17
- generate(): Promise<string>;
9
+ protected lastEnsuredDatabase?: string;
10
+ static register(orm: MikroORM): void;
18
11
  createSchema(options?: {
19
12
  wrap?: boolean;
20
13
  schema?: string;
@@ -34,6 +27,10 @@ export declare class SchemaGenerator extends AbstractSchemaGenerator<AbstractSql
34
27
  dropDb?: boolean;
35
28
  schema?: string;
36
29
  }): Promise<void>;
30
+ clearDatabase(options?: {
31
+ schema?: string;
32
+ truncate?: boolean;
33
+ }): Promise<void>;
37
34
  getDropSchemaSQL(options?: {
38
35
  wrap?: boolean;
39
36
  dropMigrationsTable?: boolean;
@@ -95,7 +92,8 @@ export declare class SchemaGenerator extends AbstractSchemaGenerator<AbstractSql
95
92
  private createCheck;
96
93
  private dropCheck;
97
94
  private dropTable;
98
- private configureColumn;
99
95
  private createForeignKeys;
100
96
  private dump;
97
+ private get knex();
101
98
  }
99
+ export { SqlSchemaGenerator as SchemaGenerator };
@@ -1,29 +1,17 @@
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
- const SqlEntityManager_1 = require("../SqlEntityManager");
8
- /**
9
- * Should be renamed to `SqlSchemaGenerator` in v6
10
- */
11
- class SchemaGenerator extends core_1.AbstractSchemaGenerator {
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);
7
+ class SqlSchemaGenerator extends core_1.AbstractSchemaGenerator {
8
+ constructor() {
9
+ super(...arguments);
17
10
  this.helper = this.platform.getSchemaHelper();
18
- this.knex = this.connection.getKnex();
19
11
  this.options = this.config.get('schemaGenerator');
20
12
  }
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);
13
+ static register(orm) {
14
+ orm.config.registerExtension('@mikro-orm/schema-generator', () => new SqlSchemaGenerator(orm.em));
27
15
  }
28
16
  async createSchema(options) {
29
17
  await this.ensureDatabase();
@@ -35,7 +23,11 @@ class SchemaGenerator extends core_1.AbstractSchemaGenerator {
35
23
  */
36
24
  async ensureDatabase() {
37
25
  const dbName = this.config.get('dbName');
26
+ if (this.lastEnsuredDatabase === dbName) {
27
+ return true;
28
+ }
38
29
  const exists = await this.helper.databaseExists(this.connection, dbName);
30
+ this.lastEnsuredDatabase = dbName;
39
31
  if (!exists) {
40
32
  this.config.set('dbName', this.helper.getManagementDbName());
41
33
  await this.driver.reconnect();
@@ -48,7 +40,8 @@ class SchemaGenerator extends core_1.AbstractSchemaGenerator {
48
40
  }
49
41
  getTargetSchema(schema) {
50
42
  const metadata = this.getOrderedMetadata(schema);
51
- return DatabaseSchema_1.DatabaseSchema.fromMetadata(metadata, this.platform, this.config, schema ?? this.platform.getDefaultSchemaName());
43
+ const schemaName = schema ?? this.config.get('schema') ?? this.platform.getDefaultSchemaName();
44
+ return DatabaseSchema_1.DatabaseSchema.fromMetadata(metadata, this.platform, this.config, schemaName);
52
45
  }
53
46
  async getCreateSchemaSQL(options = {}) {
54
47
  const wrap = options.wrap ?? this.options.disableForeignKeys;
@@ -69,7 +62,6 @@ class SchemaGenerator extends core_1.AbstractSchemaGenerator {
69
62
  return this.wrapSchema(ret, { wrap });
70
63
  }
71
64
  async dropSchema(options = {}) {
72
- options.wrap ?? (options.wrap = true);
73
65
  if (options.dropDb) {
74
66
  const name = this.config.get('dbName');
75
67
  return this.dropDatabase(name);
@@ -77,10 +69,44 @@ class SchemaGenerator extends core_1.AbstractSchemaGenerator {
77
69
  const sql = await this.getDropSchemaSQL(options);
78
70
  await this.execute(sql);
79
71
  }
72
+ async clearDatabase(options) {
73
+ // truncate by default, so no value is considered as true
74
+ /* istanbul ignore if */
75
+ if (options?.truncate === false) {
76
+ return super.clearDatabase(options);
77
+ }
78
+ await this.execute(this.helper.disableForeignKeysSQL());
79
+ for (const meta of this.getOrderedMetadata(options?.schema).reverse()) {
80
+ await this.driver.createQueryBuilder(meta.className, this.em?.getTransactionContext(), 'write', false)
81
+ .withSchema(options?.schema)
82
+ .truncate();
83
+ }
84
+ await this.execute(this.helper.enableForeignKeysSQL());
85
+ if (this.em) {
86
+ const allowGlobalContext = this.config.get('allowGlobalContext');
87
+ this.config.set('allowGlobalContext', true);
88
+ this.em.clear();
89
+ this.config.set('allowGlobalContext', allowGlobalContext);
90
+ }
91
+ }
80
92
  async getDropSchemaSQL(options = {}) {
93
+ await this.ensureDatabase();
81
94
  const wrap = options.wrap ?? this.options.disableForeignKeys;
82
95
  const metadata = this.getOrderedMetadata(options.schema).reverse();
96
+ const schema = await DatabaseSchema_1.DatabaseSchema.create(this.connection, this.platform, this.config, options.schema);
83
97
  let ret = '';
98
+ // remove FKs explicitly if we can't use cascading statement and we don't disable FK checks (we need this for circular relations)
99
+ for (const meta of metadata) {
100
+ const table = schema.getTable(meta.tableName);
101
+ if (!this.platform.usesCascadeStatement() && table && !wrap) {
102
+ for (const fk of Object.values(table.getForeignKeys())) {
103
+ const builder = this.createSchemaBuilder(table.schema).alterTable(table.name, tbl => {
104
+ tbl.dropForeign(fk.columnNames, fk.constraintName);
105
+ });
106
+ ret += await this.dump(builder, '\n');
107
+ }
108
+ }
109
+ }
84
110
  for (const meta of metadata) {
85
111
  ret += await this.dump(this.dropTable(meta.collection, this.getSchemaName(meta, options)), '\n');
86
112
  }
@@ -90,20 +116,23 @@ class SchemaGenerator extends core_1.AbstractSchemaGenerator {
90
116
  return this.wrapSchema(ret + '\n', { wrap });
91
117
  }
92
118
  getSchemaName(meta, options) {
119
+ const schemaName = options.schema ?? this.config.get('schema');
93
120
  /* istanbul ignore next */
94
- return meta.schema === '*' ? options.schema : meta.schema;
121
+ return meta.schema && meta.schema === '*' ? schemaName : (meta.schema ?? schemaName);
95
122
  }
96
123
  async updateSchema(options = {}) {
97
124
  const sql = await this.getUpdateSchemaSQL(options);
98
125
  await this.execute(sql);
99
126
  }
100
127
  async getUpdateSchemaSQL(options = {}) {
128
+ await this.ensureDatabase();
101
129
  const { fromSchema, toSchema } = await this.prepareSchemaForComparison(options);
102
130
  const comparator = new SchemaComparator_1.SchemaComparator(this.platform);
103
131
  const diffUp = comparator.compare(fromSchema, toSchema);
104
132
  return this.diffToSQL(diffUp, options);
105
133
  }
106
134
  async getUpdateSchemaMigrationSQL(options = {}) {
135
+ await this.ensureDatabase();
107
136
  const { fromSchema, toSchema } = await this.prepareSchemaForComparison(options);
108
137
  const comparator = new SchemaComparator_1.SchemaComparator(this.platform);
109
138
  const diffUp = comparator.compare(fromSchema, toSchema);
@@ -114,9 +143,9 @@ class SchemaGenerator extends core_1.AbstractSchemaGenerator {
114
143
  };
115
144
  }
116
145
  async prepareSchemaForComparison(options) {
117
- options.wrap ?? (options.wrap = this.options.disableForeignKeys);
118
- options.safe ?? (options.safe = false);
119
- options.dropTables ?? (options.dropTables = true);
146
+ options.wrap ??= this.options.disableForeignKeys;
147
+ options.safe ??= false;
148
+ options.dropTables ??= true;
120
149
  const toSchema = this.getTargetSchema(options.schema);
121
150
  const fromSchema = options.fromSchema ?? await DatabaseSchema_1.DatabaseSchema.create(this.connection, this.platform, this.config, options.schema);
122
151
  const wildcardSchemaTables = Object.values(this.metadata.getAll()).filter(meta => meta.schema === '*').map(meta => meta.tableName);
@@ -240,7 +269,7 @@ class SchemaGenerator extends core_1.AbstractSchemaGenerator {
240
269
  }
241
270
  for (const column of Object.values(diff.addedColumns)) {
242
271
  const col = this.helper.createTableColumn(table, column, diff.fromTable);
243
- this.configureColumn(column, col);
272
+ this.helper.configureColumn(column, col, this.knex);
244
273
  const foreignKey = Object.values(diff.addedForeignKeys).find(fk => fk.columnNames.length === 1 && fk.columnNames[0] === column.name);
245
274
  if (foreignKey && this.options.createForeignKeyConstraints) {
246
275
  delete diff.addedForeignKeys[foreignKey.constraintName];
@@ -262,7 +291,7 @@ class SchemaGenerator extends core_1.AbstractSchemaGenerator {
262
291
  continue;
263
292
  }
264
293
  const col = this.helper.createTableColumn(table, column, diff.fromTable, changedProperties).alter();
265
- this.configureColumn(column, col, changedProperties);
294
+ this.helper.configureColumn(column, col, this.knex, changedProperties);
266
295
  }
267
296
  for (const { column } of Object.values(diff.changedColumns).filter(diff => diff.changedProperties.has('autoincrement'))) {
268
297
  this.helper.pushTableQuery(table, this.helper.getAlterColumnAutoincrement(tableName, column));
@@ -274,7 +303,7 @@ class SchemaGenerator extends core_1.AbstractSchemaGenerator {
274
303
  this.helper.pushTableQuery(table, this.helper.getChangeColumnCommentSQL(tableName, column));
275
304
  }
276
305
  for (const [oldColumnName, column] of Object.entries(diff.renamedColumns)) {
277
- this.helper.pushTableQuery(table, this.helper.getRenameColumnSQL(tableName, oldColumnName, column));
306
+ this.helper.pushTableQuery(table, this.helper.getRenameColumnSQL(tableName, oldColumnName, column, schemaName));
278
307
  }
279
308
  for (const foreignKey of Object.values(diff.addedForeignKeys)) {
280
309
  this.createForeignKey(table, foreignKey);
@@ -283,15 +312,15 @@ class SchemaGenerator extends core_1.AbstractSchemaGenerator {
283
312
  this.createForeignKey(table, foreignKey);
284
313
  }
285
314
  for (const index of Object.values(diff.addedIndexes)) {
286
- this.createIndex(table, index, diff.name);
315
+ this.createIndex(table, index, diff.toTable);
287
316
  }
288
317
  for (const index of Object.values(diff.changedIndexes)) {
289
- this.createIndex(table, index, diff.name, true);
318
+ this.createIndex(table, index, diff.toTable, true);
290
319
  }
291
320
  for (const [oldIndexName, index] of Object.entries(diff.renamedIndexes)) {
292
321
  if (index.unique) {
293
322
  this.dropIndex(table, index, oldIndexName);
294
- this.createIndex(table, index, diff.name);
323
+ this.createIndex(table, index, diff.toTable);
295
324
  }
296
325
  else {
297
326
  this.helper.pushTableQuery(table, this.helper.getRenameIndexSQL(diff.name, index, oldIndexName));
@@ -304,7 +333,8 @@ class SchemaGenerator extends core_1.AbstractSchemaGenerator {
304
333
  this.createCheck(table, check);
305
334
  }
306
335
  if ('changedComment' in diff) {
307
- table.comment(diff.changedComment ?? '');
336
+ const comment = diff.changedComment ? this.platform.quoteValue(diff.changedComment).replace(/^'|'$/g, '') : '';
337
+ table.comment(comment);
308
338
  }
309
339
  }));
310
340
  return ret;
@@ -323,14 +353,20 @@ class SchemaGenerator extends core_1.AbstractSchemaGenerator {
323
353
  await this.driver.execute(this.helper.getDropDatabaseSQL('' + this.knex.ref(name)));
324
354
  }
325
355
  async execute(sql, options = {}) {
326
- options.wrap ?? (options.wrap = false);
356
+ options.wrap ??= false;
327
357
  const lines = this.wrapSchema(sql, options).split('\n').filter(i => i.trim());
328
- for (const line of lines) {
329
- await this.driver.execute(line);
358
+ if (lines.length === 0) {
359
+ return;
330
360
  }
361
+ if (this.platform.supportsMultipleStatements()) {
362
+ const query = lines.join('\n');
363
+ await this.driver.execute(query);
364
+ return;
365
+ }
366
+ await core_1.Utils.runSerial(lines, line => this.driver.execute(line));
331
367
  }
332
368
  wrapSchema(sql, options = {}) {
333
- options.wrap ?? (options.wrap = this.options.disableForeignKeys);
369
+ options.wrap ??= this.options.disableForeignKeys;
334
370
  if (!options.wrap || sql.trim() === '') {
335
371
  return sql;
336
372
  }
@@ -350,16 +386,18 @@ class SchemaGenerator extends core_1.AbstractSchemaGenerator {
350
386
  return this.createSchemaBuilder(tableDef.schema).createTable(tableDef.name, table => {
351
387
  tableDef.getColumns().forEach(column => {
352
388
  const col = this.helper.createTableColumn(table, column, tableDef);
353
- this.configureColumn(column, col);
389
+ this.helper.configureColumn(column, col, this.knex);
354
390
  });
355
391
  for (const index of tableDef.getIndexes()) {
356
- this.createIndex(table, index, tableDef.name, !tableDef.getColumns().some(c => c.autoincrement));
392
+ const createPrimary = !tableDef.getColumns().some(c => c.autoincrement && c.primary) || this.helper.hasNonDefaultPrimaryKeyName(tableDef);
393
+ this.createIndex(table, index, tableDef, createPrimary);
357
394
  }
358
395
  for (const check of tableDef.getChecks()) {
359
396
  this.createCheck(table, check);
360
397
  }
361
398
  if (tableDef.comment) {
362
- table.comment(tableDef.comment);
399
+ const comment = this.platform.quoteValue(tableDef.comment).replace(/^'|'$/g, '');
400
+ table.comment(comment);
363
401
  }
364
402
  if (!this.helper.supportsSchemaConstraints()) {
365
403
  for (const fk of Object.values(tableDef.getForeignKeys())) {
@@ -369,13 +407,12 @@ class SchemaGenerator extends core_1.AbstractSchemaGenerator {
369
407
  this.helper.finalizeTable(table, this.config.get('charset'), this.config.get('collate'));
370
408
  });
371
409
  }
372
- createIndex(table, index, tableName, createPrimary = false) {
410
+ createIndex(table, index, tableDef, createPrimary = false) {
373
411
  if (index.primary && !createPrimary) {
374
412
  return;
375
413
  }
376
414
  if (index.primary) {
377
- /* istanbul ignore next */
378
- const keyName = tableName.includes('.') ? tableName.split('.').pop() + '_pkey' : undefined;
415
+ const keyName = this.helper.hasNonDefaultPrimaryKeyName(tableDef) ? index.keyName : undefined;
379
416
  table.primary(index.columnNames, keyName);
380
417
  }
381
418
  else if (index.unique) {
@@ -384,6 +421,12 @@ class SchemaGenerator extends core_1.AbstractSchemaGenerator {
384
421
  else if (index.expression) {
385
422
  this.helper.pushTableQuery(table, index.expression);
386
423
  }
424
+ else if (index.type === 'fulltext') {
425
+ const columns = index.columnNames.map(name => ({ name, type: tableDef.getColumn(name).type }));
426
+ if (this.platform.supportsCreatingFullTextIndex()) {
427
+ this.helper.pushTableQuery(table, this.platform.getFullTextIndexExpression(index.keyName, tableDef.schema, tableDef.name, columns));
428
+ }
429
+ }
387
430
  else {
388
431
  table.index(index.columnNames, index.keyName, index.type);
389
432
  }
@@ -412,9 +455,6 @@ class SchemaGenerator extends core_1.AbstractSchemaGenerator {
412
455
  }
413
456
  return builder;
414
457
  }
415
- configureColumn(column, col, changedProperties) {
416
- return this.helper.configureColumn(column, col, this.knex, changedProperties);
417
- }
418
458
  createForeignKeys(table, tableDef, schema) {
419
459
  if (!this.helper.supportsSchemaConstraints()) {
420
460
  return;
@@ -433,5 +473,9 @@ class SchemaGenerator extends core_1.AbstractSchemaGenerator {
433
473
  const tmp = dump.replace(/pragma table_.+/ig, '').replace(/\n\n+/g, '\n').trim();
434
474
  return tmp ? tmp + append : '';
435
475
  }
476
+ get knex() {
477
+ return this.connection.getKnex();
478
+ }
436
479
  }
437
- exports.SchemaGenerator = SchemaGenerator;
480
+ exports.SqlSchemaGenerator = SqlSchemaGenerator;
481
+ exports.SchemaGenerator = SqlSchemaGenerator;
@@ -1,5 +1,5 @@
1
1
  export * from './DatabaseSchema';
2
2
  export * from './DatabaseTable';
3
- export * from './SchemaGenerator';
3
+ export * from './SqlSchemaGenerator';
4
4
  export * from './SchemaHelper';
5
5
  export * from './SchemaComparator';
@@ -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.defineProperty(o, k2, { enumerable: true, get: function() { return m[k]; } });
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("./SchemaGenerator"), exports);
19
+ __exportStar(require("./SqlSchemaGenerator"), exports);
16
20
  __exportStar(require("./SchemaHelper"), exports);
17
21
  __exportStar(require("./SchemaComparator"), exports);
@@ -7,10 +7,11 @@ export interface Table {
7
7
  schema_name?: string;
8
8
  table_comment?: string;
9
9
  }
10
- export declare type KnexStringRef = Knex.Ref<string, {
10
+ export type KnexStringRef = Knex.Ref<string, {
11
11
  [alias: string]: string;
12
12
  }>;
13
- export declare type Field<T> = string | keyof T | KnexStringRef | Knex.QueryBuilder;
13
+ type AnyString = string & {};
14
+ export type Field<T> = AnyString | keyof T | 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;
@@ -42,6 +44,7 @@ export interface Column {
42
44
  unique?: boolean;
43
45
  /** mysql only */
44
46
  extra?: string;
47
+ ignoreSchemaChanges?: ('type' | 'extra')[];
45
48
  }
46
49
  export interface ForeignKey {
47
50
  columnNames: string[];
@@ -52,7 +55,7 @@ export interface ForeignKey {
52
55
  updateRule?: string;
53
56
  deleteRule?: string;
54
57
  }
55
- export interface Index {
58
+ export interface IndexDef {
56
59
  columnNames: string[];
57
60
  keyName: string;
58
61
  unique: boolean;
@@ -65,7 +68,7 @@ export interface Index {
65
68
  predicate?: Knex.QueryBuilder;
66
69
  }>;
67
70
  }
68
- export interface Check<T = unknown> {
71
+ export interface CheckDef<T = unknown> {
69
72
  name: string;
70
73
  expression: string | CheckCallback<T>;
71
74
  definition?: string;
@@ -81,17 +84,18 @@ export interface TableDifference {
81
84
  name: string;
82
85
  changedComment?: string;
83
86
  fromTable: DatabaseTable;
87
+ toTable: DatabaseTable;
84
88
  addedColumns: Dictionary<Column>;
85
89
  changedColumns: Dictionary<ColumnDifference>;
86
90
  removedColumns: Dictionary<Column>;
87
91
  renamedColumns: Dictionary<Column>;
88
- addedIndexes: Dictionary<Index>;
89
- changedIndexes: Dictionary<Index>;
90
- removedIndexes: Dictionary<Index>;
91
- renamedIndexes: Dictionary<Index>;
92
- addedChecks: Dictionary<Check>;
93
- changedChecks: Dictionary<Check>;
94
- removedChecks: Dictionary<Check>;
92
+ addedIndexes: Dictionary<IndexDef>;
93
+ changedIndexes: Dictionary<IndexDef>;
94
+ removedIndexes: Dictionary<IndexDef>;
95
+ renamedIndexes: Dictionary<IndexDef>;
96
+ addedChecks: Dictionary<CheckDef>;
97
+ changedChecks: Dictionary<CheckDef>;
98
+ removedChecks: Dictionary<CheckDef>;
95
99
  addedForeignKeys: Dictionary<ForeignKey>;
96
100
  changedForeignKeys: Dictionary<ForeignKey>;
97
101
  removedForeignKeys: Dictionary<ForeignKey>;
@@ -107,7 +111,7 @@ export interface SchemaDifference {
107
111
  }
108
112
  export interface IQueryBuilder<T> {
109
113
  readonly alias: string;
110
- readonly type: QueryType;
114
+ readonly type?: QueryType;
111
115
  _fields?: Field<T>[];
112
116
  select(fields: Field<T> | Field<T>[], distinct?: boolean): this;
113
117
  addSelect(fields: string | string[]): this;
@@ -133,18 +137,19 @@ export interface IQueryBuilder<T> {
133
137
  getAliasForJoinPath(path: string): string | undefined;
134
138
  getNextAlias(entityName?: string): string;
135
139
  }
136
- export interface ICriteriaNode {
140
+ export interface ICriteriaNode<T extends object> {
137
141
  readonly entityName: string;
138
- readonly parent?: ICriteriaNode | undefined;
142
+ readonly parent?: ICriteriaNode<T> | undefined;
139
143
  readonly key?: string | undefined;
140
144
  payload: any;
141
145
  prop?: EntityProperty;
142
146
  index?: number;
143
- process<T>(qb: IQueryBuilder<T>, alias?: string): any;
147
+ process(qb: IQueryBuilder<T>, alias?: string): any;
144
148
  shouldInline(payload: any): boolean;
145
- willAutoJoin<T>(qb: IQueryBuilder<T>, alias?: string): boolean;
149
+ willAutoJoin(qb: IQueryBuilder<T>, alias?: string): boolean;
146
150
  shouldRename(payload: any): boolean;
147
151
  renameFieldToPK<T>(qb: IQueryBuilder<T>): string;
148
152
  getPath(addIndex?: boolean): string;
149
153
  getPivotPath(path: string): string;
150
154
  }
155
+ export {};
File without changes