@mikro-orm/core 7.0.0-dev.28 → 7.0.0-dev.281
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/EntityManager.d.ts +67 -60
- package/EntityManager.js +275 -257
- package/MikroORM.d.ts +44 -35
- package/MikroORM.js +109 -143
- package/README.md +2 -0
- package/cache/FileCacheAdapter.d.ts +1 -2
- package/cache/FileCacheAdapter.js +18 -11
- package/cache/GeneratedCacheAdapter.d.ts +0 -1
- package/cache/GeneratedCacheAdapter.js +0 -2
- package/cache/index.d.ts +0 -1
- package/cache/index.js +0 -1
- package/connections/Connection.d.ts +12 -5
- package/connections/Connection.js +21 -12
- package/drivers/DatabaseDriver.d.ts +25 -16
- package/drivers/DatabaseDriver.js +118 -35
- package/drivers/IDatabaseDriver.d.ts +75 -23
- package/entity/BaseEntity.d.ts +63 -4
- package/entity/BaseEntity.js +0 -3
- package/entity/Collection.d.ts +101 -29
- package/entity/Collection.js +436 -104
- package/entity/EntityAssigner.js +17 -17
- package/entity/EntityFactory.d.ts +7 -1
- package/entity/EntityFactory.js +87 -55
- package/entity/EntityHelper.d.ts +2 -2
- package/entity/EntityHelper.js +50 -17
- package/entity/EntityLoader.d.ts +11 -10
- package/entity/EntityLoader.js +213 -82
- package/entity/EntityRepository.d.ts +28 -8
- package/entity/EntityRepository.js +8 -2
- package/entity/PolymorphicRef.d.ts +12 -0
- package/entity/PolymorphicRef.js +18 -0
- package/entity/Reference.d.ts +1 -5
- package/entity/Reference.js +15 -11
- package/entity/WrappedEntity.d.ts +3 -8
- package/entity/WrappedEntity.js +2 -7
- package/entity/defineEntity.d.ts +512 -310
- package/entity/defineEntity.js +134 -290
- package/entity/index.d.ts +2 -2
- package/entity/index.js +2 -2
- package/entity/utils.d.ts +6 -1
- package/entity/utils.js +34 -1
- package/entity/validators.d.ts +11 -0
- package/entity/validators.js +65 -0
- package/enums.d.ts +8 -6
- package/enums.js +2 -1
- package/errors.d.ts +20 -10
- package/errors.js +55 -23
- package/events/EventManager.d.ts +2 -1
- package/events/EventManager.js +19 -11
- package/hydration/Hydrator.js +1 -2
- package/hydration/ObjectHydrator.d.ts +4 -4
- package/hydration/ObjectHydrator.js +79 -34
- package/index.d.ts +2 -2
- package/index.js +1 -2
- package/logging/DefaultLogger.d.ts +1 -1
- package/logging/DefaultLogger.js +1 -0
- package/logging/SimpleLogger.d.ts +1 -1
- package/logging/colors.d.ts +1 -1
- package/logging/colors.js +7 -6
- package/logging/index.d.ts +1 -0
- package/logging/index.js +1 -0
- package/logging/inspect.d.ts +2 -0
- package/logging/inspect.js +11 -0
- package/metadata/EntitySchema.d.ts +47 -23
- package/metadata/EntitySchema.js +92 -33
- package/metadata/MetadataDiscovery.d.ts +64 -9
- package/metadata/MetadataDiscovery.js +782 -325
- package/metadata/MetadataProvider.d.ts +11 -2
- package/metadata/MetadataProvider.js +46 -2
- package/metadata/MetadataStorage.d.ts +13 -11
- package/metadata/MetadataStorage.js +71 -38
- package/metadata/MetadataValidator.d.ts +32 -9
- package/metadata/MetadataValidator.js +196 -41
- package/metadata/discover-entities.d.ts +5 -0
- package/metadata/discover-entities.js +40 -0
- package/metadata/index.d.ts +1 -1
- package/metadata/index.js +1 -1
- package/metadata/types.d.ts +577 -0
- package/metadata/types.js +1 -0
- package/naming-strategy/AbstractNamingStrategy.d.ts +16 -4
- package/naming-strategy/AbstractNamingStrategy.js +20 -2
- package/naming-strategy/EntityCaseNamingStrategy.d.ts +3 -3
- package/naming-strategy/EntityCaseNamingStrategy.js +6 -5
- package/naming-strategy/MongoNamingStrategy.d.ts +3 -3
- package/naming-strategy/MongoNamingStrategy.js +6 -6
- package/naming-strategy/NamingStrategy.d.ts +28 -4
- package/naming-strategy/UnderscoreNamingStrategy.d.ts +3 -3
- package/naming-strategy/UnderscoreNamingStrategy.js +6 -6
- package/not-supported.d.ts +2 -0
- package/not-supported.js +4 -0
- package/package.json +22 -11
- package/platforms/ExceptionConverter.js +1 -1
- package/platforms/Platform.d.ts +10 -15
- package/platforms/Platform.js +21 -44
- package/serialization/EntitySerializer.d.ts +6 -3
- package/serialization/EntitySerializer.js +46 -26
- package/serialization/EntityTransformer.js +33 -21
- package/serialization/SerializationContext.d.ts +6 -6
- package/serialization/SerializationContext.js +3 -3
- package/types/ArrayType.d.ts +1 -1
- package/types/ArrayType.js +2 -3
- package/types/BigIntType.js +1 -1
- package/types/BlobType.d.ts +0 -1
- package/types/BlobType.js +0 -3
- package/types/BooleanType.d.ts +1 -0
- package/types/BooleanType.js +3 -0
- package/types/DecimalType.js +2 -2
- package/types/DoubleType.js +1 -1
- package/types/EnumArrayType.js +1 -2
- package/types/JsonType.d.ts +1 -1
- package/types/JsonType.js +7 -2
- package/types/TinyIntType.js +1 -1
- package/types/Type.d.ts +2 -4
- package/types/Type.js +3 -3
- package/types/Uint8ArrayType.d.ts +0 -1
- package/types/Uint8ArrayType.js +1 -4
- package/types/index.d.ts +1 -1
- package/typings.d.ts +412 -155
- package/typings.js +99 -44
- package/unit-of-work/ChangeSet.d.ts +4 -6
- package/unit-of-work/ChangeSet.js +4 -5
- package/unit-of-work/ChangeSetComputer.d.ts +3 -8
- package/unit-of-work/ChangeSetComputer.js +41 -20
- package/unit-of-work/ChangeSetPersister.d.ts +13 -12
- package/unit-of-work/ChangeSetPersister.js +94 -36
- package/unit-of-work/CommitOrderCalculator.d.ts +12 -10
- package/unit-of-work/CommitOrderCalculator.js +13 -13
- package/unit-of-work/IdentityMap.d.ts +12 -0
- package/unit-of-work/IdentityMap.js +39 -1
- package/unit-of-work/UnitOfWork.d.ts +27 -3
- package/unit-of-work/UnitOfWork.js +248 -90
- package/utils/AbstractMigrator.d.ts +101 -0
- package/utils/AbstractMigrator.js +305 -0
- package/utils/AbstractSchemaGenerator.d.ts +5 -5
- package/utils/AbstractSchemaGenerator.js +28 -17
- package/utils/AsyncContext.d.ts +6 -0
- package/utils/AsyncContext.js +42 -0
- package/utils/Configuration.d.ts +795 -213
- package/utils/Configuration.js +151 -196
- package/utils/ConfigurationLoader.d.ts +1 -54
- package/utils/ConfigurationLoader.js +1 -352
- package/utils/Cursor.d.ts +0 -3
- package/utils/Cursor.js +24 -11
- package/utils/DataloaderUtils.d.ts +10 -5
- package/utils/DataloaderUtils.js +29 -12
- package/utils/EntityComparator.d.ts +16 -9
- package/utils/EntityComparator.js +154 -56
- package/utils/QueryHelper.d.ts +18 -6
- package/utils/QueryHelper.js +76 -23
- package/utils/RawQueryFragment.d.ts +28 -34
- package/utils/RawQueryFragment.js +35 -71
- package/utils/RequestContext.js +2 -2
- package/utils/TransactionContext.js +2 -2
- package/utils/TransactionManager.js +9 -6
- package/utils/Utils.d.ts +15 -128
- package/utils/Utils.js +80 -397
- package/utils/clone.js +8 -23
- package/utils/env-vars.d.ts +7 -0
- package/utils/env-vars.js +97 -0
- package/utils/fs-utils.d.ts +34 -0
- package/utils/fs-utils.js +196 -0
- package/utils/index.d.ts +1 -3
- package/utils/index.js +1 -3
- package/utils/upsert-utils.d.ts +9 -4
- package/utils/upsert-utils.js +46 -3
- package/decorators/Check.d.ts +0 -3
- package/decorators/Check.js +0 -13
- package/decorators/CreateRequestContext.d.ts +0 -3
- package/decorators/CreateRequestContext.js +0 -32
- package/decorators/Embeddable.d.ts +0 -8
- package/decorators/Embeddable.js +0 -11
- package/decorators/Embedded.d.ts +0 -12
- package/decorators/Embedded.js +0 -18
- package/decorators/Entity.d.ts +0 -33
- package/decorators/Entity.js +0 -12
- package/decorators/Enum.d.ts +0 -9
- package/decorators/Enum.js +0 -16
- package/decorators/Filter.d.ts +0 -2
- package/decorators/Filter.js +0 -8
- package/decorators/Formula.d.ts +0 -4
- package/decorators/Formula.js +0 -15
- package/decorators/Indexed.d.ts +0 -19
- package/decorators/Indexed.js +0 -20
- package/decorators/ManyToMany.d.ts +0 -42
- package/decorators/ManyToMany.js +0 -14
- package/decorators/ManyToOne.d.ts +0 -34
- package/decorators/ManyToOne.js +0 -14
- package/decorators/OneToMany.d.ts +0 -28
- package/decorators/OneToMany.js +0 -17
- package/decorators/OneToOne.d.ts +0 -28
- package/decorators/OneToOne.js +0 -7
- package/decorators/PrimaryKey.d.ts +0 -8
- package/decorators/PrimaryKey.js +0 -20
- package/decorators/Property.d.ts +0 -250
- package/decorators/Property.js +0 -32
- package/decorators/Transactional.d.ts +0 -14
- package/decorators/Transactional.js +0 -28
- package/decorators/hooks.d.ts +0 -16
- package/decorators/hooks.js +0 -47
- package/decorators/index.d.ts +0 -17
- package/decorators/index.js +0 -17
- package/entity/ArrayCollection.d.ts +0 -118
- package/entity/ArrayCollection.js +0 -407
- package/entity/EntityValidator.d.ts +0 -19
- package/entity/EntityValidator.js +0 -150
- package/metadata/ReflectMetadataProvider.d.ts +0 -8
- package/metadata/ReflectMetadataProvider.js +0 -44
- package/utils/resolveContextProvider.d.ts +0 -10
- package/utils/resolveContextProvider.js +0 -28
package/utils/Utils.js
CHANGED
|
@@ -1,15 +1,7 @@
|
|
|
1
|
-
import { createRequire } from 'node:module';
|
|
2
|
-
import { glob, isDynamicPattern } from 'tinyglobby';
|
|
3
|
-
import { extname, isAbsolute, join, normalize, relative, resolve } from 'node:path';
|
|
4
|
-
import { platform } from 'node:os';
|
|
5
|
-
import { fileURLToPath, pathToFileURL } from 'node:url';
|
|
6
|
-
import { existsSync, mkdirSync, readFileSync } from 'node:fs';
|
|
7
|
-
import { createHash } from 'node:crypto';
|
|
8
|
-
import { tokenize } from 'esprima';
|
|
9
1
|
import { clone } from './clone.js';
|
|
10
|
-
import {
|
|
2
|
+
import { GroupOperator, PlainObject, QueryOperator, ReferenceKind } from '../enums.js';
|
|
11
3
|
import { helper } from '../entity/wrap.js';
|
|
12
|
-
|
|
4
|
+
import { Raw } from './RawQueryFragment.js';
|
|
13
5
|
function compareConstructors(a, b) {
|
|
14
6
|
if (a.constructor === b.constructor) {
|
|
15
7
|
return true;
|
|
@@ -41,9 +33,8 @@ export function compareObjects(a, b) {
|
|
|
41
33
|
}
|
|
42
34
|
return timeA === timeB;
|
|
43
35
|
}
|
|
44
|
-
/* v8 ignore next
|
|
36
|
+
/* v8 ignore next */
|
|
45
37
|
if ((typeof a === 'function' && typeof b === 'function') ||
|
|
46
|
-
(typeof a === 'object' && a.client && ['Ref', 'Raw'].includes(a.constructor.name) && typeof b === 'object' && b.client && ['Ref', 'Raw'].includes(b.constructor.name)) || // knex qb
|
|
47
38
|
(a instanceof RegExp && b instanceof RegExp) ||
|
|
48
39
|
(a instanceof String && b instanceof String) ||
|
|
49
40
|
(a instanceof Number && b instanceof Number)) {
|
|
@@ -119,7 +110,7 @@ export function equals(a, b) {
|
|
|
119
110
|
const equalsFn = equals;
|
|
120
111
|
export function parseJsonSafe(value) {
|
|
121
112
|
if (typeof value === 'string') {
|
|
122
|
-
/* v8 ignore next
|
|
113
|
+
/* v8 ignore next */
|
|
123
114
|
try {
|
|
124
115
|
return JSON.parse(value);
|
|
125
116
|
}
|
|
@@ -132,52 +123,13 @@ export function parseJsonSafe(value) {
|
|
|
132
123
|
}
|
|
133
124
|
export class Utils {
|
|
134
125
|
static PK_SEPARATOR = '~~~';
|
|
135
|
-
|
|
136
|
-
static dynamicImportProvider = (id) => import(id);
|
|
137
|
-
/**
|
|
138
|
-
* Checks if the argument is not undefined
|
|
139
|
-
*/
|
|
140
|
-
static isDefined(data) {
|
|
141
|
-
return typeof data !== 'undefined';
|
|
142
|
-
}
|
|
126
|
+
static #ORM_VERSION = '7.0.0-dev.281';
|
|
143
127
|
/**
|
|
144
128
|
* Checks if the argument is instance of `Object`. Returns false for arrays.
|
|
145
129
|
*/
|
|
146
130
|
static isObject(o) {
|
|
147
131
|
return !!o && typeof o === 'object' && !Array.isArray(o);
|
|
148
132
|
}
|
|
149
|
-
/**
|
|
150
|
-
* Relation decorators allow using two signatures
|
|
151
|
-
* - using first parameter as options object
|
|
152
|
-
* - using all parameters
|
|
153
|
-
*
|
|
154
|
-
* This function validates those two ways are not mixed and returns the final options object.
|
|
155
|
-
* If the second way is used, we always consider the last parameter as options object.
|
|
156
|
-
* @internal
|
|
157
|
-
*/
|
|
158
|
-
static processDecoratorParameters(params) {
|
|
159
|
-
const keys = Object.keys(params);
|
|
160
|
-
const values = Object.values(params);
|
|
161
|
-
if (!Utils.isPlainObject(values[0])) {
|
|
162
|
-
const lastKey = keys[keys.length - 1];
|
|
163
|
-
const last = params[lastKey];
|
|
164
|
-
delete params[lastKey];
|
|
165
|
-
return { ...last, ...params };
|
|
166
|
-
}
|
|
167
|
-
// validate only first parameter is used if its an option object
|
|
168
|
-
const empty = (v) => v == null || (Utils.isPlainObject(v) && !Utils.hasObjectKeys(v));
|
|
169
|
-
if (values.slice(1).some(v => !empty(v))) {
|
|
170
|
-
throw new Error('Mixing first decorator parameter as options object with other parameters is forbidden. ' +
|
|
171
|
-
'If you want to use the options parameter at first position, provide all options inside it.');
|
|
172
|
-
}
|
|
173
|
-
return values[0];
|
|
174
|
-
}
|
|
175
|
-
/**
|
|
176
|
-
* Checks if the argument is instance of `Object`, but not one of the blacklisted types. Returns false for arrays.
|
|
177
|
-
*/
|
|
178
|
-
static isNotObject(o, not) {
|
|
179
|
-
return this.isObject(o) && !not.some(cls => o instanceof cls);
|
|
180
|
-
}
|
|
181
133
|
/**
|
|
182
134
|
* Removes `undefined` properties (recursively) so they are not saved as nulls
|
|
183
135
|
*/
|
|
@@ -225,18 +177,6 @@ export class Utils {
|
|
|
225
177
|
}
|
|
226
178
|
return false;
|
|
227
179
|
}
|
|
228
|
-
/**
|
|
229
|
-
* Checks if the argument is string
|
|
230
|
-
*/
|
|
231
|
-
static isString(s) {
|
|
232
|
-
return typeof s === 'string';
|
|
233
|
-
}
|
|
234
|
-
/**
|
|
235
|
-
* Checks if the argument is number
|
|
236
|
-
*/
|
|
237
|
-
static isNumber(s) {
|
|
238
|
-
return typeof s === 'number';
|
|
239
|
-
}
|
|
240
180
|
/**
|
|
241
181
|
* Checks if arguments are deeply (but not strictly) equal.
|
|
242
182
|
*/
|
|
@@ -282,7 +222,7 @@ export class Utils {
|
|
|
282
222
|
target[key] = Utils.copy(value);
|
|
283
223
|
continue;
|
|
284
224
|
}
|
|
285
|
-
/* v8 ignore next
|
|
225
|
+
/* v8 ignore next */
|
|
286
226
|
if (!(key in target)) {
|
|
287
227
|
Object.assign(target, { [key]: {} });
|
|
288
228
|
}
|
|
@@ -295,30 +235,6 @@ export class Utils {
|
|
|
295
235
|
}
|
|
296
236
|
return Utils._merge(target, sources, ignoreUndefined);
|
|
297
237
|
}
|
|
298
|
-
static getRootEntity(metadata, meta) {
|
|
299
|
-
const base = meta.extends && metadata.find(Utils.className(meta.extends));
|
|
300
|
-
if (!base || base === meta) { // make sure we do not fall into infinite loop
|
|
301
|
-
return meta;
|
|
302
|
-
}
|
|
303
|
-
const root = Utils.getRootEntity(metadata, base);
|
|
304
|
-
if (root.discriminatorColumn) {
|
|
305
|
-
return root;
|
|
306
|
-
}
|
|
307
|
-
return meta;
|
|
308
|
-
}
|
|
309
|
-
/**
|
|
310
|
-
* Computes difference between two objects, ignoring items missing in `b`.
|
|
311
|
-
*/
|
|
312
|
-
static diff(a, b) {
|
|
313
|
-
const ret = {};
|
|
314
|
-
for (const k of Object.keys(b)) {
|
|
315
|
-
if (Utils.equals(a[k], b[k])) {
|
|
316
|
-
continue;
|
|
317
|
-
}
|
|
318
|
-
ret[k] = b[k];
|
|
319
|
-
}
|
|
320
|
-
return ret;
|
|
321
|
-
}
|
|
322
238
|
/**
|
|
323
239
|
* Creates deep copy of given object.
|
|
324
240
|
*/
|
|
@@ -359,53 +275,37 @@ export class Utils {
|
|
|
359
275
|
}
|
|
360
276
|
}
|
|
361
277
|
/**
|
|
362
|
-
* Returns array of functions argument names. Uses
|
|
278
|
+
* Returns array of functions argument names. Uses basic regex for source code analysis, might not work with advanced syntax.
|
|
363
279
|
*/
|
|
364
|
-
static
|
|
365
|
-
|
|
366
|
-
|
|
367
|
-
|
|
368
|
-
|
|
369
|
-
try {
|
|
370
|
-
return tokenize(func.toString(), { tolerant: true });
|
|
280
|
+
static getConstructorParams(func) {
|
|
281
|
+
const source = func.toString();
|
|
282
|
+
const i = source.indexOf('constructor');
|
|
283
|
+
if (i === -1) {
|
|
284
|
+
return undefined;
|
|
371
285
|
}
|
|
372
|
-
|
|
373
|
-
|
|
286
|
+
const start = source.indexOf('(', i);
|
|
287
|
+
if (start === -1) {
|
|
288
|
+
return undefined;
|
|
374
289
|
}
|
|
375
|
-
|
|
376
|
-
|
|
377
|
-
|
|
378
|
-
|
|
379
|
-
|
|
380
|
-
const ret = [];
|
|
381
|
-
const tokens = this.tokenize(func);
|
|
382
|
-
let inside = 0;
|
|
383
|
-
let currentBlockStart = 0;
|
|
384
|
-
for (let i = 0; i < tokens.length; i++) {
|
|
385
|
-
const token = tokens[i];
|
|
386
|
-
if (token.type === 'Identifier' && token.value === methodName) {
|
|
387
|
-
inside = 1;
|
|
388
|
-
currentBlockStart = i;
|
|
389
|
-
continue;
|
|
290
|
+
let depth = 0;
|
|
291
|
+
let end = start;
|
|
292
|
+
for (; end < source.length; end++) {
|
|
293
|
+
if (source[end] === '(') {
|
|
294
|
+
depth++;
|
|
390
295
|
}
|
|
391
|
-
if (
|
|
392
|
-
|
|
393
|
-
currentBlockStart = i;
|
|
394
|
-
continue;
|
|
296
|
+
if (source[end] === ')') {
|
|
297
|
+
depth--;
|
|
395
298
|
}
|
|
396
|
-
if (
|
|
299
|
+
if (depth === 0) {
|
|
397
300
|
break;
|
|
398
301
|
}
|
|
399
|
-
if (inside === 2 && token.type === 'Punctuator' && token.value === '{' && i === currentBlockStart + 1) {
|
|
400
|
-
ret.push(ObjectBindingPattern);
|
|
401
|
-
i = tokens.findIndex((t, idx) => idx > i + 2 && t.type === 'Punctuator' && t.value === '}');
|
|
402
|
-
continue;
|
|
403
|
-
}
|
|
404
|
-
if (inside === 2 && token.type === 'Identifier') {
|
|
405
|
-
ret.push(token.value);
|
|
406
|
-
}
|
|
407
302
|
}
|
|
408
|
-
|
|
303
|
+
const raw = source.slice(start + 1, end);
|
|
304
|
+
return raw
|
|
305
|
+
.split(',')
|
|
306
|
+
.map(s => s.trim().replace(/=.*$/, '').trim())
|
|
307
|
+
.filter(Boolean)
|
|
308
|
+
.map(raw => raw.startsWith('{') && raw.endsWith('}') ? '' : raw);
|
|
409
309
|
}
|
|
410
310
|
/**
|
|
411
311
|
* Checks whether the argument looks like primary key (string, number or ObjectId).
|
|
@@ -418,7 +318,7 @@ export class Utils {
|
|
|
418
318
|
return true;
|
|
419
319
|
}
|
|
420
320
|
if (Utils.isObject(key)) {
|
|
421
|
-
if (key.constructor
|
|
321
|
+
if (key.constructor?.name === 'ObjectId') {
|
|
422
322
|
return true;
|
|
423
323
|
}
|
|
424
324
|
if (!Utils.isPlainObject(key) && !Utils.isEntity(key, true)) {
|
|
@@ -448,7 +348,7 @@ export class Utils {
|
|
|
448
348
|
if (meta.compositePK) {
|
|
449
349
|
return this.getCompositeKeyValue(data, meta);
|
|
450
350
|
}
|
|
451
|
-
return data[meta.primaryKeys[0]]
|
|
351
|
+
return data[meta.primaryKeys[0]] ?? data[meta.serializedPrimaryKey] ?? null;
|
|
452
352
|
}
|
|
453
353
|
return null;
|
|
454
354
|
}
|
|
@@ -487,9 +387,8 @@ export class Utils {
|
|
|
487
387
|
static splitPrimaryKeys(key) {
|
|
488
388
|
return key.split(this.PK_SEPARATOR);
|
|
489
389
|
}
|
|
490
|
-
|
|
491
|
-
|
|
492
|
-
/* v8 ignore next 3 */
|
|
390
|
+
static getPrimaryKeyValues(entity, meta, allowScalar = false, convertCustomTypes = false) {
|
|
391
|
+
/* v8 ignore next */
|
|
493
392
|
if (entity == null) {
|
|
494
393
|
return entity;
|
|
495
394
|
}
|
|
@@ -499,15 +398,13 @@ export class Utils {
|
|
|
499
398
|
}
|
|
500
399
|
return val;
|
|
501
400
|
}
|
|
502
|
-
const meta = Array.isArray(primaryKeys) ? undefined : primaryKeys;
|
|
503
|
-
primaryKeys = Array.isArray(primaryKeys) ? primaryKeys : meta.primaryKeys;
|
|
504
401
|
let pk;
|
|
505
402
|
if (Utils.isEntity(entity, true)) {
|
|
506
403
|
pk = helper(entity).getPrimaryKey(convertCustomTypes);
|
|
507
404
|
}
|
|
508
405
|
else {
|
|
509
|
-
pk = primaryKeys.reduce((o, pk) => {
|
|
510
|
-
const targetMeta = meta
|
|
406
|
+
pk = meta.primaryKeys.reduce((o, pk) => {
|
|
407
|
+
const targetMeta = meta.properties[pk].targetMeta;
|
|
511
408
|
if (targetMeta && Utils.isPlainObject(entity[pk])) {
|
|
512
409
|
o[pk] = Utils.getPrimaryKeyValues(entity[pk], targetMeta, allowScalar, convertCustomTypes);
|
|
513
410
|
}
|
|
@@ -517,12 +414,12 @@ export class Utils {
|
|
|
517
414
|
return o;
|
|
518
415
|
}, {});
|
|
519
416
|
}
|
|
520
|
-
if (primaryKeys.length > 1) {
|
|
417
|
+
if (meta.primaryKeys.length > 1) {
|
|
521
418
|
return toArray(pk);
|
|
522
419
|
}
|
|
523
420
|
if (allowScalar) {
|
|
524
421
|
if (Utils.isPlainObject(pk)) {
|
|
525
|
-
return pk[primaryKeys[0]];
|
|
422
|
+
return pk[(meta.primaryKeys)[0]];
|
|
526
423
|
}
|
|
527
424
|
return pk;
|
|
528
425
|
}
|
|
@@ -606,12 +503,6 @@ export class Utils {
|
|
|
606
503
|
static isScalarReference(data, allowReference = false) {
|
|
607
504
|
return typeof data === 'object' && data?.__scalarReference;
|
|
608
505
|
}
|
|
609
|
-
/**
|
|
610
|
-
* Checks whether the argument is ObjectId instance
|
|
611
|
-
*/
|
|
612
|
-
static isObjectID(key) {
|
|
613
|
-
return Utils.isObject(key) && key.constructor && key.constructor.name.toLowerCase() === 'objectid';
|
|
614
|
-
}
|
|
615
506
|
/**
|
|
616
507
|
* Checks whether the argument is empty (array without items, object without keys or falsy value).
|
|
617
508
|
*/
|
|
@@ -642,56 +533,21 @@ export class Utils {
|
|
|
642
533
|
* Tries to detect TypeScript support.
|
|
643
534
|
*/
|
|
644
535
|
static detectTypeScriptSupport() {
|
|
645
|
-
/* v8 ignore next
|
|
646
|
-
|
|
647
|
-
|
|
648
|
-
|
|
649
|
-
|| !!process.env
|
|
650
|
-
|| !!process.
|
|
651
|
-
|| process.
|
|
652
|
-
|| process.
|
|
536
|
+
/* v8 ignore next */
|
|
537
|
+
const process = globalThis.process ?? {};
|
|
538
|
+
/* v8 ignore next */
|
|
539
|
+
return process.argv?.[0]?.endsWith('ts-node') // running via ts-node directly
|
|
540
|
+
|| !!process.env?.MIKRO_ORM_CLI_ALWAYS_ALLOW_TS // forced explicitly or enabled via `registerTypeScriptSupport()`
|
|
541
|
+
|| !!process.env?.TS_JEST // check if ts-jest is used
|
|
542
|
+
|| !!process.env?.VITEST // check if vitest is used
|
|
543
|
+
|| !!process.versions?.bun // check if bun is used
|
|
544
|
+
|| process.argv?.slice(1).some(arg => arg.match(/\.([mc]?ts|tsx)$/)) // executing `.ts` file
|
|
545
|
+
|| process.execArgv?.some(arg => {
|
|
653
546
|
return arg.includes('ts-node') // check for ts-node loader
|
|
654
547
|
|| arg.includes('@swc-node/register') // check for swc-node/register loader
|
|
655
548
|
|| arg.includes('node_modules/tsx/'); // check for tsx loader
|
|
656
549
|
});
|
|
657
550
|
}
|
|
658
|
-
/**
|
|
659
|
-
* Uses some dark magic to get source path to caller where decorator is used.
|
|
660
|
-
* Analyses stack trace of error created inside the function call.
|
|
661
|
-
*/
|
|
662
|
-
static lookupPathFromDecorator(name, stack) {
|
|
663
|
-
// use some dark magic to get source path to caller
|
|
664
|
-
stack = stack || new Error().stack.split('\n');
|
|
665
|
-
// In some situations (e.g. swc 1.3.4+), the presence of a source map can obscure the call to
|
|
666
|
-
// __decorate(), replacing it with the constructor name. To support these cases we look for
|
|
667
|
-
// Reflect.decorate() as well. Also when babel is used, we need to check
|
|
668
|
-
// the `_applyDecoratedDescriptor` method instead.
|
|
669
|
-
let line = stack.findIndex(line => line.match(/__decorate|Reflect\.decorate|_applyDecoratedDescriptor/));
|
|
670
|
-
// bun does not have those lines at all, only the DecorateProperty/DecorateConstructor,
|
|
671
|
-
// but those are also present in node, so we need to check this only if they weren't found.
|
|
672
|
-
if (line === -1) {
|
|
673
|
-
// here we handle bun which stack is different from nodejs so we search for reflect-metadata
|
|
674
|
-
// Different bun versions might have different stack traces. The "last index" works for both 1.2.6 and 1.2.7.
|
|
675
|
-
const reflectLine = stack.findLastIndex(line => Utils.normalizePath(line).includes('node_modules/reflect-metadata/Reflect.js'));
|
|
676
|
-
if (reflectLine === -1 || reflectLine + 2 >= stack.length || !stack[reflectLine + 1].includes('bun:wrap')) {
|
|
677
|
-
return name;
|
|
678
|
-
}
|
|
679
|
-
line = reflectLine + 2;
|
|
680
|
-
}
|
|
681
|
-
if (stack[line].includes('Reflect.decorate')) {
|
|
682
|
-
line++;
|
|
683
|
-
}
|
|
684
|
-
if (Utils.normalizePath(stack[line]).includes('node_modules/tslib/tslib')) {
|
|
685
|
-
line++;
|
|
686
|
-
}
|
|
687
|
-
try {
|
|
688
|
-
const re = stack[line].match(/\(.+\)/i) ? /\((.*):\d+:\d+\)/ : /at\s*(.*):\d+:\d+$/;
|
|
689
|
-
return Utils.normalizePath(stack[line].match(re)[1]);
|
|
690
|
-
}
|
|
691
|
-
catch {
|
|
692
|
-
return name;
|
|
693
|
-
}
|
|
694
|
-
}
|
|
695
551
|
/**
|
|
696
552
|
* Gets the type of the argument.
|
|
697
553
|
*/
|
|
@@ -701,11 +557,11 @@ export class Utils {
|
|
|
701
557
|
return simple;
|
|
702
558
|
}
|
|
703
559
|
const objectType = Object.prototype.toString.call(value);
|
|
704
|
-
const type = objectType.match(
|
|
560
|
+
const type = objectType.match(/^\[object (.+)]$/)[1];
|
|
705
561
|
if (type === 'Uint8Array') {
|
|
706
562
|
return 'Buffer';
|
|
707
563
|
}
|
|
708
|
-
return
|
|
564
|
+
return type;
|
|
709
565
|
}
|
|
710
566
|
/**
|
|
711
567
|
* Checks whether the value is POJO (e.g. `{ foo: 'bar' }`, and not instance of `Foo`)
|
|
@@ -731,67 +587,14 @@ export class Utils {
|
|
|
731
587
|
static isCollection(item) {
|
|
732
588
|
return item?.__collection;
|
|
733
589
|
}
|
|
734
|
-
|
|
735
|
-
|
|
736
|
-
|
|
737
|
-
|
|
738
|
-
|
|
739
|
-
|
|
740
|
-
* If any part is a `file:` URL, it is converted to a local path. If any part is an
|
|
741
|
-
* absolute path, it replaces preceding paths (similar to `path.resolve` in NodeJS).
|
|
742
|
-
* Trailing directory separators are removed, and all directory separators are converted
|
|
743
|
-
* to POSIX-style separators (`/`).
|
|
744
|
-
*/
|
|
745
|
-
static normalizePath(...parts) {
|
|
746
|
-
let start = 0;
|
|
747
|
-
for (let i = 0; i < parts.length; i++) {
|
|
748
|
-
const part = parts[i];
|
|
749
|
-
if (isAbsolute(part)) {
|
|
750
|
-
start = i;
|
|
751
|
-
}
|
|
752
|
-
else if (part.startsWith('file:')) {
|
|
753
|
-
start = i;
|
|
754
|
-
parts[i] = Utils.fileURLToPath(part);
|
|
755
|
-
}
|
|
756
|
-
}
|
|
757
|
-
if (start > 0) {
|
|
758
|
-
parts = parts.slice(start);
|
|
590
|
+
// FNV-1a 64-bit
|
|
591
|
+
static hash(data, length) {
|
|
592
|
+
let h1 = 0xcbf29ce484222325n;
|
|
593
|
+
for (let i = 0; i < data.length; i++) {
|
|
594
|
+
h1 ^= BigInt(data.charCodeAt(i));
|
|
595
|
+
h1 = (h1 * 0x100000001b3n) & 0xffffffffffffffffn;
|
|
759
596
|
}
|
|
760
|
-
|
|
761
|
-
path = normalize(path).replace(/\\/g, '/');
|
|
762
|
-
return (path.match(/^[/.]|[a-zA-Z]:/) || path.startsWith('!')) ? path : './' + path;
|
|
763
|
-
}
|
|
764
|
-
/**
|
|
765
|
-
* Determines the relative path between two paths. If either path is a `file:` URL,
|
|
766
|
-
* it is converted to a local path.
|
|
767
|
-
*/
|
|
768
|
-
static relativePath(path, relativeTo) {
|
|
769
|
-
if (!path) {
|
|
770
|
-
return path;
|
|
771
|
-
}
|
|
772
|
-
path = Utils.normalizePath(path);
|
|
773
|
-
if (path.startsWith('.')) {
|
|
774
|
-
return path;
|
|
775
|
-
}
|
|
776
|
-
path = relative(Utils.normalizePath(relativeTo), path);
|
|
777
|
-
return Utils.normalizePath(path);
|
|
778
|
-
}
|
|
779
|
-
/**
|
|
780
|
-
* Computes the absolute path to for the given path relative to the provided base directory.
|
|
781
|
-
* If either `path` or `baseDir` are `file:` URLs, they are converted to local paths.
|
|
782
|
-
*/
|
|
783
|
-
static absolutePath(path, baseDir = process.cwd()) {
|
|
784
|
-
if (!path) {
|
|
785
|
-
return Utils.normalizePath(baseDir);
|
|
786
|
-
}
|
|
787
|
-
if (!isAbsolute(path) && !path.startsWith('file://')) {
|
|
788
|
-
path = baseDir + '/' + path;
|
|
789
|
-
}
|
|
790
|
-
return Utils.normalizePath(path);
|
|
791
|
-
}
|
|
792
|
-
static hash(data, length, algorithm) {
|
|
793
|
-
const hashAlgorithm = algorithm || 'sha256';
|
|
794
|
-
const hash = createHash(hashAlgorithm).update(data).digest('hex');
|
|
597
|
+
const hash = h1.toString(16).padStart(16, '0');
|
|
795
598
|
if (length) {
|
|
796
599
|
return hash.substring(0, length);
|
|
797
600
|
}
|
|
@@ -823,13 +626,6 @@ export class Utils {
|
|
|
823
626
|
static randomInt(min, max) {
|
|
824
627
|
return Math.round(Math.random() * (max - min)) + min;
|
|
825
628
|
}
|
|
826
|
-
static async pathExists(path, options = {}) {
|
|
827
|
-
if (isDynamicPattern(path)) {
|
|
828
|
-
const found = await glob(path, options);
|
|
829
|
-
return found.length > 0;
|
|
830
|
-
}
|
|
831
|
-
return this.pathExistsSync(path);
|
|
832
|
-
}
|
|
833
629
|
/**
|
|
834
630
|
* Extracts all possible values of a TS enum. Works with both string and numeric enums.
|
|
835
631
|
*/
|
|
@@ -846,8 +642,8 @@ export class Utils {
|
|
|
846
642
|
}
|
|
847
643
|
return values;
|
|
848
644
|
}
|
|
849
|
-
static flatten(arrays) {
|
|
850
|
-
return
|
|
645
|
+
static flatten(arrays, deep) {
|
|
646
|
+
return arrays.flatMap(v => deep && Array.isArray(v) ? this.flatten(v, true) : v);
|
|
851
647
|
}
|
|
852
648
|
static isOperator(key, includeGroupOperators = true) {
|
|
853
649
|
if (!includeGroupOperators) {
|
|
@@ -855,15 +651,6 @@ export class Utils {
|
|
|
855
651
|
}
|
|
856
652
|
return key in GroupOperator || key in QueryOperator;
|
|
857
653
|
}
|
|
858
|
-
static isGroupOperator(key) {
|
|
859
|
-
return key in GroupOperator;
|
|
860
|
-
}
|
|
861
|
-
static isArrayOperator(key) {
|
|
862
|
-
return ARRAY_OPERATORS.includes(key);
|
|
863
|
-
}
|
|
864
|
-
static isJsonKeyOperator(key) {
|
|
865
|
-
return JSON_KEY_OPERATORS.includes(key);
|
|
866
|
-
}
|
|
867
654
|
static hasNestedKey(object, key) {
|
|
868
655
|
if (!object) {
|
|
869
656
|
return false;
|
|
@@ -876,86 +663,16 @@ export class Utils {
|
|
|
876
663
|
}
|
|
877
664
|
return false;
|
|
878
665
|
}
|
|
879
|
-
static getGlobalStorage(namespace) {
|
|
880
|
-
const key = `mikro-orm-${namespace}`;
|
|
881
|
-
globalThis[key] = globalThis[key] || {};
|
|
882
|
-
return globalThis[key];
|
|
883
|
-
}
|
|
884
|
-
/**
|
|
885
|
-
* Require a module from a specific location
|
|
886
|
-
* @param id The module to require
|
|
887
|
-
* @param [from] Location to start the node resolution
|
|
888
|
-
*/
|
|
889
|
-
static requireFrom(id, from = process.cwd()) {
|
|
890
|
-
if (!extname(from)) {
|
|
891
|
-
from = join(from, '__fake.js');
|
|
892
|
-
}
|
|
893
|
-
return createRequire(resolve(from))(id);
|
|
894
|
-
}
|
|
895
|
-
/**
|
|
896
|
-
* Resolve path to a module.
|
|
897
|
-
* @param id The module to require
|
|
898
|
-
* @param [from] Location to start the node resolution
|
|
899
|
-
*/
|
|
900
|
-
static resolveModulePath(id, from = process.cwd()) {
|
|
901
|
-
if (!extname(from)) {
|
|
902
|
-
from = join(from, '__fake.js');
|
|
903
|
-
}
|
|
904
|
-
const path = Utils.normalizePath(createRequire(resolve(from)).resolve(id));
|
|
905
|
-
const parts = path.split('/');
|
|
906
|
-
const idx = parts.lastIndexOf(id) + 1;
|
|
907
|
-
parts.splice(idx, parts.length - idx);
|
|
908
|
-
return parts.join('/');
|
|
909
|
-
}
|
|
910
|
-
static async dynamicImport(id) {
|
|
911
|
-
/* v8 ignore next 7 */
|
|
912
|
-
if (platform() === 'win32') {
|
|
913
|
-
try {
|
|
914
|
-
id = pathToFileURL(id).toString();
|
|
915
|
-
}
|
|
916
|
-
catch {
|
|
917
|
-
// ignore
|
|
918
|
-
}
|
|
919
|
-
}
|
|
920
|
-
/* v8 ignore next */
|
|
921
|
-
return this.dynamicImportProvider(id);
|
|
922
|
-
}
|
|
923
|
-
/* v8 ignore next 3 */
|
|
924
|
-
static setDynamicImportProvider(provider) {
|
|
925
|
-
this.dynamicImportProvider = provider;
|
|
926
|
-
}
|
|
927
|
-
static ensureDir(path) {
|
|
928
|
-
if (!existsSync(path)) {
|
|
929
|
-
mkdirSync(path, { recursive: true });
|
|
930
|
-
}
|
|
931
|
-
}
|
|
932
|
-
static pathExistsSync(path) {
|
|
933
|
-
return existsSync(path);
|
|
934
|
-
}
|
|
935
|
-
static readJSONSync(path) {
|
|
936
|
-
const file = readFileSync(path);
|
|
937
|
-
return JSON.parse(file.toString());
|
|
938
|
-
}
|
|
939
666
|
static getORMVersion() {
|
|
940
|
-
|
|
941
|
-
// this works during development where we have `src` folder
|
|
942
|
-
return this.requireFrom('../../package.json', import.meta.dirname).version;
|
|
943
|
-
/* v8 ignore next 5 */
|
|
944
|
-
}
|
|
945
|
-
catch {
|
|
946
|
-
try {
|
|
947
|
-
// this works in production build where we do not have the `src` folder
|
|
948
|
-
return this.requireFrom('../package.json', import.meta.dirname).version;
|
|
949
|
-
}
|
|
950
|
-
catch {
|
|
951
|
-
return 'N/A';
|
|
952
|
-
}
|
|
953
|
-
}
|
|
667
|
+
return this.#ORM_VERSION;
|
|
954
668
|
}
|
|
955
|
-
static createFunction(context, code) {
|
|
669
|
+
static createFunction(context, code, compiledFunctions, key) {
|
|
670
|
+
if (key && compiledFunctions?.[key]) {
|
|
671
|
+
return compiledFunctions[key](...context.values());
|
|
672
|
+
}
|
|
956
673
|
try {
|
|
957
674
|
return new Function(...context.keys(), `'use strict';\n` + code)(...context.values());
|
|
958
|
-
/* v8 ignore next
|
|
675
|
+
/* v8 ignore next */
|
|
959
676
|
}
|
|
960
677
|
catch (e) {
|
|
961
678
|
// eslint-disable-next-line no-console
|
|
@@ -968,7 +685,7 @@ export class Utils {
|
|
|
968
685
|
return fn(...args);
|
|
969
686
|
}
|
|
970
687
|
catch (e) {
|
|
971
|
-
/* v8 ignore
|
|
688
|
+
/* v8 ignore next */
|
|
972
689
|
if ([SyntaxError, TypeError, EvalError, ReferenceError].some(t => e instanceof t)) {
|
|
973
690
|
const position = e.stack.match(/<anonymous>:(\d+):(\d+)/);
|
|
974
691
|
let code = fn.toString();
|
|
@@ -985,18 +702,9 @@ export class Utils {
|
|
|
985
702
|
// eslint-disable-next-line no-console
|
|
986
703
|
console.error(`JIT runtime error: ${e.message}\n\n${code}`);
|
|
987
704
|
}
|
|
988
|
-
/* v8 ignore stop */
|
|
989
705
|
throw e;
|
|
990
706
|
}
|
|
991
707
|
}
|
|
992
|
-
/**
|
|
993
|
-
* @see https://github.com/mikro-orm/mikro-orm/issues/840
|
|
994
|
-
*/
|
|
995
|
-
static propertyDecoratorReturnValue() {
|
|
996
|
-
if (process.env.BABEL_DECORATORS_COMPAT) {
|
|
997
|
-
return {};
|
|
998
|
-
}
|
|
999
|
-
}
|
|
1000
708
|
static unwrapProperty(entity, meta, prop, payload = false) {
|
|
1001
709
|
let p = prop;
|
|
1002
710
|
const path = [];
|
|
@@ -1076,54 +784,21 @@ export class Utils {
|
|
|
1076
784
|
}
|
|
1077
785
|
}
|
|
1078
786
|
}
|
|
1079
|
-
static
|
|
1080
|
-
allowError ??= `Cannot find module '${module}'`;
|
|
1081
|
-
from ??= process.cwd();
|
|
787
|
+
static async tryImport({ module, warning }) {
|
|
1082
788
|
try {
|
|
1083
|
-
return
|
|
789
|
+
return await import(module);
|
|
1084
790
|
}
|
|
1085
791
|
catch (err) {
|
|
1086
|
-
if (err.
|
|
1087
|
-
|
|
1088
|
-
|
|
792
|
+
if (err.code === 'ERR_MODULE_NOT_FOUND') {
|
|
793
|
+
if (warning) {
|
|
794
|
+
// eslint-disable-next-line no-console
|
|
795
|
+
console.warn(warning);
|
|
796
|
+
}
|
|
1089
797
|
return undefined;
|
|
1090
798
|
}
|
|
1091
799
|
throw err;
|
|
1092
800
|
}
|
|
1093
801
|
}
|
|
1094
|
-
static async tryImport({ module, warning }) {
|
|
1095
|
-
try {
|
|
1096
|
-
return await this.dynamicImport(module);
|
|
1097
|
-
}
|
|
1098
|
-
catch (err) {
|
|
1099
|
-
// eslint-disable-next-line no-console
|
|
1100
|
-
console.warn(warning);
|
|
1101
|
-
return undefined;
|
|
1102
|
-
}
|
|
1103
|
-
}
|
|
1104
|
-
static stripRelativePath(str) {
|
|
1105
|
-
return str.replace(/^(?:\.\.\/|\.\/)+/, '/');
|
|
1106
|
-
}
|
|
1107
|
-
/**
|
|
1108
|
-
* simple process.argv parser, supports only properties with long names, prefixed with `--`
|
|
1109
|
-
*/
|
|
1110
|
-
static parseArgs() {
|
|
1111
|
-
let lastKey;
|
|
1112
|
-
return process.argv.slice(2).reduce((args, arg) => {
|
|
1113
|
-
if (arg.includes('=')) {
|
|
1114
|
-
const [key, value] = arg.split('=');
|
|
1115
|
-
args[key.substring(2)] = value;
|
|
1116
|
-
}
|
|
1117
|
-
else if (lastKey) {
|
|
1118
|
-
args[lastKey] = arg;
|
|
1119
|
-
lastKey = undefined;
|
|
1120
|
-
}
|
|
1121
|
-
else if (arg.startsWith('--')) {
|
|
1122
|
-
lastKey = arg.substring(2);
|
|
1123
|
-
}
|
|
1124
|
-
return args;
|
|
1125
|
-
}, {});
|
|
1126
|
-
}
|
|
1127
802
|
static xor(a, b) {
|
|
1128
803
|
return (a || b) && !(a && b);
|
|
1129
804
|
}
|
|
@@ -1152,4 +827,12 @@ export class Utils {
|
|
|
1152
827
|
return o;
|
|
1153
828
|
}, {});
|
|
1154
829
|
}
|
|
830
|
+
static getObjectQueryKeys(obj) {
|
|
831
|
+
return Reflect.ownKeys(obj).filter(key => {
|
|
832
|
+
if (!Object.prototype.propertyIsEnumerable.call(obj, key)) {
|
|
833
|
+
return false;
|
|
834
|
+
}
|
|
835
|
+
return typeof key === 'string' || Raw.isKnownFragmentSymbol(key);
|
|
836
|
+
});
|
|
837
|
+
}
|
|
1155
838
|
}
|