@mikro-orm/knex 6.0.0-dev.2 → 6.0.0-dev.200
This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
- package/{dist/AbstractSqlConnection.d.ts → AbstractSqlConnection.d.ts} +8 -4
- package/{dist/AbstractSqlConnection.js → AbstractSqlConnection.js} +35 -18
- package/AbstractSqlDriver.d.ts +73 -0
- package/AbstractSqlDriver.js +1085 -0
- package/AbstractSqlPlatform.d.ts +22 -0
- package/{dist/AbstractSqlPlatform.js → AbstractSqlPlatform.js} +46 -19
- package/{dist/MonkeyPatchable.d.ts → MonkeyPatchable.d.ts} +1 -0
- package/{dist/MonkeyPatchable.js → MonkeyPatchable.js} +3 -0
- package/PivotCollectionPersister.d.ts +17 -0
- package/PivotCollectionPersister.js +128 -0
- package/{dist/README.md → README.md} +66 -109
- package/{dist/SqlEntityManager.d.ts → SqlEntityManager.d.ts} +6 -10
- package/{dist/SqlEntityManager.js → SqlEntityManager.js} +5 -10
- package/SqlEntityRepository.d.ts +24 -0
- package/{dist/SqlEntityRepository.js → SqlEntityRepository.js} +11 -8
- package/{dist/index.d.ts → index.d.ts} +1 -0
- package/{dist/index.js → index.js} +6 -1
- package/index.mjs +211 -0
- package/package.json +20 -49
- package/query/ArrayCriteriaNode.d.ts +10 -0
- package/{dist/query → query}/ArrayCriteriaNode.js +7 -2
- package/{dist/query → query}/CriteriaNode.d.ts +11 -9
- package/{dist/query → query}/CriteriaNode.js +31 -15
- package/query/CriteriaNodeFactory.d.ts +12 -0
- package/{dist/query → query}/CriteriaNodeFactory.js +32 -15
- package/query/ObjectCriteriaNode.d.ts +15 -0
- package/{dist/query → query}/ObjectCriteriaNode.js +68 -21
- package/query/QueryBuilder.d.ts +284 -0
- package/query/QueryBuilder.js +1218 -0
- package/{dist/query → query}/QueryBuilderHelper.d.ts +26 -23
- package/query/QueryBuilderHelper.js +661 -0
- package/query/ScalarCriteriaNode.d.ts +10 -0
- package/{dist/query → query}/ScalarCriteriaNode.js +12 -8
- package/query/enums.d.ts +15 -0
- package/query/enums.js +20 -0
- package/{dist/query → query}/index.js +5 -1
- package/{dist/schema → schema}/DatabaseSchema.d.ts +6 -3
- package/{dist/schema → schema}/DatabaseSchema.js +35 -25
- package/{dist/schema → schema}/DatabaseTable.d.ts +21 -13
- package/schema/DatabaseTable.js +645 -0
- package/{dist/schema → schema}/SchemaComparator.d.ts +9 -7
- package/{dist/schema → schema}/SchemaComparator.js +99 -25
- package/{dist/schema → schema}/SchemaHelper.d.ts +20 -13
- package/{dist/schema → schema}/SchemaHelper.js +70 -18
- package/{dist/schema/SchemaGenerator.d.ts → schema/SqlSchemaGenerator.d.ts} +11 -14
- package/{dist/schema/SchemaGenerator.js → schema/SqlSchemaGenerator.js} +149 -63
- package/{dist/schema → schema}/index.d.ts +1 -1
- package/{dist/schema → schema}/index.js +6 -2
- package/{dist/typings.d.ts → typings.d.ts} +45 -22
- package/CHANGELOG.md +0 -661
- package/dist/AbstractSqlDriver.d.ts +0 -55
- package/dist/AbstractSqlDriver.js +0 -644
- package/dist/AbstractSqlPlatform.d.ts +0 -21
- package/dist/LICENSE +0 -21
- package/dist/SqlEntityRepository.d.ts +0 -23
- package/dist/index.mjs +0 -26
- package/dist/package.json +0 -99
- package/dist/query/ArrayCriteriaNode.d.ts +0 -9
- package/dist/query/CriteriaNodeFactory.d.ts +0 -12
- package/dist/query/ObjectCriteriaNode.d.ts +0 -14
- package/dist/query/QueryBuilder.d.ts +0 -211
- package/dist/query/QueryBuilder.js +0 -688
- package/dist/query/QueryBuilderHelper.js +0 -523
- package/dist/query/ScalarCriteriaNode.d.ts +0 -9
- package/dist/query/enums.d.ts +0 -8
- package/dist/query/enums.js +0 -12
- package/dist/schema/DatabaseTable.js +0 -319
- /package/{dist/query → query}/index.d.ts +0 -0
- /package/{dist/typings.js → typings.js} +0 -0
|
@@ -1,319 +0,0 @@
|
|
|
1
|
-
"use strict";
|
|
2
|
-
Object.defineProperty(exports, "__esModule", { value: true });
|
|
3
|
-
exports.DatabaseTable = void 0;
|
|
4
|
-
const core_1 = require("@mikro-orm/core");
|
|
5
|
-
/**
|
|
6
|
-
* @internal
|
|
7
|
-
*/
|
|
8
|
-
class DatabaseTable {
|
|
9
|
-
constructor(platform, name, schema) {
|
|
10
|
-
this.platform = platform;
|
|
11
|
-
this.name = name;
|
|
12
|
-
this.schema = schema;
|
|
13
|
-
this.columns = {};
|
|
14
|
-
this.indexes = [];
|
|
15
|
-
this.checks = [];
|
|
16
|
-
this.foreignKeys = {};
|
|
17
|
-
Object.defineProperties(this, {
|
|
18
|
-
platform: { enumerable: false, writable: true },
|
|
19
|
-
});
|
|
20
|
-
}
|
|
21
|
-
getColumns() {
|
|
22
|
-
return Object.values(this.columns);
|
|
23
|
-
}
|
|
24
|
-
getColumn(name) {
|
|
25
|
-
return this.columns[name];
|
|
26
|
-
}
|
|
27
|
-
getIndexes() {
|
|
28
|
-
return this.indexes;
|
|
29
|
-
}
|
|
30
|
-
getChecks() {
|
|
31
|
-
return this.checks;
|
|
32
|
-
}
|
|
33
|
-
init(cols, indexes, checks, pks, fks, enums) {
|
|
34
|
-
this.indexes = indexes;
|
|
35
|
-
this.checks = checks;
|
|
36
|
-
this.foreignKeys = fks;
|
|
37
|
-
this.columns = cols.reduce((o, v) => {
|
|
38
|
-
const index = indexes.filter(i => i.columnNames[0] === v.name);
|
|
39
|
-
v.primary = v.primary || pks.includes(v.name);
|
|
40
|
-
v.unique = index.some(i => i.unique && !i.primary);
|
|
41
|
-
const type = v.name in enums ? 'enum' : v.type;
|
|
42
|
-
v.mappedType = this.platform.getMappedType(type);
|
|
43
|
-
v.default = v.default?.toString().startsWith('nextval(') ? null : v.default;
|
|
44
|
-
v.enumItems = enums[v.name] || [];
|
|
45
|
-
o[v.name] = v;
|
|
46
|
-
return o;
|
|
47
|
-
}, {});
|
|
48
|
-
}
|
|
49
|
-
addColumn(column) {
|
|
50
|
-
this.columns[column.name] = column;
|
|
51
|
-
}
|
|
52
|
-
addColumnFromProperty(prop, meta) {
|
|
53
|
-
prop.fieldNames.forEach((field, idx) => {
|
|
54
|
-
var _a;
|
|
55
|
-
const type = prop.enum ? 'enum' : prop.columnTypes[idx];
|
|
56
|
-
const mappedType = this.platform.getMappedType(type);
|
|
57
|
-
if (mappedType instanceof core_1.DecimalType) {
|
|
58
|
-
const match = prop.columnTypes[idx].match(/\w+\((\d+), ?(\d+)\)/);
|
|
59
|
-
/* istanbul ignore else */
|
|
60
|
-
if (match) {
|
|
61
|
-
prop.precision = +match[1];
|
|
62
|
-
prop.scale = +match[2];
|
|
63
|
-
prop.length = undefined;
|
|
64
|
-
}
|
|
65
|
-
}
|
|
66
|
-
if (mappedType instanceof core_1.DateTimeType) {
|
|
67
|
-
prop.length ?? (prop.length = this.platform.getDefaultDateTimeLength());
|
|
68
|
-
}
|
|
69
|
-
const primary = !meta.compositePK && !!prop.primary && prop.reference === core_1.ReferenceType.SCALAR && this.platform.isNumericColumn(mappedType);
|
|
70
|
-
this.columns[field] = {
|
|
71
|
-
name: prop.fieldNames[idx],
|
|
72
|
-
type: prop.columnTypes[idx],
|
|
73
|
-
mappedType,
|
|
74
|
-
unsigned: prop.unsigned && this.platform.isNumericColumn(mappedType),
|
|
75
|
-
autoincrement: prop.autoincrement ?? primary,
|
|
76
|
-
primary,
|
|
77
|
-
nullable: !!prop.nullable,
|
|
78
|
-
length: prop.length,
|
|
79
|
-
precision: prop.precision,
|
|
80
|
-
scale: prop.scale,
|
|
81
|
-
default: prop.defaultRaw,
|
|
82
|
-
enumItems: prop.items?.every(core_1.Utils.isString) ? prop.items : undefined,
|
|
83
|
-
comment: prop.comment,
|
|
84
|
-
extra: prop.extra,
|
|
85
|
-
};
|
|
86
|
-
(_a = this.columns[field]).unsigned || (_a.unsigned = this.columns[field].autoincrement);
|
|
87
|
-
const defaultValue = this.platform.getSchemaHelper().normalizeDefaultValue(prop.defaultRaw, prop.length);
|
|
88
|
-
this.columns[field].default = defaultValue;
|
|
89
|
-
});
|
|
90
|
-
if ([core_1.ReferenceType.MANY_TO_ONE, core_1.ReferenceType.ONE_TO_ONE].includes(prop.reference)) {
|
|
91
|
-
const constraintName = this.getIndexName(true, prop.fieldNames, 'foreign');
|
|
92
|
-
let schema = prop.targetMeta.schema === '*' ? this.schema : prop.targetMeta.schema ?? this.schema;
|
|
93
|
-
if (prop.referencedTableName.includes('.')) {
|
|
94
|
-
schema = undefined;
|
|
95
|
-
}
|
|
96
|
-
this.foreignKeys[constraintName] = {
|
|
97
|
-
constraintName,
|
|
98
|
-
columnNames: prop.fieldNames,
|
|
99
|
-
localTableName: this.getShortestName(),
|
|
100
|
-
referencedColumnNames: prop.referencedColumnNames,
|
|
101
|
-
referencedTableName: schema ? `${schema}.${prop.referencedTableName}` : prop.referencedTableName,
|
|
102
|
-
};
|
|
103
|
-
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');
|
|
106
|
-
}
|
|
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';
|
|
109
|
-
}
|
|
110
|
-
}
|
|
111
|
-
if (prop.index) {
|
|
112
|
-
this.indexes.push({
|
|
113
|
-
columnNames: prop.fieldNames,
|
|
114
|
-
composite: prop.fieldNames.length > 1,
|
|
115
|
-
keyName: this.getIndexName(prop.index, prop.fieldNames, 'index'),
|
|
116
|
-
primary: false,
|
|
117
|
-
unique: false,
|
|
118
|
-
});
|
|
119
|
-
}
|
|
120
|
-
if (prop.unique && !(prop.primary && !meta.compositePK)) {
|
|
121
|
-
this.indexes.push({
|
|
122
|
-
columnNames: prop.fieldNames,
|
|
123
|
-
composite: prop.fieldNames.length > 1,
|
|
124
|
-
keyName: this.getIndexName(prop.unique, prop.fieldNames, 'unique'),
|
|
125
|
-
primary: false,
|
|
126
|
-
unique: true,
|
|
127
|
-
});
|
|
128
|
-
}
|
|
129
|
-
}
|
|
130
|
-
getIndexName(value, columnNames, type) {
|
|
131
|
-
if (core_1.Utils.isString(value)) {
|
|
132
|
-
return value;
|
|
133
|
-
}
|
|
134
|
-
return this.platform.getIndexName(this.name, columnNames, type);
|
|
135
|
-
}
|
|
136
|
-
getEntityDeclaration(namingStrategy, schemaHelper) {
|
|
137
|
-
let name = namingStrategy.getClassName(this.name, '_');
|
|
138
|
-
name = name.match(/^\d/) ? 'E' + name : name;
|
|
139
|
-
const schema = new core_1.EntitySchema({ name, collection: this.name, schema: this.schema });
|
|
140
|
-
const compositeFkIndexes = {};
|
|
141
|
-
const compositeFkUniques = {};
|
|
142
|
-
for (const index of this.indexes.filter(index => index.columnNames.length > 1)) {
|
|
143
|
-
const properties = index.columnNames.map(col => this.getPropertyName(namingStrategy, this.getColumn(col)));
|
|
144
|
-
const ret = { name: index.keyName, properties: core_1.Utils.unique(properties) };
|
|
145
|
-
if (ret.properties.length === 1) {
|
|
146
|
-
const map = index.unique ? compositeFkUniques : compositeFkIndexes;
|
|
147
|
-
map[ret.properties[0]] = { keyName: index.keyName };
|
|
148
|
-
continue;
|
|
149
|
-
}
|
|
150
|
-
if (index.primary) {
|
|
151
|
-
//
|
|
152
|
-
}
|
|
153
|
-
else if (index.unique) {
|
|
154
|
-
schema.addUnique(ret);
|
|
155
|
-
}
|
|
156
|
-
else {
|
|
157
|
-
schema.addIndex(ret);
|
|
158
|
-
}
|
|
159
|
-
}
|
|
160
|
-
for (const column of this.getColumns()) {
|
|
161
|
-
const prop = this.getPropertyDeclaration(column, namingStrategy, schemaHelper, compositeFkIndexes, compositeFkUniques);
|
|
162
|
-
schema.addProperty(prop.name, prop.type, prop);
|
|
163
|
-
}
|
|
164
|
-
const meta = schema.init().meta;
|
|
165
|
-
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);
|
|
168
|
-
return meta;
|
|
169
|
-
}
|
|
170
|
-
/**
|
|
171
|
-
* The shortest name is stripped of the default namespace. All other namespaced elements are returned as full-qualified names.
|
|
172
|
-
*/
|
|
173
|
-
getShortestName() {
|
|
174
|
-
if (!this.schema || this.name.startsWith(this.schema + '.')) {
|
|
175
|
-
return this.name;
|
|
176
|
-
}
|
|
177
|
-
return `${this.schema}.${this.name}`;
|
|
178
|
-
}
|
|
179
|
-
getForeignKeys() {
|
|
180
|
-
return this.foreignKeys;
|
|
181
|
-
}
|
|
182
|
-
hasColumn(columnName) {
|
|
183
|
-
return columnName in this.columns;
|
|
184
|
-
}
|
|
185
|
-
getIndex(indexName) {
|
|
186
|
-
return this.indexes.find(i => i.keyName === indexName);
|
|
187
|
-
}
|
|
188
|
-
hasIndex(indexName) {
|
|
189
|
-
return !!this.getIndex(indexName);
|
|
190
|
-
}
|
|
191
|
-
getCheck(checkName) {
|
|
192
|
-
return this.checks.find(i => i.name === checkName);
|
|
193
|
-
}
|
|
194
|
-
hasCheck(checkName) {
|
|
195
|
-
return !!this.getCheck(checkName);
|
|
196
|
-
}
|
|
197
|
-
getPrimaryKey() {
|
|
198
|
-
return this.indexes.find(i => i.primary);
|
|
199
|
-
}
|
|
200
|
-
hasPrimaryKey() {
|
|
201
|
-
return !!this.getPrimaryKey();
|
|
202
|
-
}
|
|
203
|
-
getPropertyDeclaration(column, namingStrategy, schemaHelper, compositeFkIndexes, compositeFkUniques) {
|
|
204
|
-
const fk = Object.values(this.foreignKeys).find(fk => fk.columnNames.includes(column.name));
|
|
205
|
-
const prop = this.getPropertyName(namingStrategy, column);
|
|
206
|
-
const index = compositeFkIndexes[prop] || this.indexes.find(idx => idx.columnNames[0] === column.name && !idx.composite && !idx.unique && !idx.primary);
|
|
207
|
-
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);
|
|
209
|
-
const type = this.getPropertyType(namingStrategy, column, fk);
|
|
210
|
-
const fkOptions = {};
|
|
211
|
-
if (fk) {
|
|
212
|
-
fkOptions.fieldNames = fk.columnNames;
|
|
213
|
-
fkOptions.referencedTableName = fk.referencedTableName;
|
|
214
|
-
fkOptions.referencedColumnNames = fk.referencedColumnNames;
|
|
215
|
-
fkOptions.onUpdateIntegrity = fk.updateRule?.toLowerCase();
|
|
216
|
-
fkOptions.onDelete = fk.deleteRule?.toLowerCase();
|
|
217
|
-
}
|
|
218
|
-
return {
|
|
219
|
-
name: prop,
|
|
220
|
-
type,
|
|
221
|
-
reference,
|
|
222
|
-
columnType: column.type,
|
|
223
|
-
default: this.getPropertyDefaultValue(schemaHelper, column, type),
|
|
224
|
-
defaultRaw: this.getPropertyDefaultValue(schemaHelper, column, type, true),
|
|
225
|
-
nullable: column.nullable,
|
|
226
|
-
primary: column.primary,
|
|
227
|
-
fieldName: column.name,
|
|
228
|
-
length: column.length,
|
|
229
|
-
index: index ? index.keyName : undefined,
|
|
230
|
-
unique: unique ? unique.keyName : undefined,
|
|
231
|
-
enum: !!column.enumItems?.length,
|
|
232
|
-
items: column.enumItems,
|
|
233
|
-
...fkOptions,
|
|
234
|
-
};
|
|
235
|
-
}
|
|
236
|
-
getReferenceType(fk, unique) {
|
|
237
|
-
if (fk && unique) {
|
|
238
|
-
return core_1.ReferenceType.ONE_TO_ONE;
|
|
239
|
-
}
|
|
240
|
-
if (fk) {
|
|
241
|
-
return core_1.ReferenceType.MANY_TO_ONE;
|
|
242
|
-
}
|
|
243
|
-
return core_1.ReferenceType.SCALAR;
|
|
244
|
-
}
|
|
245
|
-
getPropertyName(namingStrategy, column) {
|
|
246
|
-
const fk = Object.values(this.foreignKeys).find(fk => fk.columnNames.includes(column.name));
|
|
247
|
-
let field = column.name;
|
|
248
|
-
if (fk) {
|
|
249
|
-
const idx = fk.columnNames.indexOf(column.name);
|
|
250
|
-
field = field.replace(new RegExp(`_${fk.referencedColumnNames[idx]}$`), '');
|
|
251
|
-
}
|
|
252
|
-
return namingStrategy.columnNameToProperty(field);
|
|
253
|
-
}
|
|
254
|
-
getPropertyType(namingStrategy, column, fk) {
|
|
255
|
-
if (fk) {
|
|
256
|
-
const parts = fk.referencedTableName.split('.', 2);
|
|
257
|
-
return namingStrategy.getClassName(parts.length > 1 ? parts[1] : parts[0], '_');
|
|
258
|
-
}
|
|
259
|
-
// If this column is using an enum.
|
|
260
|
-
if (column.enumItems?.length) {
|
|
261
|
-
// We will create a new enum name for this type and set it as the property type as well.
|
|
262
|
-
// The enum name will be a concatenation of the table name and the column name.
|
|
263
|
-
return namingStrategy.getClassName(this.name + '_' + column.name, '_');
|
|
264
|
-
}
|
|
265
|
-
return column.mappedType?.compareAsType() ?? 'unknown';
|
|
266
|
-
}
|
|
267
|
-
getPropertyDefaultValue(schemaHelper, column, propType, raw = false) {
|
|
268
|
-
const empty = raw ? 'null' : undefined;
|
|
269
|
-
if (!column.default) {
|
|
270
|
-
return empty;
|
|
271
|
-
}
|
|
272
|
-
const val = schemaHelper.normalizeDefaultValue(column.default, column.length);
|
|
273
|
-
if (column.nullable && val === 'null') {
|
|
274
|
-
return empty;
|
|
275
|
-
}
|
|
276
|
-
if (propType === 'boolean' && !raw) {
|
|
277
|
-
return !['0', 'false', 'f', 'n', 'no', 'off'].includes('' + column.default);
|
|
278
|
-
}
|
|
279
|
-
if (propType === 'number') {
|
|
280
|
-
return +column.default;
|
|
281
|
-
}
|
|
282
|
-
// unquote string defaults if `raw = false`
|
|
283
|
-
const match = ('' + val).match(/^'(.*)'$/);
|
|
284
|
-
if (!raw && match) {
|
|
285
|
-
return match[1];
|
|
286
|
-
}
|
|
287
|
-
return '' + val;
|
|
288
|
-
}
|
|
289
|
-
addIndex(meta, index, type) {
|
|
290
|
-
const properties = core_1.Utils.flatten(core_1.Utils.asArray(index.properties).map(prop => meta.properties[prop].fieldNames));
|
|
291
|
-
if (properties.length === 0 && !index.expression) {
|
|
292
|
-
return;
|
|
293
|
-
}
|
|
294
|
-
const name = this.getIndexName(index.name, properties, type);
|
|
295
|
-
this.indexes.push({
|
|
296
|
-
keyName: name,
|
|
297
|
-
columnNames: properties,
|
|
298
|
-
composite: properties.length > 1,
|
|
299
|
-
primary: type === 'primary',
|
|
300
|
-
unique: type !== 'index',
|
|
301
|
-
type: index.type,
|
|
302
|
-
expression: index.expression,
|
|
303
|
-
});
|
|
304
|
-
}
|
|
305
|
-
addCheck(check) {
|
|
306
|
-
this.checks.push(check);
|
|
307
|
-
}
|
|
308
|
-
toJSON() {
|
|
309
|
-
const { platform, columns, ...rest } = this;
|
|
310
|
-
const columnsMapped = Object.keys(columns).reduce((o, col) => {
|
|
311
|
-
const { mappedType, ...restCol } = columns[col];
|
|
312
|
-
o[col] = restCol;
|
|
313
|
-
o[col].mappedType = Object.keys(core_1.t).find(k => core_1.t[k] === mappedType.constructor);
|
|
314
|
-
return o;
|
|
315
|
-
}, {});
|
|
316
|
-
return { columns: columnsMapped, ...rest };
|
|
317
|
-
}
|
|
318
|
-
}
|
|
319
|
-
exports.DatabaseTable = DatabaseTable;
|
|
File without changes
|
|
File without changes
|