@nestjs/typeorm
Version:
Nest - modern, fast, powerful node.js web framework (@typeorm)
46 lines (45 loc) • 1.92 kB
JavaScript
;
/**
* Runtime compatibility helpers for accessing TypeORM APIs that were
* removed in TypeORM v1.0.0 (notably `Connection` and `AbstractRepository`).
*
* Importing these symbols statically from `typeorm` would produce
* TypeScript errors for consumers on TypeORM v1 when `skipLibCheck` is
* disabled, and would cause runtime crashes inside `@nestjs/typeorm`
* because the values resolve to `undefined`.
*
* Resolving them lazily via `require()` keeps the package working on
* both TypeORM 0.3.x (where the symbols still exist) and 1.0.x
* (where they have been removed) without forcing a type dependency.
*/
Object.defineProperty(exports, "__esModule", { value: true });
exports.AbstractRepository = exports.Connection = void 0;
/**
* Safely resolves an optional export from the installed `typeorm`
* package. Returns `undefined` when the export is not present (e.g.
* TypeORM v1.0.0 has removed it) or when the module fails to load.
*/
function resolveTypeormExport(exportName) {
try {
// Using `require` here (rather than a static import) ensures the
// reference is resolved at runtime and is not included in the
// emitted type definitions — which is required for forward
// compatibility with TypeORM v1 where these symbols no longer exist.
// eslint-disable-next-line @typescript-eslint/no-require-imports
const typeorm = require('typeorm');
return typeorm[exportName];
}
catch {
return undefined;
}
}
/**
* The TypeORM `Connection` class (removed in TypeORM v1.0.0, replaced by
* `DataSource` since 0.3.0). Resolves to `undefined` on TypeORM v1+.
*/
exports.Connection = resolveTypeormExport('Connection');
/**
* The TypeORM `AbstractRepository` class (removed in TypeORM v1.0.0).
* Resolves to `undefined` on TypeORM v1+.
*/
exports.AbstractRepository = resolveTypeormExport('AbstractRepository');