@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.
Files changed (60) hide show
  1. package/{dist/AbstractSqlConnection.d.ts → AbstractSqlConnection.d.ts} +8 -4
  2. package/{dist/AbstractSqlConnection.js → AbstractSqlConnection.js} +35 -18
  3. package/AbstractSqlDriver.d.ts +66 -0
  4. package/{dist/AbstractSqlDriver.js → AbstractSqlDriver.js} +422 -129
  5. package/AbstractSqlPlatform.d.ts +22 -0
  6. package/{dist/AbstractSqlPlatform.js → AbstractSqlPlatform.js} +46 -19
  7. package/{dist/MonkeyPatchable.d.ts → MonkeyPatchable.d.ts} +1 -0
  8. package/{dist/MonkeyPatchable.js → MonkeyPatchable.js} +3 -0
  9. package/{dist/README.md → README.md} +66 -109
  10. package/{dist/SqlEntityManager.d.ts → SqlEntityManager.d.ts} +6 -10
  11. package/{dist/SqlEntityManager.js → SqlEntityManager.js} +5 -10
  12. package/SqlEntityRepository.d.ts +24 -0
  13. package/{dist/SqlEntityRepository.js → SqlEntityRepository.js} +11 -8
  14. package/{dist/index.d.ts → index.d.ts} +1 -0
  15. package/{dist/index.js → index.js} +6 -1
  16. package/index.mjs +208 -0
  17. package/package.json +20 -49
  18. package/query/ArrayCriteriaNode.d.ts +9 -0
  19. package/{dist/query → query}/CriteriaNode.d.ts +8 -8
  20. package/{dist/query → query}/CriteriaNode.js +23 -13
  21. package/query/CriteriaNodeFactory.d.ts +12 -0
  22. package/{dist/query → query}/CriteriaNodeFactory.js +10 -13
  23. package/{dist/query → query}/ObjectCriteriaNode.d.ts +3 -3
  24. package/{dist/query → query}/ObjectCriteriaNode.js +26 -9
  25. package/{dist/query → query}/QueryBuilder.d.ts +90 -34
  26. package/query/QueryBuilder.js +1098 -0
  27. package/{dist/query → query}/QueryBuilderHelper.d.ts +23 -19
  28. package/{dist/query → query}/QueryBuilderHelper.js +300 -176
  29. package/query/ScalarCriteriaNode.d.ts +10 -0
  30. package/{dist/query → query}/ScalarCriteriaNode.js +9 -6
  31. package/{dist/query → query}/enums.js +1 -1
  32. package/{dist/query → query}/index.js +5 -1
  33. package/{dist/schema → schema}/DatabaseSchema.d.ts +6 -3
  34. package/{dist/schema → schema}/DatabaseSchema.js +33 -23
  35. package/{dist/schema → schema}/DatabaseTable.d.ts +12 -10
  36. package/{dist/schema → schema}/DatabaseTable.js +55 -29
  37. package/{dist/schema → schema}/SchemaComparator.d.ts +7 -5
  38. package/{dist/schema → schema}/SchemaComparator.js +68 -10
  39. package/{dist/schema → schema}/SchemaHelper.d.ts +20 -13
  40. package/{dist/schema → schema}/SchemaHelper.js +56 -11
  41. package/{dist/schema/SchemaGenerator.d.ts → schema/SqlSchemaGenerator.d.ts} +11 -14
  42. package/{dist/schema/SchemaGenerator.js → schema/SqlSchemaGenerator.js} +139 -55
  43. package/{dist/schema → schema}/index.d.ts +1 -1
  44. package/{dist/schema → schema}/index.js +6 -2
  45. package/{dist/typings.d.ts → typings.d.ts} +27 -18
  46. package/CHANGELOG.md +0 -661
  47. package/dist/AbstractSqlDriver.d.ts +0 -55
  48. package/dist/AbstractSqlPlatform.d.ts +0 -21
  49. package/dist/LICENSE +0 -21
  50. package/dist/SqlEntityRepository.d.ts +0 -23
  51. package/dist/index.mjs +0 -26
  52. package/dist/package.json +0 -99
  53. package/dist/query/ArrayCriteriaNode.d.ts +0 -9
  54. package/dist/query/CriteriaNodeFactory.d.ts +0 -12
  55. package/dist/query/QueryBuilder.js +0 -688
  56. package/dist/query/ScalarCriteriaNode.d.ts +0 -9
  57. /package/{dist/query → query}/ArrayCriteriaNode.js +0 -0
  58. /package/{dist/query → query}/enums.d.ts +0 -0
  59. /package/{dist/query → query}/index.d.ts +0 -0
  60. /package/{dist/typings.js → typings.js} +0 -0
