@yandjin-mikro-orm/knex 6.1.4-rc-sti-changes-1
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 +58 -0
- package/AbstractSqlConnection.js +206 -0
- package/AbstractSqlDriver.d.ts +73 -0
- package/AbstractSqlDriver.js +1378 -0
- package/AbstractSqlPlatform.d.ts +25 -0
- package/AbstractSqlPlatform.js +86 -0
- package/LICENSE +21 -0
- package/MonkeyPatchable.d.ts +11 -0
- package/MonkeyPatchable.js +39 -0
- package/PivotCollectionPersister.d.ts +17 -0
- package/PivotCollectionPersister.js +131 -0
- package/README.md +383 -0
- package/SqlEntityManager.d.ts +25 -0
- package/SqlEntityManager.js +40 -0
- package/SqlEntityRepository.d.ts +24 -0
- package/SqlEntityRepository.js +36 -0
- package/index.d.ts +18 -0
- package/index.js +40 -0
- package/index.mjs +215 -0
- package/package.json +71 -0
- package/query/ArrayCriteriaNode.d.ts +10 -0
- package/query/ArrayCriteriaNode.js +25 -0
- package/query/CriteriaNode.d.ts +31 -0
- package/query/CriteriaNode.js +147 -0
- package/query/CriteriaNodeFactory.d.ts +12 -0
- package/query/CriteriaNodeFactory.js +90 -0
- package/query/ObjectCriteriaNode.d.ts +15 -0
- package/query/ObjectCriteriaNode.js +233 -0
- package/query/QueryBuilder.d.ts +291 -0
- package/query/QueryBuilder.js +1445 -0
- package/query/QueryBuilderHelper.d.ts +64 -0
- package/query/QueryBuilderHelper.js +747 -0
- package/query/ScalarCriteriaNode.d.ts +10 -0
- package/query/ScalarCriteriaNode.js +56 -0
- package/query/enums.d.ts +15 -0
- package/query/enums.js +20 -0
- package/query/index.d.ts +8 -0
- package/query/index.js +24 -0
- package/schema/DatabaseSchema.d.ts +29 -0
- package/schema/DatabaseSchema.js +140 -0
- package/schema/DatabaseTable.d.ts +61 -0
- package/schema/DatabaseTable.js +727 -0
- package/schema/SchemaComparator.d.ts +59 -0
- package/schema/SchemaComparator.js +603 -0
- package/schema/SchemaHelper.d.ts +56 -0
- package/schema/SchemaHelper.js +274 -0
- package/schema/SqlSchemaGenerator.d.ts +63 -0
- package/schema/SqlSchemaGenerator.js +598 -0
- package/schema/index.d.ts +5 -0
- package/schema/index.js +21 -0
- package/typings.d.ts +174 -0
- package/typings.js +2 -0
|
@@ -0,0 +1,727 @@
|
|
|
1
|
+
"use strict";
|
|
2
|
+
Object.defineProperty(exports, "__esModule", { value: true });
|
|
3
|
+
exports.DatabaseTable = void 0;
|
|
4
|
+
const core_1 = require("@yandjin-mikro-orm/core");
|
|
5
|
+
/**
|
|
6
|
+
* @internal
|
|
7
|
+
*/
|
|
8
|
+
class DatabaseTable {
|
|
9
|
+
platform;
|
|
10
|
+
name;
|
|
11
|
+
schema;
|
|
12
|
+
columns = {};
|
|
13
|
+
indexes = [];
|
|
14
|
+
checks = [];
|
|
15
|
+
foreignKeys = {};
|
|
16
|
+
nativeEnums = {}; // for postgres
|
|
17
|
+
comment;
|
|
18
|
+
constructor(platform, name, schema) {
|
|
19
|
+
this.platform = platform;
|
|
20
|
+
this.name = name;
|
|
21
|
+
this.schema = schema;
|
|
22
|
+
Object.defineProperties(this, {
|
|
23
|
+
platform: { enumerable: false, writable: true },
|
|
24
|
+
});
|
|
25
|
+
}
|
|
26
|
+
getColumns() {
|
|
27
|
+
return Object.values(this.columns);
|
|
28
|
+
}
|
|
29
|
+
getColumn(name) {
|
|
30
|
+
return this.columns[name];
|
|
31
|
+
}
|
|
32
|
+
removeColumn(name) {
|
|
33
|
+
delete this.columns[name];
|
|
34
|
+
}
|
|
35
|
+
getIndexes() {
|
|
36
|
+
return core_1.Utils.removeDuplicates(this.indexes);
|
|
37
|
+
}
|
|
38
|
+
getChecks() {
|
|
39
|
+
return this.checks;
|
|
40
|
+
}
|
|
41
|
+
init(cols, indexes = [], checks = [], pks, fks = {}, enums = {}) {
|
|
42
|
+
this.indexes = indexes;
|
|
43
|
+
this.checks = checks;
|
|
44
|
+
this.foreignKeys = fks;
|
|
45
|
+
this.columns = cols.reduce((o, v) => {
|
|
46
|
+
const index = indexes.filter((i) => i.columnNames[0] === v.name);
|
|
47
|
+
v.primary = v.primary || pks.includes(v.name);
|
|
48
|
+
v.unique = index.some((i) => i.unique && !i.primary);
|
|
49
|
+
const type = v.name in enums ? "enum" : v.type;
|
|
50
|
+
v.mappedType = this.platform.getMappedType(type);
|
|
51
|
+
v.default = v.default?.toString().startsWith("nextval(")
|
|
52
|
+
? null
|
|
53
|
+
: v.default;
|
|
54
|
+
v.enumItems ??= enums[v.name] || [];
|
|
55
|
+
o[v.name] = v;
|
|
56
|
+
return o;
|
|
57
|
+
}, {});
|
|
58
|
+
}
|
|
59
|
+
addColumn(column) {
|
|
60
|
+
this.columns[column.name] = column;
|
|
61
|
+
}
|
|
62
|
+
addColumnFromProperty(prop, meta, config) {
|
|
63
|
+
prop.fieldNames.forEach((field, idx) => {
|
|
64
|
+
const type = prop.enum ? "enum" : prop.columnTypes[idx];
|
|
65
|
+
const mappedType = this.platform.getMappedType(type);
|
|
66
|
+
if (mappedType instanceof core_1.DecimalType) {
|
|
67
|
+
const match = prop.columnTypes[idx].match(/\w+\((\d+), ?(\d+)\)/);
|
|
68
|
+
/* istanbul ignore else */
|
|
69
|
+
if (match) {
|
|
70
|
+
prop.precision ??= +match[1];
|
|
71
|
+
prop.scale ??= +match[2];
|
|
72
|
+
prop.length = undefined;
|
|
73
|
+
}
|
|
74
|
+
}
|
|
75
|
+
if (mappedType instanceof core_1.DateTimeType ||
|
|
76
|
+
mappedType instanceof core_1.IntervalType) {
|
|
77
|
+
const match = prop.columnTypes[idx].match(/\w+\((\d+)\)/);
|
|
78
|
+
if (match) {
|
|
79
|
+
prop.length ??= +match[1];
|
|
80
|
+
}
|
|
81
|
+
else {
|
|
82
|
+
prop.length ??= this.platform.getDefaultDateTimeLength();
|
|
83
|
+
}
|
|
84
|
+
}
|
|
85
|
+
const nullable = !(prop.name in meta.root.properties)
|
|
86
|
+
? true
|
|
87
|
+
: (this.columns[field]?.nullable ?? !!prop.nullable);
|
|
88
|
+
const primary = !meta.compositePK &&
|
|
89
|
+
!!prop.primary &&
|
|
90
|
+
prop.kind === core_1.ReferenceKind.SCALAR &&
|
|
91
|
+
this.platform.isNumericColumn(mappedType);
|
|
92
|
+
this.columns[field] = {
|
|
93
|
+
name: prop.fieldNames[idx],
|
|
94
|
+
type: prop.columnTypes[idx],
|
|
95
|
+
generated: prop.generated,
|
|
96
|
+
mappedType,
|
|
97
|
+
unsigned: prop.unsigned && this.platform.isNumericColumn(mappedType),
|
|
98
|
+
autoincrement: prop.autoincrement ?? primary,
|
|
99
|
+
primary,
|
|
100
|
+
nullable,
|
|
101
|
+
nativeEnumName: prop.nativeEnumName,
|
|
102
|
+
length: prop.length,
|
|
103
|
+
precision: prop.precision,
|
|
104
|
+
scale: prop.scale,
|
|
105
|
+
default: prop.defaultRaw,
|
|
106
|
+
enumItems: prop.nativeEnumName || prop.items?.every(core_1.Utils.isString)
|
|
107
|
+
? prop.items
|
|
108
|
+
: undefined,
|
|
109
|
+
comment: prop.comment,
|
|
110
|
+
extra: prop.extra,
|
|
111
|
+
ignoreSchemaChanges: prop.ignoreSchemaChanges,
|
|
112
|
+
};
|
|
113
|
+
this.columns[field].unsigned ||= this.columns[field].autoincrement;
|
|
114
|
+
const defaultValue = this.platform
|
|
115
|
+
.getSchemaHelper()
|
|
116
|
+
.normalizeDefaultValue(prop.defaultRaw, prop.length);
|
|
117
|
+
this.columns[field].default = defaultValue;
|
|
118
|
+
});
|
|
119
|
+
if ([core_1.ReferenceKind.MANY_TO_ONE, core_1.ReferenceKind.ONE_TO_ONE].includes(prop.kind)) {
|
|
120
|
+
const constraintName = this.getIndexName(true, prop.fieldNames, "foreign");
|
|
121
|
+
let schema = prop.targetMeta.root.schema === "*"
|
|
122
|
+
? this.schema
|
|
123
|
+
: (prop.targetMeta.root.schema ??
|
|
124
|
+
config.get("schema", this.platform.getDefaultSchemaName()));
|
|
125
|
+
if (prop.referencedTableName.includes(".")) {
|
|
126
|
+
schema = undefined;
|
|
127
|
+
}
|
|
128
|
+
this.foreignKeys[constraintName] = {
|
|
129
|
+
constraintName,
|
|
130
|
+
columnNames: prop.fieldNames,
|
|
131
|
+
localTableName: this.getShortestName(),
|
|
132
|
+
referencedColumnNames: prop.referencedColumnNames,
|
|
133
|
+
referencedTableName: schema
|
|
134
|
+
? `${schema}.${prop.referencedTableName}`
|
|
135
|
+
: prop.referencedTableName,
|
|
136
|
+
};
|
|
137
|
+
const cascade = prop.cascade.includes(core_1.Cascade.REMOVE) ||
|
|
138
|
+
prop.cascade.includes(core_1.Cascade.ALL);
|
|
139
|
+
if (prop.deleteRule || cascade || prop.nullable) {
|
|
140
|
+
this.foreignKeys[constraintName].deleteRule =
|
|
141
|
+
prop.deleteRule || (cascade ? "cascade" : "set null");
|
|
142
|
+
}
|
|
143
|
+
if (prop.updateRule ||
|
|
144
|
+
prop.cascade.includes(core_1.Cascade.PERSIST) ||
|
|
145
|
+
prop.cascade.includes(core_1.Cascade.ALL)) {
|
|
146
|
+
this.foreignKeys[constraintName].updateRule =
|
|
147
|
+
prop.updateRule || "cascade";
|
|
148
|
+
}
|
|
149
|
+
}
|
|
150
|
+
if (prop.index) {
|
|
151
|
+
const keyName = this.getIndexName(prop.index, prop.fieldNames, "index");
|
|
152
|
+
// const indexExists = this.indexes.find(index => index.keyName === keyName);
|
|
153
|
+
// if (!indexExists) {
|
|
154
|
+
this.indexes.push({
|
|
155
|
+
columnNames: prop.fieldNames,
|
|
156
|
+
composite: prop.fieldNames.length > 1,
|
|
157
|
+
keyName,
|
|
158
|
+
constraint: false,
|
|
159
|
+
primary: false,
|
|
160
|
+
unique: false,
|
|
161
|
+
});
|
|
162
|
+
// }
|
|
163
|
+
}
|
|
164
|
+
if (prop.unique && !(prop.primary && !meta.compositePK)) {
|
|
165
|
+
const keyName = this.getIndexName(prop.unique, prop.fieldNames, "unique");
|
|
166
|
+
// const indexExists = this.indexes.find(index => index.keyName === keyName);
|
|
167
|
+
// if (!indexExists) {
|
|
168
|
+
this.indexes.push({
|
|
169
|
+
columnNames: prop.fieldNames,
|
|
170
|
+
composite: prop.fieldNames.length > 1,
|
|
171
|
+
keyName,
|
|
172
|
+
constraint: !prop.fieldNames.some((d) => d.includes(".")),
|
|
173
|
+
primary: false,
|
|
174
|
+
unique: true,
|
|
175
|
+
});
|
|
176
|
+
// }
|
|
177
|
+
}
|
|
178
|
+
}
|
|
179
|
+
getIndexName(value, columnNames, type) {
|
|
180
|
+
if (core_1.Utils.isString(value)) {
|
|
181
|
+
return value;
|
|
182
|
+
}
|
|
183
|
+
return this.platform.getIndexName(this.name, columnNames, type);
|
|
184
|
+
}
|
|
185
|
+
getEntityDeclaration(namingStrategy, schemaHelper, scalarPropertiesForRelations) {
|
|
186
|
+
const { fksOnColumnProps, fksOnStandaloneProps, columnFks, fkIndexes, nullableForeignKeys, skippedColumnNames, } = this.foreignKeysToProps(namingStrategy, scalarPropertiesForRelations);
|
|
187
|
+
let name = namingStrategy.getEntityName(this.name, this.schema);
|
|
188
|
+
name = name.match(/^\d/) ? "E" + name : name;
|
|
189
|
+
const schema = new core_1.EntitySchema({
|
|
190
|
+
name,
|
|
191
|
+
collection: this.name,
|
|
192
|
+
schema: this.schema,
|
|
193
|
+
});
|
|
194
|
+
const compositeFkIndexes = {};
|
|
195
|
+
const compositeFkUniques = {};
|
|
196
|
+
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.
|
|
197
|
+
(index.columnNames.length > 1 || // All composite indexes are to be mapped to entity decorators or FK props.
|
|
198
|
+
!(index.columnNames[0] in columnFks) || // Non-composite indexes for scalar props are to be mapped to the column.
|
|
199
|
+
skippedColumnNames.includes(index.columnNames[0])) && // Non-composite indexes for skipped columns are to be mapped as entity decorators.
|
|
200
|
+
// ignore indexes that don't have all column names (this can happen in sqlite where there is no way to infer this for expressions)
|
|
201
|
+
!(index.columnNames.some((col) => !col) && !index.expression));
|
|
202
|
+
for (const index of potentiallyUnmappedIndexes) {
|
|
203
|
+
const ret = { name: index.keyName };
|
|
204
|
+
// Index is for FK. Map to the FK prop and move on.
|
|
205
|
+
const fkForIndex = fkIndexes.get(index);
|
|
206
|
+
if (fkForIndex &&
|
|
207
|
+
!fkForIndex.fk.columnNames.some((col) => !index.columnNames.includes(col))) {
|
|
208
|
+
ret.properties = [
|
|
209
|
+
this.getPropertyName(namingStrategy, fkForIndex.baseName, fkForIndex.fk),
|
|
210
|
+
];
|
|
211
|
+
const map = index.unique ? compositeFkUniques : compositeFkIndexes;
|
|
212
|
+
map[ret.properties[0]] = { keyName: index.keyName };
|
|
213
|
+
continue;
|
|
214
|
+
}
|
|
215
|
+
const properties = this.getIndexProperties(index, columnFks, fksOnColumnProps, fksOnStandaloneProps, namingStrategy);
|
|
216
|
+
// If there is a column that cannot be unambiguously mapped to a prop, render an expression.
|
|
217
|
+
if (index.expression) {
|
|
218
|
+
ret.expression = index.expression;
|
|
219
|
+
}
|
|
220
|
+
else if (typeof properties === "undefined") {
|
|
221
|
+
ret.expression = schemaHelper.getCreateIndexSQL(this.name, index);
|
|
222
|
+
}
|
|
223
|
+
else {
|
|
224
|
+
ret.properties = properties;
|
|
225
|
+
// If the index is for one property that is not a FK prop, map to the column prop and move on.
|
|
226
|
+
if (properties.length === 1 &&
|
|
227
|
+
!fksOnStandaloneProps.has(properties[0])) {
|
|
228
|
+
const map = index.unique ? compositeFkUniques : compositeFkIndexes;
|
|
229
|
+
map[properties[0]] = { keyName: index.keyName };
|
|
230
|
+
continue;
|
|
231
|
+
}
|
|
232
|
+
}
|
|
233
|
+
// Composite indexes that aren't exclusively mapped to FK props get an entity decorator.
|
|
234
|
+
if (index.unique) {
|
|
235
|
+
schema.addUnique(ret);
|
|
236
|
+
continue;
|
|
237
|
+
}
|
|
238
|
+
schema.addIndex(ret);
|
|
239
|
+
}
|
|
240
|
+
const addedStandaloneFkPropsBasedOnColumn = new Set();
|
|
241
|
+
const nonSkippedColumns = this.getColumns().filter((column) => !skippedColumnNames.includes(column.name));
|
|
242
|
+
for (const column of nonSkippedColumns) {
|
|
243
|
+
const columnName = column.name;
|
|
244
|
+
const standaloneFkPropBasedOnColumn = fksOnStandaloneProps.get(columnName);
|
|
245
|
+
if (standaloneFkPropBasedOnColumn && !fksOnColumnProps.get(columnName)) {
|
|
246
|
+
addedStandaloneFkPropsBasedOnColumn.add(columnName);
|
|
247
|
+
const [fkIndex, currentFk] = standaloneFkPropBasedOnColumn;
|
|
248
|
+
const prop = this.getForeignKeyDeclaration(currentFk, namingStrategy, schemaHelper, fkIndex, nullableForeignKeys.has(currentFk), columnName);
|
|
249
|
+
schema.addProperty(prop.name, prop.type, prop);
|
|
250
|
+
}
|
|
251
|
+
const prop = this.getPropertyDeclaration(column, namingStrategy, schemaHelper, compositeFkIndexes, compositeFkUniques, columnFks, fksOnColumnProps.get(columnName));
|
|
252
|
+
schema.addProperty(prop.name, prop.type, prop);
|
|
253
|
+
}
|
|
254
|
+
for (const [propBaseName, [fkIndex, currentFk],] of fksOnStandaloneProps.entries()) {
|
|
255
|
+
if (addedStandaloneFkPropsBasedOnColumn.has(propBaseName)) {
|
|
256
|
+
continue;
|
|
257
|
+
}
|
|
258
|
+
const prop = this.getForeignKeyDeclaration(currentFk, namingStrategy, schemaHelper, fkIndex, nullableForeignKeys.has(currentFk), propBaseName);
|
|
259
|
+
schema.addProperty(prop.name, prop.type, prop);
|
|
260
|
+
}
|
|
261
|
+
const meta = schema.init().meta;
|
|
262
|
+
const oneToOneCandidateProperties = meta.relations.filter((prop) => prop.primary && prop.kind === core_1.ReferenceKind.MANY_TO_ONE);
|
|
263
|
+
if (oneToOneCandidateProperties.length === 1 &&
|
|
264
|
+
oneToOneCandidateProperties[0].fieldNames.length ===
|
|
265
|
+
new Set(meta.getPrimaryProps().flatMap((prop) => prop.fieldNames)).size) {
|
|
266
|
+
oneToOneCandidateProperties[0].kind = core_1.ReferenceKind.ONE_TO_ONE;
|
|
267
|
+
}
|
|
268
|
+
return meta;
|
|
269
|
+
}
|
|
270
|
+
foreignKeysToProps(namingStrategy, scalarPropertiesForRelations) {
|
|
271
|
+
const fks = Object.values(this.getForeignKeys());
|
|
272
|
+
const fksOnColumnProps = new Map();
|
|
273
|
+
const fksOnStandaloneProps = new Map();
|
|
274
|
+
const columnFks = {};
|
|
275
|
+
const fkIndexes = new Map();
|
|
276
|
+
const nullableForeignKeys = new Set();
|
|
277
|
+
for (const currentFk of fks) {
|
|
278
|
+
const fkIndex = this.findFkIndex(currentFk);
|
|
279
|
+
if (currentFk.columnNames.length === 1 &&
|
|
280
|
+
!fks.some((fk) => fk !== currentFk &&
|
|
281
|
+
fk.columnNames.length === 1 &&
|
|
282
|
+
currentFk.columnNames[0] === fk.columnNames[0])) {
|
|
283
|
+
// Non-composite FK is the only possible one for a column. Render the column with it.
|
|
284
|
+
const columnName = currentFk.columnNames[0];
|
|
285
|
+
columnFks[columnName] ??= [];
|
|
286
|
+
columnFks[columnName].push(currentFk);
|
|
287
|
+
if (this.getColumn(columnName)?.nullable) {
|
|
288
|
+
nullableForeignKeys.add(currentFk);
|
|
289
|
+
}
|
|
290
|
+
if (scalarPropertiesForRelations === "always") {
|
|
291
|
+
const baseName = this.getSafeBaseNameForFkProp(namingStrategy, currentFk, fks, columnName);
|
|
292
|
+
fksOnStandaloneProps.set(baseName, [fkIndex, currentFk]);
|
|
293
|
+
fkIndexes.set(fkIndex, { fk: currentFk, baseName });
|
|
294
|
+
}
|
|
295
|
+
else {
|
|
296
|
+
fksOnColumnProps.set(columnName, currentFk);
|
|
297
|
+
fkIndexes.set(fkIndex, { fk: currentFk, baseName: columnName });
|
|
298
|
+
}
|
|
299
|
+
continue;
|
|
300
|
+
}
|
|
301
|
+
const specificColumnNames = [];
|
|
302
|
+
const nullableColumnsInFk = [];
|
|
303
|
+
for (const columnName of currentFk.columnNames) {
|
|
304
|
+
columnFks[columnName] ??= [];
|
|
305
|
+
columnFks[columnName].push(currentFk);
|
|
306
|
+
if (!fks.some((fk) => fk !== currentFk && fk.columnNames.includes(columnName))) {
|
|
307
|
+
specificColumnNames.push(columnName);
|
|
308
|
+
}
|
|
309
|
+
if (this.getColumn(columnName)?.nullable) {
|
|
310
|
+
nullableColumnsInFk.push(columnName);
|
|
311
|
+
}
|
|
312
|
+
}
|
|
313
|
+
if (nullableColumnsInFk.length > 0) {
|
|
314
|
+
nullableForeignKeys.add(currentFk);
|
|
315
|
+
}
|
|
316
|
+
if (specificColumnNames.length === 1 &&
|
|
317
|
+
(nullableColumnsInFk.length === currentFk.columnNames.length ||
|
|
318
|
+
nullableColumnsInFk.length === 0 ||
|
|
319
|
+
(nullableColumnsInFk.length === 1 &&
|
|
320
|
+
nullableColumnsInFk[0] === specificColumnNames[0]))) {
|
|
321
|
+
// Composite FK has exactly one column which is not used in any other FK.
|
|
322
|
+
// The FK also doesn't have a mix of nullable and non-nullable columns,
|
|
323
|
+
// or its only nullable column is this very one.
|
|
324
|
+
// It is safe to just render this FK attached to the specific column.
|
|
325
|
+
const columnName = specificColumnNames[0];
|
|
326
|
+
if (scalarPropertiesForRelations === "always") {
|
|
327
|
+
const baseName = this.getSafeBaseNameForFkProp(namingStrategy, currentFk, fks, columnName);
|
|
328
|
+
fksOnStandaloneProps.set(baseName, [fkIndex, currentFk]);
|
|
329
|
+
fkIndexes.set(fkIndex, { fk: currentFk, baseName });
|
|
330
|
+
}
|
|
331
|
+
else {
|
|
332
|
+
fksOnColumnProps.set(columnName, currentFk);
|
|
333
|
+
fkIndexes.set(fkIndex, { fk: currentFk, baseName: columnName });
|
|
334
|
+
}
|
|
335
|
+
continue;
|
|
336
|
+
}
|
|
337
|
+
if (specificColumnNames.length === currentFk.columnNames.length) {
|
|
338
|
+
// All columns involved with this FK are only covered by this one FK.
|
|
339
|
+
if (nullableColumnsInFk.length <= 1) {
|
|
340
|
+
// Also, this FK is either not nullable, or has only one nullable column.
|
|
341
|
+
// It is safe to name the FK after the nullable column, or any non-nullable one (the first one is picked).
|
|
342
|
+
const columnName = nullableColumnsInFk.at(0) ?? currentFk.columnNames[0];
|
|
343
|
+
if (scalarPropertiesForRelations === "always") {
|
|
344
|
+
const baseName = this.getSafeBaseNameForFkProp(namingStrategy, currentFk, fks, columnName);
|
|
345
|
+
fksOnStandaloneProps.set(baseName, [fkIndex, currentFk]);
|
|
346
|
+
fkIndexes.set(fkIndex, { fk: currentFk, baseName });
|
|
347
|
+
}
|
|
348
|
+
else {
|
|
349
|
+
fksOnColumnProps.set(columnName, currentFk);
|
|
350
|
+
fkIndexes.set(fkIndex, { fk: currentFk, baseName: columnName });
|
|
351
|
+
}
|
|
352
|
+
continue;
|
|
353
|
+
}
|
|
354
|
+
// If the first nullable column's name with FK is different from the name without FK,
|
|
355
|
+
// name a standalone prop after the column, but treat the column prop itself as not having FK.
|
|
356
|
+
const columnName = nullableColumnsInFk[0];
|
|
357
|
+
const baseName = this.getSafeBaseNameForFkProp(namingStrategy, currentFk, fks, columnName);
|
|
358
|
+
fksOnStandaloneProps.set(baseName, [fkIndex, currentFk]);
|
|
359
|
+
fkIndexes.set(fkIndex, { fk: currentFk, baseName });
|
|
360
|
+
continue;
|
|
361
|
+
}
|
|
362
|
+
// FK is not unambiguously mappable to a column. Pick another name for a standalone FK prop.
|
|
363
|
+
const baseName = this.getSafeBaseNameForFkProp(namingStrategy, currentFk, fks);
|
|
364
|
+
fksOnStandaloneProps.set(baseName, [fkIndex, currentFk]);
|
|
365
|
+
fkIndexes.set(fkIndex, { fk: currentFk, baseName });
|
|
366
|
+
}
|
|
367
|
+
const columnsInFks = Object.keys(columnFks);
|
|
368
|
+
const skippingHandlers = {
|
|
369
|
+
// Never generate scalar props for composite keys,
|
|
370
|
+
// i.e. always skip columns if they are covered by foreign keys.
|
|
371
|
+
never: (column) => columnsInFks.includes(column.name) &&
|
|
372
|
+
!fksOnColumnProps.has(column.name),
|
|
373
|
+
// Always generate scalar props for composite keys,
|
|
374
|
+
// i.e. do not skip columns, even if they are covered by foreign keys.
|
|
375
|
+
always: (column) => false,
|
|
376
|
+
// Smart scalar props generation.
|
|
377
|
+
// Skips columns if they are covered by foreign keys.
|
|
378
|
+
// But also does not skip if the column is not nullable, and yet all involved FKs are nullable,
|
|
379
|
+
// or if one or more FKs involved has multiple nullable columns.
|
|
380
|
+
smart: (column) => {
|
|
381
|
+
return (columnsInFks.includes(column.name) &&
|
|
382
|
+
!fksOnColumnProps.has(column.name) &&
|
|
383
|
+
(column.nullable
|
|
384
|
+
? columnFks[column.name].some((fk) => !fk.columnNames.some((fkColumnName) => fkColumnName !== column.name &&
|
|
385
|
+
this.getColumn(fkColumnName)?.nullable))
|
|
386
|
+
: columnFks[column.name].some((fk) => !nullableForeignKeys.has(fk))));
|
|
387
|
+
},
|
|
388
|
+
};
|
|
389
|
+
const skippedColumnNames = this.getColumns()
|
|
390
|
+
.filter(skippingHandlers[scalarPropertiesForRelations])
|
|
391
|
+
.map((column) => column.name);
|
|
392
|
+
return {
|
|
393
|
+
fksOnColumnProps,
|
|
394
|
+
fksOnStandaloneProps,
|
|
395
|
+
columnFks,
|
|
396
|
+
fkIndexes,
|
|
397
|
+
nullableForeignKeys,
|
|
398
|
+
skippedColumnNames,
|
|
399
|
+
};
|
|
400
|
+
}
|
|
401
|
+
findFkIndex(currentFk) {
|
|
402
|
+
const fkColumnsLength = currentFk.columnNames.length;
|
|
403
|
+
const possibleIndexes = this.indexes.filter((index) => {
|
|
404
|
+
return (index.columnNames.length >= fkColumnsLength &&
|
|
405
|
+
!currentFk.columnNames.some((columnName, i) => index.columnNames[i] !== columnName));
|
|
406
|
+
});
|
|
407
|
+
possibleIndexes.sort((a, b) => {
|
|
408
|
+
if (a.columnNames.length !== b.columnNames.length) {
|
|
409
|
+
return a.columnNames.length < b.columnNames.length ? -1 : 1;
|
|
410
|
+
}
|
|
411
|
+
if (a.primary !== b.primary) {
|
|
412
|
+
return a.primary ? -1 : 1;
|
|
413
|
+
}
|
|
414
|
+
if (a.unique !== b.unique) {
|
|
415
|
+
return a.unique ? -1 : 1;
|
|
416
|
+
}
|
|
417
|
+
return a.keyName.localeCompare(b.keyName);
|
|
418
|
+
});
|
|
419
|
+
return possibleIndexes[0];
|
|
420
|
+
}
|
|
421
|
+
getIndexProperties(index, columnFks, fksOnColumnProps, fksOnStandaloneProps, namingStrategy) {
|
|
422
|
+
const propBaseNames = new Set();
|
|
423
|
+
const columnNames = index.columnNames;
|
|
424
|
+
const l = columnNames.length;
|
|
425
|
+
if (columnNames.some((col) => !col)) {
|
|
426
|
+
return;
|
|
427
|
+
}
|
|
428
|
+
for (let i = 0; i < l; ++i) {
|
|
429
|
+
const columnName = columnNames[i];
|
|
430
|
+
// The column is not involved with FKs.
|
|
431
|
+
// It has a prop named after it.
|
|
432
|
+
// Add it and move on.
|
|
433
|
+
if (!(columnName in columnFks)) {
|
|
434
|
+
propBaseNames.add(columnName);
|
|
435
|
+
continue;
|
|
436
|
+
}
|
|
437
|
+
// If the prop named after the column has a FK and the FK's columns are a subset of this index,
|
|
438
|
+
// include this prop and move on.
|
|
439
|
+
const columnPropFk = fksOnColumnProps.get(columnName);
|
|
440
|
+
if (columnPropFk &&
|
|
441
|
+
!columnPropFk.columnNames.some((fkColumnName) => !columnNames.includes(fkColumnName))) {
|
|
442
|
+
propBaseNames.add(columnName);
|
|
443
|
+
continue;
|
|
444
|
+
}
|
|
445
|
+
// If there is at least one standalone FK featuring this column,
|
|
446
|
+
// and all of its columns are a subset of this index,
|
|
447
|
+
// include that FK, and consider mapping of this column to a prop a success.
|
|
448
|
+
let propAdded = false;
|
|
449
|
+
for (const [propName, [, fk]] of fksOnStandaloneProps) {
|
|
450
|
+
if (!columnFks[columnName].includes(fk)) {
|
|
451
|
+
continue;
|
|
452
|
+
}
|
|
453
|
+
if (!fk.columnNames.some((fkColumnName) => !columnNames.includes(fkColumnName))) {
|
|
454
|
+
propBaseNames.add(propName);
|
|
455
|
+
propAdded = true;
|
|
456
|
+
}
|
|
457
|
+
}
|
|
458
|
+
if (propAdded) {
|
|
459
|
+
continue;
|
|
460
|
+
}
|
|
461
|
+
// If we have reached this point, it means the column is not mappable to a prop name.
|
|
462
|
+
// Break the whole prop creation.
|
|
463
|
+
return;
|
|
464
|
+
}
|
|
465
|
+
return Array.from(propBaseNames).map((baseName) => this.getPropertyName(namingStrategy, baseName, fksOnColumnProps.get(baseName)));
|
|
466
|
+
}
|
|
467
|
+
getSafeBaseNameForFkProp(namingStrategy, currentFk, fks, columnName) {
|
|
468
|
+
if (columnName &&
|
|
469
|
+
this.getPropertyName(namingStrategy, columnName, currentFk) !==
|
|
470
|
+
this.getPropertyName(namingStrategy, columnName)) {
|
|
471
|
+
// The eligible scalar column name is different from the name of the FK prop of the same column.
|
|
472
|
+
// Both can be safely rendered.
|
|
473
|
+
// Use the column name as a base for the FK prop.
|
|
474
|
+
return columnName;
|
|
475
|
+
}
|
|
476
|
+
if (!fks.some((fk) => fk !== currentFk &&
|
|
477
|
+
fk.referencedTableName === currentFk.referencedTableName) &&
|
|
478
|
+
!this.getColumn(currentFk.referencedTableName)) {
|
|
479
|
+
// FK is the only one in this table that references this other table.
|
|
480
|
+
// The name of the referenced table is not shared with a column in this table,
|
|
481
|
+
// so it is safe to output prop name based on the referenced entity.
|
|
482
|
+
return currentFk.referencedTableName;
|
|
483
|
+
}
|
|
484
|
+
// Any ambiguous FK is rendered with a name based on the FK constraint name
|
|
485
|
+
let finalPropBaseName = currentFk.constraintName;
|
|
486
|
+
while (this.getColumn(finalPropBaseName)) {
|
|
487
|
+
// In the unlikely event that the FK constraint name is shared by a column name, generate a name by
|
|
488
|
+
// continuously prefixing with "fk_", until a non-existent column is hit.
|
|
489
|
+
// The worst case scenario is a very long name with several repeated "fk_"
|
|
490
|
+
// that is not really a valid DB identifier but a valid JS variable name.
|
|
491
|
+
finalPropBaseName = `fk_${finalPropBaseName}`;
|
|
492
|
+
}
|
|
493
|
+
return finalPropBaseName;
|
|
494
|
+
}
|
|
495
|
+
/**
|
|
496
|
+
* The shortest name is stripped of the default namespace. All other namespaced elements are returned as full-qualified names.
|
|
497
|
+
*/
|
|
498
|
+
getShortestName() {
|
|
499
|
+
if (!this.schema || this.name.startsWith(this.schema + ".")) {
|
|
500
|
+
return this.name;
|
|
501
|
+
}
|
|
502
|
+
return `${this.schema}.${this.name}`;
|
|
503
|
+
}
|
|
504
|
+
getForeignKeys() {
|
|
505
|
+
return this.foreignKeys;
|
|
506
|
+
}
|
|
507
|
+
hasColumn(columnName) {
|
|
508
|
+
return columnName in this.columns;
|
|
509
|
+
}
|
|
510
|
+
getIndex(indexName) {
|
|
511
|
+
return this.indexes.find((i) => i.keyName === indexName);
|
|
512
|
+
}
|
|
513
|
+
hasIndex(indexName) {
|
|
514
|
+
return !!this.getIndex(indexName);
|
|
515
|
+
}
|
|
516
|
+
getCheck(checkName) {
|
|
517
|
+
return this.checks.find((i) => i.name === checkName);
|
|
518
|
+
}
|
|
519
|
+
hasCheck(checkName) {
|
|
520
|
+
return !!this.getCheck(checkName);
|
|
521
|
+
}
|
|
522
|
+
getPrimaryKey() {
|
|
523
|
+
return this.indexes.find((i) => i.primary);
|
|
524
|
+
}
|
|
525
|
+
hasPrimaryKey() {
|
|
526
|
+
return !!this.getPrimaryKey();
|
|
527
|
+
}
|
|
528
|
+
getForeignKeyDeclaration(fk, namingStrategy, schemaHelper, fkIndex, nullable, propNameBase) {
|
|
529
|
+
const prop = this.getPropertyName(namingStrategy, propNameBase, fk);
|
|
530
|
+
const kind = fkIndex.unique && !fkIndex.primary
|
|
531
|
+
? this.getReferenceKind(fk, fkIndex)
|
|
532
|
+
: this.getReferenceKind(fk);
|
|
533
|
+
const type = this.getPropertyTypeForForeignKey(namingStrategy, fk);
|
|
534
|
+
const fkOptions = {};
|
|
535
|
+
fkOptions.fieldNames = fk.columnNames;
|
|
536
|
+
fkOptions.referencedTableName = fk.referencedTableName;
|
|
537
|
+
fkOptions.referencedColumnNames = fk.referencedColumnNames;
|
|
538
|
+
fkOptions.updateRule = fk.updateRule?.toLowerCase();
|
|
539
|
+
fkOptions.deleteRule = fk.deleteRule?.toLowerCase();
|
|
540
|
+
fkOptions.columnTypes = fk.columnNames.map((c) => this.getColumn(c).type);
|
|
541
|
+
const columnOptions = {};
|
|
542
|
+
if (fk.columnNames.length === 1) {
|
|
543
|
+
const column = this.getColumn(fk.columnNames[0]);
|
|
544
|
+
columnOptions.default = this.getPropertyDefaultValue(schemaHelper, column, type);
|
|
545
|
+
columnOptions.defaultRaw = this.getPropertyDefaultValue(schemaHelper, column, type, true);
|
|
546
|
+
columnOptions.generated = column.generated;
|
|
547
|
+
columnOptions.nullable = column.nullable;
|
|
548
|
+
columnOptions.primary = column.primary;
|
|
549
|
+
columnOptions.length = column.length;
|
|
550
|
+
columnOptions.precision = column.precision;
|
|
551
|
+
columnOptions.scale = column.scale;
|
|
552
|
+
columnOptions.enum = !!column.enumItems?.length;
|
|
553
|
+
columnOptions.items = column.enumItems;
|
|
554
|
+
}
|
|
555
|
+
return {
|
|
556
|
+
name: prop,
|
|
557
|
+
type,
|
|
558
|
+
kind,
|
|
559
|
+
...columnOptions,
|
|
560
|
+
nullable,
|
|
561
|
+
primary: fkIndex.primary ||
|
|
562
|
+
!fk.columnNames.some((columnName) => !this.getPrimaryKey()?.columnNames.includes(columnName)),
|
|
563
|
+
index: !fkIndex.unique ? fkIndex.keyName : undefined,
|
|
564
|
+
unique: fkIndex.unique && !fkIndex.primary ? fkIndex.keyName : undefined,
|
|
565
|
+
...fkOptions,
|
|
566
|
+
};
|
|
567
|
+
}
|
|
568
|
+
getPropertyDeclaration(column, namingStrategy, schemaHelper, compositeFkIndexes, compositeFkUniques, columnFks, fk) {
|
|
569
|
+
const prop = this.getPropertyName(namingStrategy, column.name, fk);
|
|
570
|
+
const persist = !(column.name in columnFks && typeof fk === "undefined");
|
|
571
|
+
const index = compositeFkIndexes[prop] ||
|
|
572
|
+
this.indexes.find((idx) => idx.columnNames[0] === column.name &&
|
|
573
|
+
!idx.composite &&
|
|
574
|
+
!idx.unique &&
|
|
575
|
+
!idx.primary);
|
|
576
|
+
const unique = compositeFkUniques[prop] ||
|
|
577
|
+
this.indexes.find((idx) => idx.columnNames[0] === column.name &&
|
|
578
|
+
!idx.composite &&
|
|
579
|
+
idx.unique &&
|
|
580
|
+
!idx.primary);
|
|
581
|
+
const kind = this.getReferenceKind(fk, unique);
|
|
582
|
+
const type = this.getPropertyTypeForColumn(namingStrategy, column, fk);
|
|
583
|
+
const fkOptions = {};
|
|
584
|
+
if (fk) {
|
|
585
|
+
fkOptions.fieldNames = fk.columnNames;
|
|
586
|
+
fkOptions.referencedTableName = fk.referencedTableName;
|
|
587
|
+
fkOptions.referencedColumnNames = fk.referencedColumnNames;
|
|
588
|
+
fkOptions.updateRule = fk.updateRule?.toLowerCase();
|
|
589
|
+
fkOptions.deleteRule = fk.deleteRule?.toLowerCase();
|
|
590
|
+
}
|
|
591
|
+
return {
|
|
592
|
+
name: prop,
|
|
593
|
+
type,
|
|
594
|
+
kind,
|
|
595
|
+
generated: column.generated,
|
|
596
|
+
columnType: column.type,
|
|
597
|
+
default: this.getPropertyDefaultValue(schemaHelper, column, type),
|
|
598
|
+
defaultRaw: this.getPropertyDefaultValue(schemaHelper, column, type, true),
|
|
599
|
+
nullable: column.nullable,
|
|
600
|
+
primary: column.primary && persist,
|
|
601
|
+
autoincrement: column.autoincrement,
|
|
602
|
+
fieldName: column.name,
|
|
603
|
+
length: column.length,
|
|
604
|
+
precision: column.precision,
|
|
605
|
+
scale: column.scale,
|
|
606
|
+
index: index ? index.keyName : undefined,
|
|
607
|
+
unique: unique ? unique.keyName : undefined,
|
|
608
|
+
enum: !!column.enumItems?.length,
|
|
609
|
+
items: column.enumItems,
|
|
610
|
+
persist,
|
|
611
|
+
...fkOptions,
|
|
612
|
+
};
|
|
613
|
+
}
|
|
614
|
+
getReferenceKind(fk, unique) {
|
|
615
|
+
if (fk && unique) {
|
|
616
|
+
return core_1.ReferenceKind.ONE_TO_ONE;
|
|
617
|
+
}
|
|
618
|
+
if (fk) {
|
|
619
|
+
return core_1.ReferenceKind.MANY_TO_ONE;
|
|
620
|
+
}
|
|
621
|
+
return core_1.ReferenceKind.SCALAR;
|
|
622
|
+
}
|
|
623
|
+
getPropertyName(namingStrategy, baseName, fk) {
|
|
624
|
+
let field = baseName;
|
|
625
|
+
if (fk) {
|
|
626
|
+
const idx = fk.columnNames.indexOf(baseName);
|
|
627
|
+
let replacedFieldName = field.replace(new RegExp(`_${fk.referencedColumnNames[idx]}$`), "");
|
|
628
|
+
if (replacedFieldName === field) {
|
|
629
|
+
replacedFieldName = field.replace(new RegExp(`_${namingStrategy.referenceColumnName()}$`), "");
|
|
630
|
+
}
|
|
631
|
+
field = replacedFieldName;
|
|
632
|
+
}
|
|
633
|
+
if (field.startsWith("_")) {
|
|
634
|
+
return field;
|
|
635
|
+
}
|
|
636
|
+
return namingStrategy.columnNameToProperty(field);
|
|
637
|
+
}
|
|
638
|
+
getPropertyTypeForForeignKey(namingStrategy, fk) {
|
|
639
|
+
const parts = fk.referencedTableName.split(".", 2);
|
|
640
|
+
return namingStrategy.getEntityName(...parts.reverse());
|
|
641
|
+
}
|
|
642
|
+
getPropertyTypeForColumn(namingStrategy, column, fk) {
|
|
643
|
+
if (fk) {
|
|
644
|
+
return this.getPropertyTypeForForeignKey(namingStrategy, fk);
|
|
645
|
+
}
|
|
646
|
+
// If this column is using an enum.
|
|
647
|
+
if (column.enumItems?.length) {
|
|
648
|
+
// We will create a new enum name for this type and set it as the property type as well.
|
|
649
|
+
// The enum name will be a concatenation of the table name and the column name.
|
|
650
|
+
return namingStrategy.getClassName(this.name + "_" + column.name, "_");
|
|
651
|
+
}
|
|
652
|
+
return column.mappedType?.compareAsType() ?? "unknown";
|
|
653
|
+
}
|
|
654
|
+
getPropertyDefaultValue(schemaHelper, column, propType, raw = false) {
|
|
655
|
+
const empty = raw ? "null" : undefined;
|
|
656
|
+
if (!column.default) {
|
|
657
|
+
return empty;
|
|
658
|
+
}
|
|
659
|
+
const val = schemaHelper.normalizeDefaultValue(column.default, column.length);
|
|
660
|
+
if (column.nullable && val === "null") {
|
|
661
|
+
return empty;
|
|
662
|
+
}
|
|
663
|
+
if (propType === "boolean" && !raw) {
|
|
664
|
+
return !["0", "false", "f", "n", "no", "off"].includes("" + column.default);
|
|
665
|
+
}
|
|
666
|
+
if (propType === "number") {
|
|
667
|
+
return +column.default;
|
|
668
|
+
}
|
|
669
|
+
// unquote string defaults if `raw = false`
|
|
670
|
+
const match = ("" + val).match(/^'(.*)'$/);
|
|
671
|
+
if (!raw && match) {
|
|
672
|
+
return match[1];
|
|
673
|
+
}
|
|
674
|
+
return "" + val;
|
|
675
|
+
}
|
|
676
|
+
addIndex(meta, index, type) {
|
|
677
|
+
const properties = core_1.Utils.unique(core_1.Utils.flatten(core_1.Utils.asArray(index.properties).map((prop) => {
|
|
678
|
+
const root = prop.replace(/\..+$/, "");
|
|
679
|
+
if (meta.properties[prop]) {
|
|
680
|
+
if (meta.properties[prop].embeddedPath) {
|
|
681
|
+
return [meta.properties[prop].embeddedPath.join(".")];
|
|
682
|
+
}
|
|
683
|
+
return meta.properties[prop].fieldNames;
|
|
684
|
+
}
|
|
685
|
+
// json index, we need to rename the column only
|
|
686
|
+
if (meta.properties[root]) {
|
|
687
|
+
return [prop.replace(root, meta.properties[root].fieldNames[0])];
|
|
688
|
+
}
|
|
689
|
+
/* istanbul ignore next */
|
|
690
|
+
return [prop];
|
|
691
|
+
})));
|
|
692
|
+
if (properties.length === 0 && !index.expression) {
|
|
693
|
+
return;
|
|
694
|
+
}
|
|
695
|
+
const name = this.getIndexName(index.name, properties, type);
|
|
696
|
+
// const indexExists = this.indexes.find(i => i.keyName === name);
|
|
697
|
+
// if (indexExists) {
|
|
698
|
+
// return;
|
|
699
|
+
// }
|
|
700
|
+
this.indexes.push({
|
|
701
|
+
keyName: name,
|
|
702
|
+
columnNames: properties,
|
|
703
|
+
composite: properties.length > 1,
|
|
704
|
+
// JSON columns can have unique index but not unique constraint, and we need to distinguish those, so we can properly drop them
|
|
705
|
+
constraint: type !== "index" && !properties.some((d) => d.includes(".")),
|
|
706
|
+
primary: type === "primary",
|
|
707
|
+
unique: type !== "index",
|
|
708
|
+
type: index.type,
|
|
709
|
+
expression: index.expression,
|
|
710
|
+
options: index.options,
|
|
711
|
+
});
|
|
712
|
+
}
|
|
713
|
+
addCheck(check) {
|
|
714
|
+
this.checks.push(check);
|
|
715
|
+
}
|
|
716
|
+
toJSON() {
|
|
717
|
+
const { platform, columns, ...rest } = this;
|
|
718
|
+
const columnsMapped = core_1.Utils.keys(columns).reduce((o, col) => {
|
|
719
|
+
const { mappedType, ...restCol } = columns[col];
|
|
720
|
+
o[col] = restCol;
|
|
721
|
+
o[col].mappedType = core_1.Utils.keys(core_1.t).find((k) => core_1.t[k] === mappedType.constructor);
|
|
722
|
+
return o;
|
|
723
|
+
}, {});
|
|
724
|
+
return { columns: columnsMapped, ...rest };
|
|
725
|
+
}
|
|
726
|
+
}
|
|
727
|
+
exports.DatabaseTable = DatabaseTable;
|