@mikro-orm/core 7.0.0-dev.7 → 7.0.0-dev.70
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 +85 -42
- package/EntityManager.js +282 -194
- package/MikroORM.d.ts +11 -29
- package/MikroORM.js +33 -127
- package/README.md +3 -2
- package/cache/FileCacheAdapter.d.ts +2 -1
- package/cache/FileCacheAdapter.js +5 -4
- package/connections/Connection.d.ts +11 -7
- package/connections/Connection.js +16 -13
- package/drivers/DatabaseDriver.d.ts +11 -5
- package/drivers/DatabaseDriver.js +13 -4
- package/drivers/IDatabaseDriver.d.ts +27 -5
- package/entity/BaseEntity.d.ts +0 -1
- package/entity/BaseEntity.js +0 -3
- package/entity/Collection.d.ts +98 -30
- package/entity/Collection.js +432 -93
- package/entity/EntityAssigner.d.ts +1 -1
- package/entity/EntityAssigner.js +9 -1
- package/entity/EntityFactory.d.ts +7 -0
- package/entity/EntityFactory.js +63 -40
- package/entity/EntityHelper.js +26 -9
- package/entity/EntityLoader.d.ts +5 -4
- package/entity/EntityLoader.js +69 -36
- package/entity/EntityRepository.d.ts +1 -1
- package/entity/EntityValidator.js +4 -4
- package/entity/Reference.d.ts +9 -7
- package/entity/Reference.js +32 -5
- package/entity/WrappedEntity.d.ts +0 -2
- package/entity/WrappedEntity.js +1 -5
- package/entity/defineEntity.d.ts +549 -0
- package/entity/defineEntity.js +529 -0
- package/entity/index.d.ts +2 -1
- package/entity/index.js +2 -1
- package/entity/utils.d.ts +7 -0
- package/entity/utils.js +15 -3
- package/enums.d.ts +20 -5
- package/enums.js +13 -0
- package/errors.d.ts +6 -1
- package/errors.js +14 -4
- package/events/EventSubscriber.d.ts +3 -1
- package/hydration/ObjectHydrator.d.ts +4 -4
- package/hydration/ObjectHydrator.js +35 -24
- package/index.d.ts +2 -2
- package/index.js +1 -2
- package/logging/DefaultLogger.d.ts +1 -1
- package/logging/SimpleLogger.d.ts +1 -1
- package/metadata/EntitySchema.d.ts +9 -13
- package/metadata/EntitySchema.js +44 -26
- package/metadata/MetadataDiscovery.d.ts +6 -7
- package/metadata/MetadataDiscovery.js +161 -162
- package/metadata/MetadataProvider.d.ts +2 -2
- package/metadata/MetadataProvider.js +15 -0
- package/metadata/MetadataStorage.d.ts +0 -4
- package/metadata/MetadataStorage.js +7 -11
- package/metadata/MetadataValidator.d.ts +0 -7
- package/metadata/MetadataValidator.js +4 -13
- package/metadata/discover-entities.d.ts +5 -0
- package/metadata/discover-entities.js +39 -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 +7 -1
- package/naming-strategy/NamingStrategy.d.ts +11 -1
- package/package.json +11 -10
- package/platforms/Platform.d.ts +6 -10
- package/platforms/Platform.js +6 -22
- package/serialization/EntitySerializer.d.ts +2 -0
- package/serialization/EntitySerializer.js +29 -11
- package/serialization/EntityTransformer.js +22 -12
- package/serialization/SerializationContext.js +14 -11
- package/types/ArrayType.d.ts +1 -1
- package/types/ArrayType.js +1 -2
- package/types/BigIntType.d.ts +8 -6
- 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 +1 -1
- package/types/DoubleType.js +1 -1
- package/types/JsonType.d.ts +1 -1
- package/types/JsonType.js +7 -2
- package/types/Type.d.ts +2 -1
- package/types/Type.js +1 -1
- package/types/Uint8ArrayType.d.ts +0 -1
- package/types/Uint8ArrayType.js +0 -3
- package/types/index.d.ts +1 -1
- package/typings.d.ts +112 -77
- package/typings.js +32 -32
- package/unit-of-work/ChangeSetComputer.js +8 -3
- package/unit-of-work/ChangeSetPersister.d.ts +4 -2
- package/unit-of-work/ChangeSetPersister.js +37 -16
- package/unit-of-work/UnitOfWork.d.ts +8 -1
- package/unit-of-work/UnitOfWork.js +111 -54
- package/utils/AbstractSchemaGenerator.d.ts +5 -5
- package/utils/AbstractSchemaGenerator.js +10 -8
- package/utils/Configuration.d.ts +202 -191
- package/utils/Configuration.js +143 -153
- package/utils/ConfigurationLoader.d.ts +3 -44
- package/utils/ConfigurationLoader.js +26 -239
- package/utils/Cursor.d.ts +3 -3
- package/utils/Cursor.js +3 -0
- package/utils/DataloaderUtils.d.ts +15 -5
- package/utils/DataloaderUtils.js +53 -7
- package/utils/EntityComparator.d.ts +8 -4
- package/utils/EntityComparator.js +107 -60
- package/utils/QueryHelper.d.ts +9 -1
- package/utils/QueryHelper.js +69 -8
- package/utils/RawQueryFragment.d.ts +36 -4
- package/utils/RawQueryFragment.js +34 -13
- package/utils/TransactionManager.d.ts +65 -0
- package/utils/TransactionManager.js +223 -0
- package/utils/Utils.d.ts +18 -85
- package/utils/Utils.js +128 -252
- package/utils/index.d.ts +1 -0
- package/utils/index.js +1 -0
- package/utils/upsert-utils.d.ts +7 -2
- package/utils/upsert-utils.js +52 -1
- 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/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,11 @@
|
|
|
1
1
|
import { createRequire } from 'node:module';
|
|
2
|
-
import globby from 'globby';
|
|
3
2
|
import { extname, isAbsolute, join, normalize, relative, resolve } from 'node:path';
|
|
4
|
-
import { platform } from 'node:os';
|
|
5
3
|
import { fileURLToPath, pathToFileURL } from 'node:url';
|
|
6
|
-
import { existsSync, mkdirSync, readFileSync } from 'node:fs';
|
|
4
|
+
import { existsSync, globSync, statSync, mkdirSync, readFileSync } from 'node:fs';
|
|
7
5
|
import { createHash } from 'node:crypto';
|
|
8
|
-
import { tokenize } from 'esprima';
|
|
9
6
|
import { clone } from './clone.js';
|
|
10
|
-
import {
|
|
7
|
+
import { GroupOperator, PlainObject, QueryOperator, ReferenceKind } from '../enums.js';
|
|
11
8
|
import { helper } from '../entity/wrap.js';
|
|
12
|
-
export const ObjectBindingPattern = Symbol('ObjectBindingPattern');
|
|
13
9
|
function compareConstructors(a, b) {
|
|
14
10
|
if (a.constructor === b.constructor) {
|
|
15
11
|
return true;
|
|
@@ -134,50 +130,12 @@ export class Utils {
|
|
|
134
130
|
static PK_SEPARATOR = '~~~';
|
|
135
131
|
/* v8 ignore next */
|
|
136
132
|
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
133
|
/**
|
|
144
134
|
* Checks if the argument is instance of `Object`. Returns false for arrays.
|
|
145
135
|
*/
|
|
146
136
|
static isObject(o) {
|
|
147
137
|
return !!o && typeof o === 'object' && !Array.isArray(o);
|
|
148
138
|
}
|
|
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
139
|
/**
|
|
182
140
|
* Removes `undefined` properties (recursively) so they are not saved as nulls
|
|
183
141
|
*/
|
|
@@ -225,18 +183,6 @@ export class Utils {
|
|
|
225
183
|
}
|
|
226
184
|
return false;
|
|
227
185
|
}
|
|
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
186
|
/**
|
|
241
187
|
* Checks if arguments are deeply (but not strictly) equal.
|
|
242
188
|
*/
|
|
@@ -295,30 +241,6 @@ export class Utils {
|
|
|
295
241
|
}
|
|
296
242
|
return Utils._merge(target, sources, ignoreUndefined);
|
|
297
243
|
}
|
|
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
244
|
/**
|
|
323
245
|
* Creates deep copy of given object.
|
|
324
246
|
*/
|
|
@@ -359,53 +281,37 @@ export class Utils {
|
|
|
359
281
|
}
|
|
360
282
|
}
|
|
361
283
|
/**
|
|
362
|
-
* Returns array of functions argument names. Uses
|
|
284
|
+
* Returns array of functions argument names. Uses basic regex for source code analysis, might not work with advanced syntax.
|
|
363
285
|
*/
|
|
364
|
-
static
|
|
365
|
-
|
|
366
|
-
|
|
367
|
-
|
|
368
|
-
|
|
369
|
-
try {
|
|
370
|
-
return tokenize(func.toString(), { tolerant: true });
|
|
286
|
+
static getConstructorParams(func) {
|
|
287
|
+
const source = func.toString();
|
|
288
|
+
const i = source.indexOf('constructor');
|
|
289
|
+
if (i === -1) {
|
|
290
|
+
return undefined;
|
|
371
291
|
}
|
|
372
|
-
|
|
373
|
-
|
|
292
|
+
const start = source.indexOf('(', i);
|
|
293
|
+
if (start === -1) {
|
|
294
|
+
return undefined;
|
|
374
295
|
}
|
|
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;
|
|
296
|
+
let depth = 0;
|
|
297
|
+
let end = start;
|
|
298
|
+
for (; end < source.length; end++) {
|
|
299
|
+
if (source[end] === '(') {
|
|
300
|
+
depth++;
|
|
390
301
|
}
|
|
391
|
-
if (
|
|
392
|
-
|
|
393
|
-
currentBlockStart = i;
|
|
394
|
-
continue;
|
|
302
|
+
if (source[end] === ')') {
|
|
303
|
+
depth--;
|
|
395
304
|
}
|
|
396
|
-
if (
|
|
305
|
+
if (depth === 0) {
|
|
397
306
|
break;
|
|
398
307
|
}
|
|
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
308
|
}
|
|
408
|
-
|
|
309
|
+
const raw = source.slice(start + 1, end);
|
|
310
|
+
return raw
|
|
311
|
+
.split(',')
|
|
312
|
+
.map(s => s.trim().replace(/=.*$/, '').trim())
|
|
313
|
+
.filter(Boolean)
|
|
314
|
+
.map(raw => raw.startsWith('{') && raw.endsWith('}') ? '' : raw);
|
|
409
315
|
}
|
|
410
316
|
/**
|
|
411
317
|
* Checks whether the argument looks like primary key (string, number or ObjectId).
|
|
@@ -418,7 +324,7 @@ export class Utils {
|
|
|
418
324
|
return true;
|
|
419
325
|
}
|
|
420
326
|
if (Utils.isObject(key)) {
|
|
421
|
-
if (key.constructor
|
|
327
|
+
if (key.constructor?.name === 'ObjectId') {
|
|
422
328
|
return true;
|
|
423
329
|
}
|
|
424
330
|
if (!Utils.isPlainObject(key) && !Utils.isEntity(key, true)) {
|
|
@@ -435,7 +341,11 @@ export class Utils {
|
|
|
435
341
|
return data;
|
|
436
342
|
}
|
|
437
343
|
if (Utils.isEntity(data, true)) {
|
|
438
|
-
|
|
344
|
+
const wrapped = helper(data);
|
|
345
|
+
if (wrapped.__meta.compositePK) {
|
|
346
|
+
return wrapped.getPrimaryKeys();
|
|
347
|
+
}
|
|
348
|
+
return wrapped.getPrimaryKey();
|
|
439
349
|
}
|
|
440
350
|
if (strict && meta && Utils.getObjectKeysSize(data) !== meta.primaryKeys.length) {
|
|
441
351
|
return null;
|
|
@@ -444,7 +354,7 @@ export class Utils {
|
|
|
444
354
|
if (meta.compositePK) {
|
|
445
355
|
return this.getCompositeKeyValue(data, meta);
|
|
446
356
|
}
|
|
447
|
-
return data[meta.primaryKeys[0]]
|
|
357
|
+
return data[meta.primaryKeys[0]] ?? data[meta.serializedPrimaryKey] ?? null;
|
|
448
358
|
}
|
|
449
359
|
return null;
|
|
450
360
|
}
|
|
@@ -483,7 +393,7 @@ export class Utils {
|
|
|
483
393
|
static splitPrimaryKeys(key) {
|
|
484
394
|
return key.split(this.PK_SEPARATOR);
|
|
485
395
|
}
|
|
486
|
-
static getPrimaryKeyValues(entity,
|
|
396
|
+
static getPrimaryKeyValues(entity, meta, allowScalar = false, convertCustomTypes = false) {
|
|
487
397
|
/* v8 ignore next 3 */
|
|
488
398
|
if (entity == null) {
|
|
489
399
|
return entity;
|
|
@@ -494,15 +404,28 @@ export class Utils {
|
|
|
494
404
|
}
|
|
495
405
|
return val;
|
|
496
406
|
}
|
|
497
|
-
|
|
498
|
-
|
|
499
|
-
|
|
500
|
-
|
|
407
|
+
let pk;
|
|
408
|
+
if (Utils.isEntity(entity, true)) {
|
|
409
|
+
pk = helper(entity).getPrimaryKey(convertCustomTypes);
|
|
410
|
+
}
|
|
411
|
+
else {
|
|
412
|
+
pk = meta.primaryKeys.reduce((o, pk) => {
|
|
413
|
+
const targetMeta = meta.properties[pk].targetMeta;
|
|
414
|
+
if (targetMeta && Utils.isPlainObject(entity[pk])) {
|
|
415
|
+
o[pk] = Utils.getPrimaryKeyValues(entity[pk], targetMeta, allowScalar, convertCustomTypes);
|
|
416
|
+
}
|
|
417
|
+
else {
|
|
418
|
+
o[pk] = entity[pk];
|
|
419
|
+
}
|
|
420
|
+
return o;
|
|
421
|
+
}, {});
|
|
422
|
+
}
|
|
423
|
+
if (meta.primaryKeys.length > 1) {
|
|
501
424
|
return toArray(pk);
|
|
502
425
|
}
|
|
503
426
|
if (allowScalar) {
|
|
504
427
|
if (Utils.isPlainObject(pk)) {
|
|
505
|
-
return pk[primaryKeys[0]];
|
|
428
|
+
return pk[(meta.primaryKeys)[0]];
|
|
506
429
|
}
|
|
507
430
|
return pk;
|
|
508
431
|
}
|
|
@@ -546,7 +469,7 @@ export class Utils {
|
|
|
546
469
|
return o;
|
|
547
470
|
}, {});
|
|
548
471
|
}
|
|
549
|
-
static getOrderedPrimaryKeys(id, meta, platform, convertCustomTypes = false) {
|
|
472
|
+
static getOrderedPrimaryKeys(id, meta, platform, convertCustomTypes = false, allowScalar = false) {
|
|
550
473
|
const data = (Utils.isPrimaryKey(id) ? { [meta.primaryKeys[0]]: id } : id);
|
|
551
474
|
const pks = meta.primaryKeys.map((pk, idx) => {
|
|
552
475
|
const prop = meta.properties[pk];
|
|
@@ -556,11 +479,14 @@ export class Utils {
|
|
|
556
479
|
value = prop.customType.convertToJSValue(value, platform);
|
|
557
480
|
}
|
|
558
481
|
if (prop.kind !== ReferenceKind.SCALAR && prop.targetMeta) {
|
|
559
|
-
const value2 = this.getOrderedPrimaryKeys(value, prop.targetMeta, platform, convertCustomTypes);
|
|
482
|
+
const value2 = this.getOrderedPrimaryKeys(value, prop.targetMeta, platform, convertCustomTypes, allowScalar);
|
|
560
483
|
value = value2.length > 1 ? value2 : value2[0];
|
|
561
484
|
}
|
|
562
485
|
return value;
|
|
563
486
|
});
|
|
487
|
+
if (allowScalar && pks.length === 1) {
|
|
488
|
+
return pks[0];
|
|
489
|
+
}
|
|
564
490
|
// we need to flatten the PKs as composite PKs can be build from another composite PKs
|
|
565
491
|
// and this method is used to get the PK hash in identity map, that expects flat array
|
|
566
492
|
return Utils.flatten(pks);
|
|
@@ -583,12 +509,6 @@ export class Utils {
|
|
|
583
509
|
static isScalarReference(data, allowReference = false) {
|
|
584
510
|
return typeof data === 'object' && data?.__scalarReference;
|
|
585
511
|
}
|
|
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
512
|
/**
|
|
593
513
|
* Checks whether the argument is empty (array without items, object without keys or falsy value).
|
|
594
514
|
*/
|
|
@@ -625,44 +545,12 @@ export class Utils {
|
|
|
625
545
|
|| !!process.env.TS_JEST // check if ts-jest is used (works only with v27.0.4+)
|
|
626
546
|
|| !!process.env.VITEST // check if vitest is used
|
|
627
547
|
|| !!process.versions.bun // check if bun is used
|
|
628
|
-
|| process.argv.slice(1).some(arg => arg.
|
|
629
|
-
|| process.execArgv.some(arg =>
|
|
630
|
-
|
|
631
|
-
|
|
632
|
-
|
|
633
|
-
|
|
634
|
-
*/
|
|
635
|
-
static lookupPathFromDecorator(name, stack) {
|
|
636
|
-
// use some dark magic to get source path to caller
|
|
637
|
-
stack = stack || new Error().stack.split('\n');
|
|
638
|
-
// In some situations (e.g. swc 1.3.4+), the presence of a source map can obscure the call to
|
|
639
|
-
// __decorate(), replacing it with the constructor name. To support these cases we look for
|
|
640
|
-
// Reflect.decorate() as well. Also when babel is used, we need to check
|
|
641
|
-
// the `_applyDecoratedDescriptor` method instead.
|
|
642
|
-
let line = stack.findIndex(line => line.match(/__decorate|Reflect\.decorate|_applyDecoratedDescriptor/));
|
|
643
|
-
// bun does not have those lines at all, only the DecorateProperty/DecorateConstructor,
|
|
644
|
-
// but those are also present in node, so we need to check this only if they weren't found.
|
|
645
|
-
if (line === -1) {
|
|
646
|
-
// here we handle bun which stack is different from nodejs so we search for reflect-metadata
|
|
647
|
-
const reflectLine = stack.findIndex(line => Utils.normalizePath(line).includes('node_modules/reflect-metadata/Reflect.js'));
|
|
648
|
-
if (reflectLine === -1 || reflectLine + 2 >= stack.length || !stack[reflectLine + 1].includes('bun:wrap')) {
|
|
649
|
-
return name;
|
|
650
|
-
}
|
|
651
|
-
line = reflectLine + 2;
|
|
652
|
-
}
|
|
653
|
-
if (stack[line].includes('Reflect.decorate')) {
|
|
654
|
-
line++;
|
|
655
|
-
}
|
|
656
|
-
if (Utils.normalizePath(stack[line]).includes('node_modules/tslib/tslib')) {
|
|
657
|
-
line++;
|
|
658
|
-
}
|
|
659
|
-
try {
|
|
660
|
-
const re = stack[line].match(/\(.+\)/i) ? /\((.*):\d+:\d+\)/ : /at\s*(.*):\d+:\d+$/;
|
|
661
|
-
return Utils.normalizePath(stack[line].match(re)[1]);
|
|
662
|
-
}
|
|
663
|
-
catch {
|
|
664
|
-
return name;
|
|
665
|
-
}
|
|
548
|
+
|| process.argv.slice(1).some(arg => arg.match(/\.([mc]?ts|tsx)$/)) // executing `.ts` file
|
|
549
|
+
|| process.execArgv.some(arg => {
|
|
550
|
+
return arg.includes('ts-node') // check for ts-node loader
|
|
551
|
+
|| arg.includes('@swc-node/register') // check for swc-node/register loader
|
|
552
|
+
|| arg.includes('node_modules/tsx/'); // check for tsx loader
|
|
553
|
+
});
|
|
666
554
|
}
|
|
667
555
|
/**
|
|
668
556
|
* Gets the type of the argument.
|
|
@@ -673,11 +561,11 @@ export class Utils {
|
|
|
673
561
|
return simple;
|
|
674
562
|
}
|
|
675
563
|
const objectType = Object.prototype.toString.call(value);
|
|
676
|
-
const type = objectType.match(
|
|
564
|
+
const type = objectType.match(/^\[object (.+)]$/)[1];
|
|
677
565
|
if (type === 'Uint8Array') {
|
|
678
566
|
return 'Buffer';
|
|
679
567
|
}
|
|
680
|
-
return
|
|
568
|
+
return type;
|
|
681
569
|
}
|
|
682
570
|
/**
|
|
683
571
|
* Checks whether the value is POJO (e.g. `{ foo: 'bar' }`, and not instance of `Foo`)
|
|
@@ -761,8 +649,9 @@ export class Utils {
|
|
|
761
649
|
}
|
|
762
650
|
return Utils.normalizePath(path);
|
|
763
651
|
}
|
|
764
|
-
static hash(data, length) {
|
|
765
|
-
const
|
|
652
|
+
static hash(data, length, algorithm) {
|
|
653
|
+
const hashAlgorithm = algorithm || 'sha256';
|
|
654
|
+
const hash = createHash(hashAlgorithm).update(data).digest('hex');
|
|
766
655
|
if (length) {
|
|
767
656
|
return hash.substring(0, length);
|
|
768
657
|
}
|
|
@@ -794,12 +683,32 @@ export class Utils {
|
|
|
794
683
|
static randomInt(min, max) {
|
|
795
684
|
return Math.round(Math.random() * (max - min)) + min;
|
|
796
685
|
}
|
|
797
|
-
static
|
|
798
|
-
if (
|
|
799
|
-
|
|
686
|
+
static glob(input, cwd) {
|
|
687
|
+
if (Array.isArray(input)) {
|
|
688
|
+
return input.flatMap(paths => this.glob(paths, cwd));
|
|
689
|
+
}
|
|
690
|
+
const hasGlobChars = /[*?[\]]/.test(input);
|
|
691
|
+
if (!hasGlobChars) {
|
|
692
|
+
try {
|
|
693
|
+
const s = statSync(cwd ? Utils.normalizePath(cwd, input) : input);
|
|
694
|
+
if (s.isDirectory()) {
|
|
695
|
+
const files = globSync(join(input, '**'), { cwd, withFileTypes: true });
|
|
696
|
+
return files.filter(f => f.isFile()).map(f => join(f.parentPath, f.name));
|
|
697
|
+
}
|
|
698
|
+
}
|
|
699
|
+
catch {
|
|
700
|
+
// ignore
|
|
701
|
+
}
|
|
702
|
+
}
|
|
703
|
+
const files = globSync(input, { cwd, withFileTypes: true });
|
|
704
|
+
return files.filter(f => f.isFile()).map(f => join(f.parentPath, f.name));
|
|
705
|
+
}
|
|
706
|
+
static pathExists(path) {
|
|
707
|
+
if (/[*?[\]]/.test(path)) {
|
|
708
|
+
const found = globSync(path);
|
|
800
709
|
return found.length > 0;
|
|
801
710
|
}
|
|
802
|
-
return
|
|
711
|
+
return existsSync(path);
|
|
803
712
|
}
|
|
804
713
|
/**
|
|
805
714
|
* Extracts all possible values of a TS enum. Works with both string and numeric enums.
|
|
@@ -826,15 +735,6 @@ export class Utils {
|
|
|
826
735
|
}
|
|
827
736
|
return key in GroupOperator || key in QueryOperator;
|
|
828
737
|
}
|
|
829
|
-
static isGroupOperator(key) {
|
|
830
|
-
return key in GroupOperator;
|
|
831
|
-
}
|
|
832
|
-
static isArrayOperator(key) {
|
|
833
|
-
return ARRAY_OPERATORS.includes(key);
|
|
834
|
-
}
|
|
835
|
-
static isJsonKeyOperator(key) {
|
|
836
|
-
return JSON_KEY_OPERATORS.includes(key);
|
|
837
|
-
}
|
|
838
738
|
static hasNestedKey(object, key) {
|
|
839
739
|
if (!object) {
|
|
840
740
|
return false;
|
|
@@ -847,11 +747,6 @@ export class Utils {
|
|
|
847
747
|
}
|
|
848
748
|
return false;
|
|
849
749
|
}
|
|
850
|
-
static getGlobalStorage(namespace) {
|
|
851
|
-
const key = `mikro-orm-${namespace}`;
|
|
852
|
-
globalThis[key] = globalThis[key] || {};
|
|
853
|
-
return globalThis[key];
|
|
854
|
-
}
|
|
855
750
|
/**
|
|
856
751
|
* Require a module from a specific location
|
|
857
752
|
* @param id The module to require
|
|
@@ -863,31 +758,31 @@ export class Utils {
|
|
|
863
758
|
}
|
|
864
759
|
return createRequire(resolve(from))(id);
|
|
865
760
|
}
|
|
866
|
-
|
|
867
|
-
|
|
868
|
-
|
|
869
|
-
|
|
870
|
-
|
|
871
|
-
|
|
872
|
-
|
|
873
|
-
|
|
874
|
-
}
|
|
761
|
+
/**
|
|
762
|
+
* Resolve path to a module.
|
|
763
|
+
* @param id The module to require
|
|
764
|
+
* @param [from] Location to start the node resolution
|
|
765
|
+
*/
|
|
766
|
+
static resolveModulePath(id, from = process.cwd()) {
|
|
767
|
+
if (!extname(from)) {
|
|
768
|
+
from = join(from, '__fake.js');
|
|
875
769
|
}
|
|
876
|
-
|
|
877
|
-
|
|
770
|
+
const path = Utils.normalizePath(createRequire(resolve(from)).resolve(id));
|
|
771
|
+
const parts = path.split('/');
|
|
772
|
+
const idx = parts.lastIndexOf(id) + 1;
|
|
773
|
+
parts.splice(idx, parts.length - idx);
|
|
774
|
+
return parts.join('/');
|
|
878
775
|
}
|
|
879
|
-
|
|
880
|
-
|
|
881
|
-
|
|
776
|
+
static async dynamicImport(id) {
|
|
777
|
+
/* v8 ignore next */
|
|
778
|
+
const specifier = id.startsWith('file://') ? id : pathToFileURL(id).href;
|
|
779
|
+
return this.dynamicImportProvider(specifier);
|
|
882
780
|
}
|
|
883
781
|
static ensureDir(path) {
|
|
884
782
|
if (!existsSync(path)) {
|
|
885
783
|
mkdirSync(path, { recursive: true });
|
|
886
784
|
}
|
|
887
785
|
}
|
|
888
|
-
static pathExistsSync(path) {
|
|
889
|
-
return existsSync(path);
|
|
890
|
-
}
|
|
891
786
|
static readJSONSync(path) {
|
|
892
787
|
const file = readFileSync(path);
|
|
893
788
|
return JSON.parse(file.toString());
|
|
@@ -899,8 +794,13 @@ export class Utils {
|
|
|
899
794
|
/* v8 ignore next 5 */
|
|
900
795
|
}
|
|
901
796
|
catch {
|
|
902
|
-
|
|
903
|
-
|
|
797
|
+
try {
|
|
798
|
+
// this works in production build where we do not have the `src` folder
|
|
799
|
+
return this.requireFrom('../package.json', import.meta.dirname).version;
|
|
800
|
+
}
|
|
801
|
+
catch {
|
|
802
|
+
return 'N/A';
|
|
803
|
+
}
|
|
904
804
|
}
|
|
905
805
|
}
|
|
906
806
|
static createFunction(context, code) {
|
|
@@ -940,14 +840,6 @@ export class Utils {
|
|
|
940
840
|
throw e;
|
|
941
841
|
}
|
|
942
842
|
}
|
|
943
|
-
/**
|
|
944
|
-
* @see https://github.com/mikro-orm/mikro-orm/issues/840
|
|
945
|
-
*/
|
|
946
|
-
static propertyDecoratorReturnValue() {
|
|
947
|
-
if (process.env.BABEL_DECORATORS_COMPAT) {
|
|
948
|
-
return {};
|
|
949
|
-
}
|
|
950
|
-
}
|
|
951
843
|
static unwrapProperty(entity, meta, prop, payload = false) {
|
|
952
844
|
let p = prop;
|
|
953
845
|
const path = [];
|
|
@@ -1035,8 +927,10 @@ export class Utils {
|
|
|
1035
927
|
}
|
|
1036
928
|
catch (err) {
|
|
1037
929
|
if (err.message.includes(allowError)) {
|
|
1038
|
-
|
|
1039
|
-
|
|
930
|
+
if (warning) {
|
|
931
|
+
// eslint-disable-next-line no-console
|
|
932
|
+
console.warn(warning);
|
|
933
|
+
}
|
|
1040
934
|
return undefined;
|
|
1041
935
|
}
|
|
1042
936
|
throw err;
|
|
@@ -1044,36 +938,18 @@ export class Utils {
|
|
|
1044
938
|
}
|
|
1045
939
|
static async tryImport({ module, warning }) {
|
|
1046
940
|
try {
|
|
1047
|
-
return await
|
|
941
|
+
return await import(module);
|
|
1048
942
|
}
|
|
1049
943
|
catch (err) {
|
|
1050
|
-
|
|
1051
|
-
|
|
1052
|
-
|
|
1053
|
-
|
|
1054
|
-
|
|
1055
|
-
|
|
1056
|
-
return str.replace(/^(?:\.\.\/|\.\/)+/, '/');
|
|
1057
|
-
}
|
|
1058
|
-
/**
|
|
1059
|
-
* simple process.argv parser, supports only properties with long names, prefixed with `--`
|
|
1060
|
-
*/
|
|
1061
|
-
static parseArgs() {
|
|
1062
|
-
let lastKey;
|
|
1063
|
-
return process.argv.slice(2).reduce((args, arg) => {
|
|
1064
|
-
if (arg.includes('=')) {
|
|
1065
|
-
const [key, value] = arg.split('=');
|
|
1066
|
-
args[key.substring(2)] = value;
|
|
1067
|
-
}
|
|
1068
|
-
else if (lastKey) {
|
|
1069
|
-
args[lastKey] = arg;
|
|
1070
|
-
lastKey = undefined;
|
|
1071
|
-
}
|
|
1072
|
-
else if (arg.startsWith('--')) {
|
|
1073
|
-
lastKey = arg.substring(2);
|
|
944
|
+
if (err.code === 'ERR_MODULE_NOT_FOUND') {
|
|
945
|
+
if (warning) {
|
|
946
|
+
// eslint-disable-next-line no-console
|
|
947
|
+
console.warn(warning);
|
|
948
|
+
}
|
|
949
|
+
return undefined;
|
|
1074
950
|
}
|
|
1075
|
-
|
|
1076
|
-
}
|
|
951
|
+
throw err;
|
|
952
|
+
}
|
|
1077
953
|
}
|
|
1078
954
|
static xor(a, b) {
|
|
1079
955
|
return (a || b) && !(a && b);
|
package/utils/index.d.ts
CHANGED
|
@@ -5,6 +5,7 @@ export * from './DataloaderUtils.js';
|
|
|
5
5
|
export * from './Utils.js';
|
|
6
6
|
export * from './RequestContext.js';
|
|
7
7
|
export * from './TransactionContext.js';
|
|
8
|
+
export * from './TransactionManager.js';
|
|
8
9
|
export * from './QueryHelper.js';
|
|
9
10
|
export * from './NullHighlighter.js';
|
|
10
11
|
export * from './EntityComparator.js';
|
package/utils/index.js
CHANGED
|
@@ -5,6 +5,7 @@ export * from './DataloaderUtils.js';
|
|
|
5
5
|
export * from './Utils.js';
|
|
6
6
|
export * from './RequestContext.js';
|
|
7
7
|
export * from './TransactionContext.js';
|
|
8
|
+
export * from './TransactionManager.js';
|
|
8
9
|
export * from './QueryHelper.js';
|
|
9
10
|
export * from './NullHighlighter.js';
|
|
10
11
|
export * from './EntityComparator.js';
|
package/utils/upsert-utils.d.ts
CHANGED
|
@@ -1,7 +1,12 @@
|
|
|
1
|
-
import type { EntityData, EntityMetadata } from '../typings.js';
|
|
1
|
+
import type { EntityData, EntityMetadata, FilterQuery } from '../typings.js';
|
|
2
2
|
import type { UpsertOptions } from '../drivers/IDatabaseDriver.js';
|
|
3
|
-
import type
|
|
3
|
+
import { type RawQueryFragment } from '../utils/RawQueryFragment.js';
|
|
4
4
|
/** @internal */
|
|
5
5
|
export declare function getOnConflictFields<T>(meta: EntityMetadata<T> | undefined, data: EntityData<T>, uniqueFields: (keyof T)[] | RawQueryFragment, options: UpsertOptions<T>): (keyof T)[];
|
|
6
6
|
/** @internal */
|
|
7
7
|
export declare function getOnConflictReturningFields<T, P extends string>(meta: EntityMetadata<T> | undefined, data: EntityData<T>, uniqueFields: (keyof T)[] | RawQueryFragment, options: UpsertOptions<T, P>): (keyof T)[] | '*';
|
|
8
|
+
/** @internal */
|
|
9
|
+
export declare function getWhereCondition<T extends object>(meta: EntityMetadata<T>, onConflictFields: (keyof T)[] | RawQueryFragment | undefined, data: EntityData<T>, where: FilterQuery<T>): {
|
|
10
|
+
where: FilterQuery<T>;
|
|
11
|
+
propIndex: number | false;
|
|
12
|
+
};
|