@@ -0,0 +1,10 @@
1
+ import { CriteriaNode } from './CriteriaNode';
2
+ import type { IQueryBuilder } from '../typings';
3
+ /**
4
+ * @internal
5
+ */
6
+ export declare class ScalarCriteriaNode<T extends object> extends CriteriaNode<T> {
7
+ process(qb: IQueryBuilder<T>, alias?: string): any;
8
+ willAutoJoin<T>(qb: IQueryBuilder<T>, alias?: string): boolean;
9
+ shouldJoin(): boolean;
10
+ }
@@ -13,23 +13,26 @@ class ScalarCriteriaNode extends CriteriaNode_1.CriteriaNode {
13
13
  const parentPath = this.parent.getPath(); // the parent is always there, otherwise `shouldJoin` would return `false`
14
14
  const nestedAlias = qb.getAliasForJoinPath(path) || qb.getNextAlias(this.prop?.pivotTable ?? this.entityName);
15
15
  const field = `${alias}.${this.prop.name}`;
16
- const type = this.prop.reference === core_1.ReferenceType.MANY_TO_MANY ? 'pivotJoin' : 'leftJoin';
16
+ const type = this.prop.kind === core_1.ReferenceKind.MANY_TO_MANY ? 'pivotJoin' : 'leftJoin';
17
17
  qb.join(field, nestedAlias, undefined, type, path);
18
18
  // select the owner as virtual property when joining from 1:1 inverse side, but only if the parent is root entity
19
- if (this.prop.reference === core_1.ReferenceType.ONE_TO_ONE && !parentPath.includes('.')) {
19
+ if (this.prop.kind === core_1.ReferenceKind.ONE_TO_ONE && !parentPath.includes('.')) {
20
20
  qb.addSelect(field);
21
21
  }
22
22
  }
23
23
  return this.payload;
24
24
  }
25
+ willAutoJoin(qb, alias) {
26
+ return this.shouldJoin();
27
+ }
25
28
  shouldJoin() {
26
29
  if (!this.parent || !this.prop) {
27
30
  return false;
28
31
  }
29
- switch (this.prop.reference) {
30
- case core_1.ReferenceType.ONE_TO_MANY: return true;
31
- case core_1.ReferenceType.MANY_TO_MANY: return true;
32
- case core_1.ReferenceType.ONE_TO_ONE: return !this.prop.owner;
32
+ switch (this.prop.kind) {
33
+ case core_1.ReferenceKind.ONE_TO_MANY: return true;
34
+ case core_1.ReferenceKind.MANY_TO_MANY: return true;
35
+ case core_1.ReferenceKind.ONE_TO_ONE: return !this.prop.owner;
33
36
  default: return false; // SCALAR, MANY_TO_ONE
34
37
  }
35
38
  }
@@ -9,4 +9,4 @@ var QueryType;
9
9
  QueryType["INSERT"] = "INSERT";
10
10
  QueryType["UPDATE"] = "UPDATE";
11
11
  QueryType["DELETE"] = "DELETE";
12
- })(QueryType = exports.QueryType || (exports.QueryType = {}));
12
+ })(QueryType || (exports.QueryType = QueryType = {}));
@@ -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];
@@ -1,4 +1,4 @@
1
- import type { Configuration, Dictionary, EntityMetadata } from '@mikro-orm/core';
1
+ import { type Configuration, type Dictionary, type EntityMetadata } from '@mikro-orm/core';
2
2
  import { DatabaseTable } from './DatabaseTable';
3
3
  import type { AbstractSqlConnection } from '../AbstractSqlConnection';
