@mikro-orm/core 7.0.0-dev.8 → 7.0.0-dev.81

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.
Files changed (181) hide show
  1. package/EntityManager.d.ts +85 -48
  2. package/EntityManager.js +300 -225
  3. package/MikroORM.d.ts +40 -31
  4. package/MikroORM.js +98 -137
  5. package/README.md +3 -2
  6. package/cache/FileCacheAdapter.d.ts +1 -1
  7. package/cache/FileCacheAdapter.js +6 -5
  8. package/cache/GeneratedCacheAdapter.d.ts +0 -1
  9. package/cache/GeneratedCacheAdapter.js +0 -2
  10. package/cache/index.d.ts +0 -1
  11. package/cache/index.js +0 -1
  12. package/connections/Connection.d.ts +11 -7
  13. package/connections/Connection.js +16 -14
  14. package/drivers/DatabaseDriver.d.ts +11 -5
  15. package/drivers/DatabaseDriver.js +23 -11
  16. package/drivers/IDatabaseDriver.d.ts +27 -5
  17. package/entity/BaseEntity.d.ts +0 -1
  18. package/entity/BaseEntity.js +0 -3
  19. package/entity/Collection.d.ts +98 -30
  20. package/entity/Collection.js +432 -93
  21. package/entity/EntityAssigner.d.ts +1 -1
  22. package/entity/EntityAssigner.js +15 -7
  23. package/entity/EntityFactory.d.ts +7 -0
  24. package/entity/EntityFactory.js +64 -41
  25. package/entity/EntityHelper.js +26 -9
  26. package/entity/EntityLoader.d.ts +5 -4
  27. package/entity/EntityLoader.js +73 -40
  28. package/entity/EntityRepository.d.ts +1 -1
  29. package/entity/Reference.d.ts +9 -7
  30. package/entity/Reference.js +33 -6
  31. package/entity/WrappedEntity.d.ts +2 -4
  32. package/entity/WrappedEntity.js +1 -5
  33. package/entity/defineEntity.d.ts +549 -0
  34. package/entity/defineEntity.js +529 -0
  35. package/entity/index.d.ts +3 -2
  36. package/entity/index.js +3 -2
  37. package/entity/utils.d.ts +7 -0
  38. package/entity/utils.js +16 -4
  39. package/entity/validators.d.ts +11 -0
  40. package/entity/validators.js +65 -0
  41. package/enums.d.ts +21 -6
  42. package/enums.js +14 -1
  43. package/errors.d.ts +6 -2
  44. package/errors.js +14 -9
  45. package/events/EventSubscriber.d.ts +3 -1
  46. package/hydration/Hydrator.js +1 -2
  47. package/hydration/ObjectHydrator.d.ts +4 -4
  48. package/hydration/ObjectHydrator.js +36 -25
  49. package/index.d.ts +2 -2
  50. package/index.js +1 -2
  51. package/logging/DefaultLogger.d.ts +1 -1
  52. package/logging/SimpleLogger.d.ts +1 -1
  53. package/metadata/EntitySchema.d.ts +9 -13
  54. package/metadata/EntitySchema.js +44 -26
  55. package/metadata/MetadataDiscovery.d.ts +6 -9
  56. package/metadata/MetadataDiscovery.js +167 -206
  57. package/metadata/MetadataProvider.d.ts +11 -2
  58. package/metadata/MetadataProvider.js +44 -2
  59. package/metadata/MetadataStorage.d.ts +1 -6
  60. package/metadata/MetadataStorage.js +6 -18
  61. package/metadata/MetadataValidator.d.ts +0 -7
  62. package/metadata/MetadataValidator.js +4 -13
  63. package/metadata/discover-entities.d.ts +5 -0
  64. package/metadata/discover-entities.js +40 -0
  65. package/metadata/index.d.ts +1 -1
  66. package/metadata/index.js +1 -1
  67. package/metadata/types.d.ts +480 -0
  68. package/metadata/types.js +1 -0
  69. package/naming-strategy/AbstractNamingStrategy.d.ts +5 -1
  70. package/naming-strategy/AbstractNamingStrategy.js +8 -2
  71. package/naming-strategy/NamingStrategy.d.ts +11 -1
  72. package/not-supported.d.ts +2 -0
  73. package/not-supported.js +4 -0
  74. package/package.json +18 -10
  75. package/platforms/ExceptionConverter.js +1 -1
  76. package/platforms/Platform.d.ts +6 -10
  77. package/platforms/Platform.js +14 -39
  78. package/serialization/EntitySerializer.d.ts +2 -0
  79. package/serialization/EntitySerializer.js +32 -14
  80. package/serialization/EntityTransformer.js +22 -12
  81. package/serialization/SerializationContext.js +16 -13
  82. package/types/ArrayType.d.ts +1 -1
  83. package/types/ArrayType.js +2 -3
  84. package/types/BigIntType.d.ts +8 -6
  85. package/types/BigIntType.js +1 -1
  86. package/types/BlobType.d.ts +0 -1
  87. package/types/BlobType.js +0 -3
  88. package/types/BooleanType.d.ts +2 -1
  89. package/types/BooleanType.js +3 -0
  90. package/types/DecimalType.d.ts +6 -4
  91. package/types/DecimalType.js +3 -3
  92. package/types/DoubleType.js +2 -2
  93. package/types/JsonType.d.ts +1 -1
  94. package/types/JsonType.js +7 -2
  95. package/types/TinyIntType.js +1 -1
  96. package/types/Type.d.ts +2 -1
  97. package/types/Type.js +1 -1
  98. package/types/Uint8ArrayType.d.ts +0 -1
  99. package/types/Uint8ArrayType.js +1 -4
  100. package/types/index.d.ts +1 -1
  101. package/typings.d.ts +113 -77
  102. package/typings.js +41 -35
  103. package/unit-of-work/ChangeSetComputer.d.ts +1 -3
  104. package/unit-of-work/ChangeSetComputer.js +11 -9
  105. package/unit-of-work/ChangeSetPersister.d.ts +5 -4
  106. package/unit-of-work/ChangeSetPersister.js +58 -20
  107. package/unit-of-work/UnitOfWork.d.ts +8 -1
  108. package/unit-of-work/UnitOfWork.js +115 -57
  109. package/utils/AbstractSchemaGenerator.d.ts +5 -5
  110. package/utils/AbstractSchemaGenerator.js +11 -9
  111. package/utils/Configuration.d.ts +757 -206
  112. package/utils/Configuration.js +139 -187
  113. package/utils/ConfigurationLoader.d.ts +1 -54
  114. package/utils/ConfigurationLoader.js +1 -352
  115. package/utils/Cursor.d.ts +3 -3
  116. package/utils/Cursor.js +4 -1
  117. package/utils/DataloaderUtils.d.ts +15 -5
  118. package/utils/DataloaderUtils.js +54 -8
  119. package/utils/EntityComparator.d.ts +8 -4
  120. package/utils/EntityComparator.js +111 -64
  121. package/utils/QueryHelper.d.ts +9 -1
  122. package/utils/QueryHelper.js +70 -9
  123. package/utils/RawQueryFragment.d.ts +36 -4
  124. package/utils/RawQueryFragment.js +35 -14
  125. package/utils/TransactionManager.d.ts +65 -0
  126. package/utils/TransactionManager.js +223 -0
  127. package/utils/Utils.d.ts +8 -97
  128. package/utils/Utils.js +88 -303
  129. package/utils/clone.js +2 -3
  130. package/utils/env-vars.d.ts +3 -0
  131. package/utils/env-vars.js +87 -0
  132. package/utils/fs-utils.d.ts +12 -0
  133. package/utils/fs-utils.js +96 -0
  134. package/utils/index.d.ts +2 -1
  135. package/utils/index.js +2 -1
  136. package/utils/upsert-utils.d.ts +7 -2
  137. package/utils/upsert-utils.js +55 -4
  138. package/decorators/Check.d.ts +0 -3
  139. package/decorators/Check.js +0 -13
  140. package/decorators/CreateRequestContext.d.ts +0 -3
  141. package/decorators/CreateRequestContext.js +0 -32
  142. package/decorators/Embeddable.d.ts +0 -8
  143. package/decorators/Embeddable.js +0 -11
  144. package/decorators/Embedded.d.ts +0 -18
  145. package/decorators/Embedded.js +0 -18
  146. package/decorators/Entity.d.ts +0 -18
  147. package/decorators/Entity.js +0 -12
  148. package/decorators/Enum.d.ts +0 -9
  149. package/decorators/Enum.js +0 -16
  150. package/decorators/Filter.d.ts +0 -2
  151. package/decorators/Filter.js +0 -8
  152. package/decorators/Formula.d.ts +0 -4
  153. package/decorators/Formula.js +0 -15
  154. package/decorators/Indexed.d.ts +0 -19
  155. package/decorators/Indexed.js +0 -20
  156. package/decorators/ManyToMany.d.ts +0 -40
  157. package/decorators/ManyToMany.js +0 -14
  158. package/decorators/ManyToOne.d.ts +0 -30
  159. package/decorators/ManyToOne.js +0 -14
  160. package/decorators/OneToMany.d.ts +0 -28
  161. package/decorators/OneToMany.js +0 -17
  162. package/decorators/OneToOne.d.ts +0 -24
  163. package/decorators/OneToOne.js +0 -7
  164. package/decorators/PrimaryKey.d.ts +0 -8
  165. package/decorators/PrimaryKey.js +0 -20
  166. package/decorators/Property.d.ts +0 -250
  167. package/decorators/Property.js +0 -32
  168. package/decorators/Transactional.d.ts +0 -13
  169. package/decorators/Transactional.js +0 -28
  170. package/decorators/hooks.d.ts +0 -16
  171. package/decorators/hooks.js +0 -47
  172. package/decorators/index.d.ts +0 -17
  173. package/decorators/index.js +0 -17
  174. package/entity/ArrayCollection.d.ts +0 -116
  175. package/entity/ArrayCollection.js +0 -402
  176. package/entity/EntityValidator.d.ts +0 -19
  177. package/entity/EntityValidator.js +0 -150
  178. package/metadata/ReflectMetadataProvider.d.ts +0 -8
  179. package/metadata/ReflectMetadataProvider.js +0 -44
  180. package/utils/resolveContextProvider.d.ts +0 -10
  181. package/utils/resolveContextProvider.js +0 -28
