@mikro-orm/core 7.0.0-dev.9 → 7.0.0-dev.91
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 +77 -48
- package/EntityManager.js +288 -225
- package/MikroORM.d.ts +40 -31
- package/MikroORM.js +98 -137
- package/README.md +3 -2
- package/cache/FileCacheAdapter.d.ts +1 -1
- package/cache/FileCacheAdapter.js +6 -5
- 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 +11 -7
- package/connections/Connection.js +16 -14
- package/drivers/DatabaseDriver.d.ts +11 -5
- package/drivers/DatabaseDriver.js +23 -11
- package/drivers/IDatabaseDriver.d.ts +25 -4
- package/entity/BaseEntity.d.ts +0 -1
- package/entity/BaseEntity.js +0 -3
- package/entity/Collection.d.ts +95 -30
- package/entity/Collection.js +432 -93
- package/entity/EntityAssigner.d.ts +1 -1
- package/entity/EntityAssigner.js +17 -9
- package/entity/EntityFactory.d.ts +7 -0
- package/entity/EntityFactory.js +63 -41
- package/entity/EntityHelper.js +26 -12
- package/entity/EntityLoader.d.ts +5 -4
- package/entity/EntityLoader.js +63 -38
- package/entity/EntityRepository.d.ts +1 -1
- package/entity/Reference.d.ts +6 -5
- package/entity/Reference.js +34 -9
- package/entity/WrappedEntity.d.ts +2 -7
- package/entity/WrappedEntity.js +2 -7
- package/entity/defineEntity.d.ts +568 -0
- package/entity/defineEntity.js +529 -0
- package/entity/index.d.ts +3 -2
- package/entity/index.js +3 -2
- package/entity/utils.d.ts +7 -0
- package/entity/utils.js +16 -4
- package/entity/validators.d.ts +11 -0
- package/entity/validators.js +65 -0
- package/enums.d.ts +21 -6
- package/enums.js +14 -1
- package/errors.d.ts +10 -2
- package/errors.js +29 -10
- package/events/EventManager.d.ts +2 -1
- package/events/EventManager.js +19 -11
- package/events/EventSubscriber.d.ts +3 -1
- package/hydration/Hydrator.js +1 -2
- package/hydration/ObjectHydrator.d.ts +4 -4
- package/hydration/ObjectHydrator.js +35 -25
- 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/index.d.ts +1 -0
- package/logging/index.js +1 -0
- package/logging/inspect.d.ts +2 -0
- package/logging/inspect.js +16 -0
- package/metadata/EntitySchema.d.ts +9 -13
- package/metadata/EntitySchema.js +44 -26
- package/metadata/MetadataDiscovery.d.ts +6 -9
- package/metadata/MetadataDiscovery.js +165 -205
- package/metadata/MetadataProvider.d.ts +11 -2
- package/metadata/MetadataProvider.js +44 -2
- package/metadata/MetadataStorage.d.ts +1 -6
- package/metadata/MetadataStorage.js +6 -18
- package/metadata/MetadataValidator.d.ts +0 -7
- package/metadata/MetadataValidator.js +0 -10
- 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 +480 -0
- package/metadata/types.js +1 -0
- package/naming-strategy/AbstractNamingStrategy.d.ts +5 -1
- package/naming-strategy/AbstractNamingStrategy.js +8 -2
- package/naming-strategy/NamingStrategy.d.ts +11 -1
- package/not-supported.d.ts +2 -0
- package/not-supported.js +4 -0
- package/package.json +18 -10
- package/platforms/ExceptionConverter.js +1 -1
- package/platforms/Platform.d.ts +6 -13
- package/platforms/Platform.js +15 -41
- package/serialization/EntitySerializer.d.ts +2 -0
- package/serialization/EntitySerializer.js +32 -14
- package/serialization/EntityTransformer.js +22 -12
- package/serialization/SerializationContext.js +16 -13
- package/types/ArrayType.d.ts +1 -1
- package/types/ArrayType.js +2 -3
- package/types/BigIntType.d.ts +8 -6
- package/types/BigIntType.js +1 -1
- package/types/BlobType.d.ts +0 -1
- package/types/BlobType.js +0 -3
- package/types/BooleanType.d.ts +2 -1
- package/types/BooleanType.js +3 -0
- package/types/DecimalType.d.ts +6 -4
- package/types/DecimalType.js +3 -3
- package/types/DoubleType.js +2 -2
- 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 +109 -73
- package/typings.js +38 -35
- package/unit-of-work/ChangeSet.d.ts +0 -3
- package/unit-of-work/ChangeSet.js +2 -2
- package/unit-of-work/ChangeSetComputer.d.ts +1 -3
- package/unit-of-work/ChangeSetComputer.js +11 -9
- package/unit-of-work/ChangeSetPersister.d.ts +5 -4
- package/unit-of-work/ChangeSetPersister.js +51 -19
- package/unit-of-work/UnitOfWork.d.ts +8 -1
- package/unit-of-work/UnitOfWork.js +91 -49
- package/utils/AbstractSchemaGenerator.d.ts +5 -5
- package/utils/AbstractSchemaGenerator.js +11 -9
- package/utils/Configuration.d.ts +757 -206
- package/utils/Configuration.js +140 -188
- package/utils/ConfigurationLoader.d.ts +1 -54
- package/utils/ConfigurationLoader.js +1 -352
- package/utils/Cursor.d.ts +0 -3
- package/utils/Cursor.js +6 -3
- package/utils/DataloaderUtils.d.ts +15 -5
- package/utils/DataloaderUtils.js +54 -8
- package/utils/EntityComparator.d.ts +8 -4
- package/utils/EntityComparator.js +52 -17
- package/utils/QueryHelper.d.ts +9 -1
- package/utils/QueryHelper.js +70 -9
- package/utils/RawQueryFragment.d.ts +36 -13
- package/utils/RawQueryFragment.js +36 -16
- package/utils/TransactionManager.d.ts +65 -0
- package/utils/TransactionManager.js +223 -0
- package/utils/Utils.d.ts +9 -97
- package/utils/Utils.js +83 -302
- package/utils/clone.js +2 -3
- package/utils/env-vars.d.ts +3 -0
- package/utils/env-vars.js +87 -0
- package/utils/fs-utils.d.ts +12 -0
- package/utils/fs-utils.js +97 -0
- package/utils/index.d.ts +2 -1
- package/utils/index.js +2 -1
- package/utils/upsert-utils.d.ts +7 -2
- package/utils/upsert-utils.js +55 -4
- 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 -18
- package/decorators/Embedded.js +0 -18
- package/decorators/Entity.d.ts +0 -18
- 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 -40
- package/decorators/ManyToMany.js +0 -14
- package/decorators/ManyToOne.d.ts +0 -30
- 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 -24
- 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 -13
- 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 -116
- package/entity/ArrayCollection.js +0 -402
- 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,8 @@
|
|
|
1
|
-
import {
|
|
2
|
-
import globby from 'globby';
|
|
3
|
-
import { extname, isAbsolute, join, normalize, relative, resolve } from 'node:path';
|
|
4
|
-
import { platform } from 'node:os';
|
|
1
|
+
import { isAbsolute, normalize, relative } from 'node:path';
|
|
5
2
|
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
3
|
import { clone } from './clone.js';
|
|
10
|
-
import {
|
|
4
|
+
import { GroupOperator, PlainObject, QueryOperator, ReferenceKind } from '../enums.js';
|
|
11
5
|
import { helper } from '../entity/wrap.js';
|
|
12
|
-
export const ObjectBindingPattern = Symbol('ObjectBindingPattern');
|
|
13
6
|
function compareConstructors(a, b) {
|
|
14
7
|
if (a.constructor === b.constructor) {
|
|
15
8
|
return true;
|
|
@@ -41,7 +34,7 @@ export function compareObjects(a, b) {
|
|
|
41
34
|
}
|
|
42
35
|
return timeA === timeB;
|
|
43
36
|
}
|
|
44
|
-
/* v8 ignore next
|
|
37
|
+
/* v8 ignore next */
|
|
45
38
|
if ((typeof a === 'function' && typeof b === 'function') ||
|
|
46
39
|
(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
40
|
(a instanceof RegExp && b instanceof RegExp) ||
|
|
@@ -119,7 +112,7 @@ export function equals(a, b) {
|
|
|
119
112
|
const equalsFn = equals;
|
|
120
113
|
export function parseJsonSafe(value) {
|
|
121
114
|
if (typeof value === 'string') {
|
|
122
|
-
/* v8 ignore next
|
|
115
|
+
/* v8 ignore next */
|
|
123
116
|
try {
|
|
124
117
|
return JSON.parse(value);
|
|
125
118
|
}
|
|
@@ -132,52 +125,15 @@ export function parseJsonSafe(value) {
|
|
|
132
125
|
}
|
|
133
126
|
export class Utils {
|
|
134
127
|
static PK_SEPARATOR = '~~~';
|
|
128
|
+
static #ORM_VERSION = '7.0.0-dev.91';
|
|
135
129
|
/* v8 ignore next */
|
|
136
130
|
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
|
-
}
|
|
143
131
|
/**
|
|
144
132
|
* Checks if the argument is instance of `Object`. Returns false for arrays.
|
|
145
133
|
*/
|
|
146
134
|
static isObject(o) {
|
|
147
135
|
return !!o && typeof o === 'object' && !Array.isArray(o);
|
|
148
136
|
}
|
|
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
137
|
/**
|
|
182
138
|
* Removes `undefined` properties (recursively) so they are not saved as nulls
|
|
183
139
|
*/
|
|
@@ -225,18 +181,6 @@ export class Utils {
|
|
|
225
181
|
}
|
|
226
182
|
return false;
|
|
227
183
|
}
|
|
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
184
|
/**
|
|
241
185
|
* Checks if arguments are deeply (but not strictly) equal.
|
|
242
186
|
*/
|
|
@@ -282,7 +226,7 @@ export class Utils {
|
|
|
282
226
|
target[key] = Utils.copy(value);
|
|
283
227
|
continue;
|
|
284
228
|
}
|
|
285
|
-
/* v8 ignore next
|
|
229
|
+
/* v8 ignore next */
|
|
286
230
|
if (!(key in target)) {
|
|
287
231
|
Object.assign(target, { [key]: {} });
|
|
288
232
|
}
|
|
@@ -295,30 +239,6 @@ export class Utils {
|
|
|
295
239
|
}
|
|
296
240
|
return Utils._merge(target, sources, ignoreUndefined);
|
|
297
241
|
}
|
|
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
242
|
/**
|
|
323
243
|
* Creates deep copy of given object.
|
|
324
244
|
*/
|
|
@@ -359,53 +279,37 @@ export class Utils {
|
|
|
359
279
|
}
|
|
360
280
|
}
|
|
361
281
|
/**
|
|
362
|
-
* Returns array of functions argument names. Uses
|
|
282
|
+
* Returns array of functions argument names. Uses basic regex for source code analysis, might not work with advanced syntax.
|
|
363
283
|
*/
|
|
364
|
-
static
|
|
365
|
-
|
|
366
|
-
|
|
367
|
-
|
|
368
|
-
|
|
369
|
-
try {
|
|
370
|
-
return tokenize(func.toString(), { tolerant: true });
|
|
284
|
+
static getConstructorParams(func) {
|
|
285
|
+
const source = func.toString();
|
|
286
|
+
const i = source.indexOf('constructor');
|
|
287
|
+
if (i === -1) {
|
|
288
|
+
return undefined;
|
|
371
289
|
}
|
|
372
|
-
|
|
373
|
-
|
|
290
|
+
const start = source.indexOf('(', i);
|
|
291
|
+
if (start === -1) {
|
|
292
|
+
return undefined;
|
|
374
293
|
}
|
|
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;
|
|
294
|
+
let depth = 0;
|
|
295
|
+
let end = start;
|
|
296
|
+
for (; end < source.length; end++) {
|
|
297
|
+
if (source[end] === '(') {
|
|
298
|
+
depth++;
|
|
390
299
|
}
|
|
391
|
-
if (
|
|
392
|
-
|
|
393
|
-
currentBlockStart = i;
|
|
394
|
-
continue;
|
|
300
|
+
if (source[end] === ')') {
|
|
301
|
+
depth--;
|
|
395
302
|
}
|
|
396
|
-
if (
|
|
303
|
+
if (depth === 0) {
|
|
397
304
|
break;
|
|
398
305
|
}
|
|
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
306
|
}
|
|
408
|
-
|
|
307
|
+
const raw = source.slice(start + 1, end);
|
|
308
|
+
return raw
|
|
309
|
+
.split(',')
|
|
310
|
+
.map(s => s.trim().replace(/=.*$/, '').trim())
|
|
311
|
+
.filter(Boolean)
|
|
312
|
+
.map(raw => raw.startsWith('{') && raw.endsWith('}') ? '' : raw);
|
|
409
313
|
}
|
|
410
314
|
/**
|
|
411
315
|
* Checks whether the argument looks like primary key (string, number or ObjectId).
|
|
@@ -418,7 +322,7 @@ export class Utils {
|
|
|
418
322
|
return true;
|
|
419
323
|
}
|
|
420
324
|
if (Utils.isObject(key)) {
|
|
421
|
-
if (key.constructor
|
|
325
|
+
if (key.constructor?.name === 'ObjectId') {
|
|
422
326
|
return true;
|
|
423
327
|
}
|
|
424
328
|
if (!Utils.isPlainObject(key) && !Utils.isEntity(key, true)) {
|
|
@@ -435,7 +339,11 @@ export class Utils {
|
|
|
435
339
|
return data;
|
|
436
340
|
}
|
|
437
341
|
if (Utils.isEntity(data, true)) {
|
|
438
|
-
|
|
342
|
+
const wrapped = helper(data);
|
|
343
|
+
if (wrapped.__meta.compositePK) {
|
|
344
|
+
return wrapped.getPrimaryKeys();
|
|
345
|
+
}
|
|
346
|
+
return wrapped.getPrimaryKey();
|
|
439
347
|
}
|
|
440
348
|
if (strict && meta && Utils.getObjectKeysSize(data) !== meta.primaryKeys.length) {
|
|
441
349
|
return null;
|
|
@@ -444,7 +352,7 @@ export class Utils {
|
|
|
444
352
|
if (meta.compositePK) {
|
|
445
353
|
return this.getCompositeKeyValue(data, meta);
|
|
446
354
|
}
|
|
447
|
-
return data[meta.primaryKeys[0]]
|
|
355
|
+
return data[meta.primaryKeys[0]] ?? data[meta.serializedPrimaryKey] ?? null;
|
|
448
356
|
}
|
|
449
357
|
return null;
|
|
450
358
|
}
|
|
@@ -483,8 +391,8 @@ export class Utils {
|
|
|
483
391
|
static splitPrimaryKeys(key) {
|
|
484
392
|
return key.split(this.PK_SEPARATOR);
|
|
485
393
|
}
|
|
486
|
-
static getPrimaryKeyValues(entity,
|
|
487
|
-
/* v8 ignore next
|
|
394
|
+
static getPrimaryKeyValues(entity, meta, allowScalar = false, convertCustomTypes = false) {
|
|
395
|
+
/* v8 ignore next */
|
|
488
396
|
if (entity == null) {
|
|
489
397
|
return entity;
|
|
490
398
|
}
|
|
@@ -494,15 +402,28 @@ export class Utils {
|
|
|
494
402
|
}
|
|
495
403
|
return val;
|
|
496
404
|
}
|
|
497
|
-
|
|
498
|
-
|
|
499
|
-
|
|
500
|
-
|
|
405
|
+
let pk;
|
|
406
|
+
if (Utils.isEntity(entity, true)) {
|
|
407
|
+
pk = helper(entity).getPrimaryKey(convertCustomTypes);
|
|
408
|
+
}
|
|
409
|
+
else {
|
|
410
|
+
pk = meta.primaryKeys.reduce((o, pk) => {
|
|
411
|
+
const targetMeta = meta.properties[pk].targetMeta;
|
|
412
|
+
if (targetMeta && Utils.isPlainObject(entity[pk])) {
|
|
413
|
+
o[pk] = Utils.getPrimaryKeyValues(entity[pk], targetMeta, allowScalar, convertCustomTypes);
|
|
414
|
+
}
|
|
415
|
+
else {
|
|
416
|
+
o[pk] = entity[pk];
|
|
417
|
+
}
|
|
418
|
+
return o;
|
|
419
|
+
}, {});
|
|
420
|
+
}
|
|
421
|
+
if (meta.primaryKeys.length > 1) {
|
|
501
422
|
return toArray(pk);
|
|
502
423
|
}
|
|
503
424
|
if (allowScalar) {
|
|
504
425
|
if (Utils.isPlainObject(pk)) {
|
|
505
|
-
return pk[primaryKeys[0]];
|
|
426
|
+
return pk[(meta.primaryKeys)[0]];
|
|
506
427
|
}
|
|
507
428
|
return pk;
|
|
508
429
|
}
|
|
@@ -546,7 +467,7 @@ export class Utils {
|
|
|
546
467
|
return o;
|
|
547
468
|
}, {});
|
|
548
469
|
}
|
|
549
|
-
static getOrderedPrimaryKeys(id, meta, platform, convertCustomTypes = false) {
|
|
470
|
+
static getOrderedPrimaryKeys(id, meta, platform, convertCustomTypes = false, allowScalar = false) {
|
|
550
471
|
const data = (Utils.isPrimaryKey(id) ? { [meta.primaryKeys[0]]: id } : id);
|
|
551
472
|
const pks = meta.primaryKeys.map((pk, idx) => {
|
|
552
473
|
const prop = meta.properties[pk];
|
|
@@ -556,11 +477,14 @@ export class Utils {
|
|
|
556
477
|
value = prop.customType.convertToJSValue(value, platform);
|
|
557
478
|
}
|
|
558
479
|
if (prop.kind !== ReferenceKind.SCALAR && prop.targetMeta) {
|
|
559
|
-
const value2 = this.getOrderedPrimaryKeys(value, prop.targetMeta, platform, convertCustomTypes);
|
|
480
|
+
const value2 = this.getOrderedPrimaryKeys(value, prop.targetMeta, platform, convertCustomTypes, allowScalar);
|
|
560
481
|
value = value2.length > 1 ? value2 : value2[0];
|
|
561
482
|
}
|
|
562
483
|
return value;
|
|
563
484
|
});
|
|
485
|
+
if (allowScalar && pks.length === 1) {
|
|
486
|
+
return pks[0];
|
|
487
|
+
}
|
|
564
488
|
// we need to flatten the PKs as composite PKs can be build from another composite PKs
|
|
565
489
|
// and this method is used to get the PK hash in identity map, that expects flat array
|
|
566
490
|
return Utils.flatten(pks);
|
|
@@ -583,12 +507,6 @@ export class Utils {
|
|
|
583
507
|
static isScalarReference(data, allowReference = false) {
|
|
584
508
|
return typeof data === 'object' && data?.__scalarReference;
|
|
585
509
|
}
|
|
586
|
-
/**
|
|
587
|
-
* Checks whether the argument is ObjectId instance
|
|
588
|
-
*/
|
|
589
|
-
static isObjectID(key) {
|
|
590
|
-
return Utils.isObject(key) && key.constructor && key.constructor.name.toLowerCase() === 'objectid';
|
|
591
|
-
}
|
|
592
510
|
/**
|
|
593
511
|
* Checks whether the argument is empty (array without items, object without keys or falsy value).
|
|
594
512
|
*/
|
|
@@ -619,7 +537,7 @@ export class Utils {
|
|
|
619
537
|
* Tries to detect TypeScript support.
|
|
620
538
|
*/
|
|
621
539
|
static detectTypeScriptSupport() {
|
|
622
|
-
/* v8 ignore next
|
|
540
|
+
/* v8 ignore next */
|
|
623
541
|
return process.argv[0].endsWith('ts-node') // running via ts-node directly
|
|
624
542
|
|| !!process.env.MIKRO_ORM_CLI_ALWAYS_ALLOW_TS // forced explicitly or enabled via `registerTypeScriptSupport()`
|
|
625
543
|
|| !!process.env.TS_JEST // check if ts-jest is used (works only with v27.0.4+)
|
|
@@ -632,43 +550,6 @@ export class Utils {
|
|
|
632
550
|
|| arg.includes('node_modules/tsx/'); // check for tsx loader
|
|
633
551
|
});
|
|
634
552
|
}
|
|
635
|
-
/**
|
|
636
|
-
* Uses some dark magic to get source path to caller where decorator is used.
|
|
637
|
-
* Analyses stack trace of error created inside the function call.
|
|
638
|
-
*/
|
|
639
|
-
static lookupPathFromDecorator(name, stack) {
|
|
640
|
-
// use some dark magic to get source path to caller
|
|
641
|
-
stack = stack || new Error().stack.split('\n');
|
|
642
|
-
// In some situations (e.g. swc 1.3.4+), the presence of a source map can obscure the call to
|
|
643
|
-
// __decorate(), replacing it with the constructor name. To support these cases we look for
|
|
644
|
-
// Reflect.decorate() as well. Also when babel is used, we need to check
|
|
645
|
-
// the `_applyDecoratedDescriptor` method instead.
|
|
646
|
-
let line = stack.findIndex(line => line.match(/__decorate|Reflect\.decorate|_applyDecoratedDescriptor/));
|
|
647
|
-
// bun does not have those lines at all, only the DecorateProperty/DecorateConstructor,
|
|
648
|
-
// but those are also present in node, so we need to check this only if they weren't found.
|
|
649
|
-
if (line === -1) {
|
|
650
|
-
// here we handle bun which stack is different from nodejs so we search for reflect-metadata
|
|
651
|
-
// Different bun versions might have different stack traces. The "last index" works for both 1.2.6 and 1.2.7.
|
|
652
|
-
const reflectLine = stack.findLastIndex(line => Utils.normalizePath(line).includes('node_modules/reflect-metadata/Reflect.js'));
|
|
653
|
-
if (reflectLine === -1 || reflectLine + 2 >= stack.length || !stack[reflectLine + 1].includes('bun:wrap')) {
|
|
654
|
-
return name;
|
|
655
|
-
}
|
|
656
|
-
line = reflectLine + 2;
|
|
657
|
-
}
|
|
658
|
-
if (stack[line].includes('Reflect.decorate')) {
|
|
659
|
-
line++;
|
|
660
|
-
}
|
|
661
|
-
if (Utils.normalizePath(stack[line]).includes('node_modules/tslib/tslib')) {
|
|
662
|
-
line++;
|
|
663
|
-
}
|
|
664
|
-
try {
|
|
665
|
-
const re = stack[line].match(/\(.+\)/i) ? /\((.*):\d+:\d+\)/ : /at\s*(.*):\d+:\d+$/;
|
|
666
|
-
return Utils.normalizePath(stack[line].match(re)[1]);
|
|
667
|
-
}
|
|
668
|
-
catch {
|
|
669
|
-
return name;
|
|
670
|
-
}
|
|
671
|
-
}
|
|
672
553
|
/**
|
|
673
554
|
* Gets the type of the argument.
|
|
674
555
|
*/
|
|
@@ -678,11 +559,11 @@ export class Utils {
|
|
|
678
559
|
return simple;
|
|
679
560
|
}
|
|
680
561
|
const objectType = Object.prototype.toString.call(value);
|
|
681
|
-
const type = objectType.match(
|
|
562
|
+
const type = objectType.match(/^\[object (.+)]$/)[1];
|
|
682
563
|
if (type === 'Uint8Array') {
|
|
683
564
|
return 'Buffer';
|
|
684
565
|
}
|
|
685
|
-
return
|
|
566
|
+
return type;
|
|
686
567
|
}
|
|
687
568
|
/**
|
|
688
569
|
* Checks whether the value is POJO (e.g. `{ foo: 'bar' }`, and not instance of `Foo`)
|
|
@@ -766,8 +647,14 @@ export class Utils {
|
|
|
766
647
|
}
|
|
767
648
|
return Utils.normalizePath(path);
|
|
768
649
|
}
|
|
650
|
+
// FNV-1a 64-bit
|
|
769
651
|
static hash(data, length) {
|
|
770
|
-
|
|
652
|
+
let h1 = 0xcbf29ce484222325n;
|
|
653
|
+
for (let i = 0; i < data.length; i++) {
|
|
654
|
+
h1 ^= BigInt(data.charCodeAt(i));
|
|
655
|
+
h1 = (h1 * 0x100000001b3n) & 0xffffffffffffffffn;
|
|
656
|
+
}
|
|
657
|
+
const hash = h1.toString(16).padStart(16, '0');
|
|
771
658
|
if (length) {
|
|
772
659
|
return hash.substring(0, length);
|
|
773
660
|
}
|
|
@@ -799,13 +686,6 @@ export class Utils {
|
|
|
799
686
|
static randomInt(min, max) {
|
|
800
687
|
return Math.round(Math.random() * (max - min)) + min;
|
|
801
688
|
}
|
|
802
|
-
static async pathExists(path, options = {}) {
|
|
803
|
-
if (globby.hasMagic(path)) {
|
|
804
|
-
const found = await globby(path, options);
|
|
805
|
-
return found.length > 0;
|
|
806
|
-
}
|
|
807
|
-
return this.pathExistsSync(path);
|
|
808
|
-
}
|
|
809
689
|
/**
|
|
810
690
|
* Extracts all possible values of a TS enum. Works with both string and numeric enums.
|
|
811
691
|
*/
|
|
@@ -831,15 +711,6 @@ export class Utils {
|
|
|
831
711
|
}
|
|
832
712
|
return key in GroupOperator || key in QueryOperator;
|
|
833
713
|
}
|
|
834
|
-
static isGroupOperator(key) {
|
|
835
|
-
return key in GroupOperator;
|
|
836
|
-
}
|
|
837
|
-
static isArrayOperator(key) {
|
|
838
|
-
return ARRAY_OPERATORS.includes(key);
|
|
839
|
-
}
|
|
840
|
-
static isJsonKeyOperator(key) {
|
|
841
|
-
return JSON_KEY_OPERATORS.includes(key);
|
|
842
|
-
}
|
|
843
714
|
static hasNestedKey(object, key) {
|
|
844
715
|
if (!object) {
|
|
845
716
|
return false;
|
|
@@ -852,66 +723,18 @@ export class Utils {
|
|
|
852
723
|
}
|
|
853
724
|
return false;
|
|
854
725
|
}
|
|
855
|
-
static getGlobalStorage(namespace) {
|
|
856
|
-
const key = `mikro-orm-${namespace}`;
|
|
857
|
-
globalThis[key] = globalThis[key] || {};
|
|
858
|
-
return globalThis[key];
|
|
859
|
-
}
|
|
860
|
-
/**
|
|
861
|
-
* Require a module from a specific location
|
|
862
|
-
* @param id The module to require
|
|
863
|
-
* @param [from] Location to start the node resolution
|
|
864
|
-
*/
|
|
865
|
-
static requireFrom(id, from = process.cwd()) {
|
|
866
|
-
if (!extname(from)) {
|
|
867
|
-
from = join(from, '__fake.js');
|
|
868
|
-
}
|
|
869
|
-
return createRequire(resolve(from))(id);
|
|
870
|
-
}
|
|
871
726
|
static async dynamicImport(id) {
|
|
872
|
-
/* v8 ignore next 7 */
|
|
873
|
-
if (platform() === 'win32') {
|
|
874
|
-
try {
|
|
875
|
-
id = pathToFileURL(id).toString();
|
|
876
|
-
}
|
|
877
|
-
catch {
|
|
878
|
-
// ignore
|
|
879
|
-
}
|
|
880
|
-
}
|
|
881
727
|
/* v8 ignore next */
|
|
882
|
-
|
|
883
|
-
|
|
884
|
-
/* v8 ignore next 3 */
|
|
885
|
-
static setDynamicImportProvider(provider) {
|
|
886
|
-
this.dynamicImportProvider = provider;
|
|
887
|
-
}
|
|
888
|
-
static ensureDir(path) {
|
|
889
|
-
if (!existsSync(path)) {
|
|
890
|
-
mkdirSync(path, { recursive: true });
|
|
891
|
-
}
|
|
892
|
-
}
|
|
893
|
-
static pathExistsSync(path) {
|
|
894
|
-
return existsSync(path);
|
|
895
|
-
}
|
|
896
|
-
static readJSONSync(path) {
|
|
897
|
-
const file = readFileSync(path);
|
|
898
|
-
return JSON.parse(file.toString());
|
|
728
|
+
const specifier = id.startsWith('file://') ? id : pathToFileURL(id).href;
|
|
729
|
+
return this.dynamicImportProvider(specifier);
|
|
899
730
|
}
|
|
900
731
|
static getORMVersion() {
|
|
901
|
-
|
|
902
|
-
// this works during development where we have `src` folder
|
|
903
|
-
return this.requireFrom('../../package.json', import.meta.dirname).version;
|
|
904
|
-
/* v8 ignore next 5 */
|
|
905
|
-
}
|
|
906
|
-
catch {
|
|
907
|
-
// this works in production build where we do not have the `src` folder
|
|
908
|
-
return this.requireFrom('../package.json', import.meta.dirname).version;
|
|
909
|
-
}
|
|
732
|
+
return this.#ORM_VERSION;
|
|
910
733
|
}
|
|
911
734
|
static createFunction(context, code) {
|
|
912
735
|
try {
|
|
913
736
|
return new Function(...context.keys(), `'use strict';\n` + code)(...context.values());
|
|
914
|
-
/* v8 ignore next
|
|
737
|
+
/* v8 ignore next */
|
|
915
738
|
}
|
|
916
739
|
catch (e) {
|
|
917
740
|
// eslint-disable-next-line no-console
|
|
@@ -924,7 +747,7 @@ export class Utils {
|
|
|
924
747
|
return fn(...args);
|
|
925
748
|
}
|
|
926
749
|
catch (e) {
|
|
927
|
-
/* v8 ignore
|
|
750
|
+
/* v8 ignore next */
|
|
928
751
|
if ([SyntaxError, TypeError, EvalError, ReferenceError].some(t => e instanceof t)) {
|
|
929
752
|
const position = e.stack.match(/<anonymous>:(\d+):(\d+)/);
|
|
930
753
|
let code = fn.toString();
|
|
@@ -941,18 +764,9 @@ export class Utils {
|
|
|
941
764
|
// eslint-disable-next-line no-console
|
|
942
765
|
console.error(`JIT runtime error: ${e.message}\n\n${code}`);
|
|
943
766
|
}
|
|
944
|
-
/* v8 ignore stop */
|
|
945
767
|
throw e;
|
|
946
768
|
}
|
|
947
769
|
}
|
|
948
|
-
/**
|
|
949
|
-
* @see https://github.com/mikro-orm/mikro-orm/issues/840
|
|
950
|
-
*/
|
|
951
|
-
static propertyDecoratorReturnValue() {
|
|
952
|
-
if (process.env.BABEL_DECORATORS_COMPAT) {
|
|
953
|
-
return {};
|
|
954
|
-
}
|
|
955
|
-
}
|
|
956
770
|
static unwrapProperty(entity, meta, prop, payload = false) {
|
|
957
771
|
let p = prop;
|
|
958
772
|
const path = [];
|
|
@@ -1032,54 +846,21 @@ export class Utils {
|
|
|
1032
846
|
}
|
|
1033
847
|
}
|
|
1034
848
|
}
|
|
1035
|
-
static
|
|
1036
|
-
allowError ??= `Cannot find module '${module}'`;
|
|
1037
|
-
from ??= process.cwd();
|
|
849
|
+
static async tryImport({ module, warning }) {
|
|
1038
850
|
try {
|
|
1039
|
-
return
|
|
851
|
+
return await import(module);
|
|
1040
852
|
}
|
|
1041
853
|
catch (err) {
|
|
1042
|
-
if (err.
|
|
1043
|
-
|
|
1044
|
-
|
|
854
|
+
if (err.code === 'ERR_MODULE_NOT_FOUND') {
|
|
855
|
+
if (warning) {
|
|
856
|
+
// eslint-disable-next-line no-console
|
|
857
|
+
console.warn(warning);
|
|
858
|
+
}
|
|
1045
859
|
return undefined;
|
|
1046
860
|
}
|
|
1047
861
|
throw err;
|
|
1048
862
|
}
|
|
1049
863
|
}
|
|
1050
|
-
static async tryImport({ module, warning }) {
|
|
1051
|
-
try {
|
|
1052
|
-
return await this.dynamicImport(module);
|
|
1053
|
-
}
|
|
1054
|
-
catch (err) {
|
|
1055
|
-
// eslint-disable-next-line no-console
|
|
1056
|
-
console.warn(warning);
|
|
1057
|
-
return undefined;
|
|
1058
|
-
}
|
|
1059
|
-
}
|
|
1060
|
-
static stripRelativePath(str) {
|
|
1061
|
-
return str.replace(/^(?:\.\.\/|\.\/)+/, '/');
|
|
1062
|
-
}
|
|
1063
|
-
/**
|
|
1064
|
-
* simple process.argv parser, supports only properties with long names, prefixed with `--`
|
|
1065
|
-
*/
|
|
1066
|
-
static parseArgs() {
|
|
1067
|
-
let lastKey;
|
|
1068
|
-
return process.argv.slice(2).reduce((args, arg) => {
|
|
1069
|
-
if (arg.includes('=')) {
|
|
1070
|
-
const [key, value] = arg.split('=');
|
|
1071
|
-
args[key.substring(2)] = value;
|
|
1072
|
-
}
|
|
1073
|
-
else if (lastKey) {
|
|
1074
|
-
args[lastKey] = arg;
|
|
1075
|
-
lastKey = undefined;
|
|
1076
|
-
}
|
|
1077
|
-
else if (arg.startsWith('--')) {
|
|
1078
|
-
lastKey = arg.substring(2);
|
|
1079
|
-
}
|
|
1080
|
-
return args;
|
|
1081
|
-
}, {});
|
|
1082
|
-
}
|
|
1083
864
|
static xor(a, b) {
|
|
1084
865
|
return (a || b) && !(a && b);
|
|
1085
866
|
}
|
package/utils/clone.js
CHANGED
|
@@ -3,7 +3,6 @@
|
|
|
3
3
|
* clone `EventEmitter`s to get around https://github.com/mikro-orm/mikro-orm/issues/2748
|
|
4
4
|
* @internal
|
|
5
5
|
*/
|
|
6
|
-
import { EventEmitter } from 'node:events';
|
|
7
6
|
import { RawQueryFragment } from './RawQueryFragment.js';
|
|
8
7
|
/**
|
|
9
8
|
* Get the property descriptor of a property on an object or its prototype chain.
|
|
@@ -34,7 +33,7 @@ export function clone(parent, respectCustomCloneMethod = true) {
|
|
|
34
33
|
if (raw && respectCustomCloneMethod) {
|
|
35
34
|
return raw.clone();
|
|
36
35
|
}
|
|
37
|
-
if (typeof parent !== 'object'
|
|
36
|
+
if (typeof parent !== 'object') {
|
|
38
37
|
return parent;
|
|
39
38
|
}
|
|
40
39
|
if (respectCustomCloneMethod && 'clone' in parent && typeof parent.clone === 'function') {
|
|
@@ -127,7 +126,7 @@ export function clone(parent, respectCustomCloneMethod = true) {
|
|
|
127
126
|
for (let i = 0; i < symbols.length; i++) {
|
|
128
127
|
const symbol = symbols[i];
|
|
129
128
|
const descriptor = Object.getOwnPropertyDescriptor(parent, symbol);
|
|
130
|
-
/* v8 ignore next
|
|
129
|
+
/* v8 ignore next */
|
|
131
130
|
if (descriptor && !descriptor.enumerable) {
|
|
132
131
|
continue;
|
|
133
132
|
}
|