@mikro-orm/knex 7.0.0-dev.0 → 7.0.0-dev.10
This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
- package/AbstractSqlConnection.d.ts +2 -2
- package/AbstractSqlConnection.js +36 -31
- package/AbstractSqlDriver.d.ts +14 -12
- package/AbstractSqlDriver.js +198 -186
- package/AbstractSqlPlatform.d.ts +4 -4
- package/AbstractSqlPlatform.js +17 -21
- package/PivotCollectionPersister.d.ts +1 -1
- package/PivotCollectionPersister.js +5 -8
- package/SqlEntityManager.d.ts +4 -3
- package/SqlEntityManager.js +2 -6
- package/SqlEntityRepository.d.ts +2 -2
- package/SqlEntityRepository.js +2 -6
- package/dialects/index.d.ts +4 -4
- package/dialects/index.js +4 -20
- package/dialects/mssql/MsSqlNativeQueryBuilder.d.ts +1 -1
- package/dialects/mssql/MsSqlNativeQueryBuilder.js +29 -29
- package/dialects/mssql/index.d.ts +1 -1
- package/dialects/mssql/index.js +1 -17
- package/dialects/mysql/MySqlExceptionConverter.js +16 -19
- package/dialects/mysql/MySqlNativeQueryBuilder.d.ts +1 -1
- package/dialects/mysql/MySqlNativeQueryBuilder.js +13 -17
- package/dialects/mysql/MySqlPlatform.d.ts +10 -6
- package/dialects/mysql/MySqlPlatform.js +30 -20
- package/dialects/mysql/MySqlSchemaHelper.d.ts +5 -5
- package/dialects/mysql/MySqlSchemaHelper.js +8 -12
- package/dialects/mysql/index.d.ts +4 -4
- package/dialects/mysql/index.js +4 -20
- package/dialects/postgresql/PostgreSqlNativeQueryBuilder.d.ts +1 -1
- package/dialects/postgresql/PostgreSqlNativeQueryBuilder.js +2 -6
- package/dialects/postgresql/index.d.ts +1 -1
- package/dialects/postgresql/index.js +1 -17
- package/dialects/sqlite/BaseSqliteConnection.d.ts +2 -2
- package/dialects/sqlite/BaseSqliteConnection.js +14 -12
- package/dialects/sqlite/BaseSqlitePlatform.d.ts +4 -5
- package/dialects/sqlite/BaseSqlitePlatform.js +11 -19
- package/dialects/sqlite/SqliteExceptionConverter.js +16 -19
- package/dialects/sqlite/SqliteNativeQueryBuilder.d.ts +1 -1
- package/dialects/sqlite/SqliteNativeQueryBuilder.js +2 -6
- package/dialects/sqlite/SqliteSchemaHelper.d.ts +5 -5
- package/dialects/sqlite/SqliteSchemaHelper.js +22 -26
- package/dialects/sqlite/index.d.ts +5 -5
- package/dialects/sqlite/index.js +5 -21
- package/index.d.ts +11 -11
- package/index.js +13 -34
- package/package.json +7 -16
- package/query/ArrayCriteriaNode.d.ts +2 -2
- package/query/ArrayCriteriaNode.js +2 -6
- package/query/CriteriaNode.d.ts +1 -1
- package/query/CriteriaNode.js +25 -33
- package/query/CriteriaNodeFactory.d.ts +1 -1
- package/query/CriteriaNodeFactory.js +17 -21
- package/query/NativeQueryBuilder.d.ts +2 -2
- package/query/NativeQueryBuilder.js +33 -37
- package/query/ObjectCriteriaNode.d.ts +2 -2
- package/query/ObjectCriteriaNode.js +43 -43
- package/query/QueryBuilder.d.ts +17 -15
- package/query/QueryBuilder.js +204 -193
- package/query/QueryBuilderHelper.d.ts +4 -4
- package/query/QueryBuilderHelper.js +86 -96
- package/query/ScalarCriteriaNode.d.ts +2 -2
- package/query/ScalarCriteriaNode.js +12 -16
- package/query/enums.js +4 -7
- package/query/index.d.ts +9 -9
- package/query/index.js +9 -25
- package/schema/DatabaseSchema.d.ts +3 -3
- package/schema/DatabaseSchema.js +7 -11
- package/schema/DatabaseTable.d.ts +3 -3
- package/schema/DatabaseTable.js +44 -33
- package/schema/SchemaComparator.d.ts +4 -4
- package/schema/SchemaComparator.js +15 -19
- package/schema/SchemaHelper.d.ts +5 -5
- package/schema/SchemaHelper.js +22 -26
- package/schema/SqlSchemaGenerator.d.ts +4 -4
- package/schema/SqlSchemaGenerator.js +23 -36
- package/schema/index.d.ts +5 -5
- package/schema/index.js +5 -21
- package/typings.d.ts +7 -4
- package/typings.js +1 -2
- package/index.mjs +0 -232
package/query/index.js
CHANGED
|
@@ -1,25 +1,9 @@
|
|
|
1
|
-
|
|
2
|
-
|
|
3
|
-
|
|
4
|
-
|
|
5
|
-
|
|
6
|
-
|
|
7
|
-
|
|
8
|
-
|
|
9
|
-
|
|
10
|
-
if (k2 === undefined) k2 = k;
|
|
11
|
-
o[k2] = m[k];
|
|
12
|
-
}));
|
|
13
|
-
var __exportStar = (this && this.__exportStar) || function(m, exports) {
|
|
14
|
-
for (var p in m) if (p !== "default" && !Object.prototype.hasOwnProperty.call(exports, p)) __createBinding(exports, m, p);
|
|
15
|
-
};
|
|
16
|
-
Object.defineProperty(exports, "__esModule", { value: true });
|
|
17
|
-
__exportStar(require("./enums"), exports);
|
|
18
|
-
__exportStar(require("./QueryBuilderHelper"), exports);
|
|
19
|
-
__exportStar(require("./QueryBuilder"), exports);
|
|
20
|
-
__exportStar(require("./CriteriaNode"), exports);
|
|
21
|
-
__exportStar(require("./ArrayCriteriaNode"), exports);
|
|
22
|
-
__exportStar(require("./ObjectCriteriaNode"), exports);
|
|
23
|
-
__exportStar(require("./ScalarCriteriaNode"), exports);
|
|
24
|
-
__exportStar(require("./CriteriaNodeFactory"), exports);
|
|
25
|
-
__exportStar(require("./NativeQueryBuilder"), exports);
|
|
1
|
+
export * from './enums.js';
|
|
2
|
+
export * from './QueryBuilderHelper.js';
|
|
3
|
+
export * from './QueryBuilder.js';
|
|
4
|
+
export * from './CriteriaNode.js';
|
|
5
|
+
export * from './ArrayCriteriaNode.js';
|
|
6
|
+
export * from './ObjectCriteriaNode.js';
|
|
7
|
+
export * from './ScalarCriteriaNode.js';
|
|
8
|
+
export * from './CriteriaNodeFactory.js';
|
|
9
|
+
export * from './NativeQueryBuilder.js';
|
|
@@ -1,7 +1,7 @@
|
|
|
1
1
|
import { type Configuration, type Dictionary, type EntityMetadata } from '@mikro-orm/core';
|
|
2
|
-
import { DatabaseTable } from './DatabaseTable';
|
|
3
|
-
import type { AbstractSqlConnection } from '../AbstractSqlConnection';
|
|
4
|
-
import type { AbstractSqlPlatform } from '../AbstractSqlPlatform';
|
|
2
|
+
import { DatabaseTable } from './DatabaseTable.js';
|
|
3
|
+
import type { AbstractSqlConnection } from '../AbstractSqlConnection.js';
|
|
4
|
+
import type { AbstractSqlPlatform } from '../AbstractSqlPlatform.js';
|
|
5
5
|
/**
|
|
6
6
|
* @internal
|
|
7
7
|
*/
|
package/schema/DatabaseSchema.js
CHANGED
|
@@ -1,12 +1,9 @@
|
|
|
1
|
-
|
|
2
|
-
|
|
3
|
-
exports.DatabaseSchema = void 0;
|
|
4
|
-
const core_1 = require("@mikro-orm/core");
|
|
5
|
-
const DatabaseTable_1 = require("./DatabaseTable");
|
|
1
|
+
import { ReferenceKind } from '@mikro-orm/core';
|
|
2
|
+
import { DatabaseTable } from './DatabaseTable.js';
|
|
6
3
|
/**
|
|
7
4
|
* @internal
|
|
8
5
|
*/
|
|
9
|
-
class DatabaseSchema {
|
|
6
|
+
export class DatabaseSchema {
|
|
10
7
|
platform;
|
|
11
8
|
name;
|
|
12
9
|
tables = [];
|
|
@@ -18,7 +15,7 @@ class DatabaseSchema {
|
|
|
18
15
|
}
|
|
19
16
|
addTable(name, schema, comment) {
|
|
20
17
|
const namespaceName = schema ?? this.name;
|
|
21
|
-
const table = new
|
|
18
|
+
const table = new DatabaseTable(this.platform, name, namespaceName);
|
|
22
19
|
table.nativeEnums = this.nativeEnums;
|
|
23
20
|
table.comment = comment;
|
|
24
21
|
this.tables.push(table);
|
|
@@ -136,15 +133,15 @@ class DatabaseSchema {
|
|
|
136
133
|
if (prop.persist === false || (prop.columnTypes?.length ?? 0) === 0) {
|
|
137
134
|
return false;
|
|
138
135
|
}
|
|
139
|
-
if (prop.kind ===
|
|
136
|
+
if (prop.kind === ReferenceKind.EMBEDDED && prop.object) {
|
|
140
137
|
return true;
|
|
141
138
|
}
|
|
142
139
|
const getRootProperty = (prop) => prop.embedded ? getRootProperty(meta.properties[prop.embedded[0]]) : prop;
|
|
143
140
|
const rootProp = getRootProperty(prop);
|
|
144
|
-
if (rootProp.kind ===
|
|
141
|
+
if (rootProp.kind === ReferenceKind.EMBEDDED) {
|
|
145
142
|
return prop === rootProp || !rootProp.object;
|
|
146
143
|
}
|
|
147
|
-
return [
|
|
144
|
+
return [ReferenceKind.SCALAR, ReferenceKind.MANY_TO_ONE].includes(prop.kind) || (prop.kind === ReferenceKind.ONE_TO_ONE && prop.owner);
|
|
148
145
|
}
|
|
149
146
|
toJSON() {
|
|
150
147
|
const { platform, namespaces, ...rest } = this;
|
|
@@ -165,4 +162,3 @@ class DatabaseSchema {
|
|
|
165
162
|
}
|
|
166
163
|
}
|
|
167
164
|
}
|
|
168
|
-
exports.DatabaseSchema = DatabaseSchema;
|
|
@@ -1,7 +1,7 @@
|
|
|
1
1
|
import { type Configuration, type DeferMode, type Dictionary, type EntityMetadata, type EntityProperty, type NamingStrategy } from '@mikro-orm/core';
|
|
2
|
-
import type { SchemaHelper } from './SchemaHelper';
|
|
3
|
-
import type { CheckDef, Column, ForeignKey, IndexDef } from '../typings';
|
|
4
|
-
import type { AbstractSqlPlatform } from '../AbstractSqlPlatform';
|
|
2
|
+
import type { SchemaHelper } from './SchemaHelper.js';
|
|
3
|
+
import type { CheckDef, Column, ForeignKey, IndexDef } from '../typings.js';
|
|
4
|
+
import type { AbstractSqlPlatform } from '../AbstractSqlPlatform.js';
|
|
5
5
|
/**
|
|
6
6
|
* @internal
|
|
7
7
|
*/
|
package/schema/DatabaseTable.js
CHANGED
|
@@ -1,11 +1,8 @@
|
|
|
1
|
-
|
|
2
|
-
Object.defineProperty(exports, "__esModule", { value: true });
|
|
3
|
-
exports.DatabaseTable = void 0;
|
|
4
|
-
const core_1 = require("@mikro-orm/core");
|
|
1
|
+
import { Cascade, DecimalType, EntitySchema, ReferenceKind, t, Type, UnknownType, Utils, } from '@mikro-orm/core';
|
|
5
2
|
/**
|
|
6
3
|
* @internal
|
|
7
4
|
*/
|
|
8
|
-
class DatabaseTable {
|
|
5
|
+
export class DatabaseTable {
|
|
9
6
|
platform;
|
|
10
7
|
name;
|
|
11
8
|
schema;
|
|
@@ -36,7 +33,7 @@ class DatabaseTable {
|
|
|
36
33
|
delete this.columns[name];
|
|
37
34
|
}
|
|
38
35
|
getIndexes() {
|
|
39
|
-
return
|
|
36
|
+
return Utils.removeDuplicates(this.indexes);
|
|
40
37
|
}
|
|
41
38
|
getChecks() {
|
|
42
39
|
return this.checks;
|
|
@@ -64,9 +61,9 @@ class DatabaseTable {
|
|
|
64
61
|
prop.fieldNames?.forEach((field, idx) => {
|
|
65
62
|
const type = prop.enum ? 'enum' : prop.columnTypes[idx];
|
|
66
63
|
const mappedType = this.platform.getMappedType(type);
|
|
67
|
-
if (mappedType instanceof
|
|
64
|
+
if (mappedType instanceof DecimalType) {
|
|
68
65
|
const match = prop.columnTypes[idx].match(/\w+\((\d+), ?(\d+)\)/);
|
|
69
|
-
/*
|
|
66
|
+
/* v8 ignore next 5 */
|
|
70
67
|
if (match) {
|
|
71
68
|
prop.precision ??= +match[1];
|
|
72
69
|
prop.scale ??= +match[2];
|
|
@@ -86,7 +83,7 @@ class DatabaseTable {
|
|
|
86
83
|
generated: prop.generated,
|
|
87
84
|
mappedType,
|
|
88
85
|
unsigned: prop.unsigned && this.platform.isNumericColumn(mappedType),
|
|
89
|
-
autoincrement: prop.autoincrement ?? (primary && prop.kind ===
|
|
86
|
+
autoincrement: prop.autoincrement ?? (primary && prop.kind === ReferenceKind.SCALAR && this.platform.isNumericColumn(mappedType)),
|
|
90
87
|
primary,
|
|
91
88
|
nullable: this.columns[field]?.nullable ?? !!prop.nullable,
|
|
92
89
|
nativeEnumName: prop.nativeEnumName,
|
|
@@ -94,16 +91,19 @@ class DatabaseTable {
|
|
|
94
91
|
precision: prop.precision,
|
|
95
92
|
scale: prop.scale,
|
|
96
93
|
default: prop.defaultRaw,
|
|
97
|
-
enumItems: prop.nativeEnumName || prop.items?.every(
|
|
94
|
+
enumItems: prop.nativeEnumName || prop.items?.every(Utils.isString) ? prop.items : undefined,
|
|
98
95
|
comment: prop.comment,
|
|
99
96
|
extra: prop.extra,
|
|
100
97
|
ignoreSchemaChanges: prop.ignoreSchemaChanges,
|
|
101
98
|
};
|
|
102
99
|
this.columns[field].unsigned ??= this.columns[field].autoincrement;
|
|
100
|
+
if (this.nativeEnums[type]) {
|
|
101
|
+
this.columns[field].enumItems ??= this.nativeEnums[type].items;
|
|
102
|
+
}
|
|
103
103
|
const defaultValue = this.platform.getSchemaHelper().normalizeDefaultValue(prop.defaultRaw, prop.length);
|
|
104
104
|
this.columns[field].default = defaultValue;
|
|
105
105
|
});
|
|
106
|
-
if ([
|
|
106
|
+
if ([ReferenceKind.MANY_TO_ONE, ReferenceKind.ONE_TO_ONE].includes(prop.kind)) {
|
|
107
107
|
const constraintName = this.getIndexName(true, prop.fieldNames, 'foreign');
|
|
108
108
|
let schema = prop.targetMeta.root.schema === '*' ? this.schema : (prop.targetMeta.root.schema ?? config.get('schema', this.platform.getDefaultSchemaName()));
|
|
109
109
|
if (prop.referencedTableName.includes('.')) {
|
|
@@ -116,14 +116,14 @@ class DatabaseTable {
|
|
|
116
116
|
referencedColumnNames: prop.referencedColumnNames,
|
|
117
117
|
referencedTableName: schema ? `${schema}.${prop.referencedTableName}` : prop.referencedTableName,
|
|
118
118
|
};
|
|
119
|
-
const cascade = prop.cascade.includes(
|
|
119
|
+
const cascade = prop.cascade.includes(Cascade.REMOVE) || prop.cascade.includes(Cascade.ALL);
|
|
120
120
|
if (prop.deleteRule || cascade || prop.nullable) {
|
|
121
121
|
this.foreignKeys[constraintName].deleteRule = prop.deleteRule || (cascade ? 'cascade' : 'set null');
|
|
122
122
|
}
|
|
123
123
|
if (prop.updateRule) {
|
|
124
124
|
this.foreignKeys[constraintName].updateRule = prop.updateRule || 'cascade';
|
|
125
125
|
}
|
|
126
|
-
if ((prop.cascade.includes(
|
|
126
|
+
if ((prop.cascade.includes(Cascade.PERSIST) || prop.cascade.includes(Cascade.ALL))) {
|
|
127
127
|
const hasCascadePath = Object.values(this.foreignKeys).some(fk => {
|
|
128
128
|
return fk.constraintName !== constraintName
|
|
129
129
|
&& ((fk.updateRule && fk.updateRule !== 'no action') || (fk.deleteRule && fk.deleteRule !== 'no action'))
|
|
@@ -160,7 +160,7 @@ class DatabaseTable {
|
|
|
160
160
|
}
|
|
161
161
|
}
|
|
162
162
|
getIndexName(value, columnNames, type) {
|
|
163
|
-
if (
|
|
163
|
+
if (Utils.isString(value)) {
|
|
164
164
|
return value;
|
|
165
165
|
}
|
|
166
166
|
return this.platform.getIndexName(this.name, columnNames, type);
|
|
@@ -168,14 +168,14 @@ class DatabaseTable {
|
|
|
168
168
|
getEntityDeclaration(namingStrategy, schemaHelper, scalarPropertiesForRelations) {
|
|
169
169
|
const { fksOnColumnProps, fksOnStandaloneProps, columnFks, fkIndexes, nullableForeignKeys, skippedColumnNames, } = this.foreignKeysToProps(namingStrategy, scalarPropertiesForRelations);
|
|
170
170
|
const name = namingStrategy.getEntityName(this.name, this.schema);
|
|
171
|
-
const schema = new
|
|
171
|
+
const schema = new EntitySchema({ name, collection: this.name, schema: this.schema, comment: this.comment });
|
|
172
172
|
const compositeFkIndexes = {};
|
|
173
173
|
const compositeFkUniques = {};
|
|
174
174
|
const potentiallyUnmappedIndexes = this.indexes.filter(index => !index.primary // Skip primary index. Whether it's in use by scalar column or FK, it's already mapped.
|
|
175
|
-
&& (
|
|
175
|
+
&& (( // Non-trivial non-composite indexes will be declared at the entity's metadata, though later outputted in the property
|
|
176
|
+
index.columnNames.length > 1 // All composite indexes are to be mapped to entity decorators or FK props.
|
|
176
177
|
|| skippedColumnNames.includes(index.columnNames[0]) // Non-composite indexes for skipped columns are to be mapped as entity decorators.
|
|
177
|
-
|| index.deferMode || index.expression //
|
|
178
|
-
|| !(index.columnNames[0] in columnFks) // Trivial non-composite indexes for scalar props are to be mapped to the column.
|
|
178
|
+
|| index.deferMode || index.expression || !(index.columnNames[0] in columnFks)) // Trivial non-composite indexes for scalar props are to be mapped to the column.
|
|
179
179
|
)
|
|
180
180
|
// ignore indexes that don't have all column names (this can happen in sqlite where there is no way to infer this for expressions)
|
|
181
181
|
&& !(index.columnNames.some(col => !col) && !index.expression));
|
|
@@ -245,10 +245,10 @@ class DatabaseTable {
|
|
|
245
245
|
}
|
|
246
246
|
const meta = schema.init().meta;
|
|
247
247
|
const oneToOneCandidateProperties = meta.relations
|
|
248
|
-
.filter(prop => prop.primary && prop.kind ===
|
|
248
|
+
.filter(prop => prop.primary && prop.kind === ReferenceKind.MANY_TO_ONE);
|
|
249
249
|
if (oneToOneCandidateProperties.length === 1
|
|
250
250
|
&& oneToOneCandidateProperties[0].fieldNames.length === (new Set(meta.getPrimaryProps().flatMap(prop => prop.fieldNames))).size) {
|
|
251
|
-
oneToOneCandidateProperties[0].kind =
|
|
251
|
+
oneToOneCandidateProperties[0].kind = ReferenceKind.ONE_TO_ONE;
|
|
252
252
|
}
|
|
253
253
|
return meta;
|
|
254
254
|
}
|
|
@@ -595,9 +595,9 @@ class DatabaseTable {
|
|
|
595
595
|
const unique = compositeFkUniques[prop] || this.indexes.find(idx => idx.columnNames[0] === column.name && !idx.composite && idx.unique && !idx.primary);
|
|
596
596
|
const kind = this.getReferenceKind(fk, unique);
|
|
597
597
|
const runtimeType = this.getPropertyTypeForColumn(namingStrategy, column, fk);
|
|
598
|
-
const type = fk ? runtimeType : (
|
|
598
|
+
const type = fk ? runtimeType : (Utils.keys(t).find(k => {
|
|
599
599
|
const typeInCoreMap = this.platform.getMappedType(k);
|
|
600
|
-
return (typeInCoreMap !==
|
|
600
|
+
return (typeInCoreMap !== Type.getType(UnknownType) || k === 'unknown') && typeInCoreMap === column.mappedType;
|
|
601
601
|
}) ?? runtimeType);
|
|
602
602
|
const ignoreSchemaChanges = (type === 'unknown' && column.length) ? (column.extra ? ['type', 'extra'] : ['type']) : undefined;
|
|
603
603
|
const defaultRaw = this.getPropertyDefaultValue(schemaHelper, column, runtimeType, true);
|
|
@@ -644,12 +644,12 @@ class DatabaseTable {
|
|
|
644
644
|
}
|
|
645
645
|
getReferenceKind(fk, unique) {
|
|
646
646
|
if (fk && unique) {
|
|
647
|
-
return
|
|
647
|
+
return ReferenceKind.ONE_TO_ONE;
|
|
648
648
|
}
|
|
649
649
|
if (fk) {
|
|
650
|
-
return
|
|
650
|
+
return ReferenceKind.MANY_TO_ONE;
|
|
651
651
|
}
|
|
652
|
-
return
|
|
652
|
+
return ReferenceKind.SCALAR;
|
|
653
653
|
}
|
|
654
654
|
getPropertyName(namingStrategy, baseName, fk) {
|
|
655
655
|
let field = baseName;
|
|
@@ -701,19 +701,31 @@ class DatabaseTable {
|
|
|
701
701
|
return '' + val;
|
|
702
702
|
}
|
|
703
703
|
addIndex(meta, index, type) {
|
|
704
|
-
const properties =
|
|
705
|
-
const
|
|
704
|
+
const properties = Utils.unique(Utils.flatten(Utils.asArray(index.properties).map(prop => {
|
|
705
|
+
const parts = prop.split('.');
|
|
706
|
+
const root = parts[0];
|
|
706
707
|
if (meta.properties[prop]) {
|
|
707
708
|
if (meta.properties[prop].embeddedPath) {
|
|
708
709
|
return [meta.properties[prop].embeddedPath.join('.')];
|
|
709
710
|
}
|
|
710
711
|
return meta.properties[prop].fieldNames;
|
|
711
712
|
}
|
|
713
|
+
const rootProp = meta.properties[root];
|
|
714
|
+
// inline embedded property index, we need to find the field name of the child property
|
|
715
|
+
if (rootProp?.embeddable && !rootProp.object && parts.length > 1) {
|
|
716
|
+
const expand = (p, i) => {
|
|
717
|
+
if (parts.length === i) {
|
|
718
|
+
return p.fieldNames[0];
|
|
719
|
+
}
|
|
720
|
+
return expand(p.embeddedProps[parts[i]], i + 1);
|
|
721
|
+
};
|
|
722
|
+
return [expand(rootProp, 1)];
|
|
723
|
+
}
|
|
712
724
|
// json index, we need to rename the column only
|
|
713
|
-
if (
|
|
714
|
-
return [prop.replace(root,
|
|
725
|
+
if (rootProp) {
|
|
726
|
+
return [prop.replace(root, rootProp.fieldNames[0])];
|
|
715
727
|
}
|
|
716
|
-
/*
|
|
728
|
+
/* v8 ignore next */
|
|
717
729
|
return [prop];
|
|
718
730
|
})));
|
|
719
731
|
if (properties.length === 0 && !index.expression) {
|
|
@@ -739,13 +751,12 @@ class DatabaseTable {
|
|
|
739
751
|
}
|
|
740
752
|
toJSON() {
|
|
741
753
|
const { platform, columns, ...rest } = this;
|
|
742
|
-
const columnsMapped =
|
|
754
|
+
const columnsMapped = Utils.keys(columns).reduce((o, col) => {
|
|
743
755
|
const { mappedType, ...restCol } = columns[col];
|
|
744
756
|
o[col] = restCol;
|
|
745
|
-
o[col].mappedType =
|
|
757
|
+
o[col].mappedType = Utils.keys(t).find(k => t[k] === mappedType.constructor);
|
|
746
758
|
return o;
|
|
747
759
|
}, {});
|
|
748
760
|
return { columns: columnsMapped, ...rest };
|
|
749
761
|
}
|
|
750
762
|
}
|
|
751
|
-
exports.DatabaseTable = DatabaseTable;
|
|
@@ -1,8 +1,8 @@
|
|
|
1
1
|
import { type Dictionary } from '@mikro-orm/core';
|
|
2
|
-
import type { Column, ForeignKey, IndexDef, SchemaDifference, TableDifference } from '../typings';
|
|
3
|
-
import type { DatabaseSchema } from './DatabaseSchema';
|
|
4
|
-
import type { DatabaseTable } from './DatabaseTable';
|
|
5
|
-
import type { AbstractSqlPlatform } from '../AbstractSqlPlatform';
|
|
2
|
+
import type { Column, ForeignKey, IndexDef, SchemaDifference, TableDifference } from '../typings.js';
|
|
3
|
+
import type { DatabaseSchema } from './DatabaseSchema.js';
|
|
4
|
+
import type { DatabaseTable } from './DatabaseTable.js';
|
|
5
|
+
import type { AbstractSqlPlatform } from '../AbstractSqlPlatform.js';
|
|
6
6
|
/**
|
|
7
7
|
* Compares two Schemas and return an instance of SchemaDifference.
|
|
8
8
|
*/
|
|
@@ -1,12 +1,9 @@
|
|
|
1
|
-
|
|
2
|
-
|
|
3
|
-
exports.SchemaComparator = void 0;
|
|
4
|
-
const node_util_1 = require("node:util");
|
|
5
|
-
const core_1 = require("@mikro-orm/core");
|
|
1
|
+
import { inspect } from 'node:util';
|
|
2
|
+
import { ArrayType, BooleanType, DateTimeType, JsonType, parseJsonSafe, Utils, } from '@mikro-orm/core';
|
|
6
3
|
/**
|
|
7
4
|
* Compares two Schemas and return an instance of SchemaDifference.
|
|
8
5
|
*/
|
|
9
|
-
class SchemaComparator {
|
|
6
|
+
export class SchemaComparator {
|
|
10
7
|
platform;
|
|
11
8
|
helper;
|
|
12
9
|
logger;
|
|
@@ -311,7 +308,7 @@ class SchemaComparator {
|
|
|
311
308
|
const [removedColumn, addedColumn] = candidateColumns[0];
|
|
312
309
|
const removedColumnName = removedColumn.name;
|
|
313
310
|
const addedColumnName = addedColumn.name;
|
|
314
|
-
/*
|
|
311
|
+
/* v8 ignore next 3 */
|
|
315
312
|
if (tableDifferences.renamedColumns[removedColumnName]) {
|
|
316
313
|
continue;
|
|
317
314
|
}
|
|
@@ -399,8 +396,8 @@ class SchemaComparator {
|
|
|
399
396
|
let toColumnType = this.platform.normalizeColumnType(toColumn.mappedType.getColumnType(toProp, this.platform).toLowerCase(), toProp);
|
|
400
397
|
const log = (msg, params) => {
|
|
401
398
|
if (logging) {
|
|
402
|
-
const copy =
|
|
403
|
-
|
|
399
|
+
const copy = Utils.copy(params);
|
|
400
|
+
Utils.dropUndefinedProperties(copy);
|
|
404
401
|
this.log(msg, copy);
|
|
405
402
|
}
|
|
406
403
|
};
|
|
@@ -441,8 +438,8 @@ class SchemaComparator {
|
|
|
441
438
|
log(`'comment' changed for column ${fromTable.name}.${fromColumn.name}`, { fromColumn, toColumn });
|
|
442
439
|
changedProperties.add('comment');
|
|
443
440
|
}
|
|
444
|
-
if (!(fromColumn.mappedType instanceof
|
|
445
|
-
!(toColumn.mappedType instanceof
|
|
441
|
+
if (!(fromColumn.mappedType instanceof ArrayType) &&
|
|
442
|
+
!(toColumn.mappedType instanceof ArrayType) &&
|
|
446
443
|
this.diffEnumItems(fromColumn.enumItems, toColumn.enumItems)) {
|
|
447
444
|
log(`'enumItems' changed for column ${fromTable.name}.${fromColumn.name}`, { fromColumn, toColumn });
|
|
448
445
|
changedProperties.add('enumItems');
|
|
@@ -521,30 +518,30 @@ class SchemaComparator {
|
|
|
521
518
|
return simplify(expr1) !== simplify(expr2);
|
|
522
519
|
}
|
|
523
520
|
parseJsonDefault(defaultValue) {
|
|
524
|
-
/*
|
|
521
|
+
/* v8 ignore next 3 */
|
|
525
522
|
if (!defaultValue) {
|
|
526
523
|
return null;
|
|
527
524
|
}
|
|
528
525
|
const val = defaultValue
|
|
529
526
|
.replace(/^(_\w+\\)?'(.*?)\\?'$/, '$2')
|
|
530
527
|
.replace(/^\(?'(.*?)'\)?$/, '$1');
|
|
531
|
-
return
|
|
528
|
+
return parseJsonSafe(val);
|
|
532
529
|
}
|
|
533
530
|
hasSameDefaultValue(from, to) {
|
|
534
531
|
if (from.default == null || from.default.toString().toLowerCase() === 'null' || from.default.toString().startsWith('nextval(')) {
|
|
535
532
|
return to.default == null || to.default.toLowerCase() === 'null';
|
|
536
533
|
}
|
|
537
|
-
if (to.mappedType instanceof
|
|
534
|
+
if (to.mappedType instanceof BooleanType) {
|
|
538
535
|
const defaultValueFrom = !['0', 'false', 'f', 'n', 'no', 'off'].includes('' + from.default);
|
|
539
536
|
const defaultValueTo = !['0', 'false', 'f', 'n', 'no', 'off'].includes('' + to.default);
|
|
540
537
|
return defaultValueFrom === defaultValueTo;
|
|
541
538
|
}
|
|
542
|
-
if (to.mappedType instanceof
|
|
539
|
+
if (to.mappedType instanceof JsonType) {
|
|
543
540
|
const defaultValueFrom = this.parseJsonDefault(from.default);
|
|
544
541
|
const defaultValueTo = this.parseJsonDefault(to.default);
|
|
545
|
-
return
|
|
542
|
+
return Utils.equals(defaultValueFrom, defaultValueTo);
|
|
546
543
|
}
|
|
547
|
-
if (to.mappedType instanceof
|
|
544
|
+
if (to.mappedType instanceof DateTimeType && from.default && to.default) {
|
|
548
545
|
// normalize now/current_timestamp defaults, also remove `()` from the end of default expression
|
|
549
546
|
const defaultValueFrom = from.default.toLowerCase().replace('current_timestamp', 'now').replace(/\(\)$/, '');
|
|
550
547
|
const defaultValueTo = to.default.toLowerCase().replace('current_timestamp', 'now').replace(/\(\)$/, '');
|
|
@@ -574,9 +571,8 @@ class SchemaComparator {
|
|
|
574
571
|
}
|
|
575
572
|
log(message, params) {
|
|
576
573
|
if (params) {
|
|
577
|
-
message += ' ' +
|
|
574
|
+
message += ' ' + inspect(params);
|
|
578
575
|
}
|
|
579
576
|
this.logger.log('schema', message);
|
|
580
577
|
}
|
|
581
578
|
}
|
|
582
|
-
exports.SchemaComparator = SchemaComparator;
|
package/schema/SchemaHelper.d.ts
CHANGED
|
@@ -1,9 +1,9 @@
|
|
|
1
1
|
import { type Connection, type Dictionary } from '@mikro-orm/core';
|
|
2
|
-
import type { AbstractSqlConnection } from '../AbstractSqlConnection';
|
|
3
|
-
import type { AbstractSqlPlatform } from '../AbstractSqlPlatform';
|
|
4
|
-
import type { CheckDef, Column, ForeignKey, IndexDef, Table, TableDifference } from '../typings';
|
|
5
|
-
import type { DatabaseSchema } from './DatabaseSchema';
|
|
6
|
-
import type { DatabaseTable } from './DatabaseTable';
|
|
2
|
+
import type { AbstractSqlConnection } from '../AbstractSqlConnection.js';
|
|
3
|
+
import type { AbstractSqlPlatform } from '../AbstractSqlPlatform.js';
|
|
4
|
+
import type { CheckDef, Column, ForeignKey, IndexDef, Table, TableDifference } from '../typings.js';
|
|
5
|
+
import type { DatabaseSchema } from './DatabaseSchema.js';
|
|
6
|
+
import type { DatabaseTable } from './DatabaseTable.js';
|
|
7
7
|
export declare abstract class SchemaHelper {
|
|
8
8
|
protected readonly platform: AbstractSqlPlatform;
|
|
9
9
|
constructor(platform: AbstractSqlPlatform);
|
package/schema/SchemaHelper.js
CHANGED
|
@@ -1,8 +1,5 @@
|
|
|
1
|
-
|
|
2
|
-
|
|
3
|
-
exports.SchemaHelper = void 0;
|
|
4
|
-
const core_1 = require("@mikro-orm/core");
|
|
5
|
-
class SchemaHelper {
|
|
1
|
+
import { RawQueryFragment, Utils } from '@mikro-orm/core';
|
|
2
|
+
export class SchemaHelper {
|
|
6
3
|
platform;
|
|
7
4
|
constructor(platform) {
|
|
8
5
|
this.platform = platform;
|
|
@@ -36,7 +33,7 @@ class SchemaHelper {
|
|
|
36
33
|
}
|
|
37
34
|
async getPrimaryKeys(connection, indexes = [], tableName, schemaName) {
|
|
38
35
|
const pks = indexes.filter(i => i.primary).map(pk => pk.columnNames);
|
|
39
|
-
return
|
|
36
|
+
return Utils.flatten(pks);
|
|
40
37
|
}
|
|
41
38
|
inferLengthFromColumnType(type) {
|
|
42
39
|
const match = type.match(/^\w+\s*(?:\(\s*(\d+)\s*\)|$)/);
|
|
@@ -77,7 +74,7 @@ class SchemaHelper {
|
|
|
77
74
|
return `alter table ${tableReference} rename column ${oldColumnName} to ${columnName}`;
|
|
78
75
|
}
|
|
79
76
|
getCreateIndexSQL(tableName, index) {
|
|
80
|
-
/*
|
|
77
|
+
/* v8 ignore next 3 */
|
|
81
78
|
if (index.expression) {
|
|
82
79
|
return index.expression;
|
|
83
80
|
}
|
|
@@ -121,7 +118,7 @@ class SchemaHelper {
|
|
|
121
118
|
changedNativeEnums.push([column.nativeEnumName, column.enumItems, diff.fromTable.nativeEnums[key].items]);
|
|
122
119
|
}
|
|
123
120
|
}
|
|
124
|
-
|
|
121
|
+
Utils.removeDuplicates(changedNativeEnums).forEach(([enumName, itemsNew, itemsOld]) => {
|
|
125
122
|
// postgres allows only adding new items, the values are case insensitive
|
|
126
123
|
itemsOld = itemsOld.map(v => v.toLowerCase());
|
|
127
124
|
const newItems = itemsNew.filter(val => !itemsOld.includes(val.toLowerCase()));
|
|
@@ -145,7 +142,7 @@ class SchemaHelper {
|
|
|
145
142
|
for (const check of Object.values(diff.changedChecks)) {
|
|
146
143
|
ret.push(this.dropConstraint(diff.name, check.name));
|
|
147
144
|
}
|
|
148
|
-
/*
|
|
145
|
+
/* v8 ignore next 3 */
|
|
149
146
|
if (!safe && Object.values(diff.removedColumns).length > 0) {
|
|
150
147
|
ret.push(this.getDropColumnsSQL(tableName, Object.values(diff.removedColumns), schemaName));
|
|
151
148
|
}
|
|
@@ -228,7 +225,7 @@ class SchemaHelper {
|
|
|
228
225
|
const defaultName = this.platform.getDefaultPrimaryName(table.name, pkIndex.columnNames);
|
|
229
226
|
return pkIndex?.keyName !== defaultName;
|
|
230
227
|
}
|
|
231
|
-
/*
|
|
228
|
+
/* v8 ignore next 3 */
|
|
232
229
|
castColumn(name, type) {
|
|
233
230
|
return '';
|
|
234
231
|
}
|
|
@@ -259,17 +256,17 @@ class SchemaHelper {
|
|
|
259
256
|
const columnType = column.type + (column.generated ? ` generated always as ${column.generated}` : '');
|
|
260
257
|
const useDefault = column.default != null && column.default !== 'null' && !column.autoincrement;
|
|
261
258
|
const col = [this.quote(column.name), columnType];
|
|
262
|
-
|
|
263
|
-
|
|
264
|
-
|
|
265
|
-
|
|
266
|
-
|
|
259
|
+
Utils.runIfNotEmpty(() => col.push('unsigned'), column.unsigned && this.platform.supportsUnsigned());
|
|
260
|
+
Utils.runIfNotEmpty(() => col.push('null'), column.nullable);
|
|
261
|
+
Utils.runIfNotEmpty(() => col.push('not null'), !column.nullable && !column.generated);
|
|
262
|
+
Utils.runIfNotEmpty(() => col.push('auto_increment'), column.autoincrement);
|
|
263
|
+
Utils.runIfNotEmpty(() => col.push('unique'), column.autoincrement && !column.primary);
|
|
267
264
|
if (column.autoincrement && !column.generated && !compositePK && (!changedProperties || changedProperties.has('autoincrement') || changedProperties.has('type'))) {
|
|
268
|
-
|
|
265
|
+
Utils.runIfNotEmpty(() => col.push('primary key'), primaryKey && column.primary);
|
|
269
266
|
}
|
|
270
|
-
|
|
271
|
-
|
|
272
|
-
|
|
267
|
+
Utils.runIfNotEmpty(() => col.push(`default ${column.default}`), useDefault);
|
|
268
|
+
Utils.runIfNotEmpty(() => col.push(column.extra), column.extra);
|
|
269
|
+
Utils.runIfNotEmpty(() => col.push(`comment ${this.platform.quoteValue(column.comment)}`), column.comment);
|
|
273
270
|
return col.join(' ');
|
|
274
271
|
}
|
|
275
272
|
getPreAlterTable(tableDiff, safe) {
|
|
@@ -322,7 +319,7 @@ class SchemaHelper {
|
|
|
322
319
|
if (defaultValue == null) {
|
|
323
320
|
return defaultValue;
|
|
324
321
|
}
|
|
325
|
-
const raw =
|
|
322
|
+
const raw = RawQueryFragment.getKnownFragment(defaultValue);
|
|
326
323
|
if (raw) {
|
|
327
324
|
return this.platform.formatQuery(raw.sql, raw.params);
|
|
328
325
|
}
|
|
@@ -340,11 +337,11 @@ class SchemaHelper {
|
|
|
340
337
|
getDropDatabaseSQL(name) {
|
|
341
338
|
return `drop database if exists ${this.quote(name)}`;
|
|
342
339
|
}
|
|
343
|
-
/*
|
|
340
|
+
/* v8 ignore next 3 */
|
|
344
341
|
getCreateNamespaceSQL(name) {
|
|
345
342
|
return `create schema if not exists ${this.quote(name)}`;
|
|
346
343
|
}
|
|
347
|
-
/*
|
|
344
|
+
/* v8 ignore next 3 */
|
|
348
345
|
getDropNamespaceSQL(name) {
|
|
349
346
|
return `drop schema if exists ${this.quote(name)}`;
|
|
350
347
|
}
|
|
@@ -369,13 +366,13 @@ class SchemaHelper {
|
|
|
369
366
|
if (e instanceof Error && e.message.includes(this.getDatabaseNotExistsError(name))) {
|
|
370
367
|
return false;
|
|
371
368
|
}
|
|
372
|
-
/*
|
|
369
|
+
/* v8 ignore next */
|
|
373
370
|
throw e;
|
|
374
371
|
}
|
|
375
372
|
}
|
|
376
373
|
append(array, sql, pad = false) {
|
|
377
374
|
const length = array.length;
|
|
378
|
-
for (const row of
|
|
375
|
+
for (const row of Utils.asArray(sql)) {
|
|
379
376
|
if (!row) {
|
|
380
377
|
continue;
|
|
381
378
|
}
|
|
@@ -466,7 +463,7 @@ class SchemaHelper {
|
|
|
466
463
|
getReferencedTableName(referencedTableName, schema) {
|
|
467
464
|
const [schemaName, tableName] = this.splitTableName(referencedTableName);
|
|
468
465
|
schema = schemaName ?? schema ?? this.platform.getConfig().get('schema');
|
|
469
|
-
/*
|
|
466
|
+
/* v8 ignore next 3 */
|
|
470
467
|
if (schema && schemaName === '*') {
|
|
471
468
|
return `${schema}.${referencedTableName.replace(/^\*\./, '')}`;
|
|
472
469
|
}
|
|
@@ -542,4 +539,3 @@ class SchemaHelper {
|
|
|
542
539
|
return sql;
|
|
543
540
|
}
|
|
544
541
|
}
|
|
545
|
-
exports.SchemaHelper = SchemaHelper;
|
|
@@ -1,9 +1,9 @@
|
|
|
1
1
|
import { AbstractSchemaGenerator, type ClearDatabaseOptions, type CreateSchemaOptions, type DropSchemaOptions, type EnsureDatabaseOptions, type ISchemaGenerator, type MikroORM, type Transaction, type UpdateSchemaOptions } from '@mikro-orm/core';
|
|
2
|
-
import type { SchemaDifference } from '../typings';
|
|
3
|
-
import { DatabaseSchema } from './DatabaseSchema';
|
|
4
|
-
import type { AbstractSqlDriver } from '../AbstractSqlDriver';
|
|
2
|
+
import type { SchemaDifference } from '../typings.js';
|
|
3
|
+
import { DatabaseSchema } from './DatabaseSchema.js';
|
|
4
|
+
import type { AbstractSqlDriver } from '../AbstractSqlDriver.js';
|
|
5
5
|
export declare class SqlSchemaGenerator extends AbstractSchemaGenerator<AbstractSqlDriver> implements ISchemaGenerator {
|
|
6
|
-
protected readonly helper: import("./SchemaHelper").SchemaHelper;
|
|
6
|
+
protected readonly helper: import("./SchemaHelper.js").SchemaHelper;
|
|
7
7
|
protected readonly options: {
|
|
8
8
|
disableForeignKeys?: boolean;
|
|
9
9
|
createForeignKeyConstraints?: boolean;
|