package/utils/Utils.js CHANGED
@@ -1,15 +1,8 @@
1
- import { createRequire } from 'node:module';
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 { ARRAY_OPERATORS, JSON_KEY_OPERATORS, GroupOperator, PlainObject, QueryOperator, ReferenceKind } from '../enums.js';
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 9 */
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 6 */
115
+ /* v8 ignore next */
123
116
  try {
124
117
  return JSON.parse(value);
125
118
  }
@@ -134,50 +127,12 @@ export class Utils {
134
127
  static PK_SEPARATOR = '~~~';
135
128
  /* v8 ignore next */
136
129
  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
130
  /**
144
131
  * Checks if the argument is instance of `Object`. Returns false for arrays.
145
132
  */
146
133
  static isObject(o) {
147
134
  return !!o && typeof o === 'object' && !Array.isArray(o);
148
135
  }
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
136
  /**
182
137
  * Removes `undefined` properties (recursively) so they are not saved as nulls
183
138
  */
@@ -225,18 +180,6 @@ export class Utils {
225
180
  }
226
181
  return false;
227
182
  }
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
183
  /**
241
184
  * Checks if arguments are deeply (but not strictly) equal.
242
185
  */
@@ -282,7 +225,7 @@ export class Utils {
282
225
  target[key] = Utils.copy(value);
283
226
  continue;
284
227
  }
