@mikro-orm/core 7.0.0-dev.106 → 7.0.0-dev.107

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/package.json CHANGED
@@ -1,7 +1,7 @@
1
1
  {
2
2
  "name": "@mikro-orm/core",
3
3
  "type": "module",
4
- "version": "7.0.0-dev.106",
4
+ "version": "7.0.0-dev.107",
5
5
  "description": "TypeScript ORM for Node.js based on Data Mapper, Unit of Work and Identity Map patterns. Supports MongoDB, MySQL, PostgreSQL and SQLite databases as well as usage with vanilla JavaScript.",
6
6
  "exports": {
7
7
  "./package.json": "./package.json",
@@ -1,4 +1,3 @@
1
- import { AsyncLocalStorage } from 'node:async_hooks';
2
1
  import { Collection } from '../entity/Collection.js';
3
2
  import { EntityHelper } from '../entity/EntityHelper.js';
4
3
  import { helper } from '../entity/wrap.js';
@@ -13,8 +12,9 @@ import { Cascade, DeferMode, EventType, LockMode, ReferenceKind } from '../enums
13
12
  import { OptimisticLockError, ValidationError } from '../errors.js';
14
13
  import { TransactionEventBroadcaster } from '../events/TransactionEventBroadcaster.js';
15
14
  import { IdentityMap } from './IdentityMap.js';
15
+ import { createAsyncContext } from '../utils/AsyncContext.js';
16
16
  // to deal with validation for flush inside flush hooks and `Promise.all`
17
- const insideFlush = new AsyncLocalStorage();
17
+ const insideFlush = createAsyncContext();
18
18
  export class UnitOfWork {
19
19
  em;
20
20
  /** map of references to managed entities */
@@ -0,0 +1,6 @@
1
+ export interface AsyncContext<T> {
2
+ getStore(): T | undefined;
3
+ run<R>(store: T, callback: () => R): R;
4
+ enterWith(store: T): void;
5
+ }
6
+ export declare function createAsyncContext<T>(): AsyncContext<T>;
@@ -0,0 +1,42 @@
1
+ function getNodeAsyncContext() {
2
+ const mod = globalThis.process?.getBuiltinModule?.('node:async_hooks');
3
+ /* v8 ignore next */
4
+ if (!mod?.AsyncLocalStorage) {
5
+ throw new Error('AsyncLocalStorage not available');
6
+ }
7
+ return new mod.AsyncLocalStorage();
8
+ }
9
+ /* v8 ignore next */
10
+ function createFallbackAsyncContext() {
11
+ let store;
12
+ // eslint-disable-next-line no-console
13
+ console.warn('AsyncLocalStorage not available');
14
+ return {
15
+ getStore: () => store,
16
+ enterWith: value => store = value,
17
+ run: (value, cb) => {
18
+ const prev = store;
19
+ store = value;
20
+ try {
21
+ return cb();
22
+ }
23
+ finally {
24
+ store = prev;
25
+ }
26
+ },
27
+ };
28
+ }
29
+ export function createAsyncContext() {
30
+ /* v8 ignore next */
31
+ const ALS = globalThis.AsyncLocalStorage;
32
+ /* v8 ignore next */
33
+ if (typeof ALS === 'function' && ALS.prototype?.run) {
34
+ return new ALS();
35
+ }
36
+ /* v8 ignore else */
37
+ if (globalThis.process?.versions?.node) {
38
+ return getNodeAsyncContext();
39
+ }
40
+ /* v8 ignore next */
41
+ return createFallbackAsyncContext();
42
+ }
@@ -1,10 +1,10 @@
1
- import { AsyncLocalStorage } from 'node:async_hooks';
2
1
  import { Utils } from './Utils.js';
2
+ import { createAsyncContext } from './AsyncContext.js';
3
3
  export class RawQueryFragment {
4
4
  sql;
5
5
  params;
6
6
  static #rawQueryCache = new Map();
7
- static #storage = new AsyncLocalStorage();
7
+ static #storage = createAsyncContext();
8
8
  static #index = 0n;
9
9
  static cloneRegistry;
10
10
  #used = 0;
@@ -1,10 +1,10 @@
1
- import { AsyncLocalStorage } from 'node:async_hooks';
1
+ import { createAsyncContext } from './AsyncContext.js';
2
2
  /**
3
3
  * Uses `AsyncLocalStorage` to create async context that holds the current EM fork.
4
4
  */
5
5
  export class RequestContext {
6
6
  map;
7
- static storage = new AsyncLocalStorage();
7
+ static storage = createAsyncContext();
8
8
  static counter = 1;
9
9
  id = RequestContext.counter++;
10
10
  constructor(map) {
@@ -1,7 +1,7 @@
1
- import { AsyncLocalStorage } from 'node:async_hooks';
1
+ import { createAsyncContext } from './AsyncContext.js';
2
2
  export class TransactionContext {
3
3
  em;
4
- static storage = new AsyncLocalStorage();
4
+ static storage = createAsyncContext();
5
5
  id;
6
6
  constructor(em) {
7
7
  this.em = em;
package/utils/Utils.js CHANGED
@@ -125,7 +125,7 @@ export function parseJsonSafe(value) {
125
125
  }
126
126
  export class Utils {
127
127
  static PK_SEPARATOR = '~~~';
128
- static #ORM_VERSION = '7.0.0-dev.106';
128
+ static #ORM_VERSION = '7.0.0-dev.107';
129
129
  /* v8 ignore next */
130
130
  static dynamicImportProvider = (id) => import(id);
131
131
  /**