@mikro-orm/core
Version:
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.
43 lines (42 loc) • 1.18 kB
JavaScript
function getNodeAsyncContext() {
const mod = globalThis.process?.getBuiltinModule?.('node:async_hooks');
/* v8 ignore next */
if (!mod?.AsyncLocalStorage) {
throw new Error('AsyncLocalStorage not available');
}
return new mod.AsyncLocalStorage();
}
/* v8 ignore next */
function createFallbackAsyncContext() {
let store;
// eslint-disable-next-line no-console
console.warn('AsyncLocalStorage not available');
return {
getStore: () => store,
enterWith: value => (store = value),
run: (value, cb) => {
const prev = store;
store = value;
try {
return cb();
}
finally {
store = prev;
}
},
};
}
export function createAsyncContext() {
/* v8 ignore next */
const ALS = globalThis.AsyncLocalStorage;
/* v8 ignore next */
if (typeof ALS === 'function' && ALS.prototype?.run) {
return new ALS();
}
/* v8 ignore else */
if (globalThis.process?.versions?.node) {
return getNodeAsyncContext();
}
/* v8 ignore next */
return createFallbackAsyncContext();
}