@mikro-orm/nestjs 4.2.0 → 4.3.1
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/README.md +17 -0
- package/index.js +1 -1
- package/mikro-orm-core.module.d.ts +2 -2
- package/mikro-orm-core.module.js +53 -8
- package/mikro-orm.common.js +7 -3
- package/mikro-orm.providers.d.ts +4 -3
- package/mikro-orm.providers.js +29 -11
- package/package.json +11 -10
package/README.md
CHANGED
|
@@ -300,6 +300,23 @@ export class MyService {
|
|
|
300
300
|
}
|
|
301
301
|
```
|
|
302
302
|
|
|
303
|
+
## App shutdown and cleanup
|
|
304
|
+
|
|
305
|
+
By default, NestJS does not listen for system process termination signals (for example SIGTERM). Because of this, the MikroORM shutdown logic will never executed if the process is terminated, which could lead to database connections remaining open and consuming resources. To enable this, the `enableShutdownHooks` function needs to be called when starting up the application.
|
|
306
|
+
|
|
307
|
+
```ts
|
|
308
|
+
async function bootstrap() {
|
|
309
|
+
const app = await NestFactory.create(AppModule);
|
|
310
|
+
|
|
311
|
+
// Starts listening for shutdown hooks
|
|
312
|
+
app.enableShutdownHooks();
|
|
313
|
+
|
|
314
|
+
await app.listen(3000);
|
|
315
|
+
}
|
|
316
|
+
```
|
|
317
|
+
|
|
318
|
+
More information about [enableShutdownHooks](https://docs.nestjs.com/fundamentals/lifecycle-events#application-shutdown)
|
|
319
|
+
|
|
303
320
|
## Testing
|
|
304
321
|
|
|
305
322
|
The `nestjs-mikro-orm` package exposes `getRepositoryToken()` function that returns prepared token based on a given entity to allow mocking the repository.
|
package/index.js
CHANGED
|
@@ -7,7 +7,7 @@ var __createBinding = (this && this.__createBinding) || (Object.create ? (functi
|
|
|
7
7
|
o[k2] = m[k];
|
|
8
8
|
}));
|
|
9
9
|
var __exportStar = (this && this.__exportStar) || function(m, exports) {
|
|
10
|
-
for (var p in m) if (p !== "default" && !
|
|
10
|
+
for (var p in m) if (p !== "default" && !Object.prototype.hasOwnProperty.call(exports, p)) __createBinding(exports, m, p);
|
|
11
11
|
};
|
|
12
12
|
Object.defineProperty(exports, "__esModule", { value: true });
|
|
13
13
|
__exportStar(require("./mikro-orm.module"), exports);
|
|
@@ -5,8 +5,8 @@ export declare class MikroOrmCoreModule implements OnApplicationShutdown {
|
|
|
5
5
|
private readonly options;
|
|
6
6
|
private readonly moduleRef;
|
|
7
7
|
constructor(options: MikroOrmModuleOptions, moduleRef: ModuleRef);
|
|
8
|
-
static forRoot(options?: MikroOrmModuleSyncOptions): DynamicModule
|
|
9
|
-
static forRootAsync(options: MikroOrmModuleAsyncOptions): DynamicModule
|
|
8
|
+
static forRoot(options?: MikroOrmModuleSyncOptions): Promise<DynamicModule>;
|
|
9
|
+
static forRootAsync(options: MikroOrmModuleAsyncOptions): Promise<DynamicModule>;
|
|
10
10
|
onApplicationShutdown(): Promise<void>;
|
|
11
11
|
configure(consumer: MiddlewareConsumer): void;
|
|
12
12
|
}
|
package/mikro-orm-core.module.js
CHANGED
|
@@ -1,10 +1,29 @@
|
|
|
1
1
|
"use strict";
|
|
2
|
+
var __createBinding = (this && this.__createBinding) || (Object.create ? (function(o, m, k, k2) {
|
|
3
|
+
if (k2 === undefined) k2 = k;
|
|
4
|
+
Object.defineProperty(o, k2, { enumerable: true, get: function() { return m[k]; } });
|
|
5
|
+
}) : (function(o, m, k, k2) {
|
|
6
|
+
if (k2 === undefined) k2 = k;
|
|
7
|
+
o[k2] = m[k];
|
|
8
|
+
}));
|
|
9
|
+
var __setModuleDefault = (this && this.__setModuleDefault) || (Object.create ? (function(o, v) {
|
|
10
|
+
Object.defineProperty(o, "default", { enumerable: true, value: v });
|
|
11
|
+
}) : function(o, v) {
|
|
12
|
+
o["default"] = v;
|
|
13
|
+
});
|
|
2
14
|
var __decorate = (this && this.__decorate) || function (decorators, target, key, desc) {
|
|
3
15
|
var c = arguments.length, r = c < 3 ? target : desc === null ? desc = Object.getOwnPropertyDescriptor(target, key) : desc, d;
|
|
4
16
|
if (typeof Reflect === "object" && typeof Reflect.decorate === "function") r = Reflect.decorate(decorators, target, key, desc);
|
|
5
17
|
else for (var i = decorators.length - 1; i >= 0; i--) if (d = decorators[i]) r = (c < 3 ? d(r) : c > 3 ? d(target, key, r) : d(target, key)) || r;
|
|
6
18
|
return c > 3 && r && Object.defineProperty(target, key, r), r;
|
|
7
19
|
};
|
|
20
|
+
var __importStar = (this && this.__importStar) || function (mod) {
|
|
21
|
+
if (mod && mod.__esModule) return mod;
|
|
22
|
+
var result = {};
|
|
23
|
+
if (mod != null) for (var k in mod) if (k !== "default" && Object.prototype.hasOwnProperty.call(mod, k)) __createBinding(result, mod, k);
|
|
24
|
+
__setModuleDefault(result, mod);
|
|
25
|
+
return result;
|
|
26
|
+
};
|
|
8
27
|
var __metadata = (this && this.__metadata) || function (k, v) {
|
|
9
28
|
if (typeof Reflect === "object" && typeof Reflect.metadata === "function") return Reflect.metadata(k, v);
|
|
10
29
|
};
|
|
@@ -20,25 +39,46 @@ const core_2 = require("@nestjs/core");
|
|
|
20
39
|
const mikro_orm_common_1 = require("./mikro-orm.common");
|
|
21
40
|
const mikro_orm_middleware_1 = require("./mikro-orm.middleware");
|
|
22
41
|
const mikro_orm_providers_1 = require("./mikro-orm.providers");
|
|
42
|
+
var EntityManagerModuleName;
|
|
43
|
+
(function (EntityManagerModuleName) {
|
|
44
|
+
EntityManagerModuleName["Knex"] = "@mikro-orm/knex";
|
|
45
|
+
EntityManagerModuleName["MongoDb"] = "@mikro-orm/mongodb";
|
|
46
|
+
})(EntityManagerModuleName || (EntityManagerModuleName = {}));
|
|
47
|
+
async function whenModuleAvailable(moduleName, returnFn) {
|
|
48
|
+
try {
|
|
49
|
+
const module = await Promise.resolve().then(() => __importStar(require(moduleName)));
|
|
50
|
+
// TReturnValue may be incorrect if expecting it to extend Promise<T> since its being awaited in the return
|
|
51
|
+
// casting to Awaited<TReturnValue> to satisfy the return type will prevent that confusion
|
|
52
|
+
return [await returnFn(module)];
|
|
53
|
+
}
|
|
54
|
+
catch (err) {
|
|
55
|
+
return [];
|
|
56
|
+
}
|
|
57
|
+
}
|
|
23
58
|
let MikroOrmCoreModule = MikroOrmCoreModule_1 = class MikroOrmCoreModule {
|
|
24
59
|
constructor(options, moduleRef) {
|
|
25
60
|
this.options = options;
|
|
26
61
|
this.moduleRef = moduleRef;
|
|
27
62
|
}
|
|
28
|
-
static forRoot(options) {
|
|
63
|
+
static async forRoot(options) {
|
|
29
64
|
return {
|
|
30
65
|
module: MikroOrmCoreModule_1,
|
|
31
66
|
providers: [
|
|
32
67
|
{ provide: mikro_orm_common_1.MIKRO_ORM_MODULE_OPTIONS, useValue: options || {} },
|
|
33
68
|
mikro_orm_providers_1.createMikroOrmProvider(),
|
|
34
69
|
mikro_orm_providers_1.createMikroOrmEntityManagerProvider(options === null || options === void 0 ? void 0 : options.scope),
|
|
35
|
-
mikro_orm_providers_1.createMikroOrmEntityManagerProvider(options === null || options === void 0 ? void 0 : options.scope,
|
|
36
|
-
mikro_orm_providers_1.createMikroOrmEntityManagerProvider(options === null || options === void 0 ? void 0 : options.scope,
|
|
70
|
+
...(await whenModuleAvailable(EntityManagerModuleName.Knex, ({ SqlEntityManager }) => mikro_orm_providers_1.createMikroOrmEntityManagerProvider(options === null || options === void 0 ? void 0 : options.scope, SqlEntityManager))),
|
|
71
|
+
...(await whenModuleAvailable(EntityManagerModuleName.MongoDb, ({ MongoEntityManager }) => mikro_orm_providers_1.createMikroOrmEntityManagerProvider(options === null || options === void 0 ? void 0 : options.scope, MongoEntityManager))),
|
|
72
|
+
],
|
|
73
|
+
exports: [
|
|
74
|
+
core_1.MikroORM,
|
|
75
|
+
core_1.EntityManager,
|
|
76
|
+
...(await whenModuleAvailable(EntityManagerModuleName.Knex, ({ SqlEntityManager }) => SqlEntityManager)),
|
|
77
|
+
...(await whenModuleAvailable(EntityManagerModuleName.MongoDb, ({ MongoEntityManager }) => MongoEntityManager)),
|
|
37
78
|
],
|
|
38
|
-
exports: [core_1.MikroORM, core_1.EntityManager, 'SqlEntityManager', 'MongoEntityManager'],
|
|
39
79
|
};
|
|
40
80
|
}
|
|
41
|
-
static forRootAsync(options) {
|
|
81
|
+
static async forRootAsync(options) {
|
|
42
82
|
return {
|
|
43
83
|
module: MikroOrmCoreModule_1,
|
|
44
84
|
imports: options.imports || [],
|
|
@@ -47,10 +87,15 @@ let MikroOrmCoreModule = MikroOrmCoreModule_1 = class MikroOrmCoreModule {
|
|
|
47
87
|
...mikro_orm_providers_1.createAsyncProviders(options),
|
|
48
88
|
mikro_orm_providers_1.createMikroOrmProvider(),
|
|
49
89
|
mikro_orm_providers_1.createMikroOrmEntityManagerProvider(options.scope),
|
|
50
|
-
mikro_orm_providers_1.createMikroOrmEntityManagerProvider(options.scope,
|
|
51
|
-
mikro_orm_providers_1.createMikroOrmEntityManagerProvider(options.scope,
|
|
90
|
+
...(await whenModuleAvailable(EntityManagerModuleName.Knex, ({ SqlEntityManager }) => mikro_orm_providers_1.createMikroOrmEntityManagerProvider(options.scope, SqlEntityManager))),
|
|
91
|
+
...(await whenModuleAvailable(EntityManagerModuleName.MongoDb, ({ MongoEntityManager }) => mikro_orm_providers_1.createMikroOrmEntityManagerProvider(options.scope, MongoEntityManager))),
|
|
92
|
+
],
|
|
93
|
+
exports: [
|
|
94
|
+
core_1.MikroORM,
|
|
95
|
+
core_1.EntityManager,
|
|
96
|
+
...(await whenModuleAvailable(EntityManagerModuleName.Knex, ({ SqlEntityManager }) => SqlEntityManager)),
|
|
97
|
+
...(await whenModuleAvailable(EntityManagerModuleName.MongoDb, ({ MongoEntityManager }) => MongoEntityManager)),
|
|
52
98
|
],
|
|
53
|
-
exports: [core_1.MikroORM, core_1.EntityManager, 'SqlEntityManager', 'MongoEntityManager'],
|
|
54
99
|
};
|
|
55
100
|
}
|
|
56
101
|
async onApplicationShutdown() {
|
package/mikro-orm.common.js
CHANGED
|
@@ -6,8 +6,10 @@ const common_1 = require("@nestjs/common");
|
|
|
6
6
|
exports.MIKRO_ORM_MODULE_OPTIONS = Symbol('mikro-orm-module-options');
|
|
7
7
|
exports.REGISTERED_ENTITIES = new Set();
|
|
8
8
|
exports.logger = new common_1.Logger(core_1.MikroORM.name);
|
|
9
|
-
|
|
10
|
-
exports.
|
|
9
|
+
const getRepositoryToken = (entity) => `${core_1.Utils.className(entity)}Repository`;
|
|
10
|
+
exports.getRepositoryToken = getRepositoryToken;
|
|
11
|
+
const InjectRepository = (entity) => common_1.Inject(exports.getRepositoryToken(entity));
|
|
12
|
+
exports.InjectRepository = InjectRepository;
|
|
11
13
|
function UseRequestContext() {
|
|
12
14
|
return function (target, propertyKey, descriptor) {
|
|
13
15
|
const originalMethod = descriptor.value;
|
|
@@ -17,9 +19,11 @@ function UseRequestContext() {
|
|
|
17
19
|
if (!(context.orm instanceof core_1.MikroORM)) {
|
|
18
20
|
throw new Error('@UseRequestContext() decorator can only be applied to methods of classes that carry `orm: MikroORM`');
|
|
19
21
|
}
|
|
22
|
+
let result;
|
|
20
23
|
await core_1.RequestContext.createAsync(context.orm.em, async () => {
|
|
21
|
-
await originalMethod.apply(context, args);
|
|
24
|
+
result = await originalMethod.apply(context, args);
|
|
22
25
|
});
|
|
26
|
+
return result;
|
|
23
27
|
};
|
|
24
28
|
return descriptor;
|
|
25
29
|
};
|
package/mikro-orm.providers.d.ts
CHANGED
|
@@ -1,8 +1,9 @@
|
|
|
1
|
-
import { AnyEntity, EntityName } from '@mikro-orm/core';
|
|
1
|
+
import { AnyEntity, EntityManager, EntityName, EntityManagerType, IDatabaseDriver } from '@mikro-orm/core';
|
|
2
2
|
import { MikroOrmModuleAsyncOptions } from './typings';
|
|
3
|
-
import { Provider, Scope } from '@nestjs/common';
|
|
3
|
+
import { Provider, Scope, Type } from '@nestjs/common';
|
|
4
4
|
export declare const createMikroOrmProvider: () => Provider;
|
|
5
|
-
export declare
|
|
5
|
+
export declare type EntityManagerProvider = Provider<IDatabaseDriver[typeof EntityManagerType] & EntityManager>;
|
|
6
|
+
export declare const createMikroOrmEntityManagerProvider: (scope?: Scope, entityManager?: Type<EntityManager>) => EntityManagerProvider;
|
|
6
7
|
export declare const createMikroOrmAsyncOptionsProvider: (options: MikroOrmModuleAsyncOptions) => Provider;
|
|
7
8
|
export declare const createAsyncProviders: (options: MikroOrmModuleAsyncOptions) => Provider[];
|
|
8
9
|
export declare const createMikroOrmRepositoryProviders: (entities: EntityName<AnyEntity>[]) => Provider[];
|
package/mikro-orm.providers.js
CHANGED
|
@@ -4,7 +4,7 @@ exports.createMikroOrmRepositoryProviders = exports.createAsyncProviders = expor
|
|
|
4
4
|
const mikro_orm_common_1 = require("./mikro-orm.common");
|
|
5
5
|
const core_1 = require("@mikro-orm/core");
|
|
6
6
|
const common_1 = require("@nestjs/common");
|
|
7
|
-
|
|
7
|
+
const createMikroOrmProvider = () => ({
|
|
8
8
|
provide: core_1.MikroORM,
|
|
9
9
|
useFactory: async (options) => {
|
|
10
10
|
if (options === null || options === void 0 ? void 0 : options.autoLoadEntities) {
|
|
@@ -22,13 +22,15 @@ exports.createMikroOrmProvider = () => ({
|
|
|
22
22
|
},
|
|
23
23
|
inject: [mikro_orm_common_1.MIKRO_ORM_MODULE_OPTIONS],
|
|
24
24
|
});
|
|
25
|
-
exports.
|
|
26
|
-
|
|
25
|
+
exports.createMikroOrmProvider = createMikroOrmProvider;
|
|
26
|
+
const createMikroOrmEntityManagerProvider = (scope = common_1.Scope.DEFAULT, entityManager = core_1.EntityManager) => ({
|
|
27
|
+
provide: entityManager,
|
|
27
28
|
scope,
|
|
28
29
|
useFactory: (orm) => scope === common_1.Scope.DEFAULT ? orm.em : orm.em.fork(),
|
|
29
30
|
inject: [core_1.MikroORM],
|
|
30
31
|
});
|
|
31
|
-
exports.
|
|
32
|
+
exports.createMikroOrmEntityManagerProvider = createMikroOrmEntityManagerProvider;
|
|
33
|
+
const createMikroOrmAsyncOptionsProvider = (options) => {
|
|
32
34
|
var _a;
|
|
33
35
|
if (options.useFactory) {
|
|
34
36
|
return {
|
|
@@ -47,7 +49,8 @@ exports.createMikroOrmAsyncOptionsProvider = (options) => {
|
|
|
47
49
|
inject,
|
|
48
50
|
};
|
|
49
51
|
};
|
|
50
|
-
exports.
|
|
52
|
+
exports.createMikroOrmAsyncOptionsProvider = createMikroOrmAsyncOptionsProvider;
|
|
53
|
+
const createAsyncProviders = (options) => {
|
|
51
54
|
if (options.useExisting || options.useFactory) {
|
|
52
55
|
return [exports.createMikroOrmAsyncOptionsProvider(options)];
|
|
53
56
|
}
|
|
@@ -59,10 +62,25 @@ exports.createAsyncProviders = (options) => {
|
|
|
59
62
|
}
|
|
60
63
|
throw new Error('Invalid MikroORM async options: one of `useClass`, `useExisting` or `useFactory` should be defined.');
|
|
61
64
|
};
|
|
62
|
-
exports.
|
|
63
|
-
|
|
64
|
-
|
|
65
|
-
|
|
66
|
-
|
|
67
|
-
|
|
65
|
+
exports.createAsyncProviders = createAsyncProviders;
|
|
66
|
+
const createMikroOrmRepositoryProviders = (entities) => {
|
|
67
|
+
const metadata = Object.values(core_1.MetadataStorage.getMetadata());
|
|
68
|
+
const providers = [];
|
|
69
|
+
(entities || []).forEach(entity => {
|
|
70
|
+
const meta = metadata.find(meta => meta.class === entity);
|
|
71
|
+
if (meta === null || meta === void 0 ? void 0 : meta.customRepository) {
|
|
72
|
+
providers.push({
|
|
73
|
+
provide: meta.customRepository(),
|
|
74
|
+
useFactory: em => em.getRepository(entity),
|
|
75
|
+
inject: [core_1.EntityManager],
|
|
76
|
+
});
|
|
77
|
+
}
|
|
78
|
+
providers.push({
|
|
79
|
+
provide: mikro_orm_common_1.getRepositoryToken(entity),
|
|
80
|
+
useFactory: em => em.getRepository(entity),
|
|
81
|
+
inject: [core_1.EntityManager],
|
|
82
|
+
});
|
|
83
|
+
});
|
|
84
|
+
return providers;
|
|
68
85
|
};
|
|
86
|
+
exports.createMikroOrmRepositoryProviders = createMikroOrmRepositoryProviders;
|
package/package.json
CHANGED
|
@@ -1,6 +1,6 @@
|
|
|
1
1
|
{
|
|
2
2
|
"name": "@mikro-orm/nestjs",
|
|
3
|
-
"version": "4.
|
|
3
|
+
"version": "4.3.1",
|
|
4
4
|
"license": "MIT",
|
|
5
5
|
"author": {
|
|
6
6
|
"name": "Dario Mancuso",
|
|
@@ -41,26 +41,27 @@
|
|
|
41
41
|
"lint": "eslint src/**/*.ts"
|
|
42
42
|
},
|
|
43
43
|
"peerDependencies": {
|
|
44
|
-
"@mikro-orm/core": "^4.0.0-
|
|
45
|
-
"@nestjs/common": "^7.0.0",
|
|
46
|
-
"@nestjs/core": "^7.0.0"
|
|
44
|
+
"@mikro-orm/core": "^4.0.0||^5.0.0-dev.0",
|
|
45
|
+
"@nestjs/common": "^7.0.0||^8.0.0",
|
|
46
|
+
"@nestjs/core": "^7.0.0||^8.0.0"
|
|
47
47
|
},
|
|
48
48
|
"devDependencies": {
|
|
49
|
-
"@mikro-orm/core": "^4.
|
|
50
|
-
"@mikro-orm/sqlite": "^4.
|
|
51
|
-
"@nestjs/common": "^
|
|
52
|
-
"@nestjs/core": "^
|
|
53
|
-
"@nestjs/testing": "^
|
|
49
|
+
"@mikro-orm/core": "^4.5.7",
|
|
50
|
+
"@mikro-orm/sqlite": "^4.5.7",
|
|
51
|
+
"@nestjs/common": "^8.0.6",
|
|
52
|
+
"@nestjs/core": "^8.0.6",
|
|
53
|
+
"@nestjs/testing": "^8.0.6",
|
|
54
54
|
"@types/jest": "^26.0.10",
|
|
55
55
|
"@typescript-eslint/eslint-plugin": "~3.9.1",
|
|
56
56
|
"@typescript-eslint/parser": "~3.9.1",
|
|
57
57
|
"conventional-changelog": "^3.1.23",
|
|
58
|
+
"conventional-changelog-cli": "^2.1.1",
|
|
58
59
|
"eslint": "^7.7.0",
|
|
59
60
|
"eslint-plugin-jsdoc": "^30.2.4",
|
|
60
61
|
"jest": "^26.4.0",
|
|
61
62
|
"rxjs": "^6.6.2",
|
|
62
63
|
"ts-jest": "^26.2.0",
|
|
63
|
-
"typescript": "3.
|
|
64
|
+
"typescript": "4.3.5"
|
|
64
65
|
},
|
|
65
66
|
"jest": {
|
|
66
67
|
"testTimeout": 30000,
|