4
4
  import type { AbstractSqlPlatform } from '../AbstractSqlPlatform';
@@ -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
- addTable(name: string, schema: string | undefined | null): DatabaseTable;
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;
@@ -7,15 +7,20 @@ const DatabaseTable_1 = require("./DatabaseTable");
7
7
  * @internal
8
8
  */
9
9
  class DatabaseSchema {
10
+ platform;
11
+ name;
12
+ tables = [];
13
+ namespaces = new Set();
14
+ nativeEnums = {}; // for postgres
10
15
  constructor(platform, name) {
11
16
  this.platform = platform;
12
17
  this.name = name;
13
- this.tables = [];
14
- this.namespaces = new Set();
15
18
  }
16
- addTable(name, schema) {
19
+ addTable(name, schema, comment) {
17
20
  const namespaceName = schema ?? this.name;
18
21
  const table = new DatabaseTable_1.DatabaseTable(this.platform, name, namespaceName);
22
+ table.nativeEnums = this.nativeEnums;
23
+ table.comment = comment;
19
24
  this.tables.push(table);
20
25
  if (namespaceName != null) {
21
26
  this.namespaces.add(namespaceName);
@@ -31,33 +36,38 @@ class DatabaseSchema {
31
36
  hasTable(name) {
32
37
  return !!this.getTable(name);
33
38
  }
39
+ setNativeEnums(nativeEnums) {
40
+ this.nativeEnums = nativeEnums;
41
+ this.tables.forEach(t => t.nativeEnums = nativeEnums);
42
+ }
43
+ getNativeEnums() {
44
+ return this.nativeEnums;
45
+ }
34
46
  hasNamespace(namespace) {
35
47
  return this.namespaces.has(namespace);
36
48
  }
37
49
  getNamespaces() {
38
50
  return [...this.namespaces];
39
51
  }
40
- static async create(connection, platform, config, schemaName) {
41
- const schema = new DatabaseSchema(platform, schemaName ?? config.get('schema'));
42
- const tables = await connection.execute(platform.getSchemaHelper().getListTablesSQL());
43
- for (const t of tables) {
44
- if (t.table_name === config.get('migrations').tableName) {
45
- continue;
46
- }
47
- const table = schema.addTable(t.table_name, t.schema_name);
48
- table.comment = t.table_comment;
49
- const cols = await platform.getSchemaHelper().getColumns(connection, table.name, table.schema);
50
- const indexes = await platform.getSchemaHelper().getIndexes(connection, table.name, table.schema);
51
- const checks = await platform.getSchemaHelper().getChecks(connection, table.name, table.schema);
52
- const pks = await platform.getSchemaHelper().getPrimaryKeys(connection, indexes, table.name, table.schema);
53
- const fks = await platform.getSchemaHelper().getForeignKeys(connection, table.name, table.schema);
54
- const enums = await platform.getSchemaHelper().getEnumDefinitions(connection, checks, table.name, table.schema);
55
- table.init(cols, indexes, checks, pks, fks, enums);
56
- }
52
+ static async create(connection, platform, config, schemaName, schemas) {
53
+ const schema = new DatabaseSchema(platform, schemaName ?? config.get('schema') ?? platform.getDefaultSchemaName());
54
+ const allTables = await connection.execute(platform.getSchemaHelper().getListTablesSQL());
55
+ const parts = config.get('migrations').tableName.split('.');
56
+ const migrationsTableName = parts[1] ?? parts[0];
57
+ const migrationsSchemaName = parts.length > 1 ? parts[0] : config.get('schema', platform.getDefaultSchemaName());
58
+ const tables = allTables.filter(t => t.table_name !== migrationsTableName || (t.schema_name && t.schema_name !== migrationsSchemaName));
59
+ await platform.getSchemaHelper().loadInformationSchema(schema, connection, tables, schemas && schemas.length > 0 ? schemas : undefined);
57
60
  return schema;
58
61
  }
59
62
  static fromMetadata(metadata, platform, config, schemaName) {
60
63
  const schema = new DatabaseSchema(platform, schemaName ?? config.get('schema'));
64
+ const nativeEnums = {};
65
+ for (const meta of metadata) {
66
+ meta.props
67
+ .filter(prop => prop.nativeEnumName)
68
+ .forEach(prop => nativeEnums[prop.nativeEnumName] = prop.items?.map(val => '' + val) ?? []);
69
+ }
70
+ schema.setNativeEnums(nativeEnums);
61
71
  for (const meta of metadata) {
62
72
  const table = schema.addTable(meta.collection, this.getSchemaName(meta, config, schemaName));
63
73
  table.comment = meta.comment;
@@ -86,15 +96,15 @@ class DatabaseSchema {
86
96
  if (prop.persist === false || !prop.fieldNames) {
87
97
  return false;
88
98
  }
89
- if (meta.pivotTable || (core_1.ReferenceType.EMBEDDED && prop.object)) {
99
+ if (meta.pivotTable || (core_1.ReferenceKind.EMBEDDED && prop.object)) {
90
100
  return true;
91
101
  }
92
102
  const getRootProperty = (prop) => prop.embedded ? getRootProperty(meta.properties[prop.embedded[0]]) : prop;
93
103
  const rootProp = getRootProperty(prop);
94
- if (rootProp.reference === core_1.ReferenceType.EMBEDDED) {
104
+ if (rootProp.kind === core_1.ReferenceKind.EMBEDDED) {
95
105
  return prop === rootProp || !rootProp.object;
96
106
  }
97
- return [core_1.ReferenceType.SCALAR, core_1.ReferenceType.MANY_TO_ONE].includes(prop.reference) || (prop.reference === core_1.ReferenceType.ONE_TO_ONE && prop.owner);
107
+ return [core_1.ReferenceKind.SCALAR, core_1.ReferenceKind.MANY_TO_ONE].includes(prop.kind) || (prop.kind === core_1.ReferenceKind.ONE_TO_ONE && prop.owner);
98
108
  }
99
109
  toJSON() {
100
110
  const { platform, namespaces, ...rest } = this;
@@ -1,6 +1,6 @@
1
- import type { Dictionary, EntityMetadata, EntityProperty, NamingStrategy } from '@mikro-orm/core';
1
+ import { type Dictionary, type EntityMetadata, type EntityProperty, type NamingStrategy } from '@mikro-orm/core';
2
2
  import type { SchemaHelper } from './SchemaHelper';
3
- import type { Check, Column, ForeignKey, Index } from '../typings';
3
+ import type { CheckDef, Column, ForeignKey, IndexDef } from '../typings';
4
4
  import type { AbstractSqlPlatform } from '../AbstractSqlPlatform';
5
5
  /**
6
6
  * @internal
@@ -13,13 +13,15 @@ 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[];
19
20
  getColumn(name: string): Column | undefined;
20
- getIndexes(): Index[];
21
- getChecks(): Check[];
22
- init(cols: Column[], indexes: Index[], checks: Check[], pks: string[], fks: Dictionary<ForeignKey>, enums: Dictionary<string[]>): void;
21
+ removeColumn(name: string): void;
22
+ getIndexes(): IndexDef[];
23
+ getChecks(): CheckDef[];
24
+ init(cols: Column[], indexes: IndexDef[] | undefined, checks: CheckDef<unknown>[] | undefined, pks: string[], fks?: Dictionary<ForeignKey>, enums?: Dictionary<string[]>): void;
23
25
  addColumn(column: Column): void;
24
26
  addColumnFromProperty(prop: EntityProperty, meta: EntityMetadata): void;
25
27
  private getIndexName;
@@ -30,14 +32,14 @@ export declare class DatabaseTable {
30
32
  getShortestName(): string;
31
33
  getForeignKeys(): Dictionary<ForeignKey>;
32
34
  hasColumn(columnName: string): boolean;
33
- getIndex(indexName: string): Index | undefined;
35
+ getIndex(indexName: string): IndexDef | undefined;
34
36
  hasIndex(indexName: string): boolean;
35
- getCheck(checkName: string): Check<unknown> | undefined;
37
+ getCheck(checkName: string): CheckDef<unknown> | undefined;
36
38
  hasCheck(checkName: string): boolean;
37
- getPrimaryKey(): Index | undefined;
39
+ getPrimaryKey(): IndexDef | undefined;
38
40
  hasPrimaryKey(): boolean;
39
41
  private getPropertyDeclaration;
40
- private getReferenceType;
42
+ private getReferenceKind;
41
43
  private getPropertyName;
42
44
  private getPropertyType;
43
45
  private getPropertyDefaultValue;
@@ -48,6 +50,6 @@ export declare class DatabaseTable {
48
50
  expression?: string;
49
51
  options?: Dictionary;
50
52
  }, type: 'index' | 'unique' | 'primary'): void;
51
- addCheck(check: Check): void;
53
+ addCheck(check: CheckDef): void;
52
54
  toJSON(): Dictionary;
53
55
  }
@@ -6,14 +6,19 @@ const core_1 = require("@mikro-orm/core");
6
6
  * @internal
7
7
  */
8
8
  class DatabaseTable {
9
+ platform;
10
+ name;
11
+ schema;
12
+ columns = {};
13
+ indexes = [];
14
+ checks = [];
15
+ foreignKeys = {};
16
+ nativeEnums = {}; // for postgres
17
+ comment;
9
18
  constructor(platform, name, schema) {
10
19
  this.platform = platform;
11
20
  this.name = name;
12
21
  this.schema = schema;
13
- this.columns = {};
14
- this.indexes = [];
15
- this.checks = [];
16
- this.foreignKeys = {};
17
22
  Object.defineProperties(this, {
18
23
  platform: { enumerable: false, writable: true },
19
24
  });
@@ -24,13 +29,16 @@ class DatabaseTable {
24
29
  getColumn(name) {
25
30
  return this.columns[name];
26
31
  }
32
+ removeColumn(name) {
33
+ delete this.columns[name];
34
+ }
27
35
  getIndexes() {
28
36
  return this.indexes;
29
37
  }
30
38
  getChecks() {
31
39
  return this.checks;
32
40
  }
33
- init(cols, indexes, checks, pks, fks, enums) {
41
+ init(cols, indexes = [], checks = [], pks, fks = {}, enums = {}) {
34
42
  this.indexes = indexes;
35
43
  this.checks = checks;
36
44
  this.foreignKeys = fks;
@@ -41,7 +49,7 @@ class DatabaseTable {
41
49
  const type = v.name in enums ? 'enum' : v.type;
42
50
  v.mappedType = this.platform.getMappedType(type);
43
51
  v.default = v.default?.toString().startsWith('nextval(') ? null : v.default;
44
- v.enumItems = enums[v.name] || [];
52
+ v.enumItems ??= enums[v.name] || [];
45
53
  o[v.name] = v;
46
54
  return o;
47
55
  }, {});
@@ -51,7 +59,6 @@ class DatabaseTable {
51
59
  }
52
60
  addColumnFromProperty(prop, meta) {
53
61
  prop.fieldNames.forEach((field, idx) => {
54
- var _a;
55
62
  const type = prop.enum ? 'enum' : prop.columnTypes[idx];
56
63
  const mappedType = this.platform.getMappedType(type);
57
64
  if (mappedType instanceof core_1.DecimalType) {
@@ -64,9 +71,9 @@ class DatabaseTable {
64
71
  }
65
72
  }
66
73
  if (mappedType instanceof core_1.DateTimeType) {
67
- prop.length ?? (prop.length = this.platform.getDefaultDateTimeLength());
74
+ prop.length ??= this.platform.getDefaultDateTimeLength();
68
75
  }
69
- const primary = !meta.compositePK && !!prop.primary && prop.reference === core_1.ReferenceType.SCALAR && this.platform.isNumericColumn(mappedType);
76
+ const primary = !meta.compositePK && !!prop.primary && prop.kind === core_1.ReferenceKind.SCALAR && this.platform.isNumericColumn(mappedType);
70
77
  this.columns[field] = {
71
78
  name: prop.fieldNames[idx],
72
79
  type: prop.columnTypes[idx],
@@ -74,20 +81,22 @@ class DatabaseTable {
74
81
  unsigned: prop.unsigned && this.platform.isNumericColumn(mappedType),
75
82
  autoincrement: prop.autoincrement ?? primary,
76
83
  primary,
84
+ nativeEnumName: prop.nativeEnumName,
77
85
  nullable: !!prop.nullable,
78
86
  length: prop.length,
79
87
  precision: prop.precision,
80
88
  scale: prop.scale,
81
89
  default: prop.defaultRaw,
82
- enumItems: prop.items?.every(core_1.Utils.isString) ? prop.items : undefined,
90
+ enumItems: prop.nativeEnumName || prop.items?.every(core_1.Utils.isString) ? prop.items : undefined,
83
91
  comment: prop.comment,
84
92
  extra: prop.extra,
93
+ ignoreSchemaChanges: prop.ignoreSchemaChanges,
85
94
  };
86
- (_a = this.columns[field]).unsigned || (_a.unsigned = this.columns[field].autoincrement);
95
+ this.columns[field].unsigned ||= this.columns[field].autoincrement;
87
96
  const defaultValue = this.platform.getSchemaHelper().normalizeDefaultValue(prop.defaultRaw, prop.length);
88
97
  this.columns[field].default = defaultValue;
89
98
  });
90
- if ([core_1.ReferenceType.MANY_TO_ONE, core_1.ReferenceType.ONE_TO_ONE].includes(prop.reference)) {
99
+ if ([core_1.ReferenceKind.MANY_TO_ONE, core_1.ReferenceKind.ONE_TO_ONE].includes(prop.kind)) {
91
100
  const constraintName = this.getIndexName(true, prop.fieldNames, 'foreign');
92
101
  let schema = prop.targetMeta.schema === '*' ? this.schema : prop.targetMeta.schema ?? this.schema;
93
102
  if (prop.referencedTableName.includes('.')) {
@@ -101,11 +110,11 @@ class DatabaseTable {
101
110
  referencedTableName: schema ? `${schema}.${prop.referencedTableName}` : prop.referencedTableName,
102
111
  };
103
112
  const cascade = prop.cascade.includes(core_1.Cascade.REMOVE) || prop.cascade.includes(core_1.Cascade.ALL);
104
- if (prop.onDelete || cascade || prop.nullable) {
105
- this.foreignKeys[constraintName].deleteRule = prop.onDelete || (cascade ? 'cascade' : 'set null');
113
+ if (prop.deleteRule || cascade || prop.nullable) {
114
+ this.foreignKeys[constraintName].deleteRule = prop.deleteRule || (cascade ? 'cascade' : 'set null');
106
115
  }
107
- if (prop.onUpdateIntegrity || prop.cascade.includes(core_1.Cascade.PERSIST) || prop.cascade.includes(core_1.Cascade.ALL)) {
108
- this.foreignKeys[constraintName].updateRule = prop.onUpdateIntegrity || 'cascade';
116
+ if (prop.updateRule || prop.cascade.includes(core_1.Cascade.PERSIST) || prop.cascade.includes(core_1.Cascade.ALL)) {
117
+ this.foreignKeys[constraintName].updateRule = prop.updateRule || 'cascade';
109
118
  }
110
119
  }
111
120
  if (prop.index) {
@@ -113,6 +122,7 @@ class DatabaseTable {
113
122
  columnNames: prop.fieldNames,
114
123
  composite: prop.fieldNames.length > 1,
115
124
  keyName: this.getIndexName(prop.index, prop.fieldNames, 'index'),
125
+ constraint: false,
116
126
  primary: false,
117
127
  unique: false,
118
128
  });
@@ -122,6 +132,7 @@ class DatabaseTable {
122
132
  columnNames: prop.fieldNames,
123
133
  composite: prop.fieldNames.length > 1,
124
134
  keyName: this.getIndexName(prop.unique, prop.fieldNames, 'unique'),
135
+ constraint: !prop.fieldNames.some((d) => d.includes('.')),
125
136
  primary: false,
126
137
  unique: true,
127
138
  });
@@ -163,8 +174,8 @@ class DatabaseTable {
163
174
  }
164
175
  const meta = schema.init().meta;
165
176
  meta.relations
166
- .filter(prop => prop.primary && prop.reference === core_1.ReferenceType.MANY_TO_ONE && !meta.compositePK)
167
- .forEach(prop => prop.reference = core_1.ReferenceType.ONE_TO_ONE);
177
+ .filter(prop => prop.primary && prop.kind === core_1.ReferenceKind.MANY_TO_ONE && !meta.compositePK)
178
+ .forEach(prop => prop.kind = core_1.ReferenceKind.ONE_TO_ONE);
168
179
  return meta;
169
180
  }
170
181
  /**
@@ -205,20 +216,20 @@ class DatabaseTable {
205
216
  const prop = this.getPropertyName(namingStrategy, column);
206
217
  const index = compositeFkIndexes[prop] || this.indexes.find(idx => idx.columnNames[0] === column.name && !idx.composite && !idx.unique && !idx.primary);
207
218
  const unique = compositeFkUniques[prop] || this.indexes.find(idx => idx.columnNames[0] === column.name && !idx.composite && idx.unique && !idx.primary);
208
- const reference = this.getReferenceType(fk, unique);
219
+ const kind = this.getReferenceKind(fk, unique);
209
220
  const type = this.getPropertyType(namingStrategy, column, fk);
210
221
  const fkOptions = {};
211
222
  if (fk) {
212
223
  fkOptions.fieldNames = fk.columnNames;
213
224
  fkOptions.referencedTableName = fk.referencedTableName;
214
225
  fkOptions.referencedColumnNames = fk.referencedColumnNames;
215
- fkOptions.onUpdateIntegrity = fk.updateRule?.toLowerCase();
216
- fkOptions.onDelete = fk.deleteRule?.toLowerCase();
226
+ fkOptions.updateRule = fk.updateRule?.toLowerCase();
227
+ fkOptions.deleteRule = fk.deleteRule?.toLowerCase();
217
228
  }
218
229
  return {
219
230
  name: prop,
220
231
  type,
221
- reference,
232
+ kind,
222
233
  columnType: column.type,
223
234
  default: this.getPropertyDefaultValue(schemaHelper, column, type),
224
235
  defaultRaw: this.getPropertyDefaultValue(schemaHelper, column, type, true),
@@ -226,6 +237,8 @@ class DatabaseTable {
226
237
  primary: column.primary,
227
238
  fieldName: column.name,
228
239
  length: column.length,
240
+ precision: column.precision,
241
+ scale: column.scale,
229
242
  index: index ? index.keyName : undefined,
230
243
  unique: unique ? unique.keyName : undefined,
231
244
  enum: !!column.enumItems?.length,
@@ -233,14 +246,14 @@ class DatabaseTable {
233
246
  ...fkOptions,
234
247
  };
235
248
  }
236
- getReferenceType(fk, unique) {
249
+ getReferenceKind(fk, unique) {
237
250
  if (fk && unique) {
238
- return core_1.ReferenceType.ONE_TO_ONE;
251
+ return core_1.ReferenceKind.ONE_TO_ONE;
239
252
  }
240
253
  if (fk) {
241
- return core_1.ReferenceType.MANY_TO_ONE;
254
+ return core_1.ReferenceKind.MANY_TO_ONE;
242
255
  }
243
- return core_1.ReferenceType.SCALAR;
256
+ return core_1.ReferenceKind.SCALAR;
244
257
  }
245
258
  getPropertyName(namingStrategy, column) {
246
259
  const fk = Object.values(this.foreignKeys).find(fk => fk.columnNames.includes(column.name));
@@ -287,7 +300,17 @@ class DatabaseTable {
287
300
  return '' + val;
288
301
  }
289
302
  addIndex(meta, index, type) {
290
- const properties = core_1.Utils.flatten(core_1.Utils.asArray(index.properties).map(prop => meta.properties[prop].fieldNames));
303
+ const properties = core_1.Utils.unique(core_1.Utils.flatten(core_1.Utils.asArray(index.properties).map(prop => {
304
+ const root = prop.replace(/\..+$/, '');
305
+ if (meta.properties[prop]) {
306
+ return meta.properties[prop].fieldNames;
307
+ }
308
+ // json index, we need to rename the column only
309
+ if (meta.properties[root]) {
310
+ return [prop.replace(root, meta.properties[root].fieldNames[0])];
311
+ }
312
+ return [prop];
313
+ })));
291
314
  if (properties.length === 0 && !index.expression) {
292
315
  return;
293
316
  }
@@ -296,10 +319,13 @@ class DatabaseTable {
296
319
  keyName: name,
297
320
  columnNames: properties,
298
321
  composite: properties.length > 1,
322
+ // JSON columns can have unique index but not unique constraint, and we need to distinguish those, so we can properly drop them
323
+ constraint: type !== 'index' && !properties.some((d) => d.includes('.')),
299
324
  primary: type === 'primary',
300
325
  unique: type !== 'index',
301
326
  type: index.type,
302
327
  expression: index.expression,
328
+ options: index.options,
303
329
  });
304
330
  }
305
331
  addCheck(check) {
@@ -307,10 +333,10 @@ class DatabaseTable {
307
333
  }
308
334
  toJSON() {
309
335
  const { platform, columns, ...rest } = this;
310
- const columnsMapped = Object.keys(columns).reduce((o, col) => {
336
+ const columnsMapped = core_1.Utils.keys(columns).reduce((o, col) => {
311
337
  const { mappedType, ...restCol } = columns[col];
312
338
  o[col] = restCol;
313
- o[col].mappedType = Object.keys(core_1.t).find(k => core_1.t[k] === mappedType.constructor);
339
+ o[col].mappedType = core_1.Utils.keys(core_1.t).find(k => core_1.t[k] === mappedType.constructor);
314
340
  return o;
315
341
  }, {});
316
342
  return { columns: columnsMapped, ...rest };
@@ -1,4 +1,4 @@
1
- import type { Check, Column, ForeignKey, Index, SchemaDifference, TableDifference } from '../typings';
1
+ import type { CheckDef, Column, ForeignKey, IndexDef, SchemaDifference, TableDifference } from '../typings';
2
2
  import type { DatabaseSchema } from './DatabaseSchema';
3
3
  import type { DatabaseTable } from './DatabaseTable';
4
4
  import type { AbstractSqlPlatform } from '../AbstractSqlPlatform';
@@ -8,6 +8,7 @@ import type { AbstractSqlPlatform } from '../AbstractSqlPlatform';
8
8
  export declare class SchemaComparator {
9
9
  private readonly platform;
10
10
  private readonly helper;
11
+ private readonly logger;
11
12
  constructor(platform: AbstractSqlPlatform);
12
13
  /**
13
14
  * Returns a SchemaDifference object containing the differences between the schemas fromSchema and toSchema.
@@ -37,19 +38,20 @@ export declare class SchemaComparator {
37
38
  * Returns the difference between the columns
38
39
  * If there are differences this method returns field2, otherwise the boolean false.
39
40
  */
40
- diffColumn(column1: Column, column2: Column): Set<string>;
41
+ diffColumn(column1: Column, column2: Column, tableName?: string): Set<string>;
41
42
  diffEnumItems(items1?: string[], items2?: string[]): boolean;
42
43
  diffComment(comment1?: string, comment2?: string): boolean;
43
44
  /**
44
45
  * Finds the difference between the indexes index1 and index2.
45
46
  * Compares index1 with index2 and returns index2 if there are any differences or false in case there are no differences.
46
47
  */
47
- diffIndex(index1: Index, index2: Index): boolean;
48
+ diffIndex(index1: IndexDef, index2: IndexDef): boolean;
48
49
  /**
49
50
  * Checks if the other index already fulfills all the indexing and constraint needs of the current one.
50
51
  */
51
- isIndexFulfilledBy(index1: Index, index2: Index): boolean;
52
- diffCheck(check1: Check, check2: Check): boolean;
52
+ isIndexFulfilledBy(index1: IndexDef, index2: IndexDef): boolean;
53
+ diffCheck(check1: CheckDef, check2: CheckDef): boolean;
53
54
  hasSameDefaultValue(from: Column, to: Column): boolean;
54
55
  private mapColumnToProperty;
56
+ private log;
55
57
  }