285
- /* v8 ignore next 3 */
228
+ /* v8 ignore next */
286
229
  if (!(key in target)) {
287
230
  Object.assign(target, { [key]: {} });
288
231
  }
@@ -295,30 +238,6 @@ export class Utils {
295
238
  }
296
239
  return Utils._merge(target, sources, ignoreUndefined);
297
240
  }
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
241
  /**
323
242
  * Creates deep copy of given object.
324
243
  */
@@ -359,53 +278,37 @@ export class Utils {
359
278
  }
360
279
  }
361
280
  /**
362
- * Returns array of functions argument names. Uses `esprima` for source code analysis.
281
+ * Returns array of functions argument names. Uses basic regex for source code analysis, might not work with advanced syntax.
363
282
  */
364
- static tokenize(func) {
365
- if (Array.isArray(func)) {
366
- return func;
367
- }
368
- /* v8 ignore next 5 */
369
- try {
370
- return tokenize(func.toString(), { tolerant: true });
283
+ static getConstructorParams(func) {
284
+ const source = func.toString();
285
+ const i = source.indexOf('constructor');
286
+ if (i === -1) {
287
+ return undefined;
371
288
  }
372
- catch {
373
- return [];
289
+ const start = source.indexOf('(', i);
290
+ if (start === -1) {
291
+ return undefined;
374
292
  }
375
- }
376
- /**
377
- * Returns array of functions argument names. Uses `esprima` for source code analysis.
378
- */
379
- static getParamNames(func, methodName) {
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;
293
+ let depth = 0;
294
+ let end = start;
295
+ for (; end < source.length; end++) {
296
+ if (source[end] === '(') {
297
+ depth++;
390
298
  }
391
- if (inside === 1 && token.type === 'Punctuator' && token.value === '(') {
392
- inside = 2;
393
- currentBlockStart = i;
394
- continue;
299
+ if (source[end] === ')') {
300
+ depth--;
395
301
  }
396
- if (inside === 2 && token.type === 'Punctuator' && token.value === ')') {
302
+ if (depth === 0) {
397
303
  break;
398
304
  }
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
305
  }
408
- return ret;
306
+ const raw = source.slice(start + 1, end);
307
+ return raw
308
+ .split(',')
309
+ .map(s => s.trim().replace(/=.*$/, '').trim())
310
+ .filter(Boolean)
311
+ .map(raw => raw.startsWith('{') && raw.endsWith('}') ? '' : raw);
409
312
  }
410
313
  /**
411
314
  * Checks whether the argument looks like primary key (string, number or ObjectId).
@@ -418,7 +321,7 @@ export class Utils {
418
321
  return true;
419
322
  }
420
323
  if (Utils.isObject(key)) {
421
- if (key.constructor && key.constructor.name.toLowerCase() === 'objectid') {
324
+ if (key.constructor?.name === 'ObjectId') {
422
325
  return true;
423
326
  }
424
327
  if (!Utils.isPlainObject(key) && !Utils.isEntity(key, true)) {
@@ -435,7 +338,11 @@ export class Utils {
435
338
  return data;
436
339
  }
437
340
  if (Utils.isEntity(data, true)) {
438
- return helper(data).getPrimaryKey();
341
+ const wrapped = helper(data);
342
+ if (wrapped.__meta.compositePK) {
343
+ return wrapped.getPrimaryKeys();
344
+ }
345
+ return wrapped.getPrimaryKey();
439
346
  }
440
347
  if (strict && meta && Utils.getObjectKeysSize(data) !== meta.primaryKeys.length) {
441
348
  return null;
@@ -444,7 +351,7 @@ export class Utils {
444
351
  if (meta.compositePK) {
445
352
  return this.getCompositeKeyValue(data, meta);
446
353
  }
447
- return data[meta.primaryKeys[0]] || data[meta.serializedPrimaryKey] || null;
354
+ return data[meta.primaryKeys[0]] ?? data[meta.serializedPrimaryKey] ?? null;
448
355
  }
449
356
  return null;
450
357
  }
@@ -483,8 +390,8 @@ export class Utils {
483
390
  static splitPrimaryKeys(key) {
484
391
  return key.split(this.PK_SEPARATOR);
485
392
  }
486
- static getPrimaryKeyValues(entity, primaryKeys, allowScalar = false, convertCustomTypes = false) {
487
- /* v8 ignore next 3 */
393
+ static getPrimaryKeyValues(entity, meta, allowScalar = false, convertCustomTypes = false) {
394
+ /* v8 ignore next */
488
395
  if (entity == null) {
489
396
  return entity;
490
397
  }
@@ -494,15 +401,28 @@ export class Utils {
494
401
  }
495
402
  return val;
496
403
  }
497
- const pk = Utils.isEntity(entity, true)
498
- ? helper(entity).getPrimaryKey(convertCustomTypes)
499
- : primaryKeys.reduce((o, pk) => { o[pk] = entity[pk]; return o; }, {});
500
- if (primaryKeys.length > 1) {
404
+ let pk;
405
+ if (Utils.isEntity(entity, true)) {
406
+ pk = helper(entity).getPrimaryKey(convertCustomTypes);
407
+ }
408
+ else {
409
+ pk = meta.primaryKeys.reduce((o, pk) => {
410
+ const targetMeta = meta.properties[pk].targetMeta;
411
+ if (targetMeta && Utils.isPlainObject(entity[pk])) {
412
+ o[pk] = Utils.getPrimaryKeyValues(entity[pk], targetMeta, allowScalar, convertCustomTypes);
413
+ }
414
+ else {
415
+ o[pk] = entity[pk];
416
+ }
417
+ return o;
418
+ }, {});
419
+ }
420
+ if (meta.primaryKeys.length > 1) {
501
421
  return toArray(pk);
502
422
  }
503
423
  if (allowScalar) {
504
424
  if (Utils.isPlainObject(pk)) {
505
- return pk[primaryKeys[0]];
425
+ return pk[(meta.primaryKeys)[0]];
506
426
  }
507
427
  return pk;
508
428
  }
@@ -546,7 +466,7 @@ export class Utils {
546
466
  return o;
547
467
  }, {});
548
468
  }
549
- static getOrderedPrimaryKeys(id, meta, platform, convertCustomTypes = false) {
469
+ static getOrderedPrimaryKeys(id, meta, platform, convertCustomTypes = false, allowScalar = false) {
550
470
  const data = (Utils.isPrimaryKey(id) ? { [meta.primaryKeys[0]]: id } : id);
551
471
  const pks = meta.primaryKeys.map((pk, idx) => {
552
472
  const prop = meta.properties[pk];
@@ -556,11 +476,14 @@ export class Utils {
556
476
  value = prop.customType.convertToJSValue(value, platform);
557
477
  }
558
478
  if (prop.kind !== ReferenceKind.SCALAR && prop.targetMeta) {
559
- const value2 = this.getOrderedPrimaryKeys(value, prop.targetMeta, platform, convertCustomTypes);
479
+ const value2 = this.getOrderedPrimaryKeys(value, prop.targetMeta, platform, convertCustomTypes, allowScalar);
560
480
  value = value2.length > 1 ? value2 : value2[0];
561
481
  }
562
482
  return value;
563
483
  });
484
+ if (allowScalar && pks.length === 1) {
485
+ return pks[0];
486
+ }
564
487
  // we need to flatten the PKs as composite PKs can be build from another composite PKs
565
488
  // and this method is used to get the PK hash in identity map, that expects flat array
566
489
  return Utils.flatten(pks);
@@ -583,12 +506,6 @@ export class Utils {
583
506
  static isScalarReference(data, allowReference = false) {
584
507
  return typeof data === 'object' && data?.__scalarReference;
585
508
  }
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
509
  /**
593
510
  * Checks whether the argument is empty (array without items, object without keys or falsy value).
594
511
  */
@@ -619,50 +536,18 @@ export class Utils {
619
536
  * Tries to detect TypeScript support.
620
537
  */
621
538
  static detectTypeScriptSupport() {
622
- /* v8 ignore next 7 */
539
+ /* v8 ignore next */
623
540
  return process.argv[0].endsWith('ts-node') // running via ts-node directly
624
541
  || !!process.env.MIKRO_ORM_CLI_ALWAYS_ALLOW_TS // forced explicitly or enabled via `registerTypeScriptSupport()`
625
542
  || !!process.env.TS_JEST // check if ts-jest is used (works only with v27.0.4+)
626
543
  || !!process.env.VITEST // check if vitest is used
627
544
  || !!process.versions.bun // check if bun is used
628
- || process.argv.slice(1).some(arg => arg.includes('ts-node')) // registering ts-node runner
629
- || process.execArgv.some(arg => arg === 'ts-node/esm'); // check for ts-node/esm module loader
630
- }
631
- /**
632
- * Uses some dark magic to get source path to caller where decorator is used.
633
- * Analyses stack trace of error created inside the function call.
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
- }
545
+ || process.argv.slice(1).some(arg => arg.match(/\.([mc]?ts|tsx)$/)) // executing `.ts` file
546
+ || process.execArgv.some(arg => {
547
+ return arg.includes('ts-node') // check for ts-node loader
548
+ || arg.includes('@swc-node/register') // check for swc-node/register loader
549
+ || arg.includes('node_modules/tsx/'); // check for tsx loader
550
+ });
666
551
  }
667
552
  /**
668
553
  * Gets the type of the argument.
@@ -673,11 +558,11 @@ export class Utils {
673
558
  return simple;
674
559
  }
675
560
  const objectType = Object.prototype.toString.call(value);
676
- const type = objectType.match(/\[object (\w+)]/)[1];
561
+ const type = objectType.match(/^\[object (.+)]$/)[1];
677
562
  if (type === 'Uint8Array') {
678
563
  return 'Buffer';
679
564
  }
680
- return ['Date', 'Buffer', 'RegExp'].includes(type) ? type : type.toLowerCase();
565
+ return type;
681
566
  }
682
567
  /**
683
568
  * Checks whether the value is POJO (e.g. `{ foo: 'bar' }`, and not instance of `Foo`)
@@ -761,8 +646,14 @@ export class Utils {
761
646
  }
762
647
  return Utils.normalizePath(path);
763
648
  }
649
+ // FNV-1a 64-bit
764
650
  static hash(data, length) {
765
- const hash = createHash('md5').update(data).digest('hex');
651
+ let h1 = 0xcbf29ce484222325n;
652
+ for (let i = 0; i < data.length; i++) {
653
+ h1 ^= BigInt(data.charCodeAt(i));
654
+ h1 = (h1 * 0x100000001b3n) & 0xffffffffffffffffn;
655
+ }
656
+ const hash = h1.toString(16).padStart(16, '0');
766
657
  if (length) {
767
658
  return hash.substring(0, length);
768
659
  }
@@ -794,13 +685,6 @@ export class Utils {
794
685
  static randomInt(min, max) {
795
686
  return Math.round(Math.random() * (max - min)) + min;
796
687
  }
797
- static async pathExists(path, options = {}) {
798
- if (globby.hasMagic(path)) {
799
- const found = await globby(path, options);
800
- return found.length > 0;
801
- }
802
- return this.pathExistsSync(path);
803
- }
804
688
  /**
805
689
  * Extracts all possible values of a TS enum. Works with both string and numeric enums.
806
690
  */
@@ -826,15 +710,6 @@ export class Utils {
826
710
  }
827
711
  return key in GroupOperator || key in QueryOperator;
828
712
  }
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
713
  static hasNestedKey(object, key) {
839
714
  if (!object) {
840
715
  return false;
@@ -847,66 +722,18 @@ export class Utils {
847
722
  }
848
723
  return false;
849
724
  }
850
- static getGlobalStorage(namespace) {
851
- const key = `mikro-orm-${namespace}`;
852
- globalThis[key] = globalThis[key] || {};
853
- return globalThis[key];
854
- }
855
- /**
856
- * Require a module from a specific location
857
- * @param id The module to require
858
- * @param [from] Location to start the node resolution
859
- */
860
- static requireFrom(id, from = process.cwd()) {
861
- if (!extname(from)) {
862
- from = join(from, '__fake.js');
863
- }
864
- return createRequire(resolve(from))(id);
865
- }
866
725
  static async dynamicImport(id) {
867
- /* v8 ignore next 7 */
868
- if (platform() === 'win32') {
869
- try {
870
- id = pathToFileURL(id).toString();
871
- }
872
- catch {
873
- // ignore
874
- }
875
- }
876
726
  /* v8 ignore next */
877
- return this.dynamicImportProvider(id);
878
- }
879
- /* v8 ignore next 3 */
880
- static setDynamicImportProvider(provider) {
881
- this.dynamicImportProvider = provider;
882
- }
883
- static ensureDir(path) {
884
- if (!existsSync(path)) {
885
- mkdirSync(path, { recursive: true });
886
- }
887
- }
888
- static pathExistsSync(path) {
889
- return existsSync(path);
890
- }
891
- static readJSONSync(path) {
892
- const file = readFileSync(path);
893
- return JSON.parse(file.toString());
727
+ const specifier = id.startsWith('file://') ? id : pathToFileURL(id).href;
728
+ return this.dynamicImportProvider(specifier);
894
729
  }
895
730
  static getORMVersion() {
896
- try {
897
- // this works during development where we have `src` folder
898
- return this.requireFrom('../../package.json', import.meta.dirname).version;
899
- /* v8 ignore next 5 */
900
- }
901
- catch {
902
- // this works in production build where we do not have the `src` folder
903
- return this.requireFrom('../package.json', import.meta.dirname).version;
904
- }
731
+ return '6.6.1';
905
732
  }
906
733
  static createFunction(context, code) {
907
734
  try {
908
735
  return new Function(...context.keys(), `'use strict';\n` + code)(...context.values());
909
- /* v8 ignore next 5 */
736
+ /* v8 ignore next */
910
737
  }
911
738
  catch (e) {
912
739
  // eslint-disable-next-line no-console
@@ -919,7 +746,7 @@ export class Utils {
919
746
  return fn(...args);
920
747
  }
921
748
  catch (e) {
922
- /* v8 ignore start */
749
+ /* v8 ignore next */
923
750
  if ([SyntaxError, TypeError, EvalError, ReferenceError].some(t => e instanceof t)) {
924
751
  const position = e.stack.match(/<anonymous>:(\d+):(\d+)/);
925
752
  let code = fn.toString();
@@ -936,18 +763,9 @@ export class Utils {
936
763
  // eslint-disable-next-line no-console
937
764
  console.error(`JIT runtime error: ${e.message}\n\n${code}`);
938
765
  }
939
- /* v8 ignore stop */
940
766
  throw e;
941
767
  }
942
768
  }
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
769
  static unwrapProperty(entity, meta, prop, payload = false) {
952
770
  let p = prop;
953
771
  const path = [];
@@ -1027,54 +845,21 @@ export class Utils {
1027
845
  }
1028
846
  }
1029
847
  }
1030
- static tryRequire({ module, from, allowError, warning }) {
1031
- allowError ??= `Cannot find module '${module}'`;
1032
- from ??= process.cwd();
848
+ static async tryImport({ module, warning }) {
1033
849
  try {
1034
- return Utils.requireFrom(module, from);
850
+ return await import(module);
1035
851
  }
1036
852
  catch (err) {
1037
- if (err.message.includes(allowError)) {
1038
- // eslint-disable-next-line no-console
1039
- console.warn(warning);
853
+ if (err.code === 'ERR_MODULE_NOT_FOUND') {
854
+ if (warning) {
855
+ // eslint-disable-next-line no-console
856
+ console.warn(warning);
857
+ }
1040
858
  return undefined;
1041
859
  }
1042
860
  throw err;
1043
861
  }
1044
862
  }
1045
- static async tryImport({ module, warning }) {
1046
- try {
1047
- return await this.dynamicImport(module);
1048
- }
1049
- catch (err) {
1050
- // eslint-disable-next-line no-console
1051
- console.warn(warning);
1052
- return undefined;
1053
- }
1054
- }
1055
- static stripRelativePath(str) {
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);
1074
- }
1075
- return args;
1076
- }, {});
1077
- }
1078
863
  static xor(a, b) {
1079
864
  return (a || b) && !(a && b);
1080
865
  }
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' || parent instanceof EventEmitter) {
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 3 */
129
+ /* v8 ignore next */
131
130
  if (descriptor && !descriptor.enumerable) {
132
131
  continue;
133
132
  }
@@ -0,0 +1,3 @@
1
+ import { type Options } from './Configuration.js';
2
+ /** @internal */
3
+ export declare function loadEnvironmentVars(): Partial<Options>;