@tsed/mikro-orm 8.0.1 → 8.0.3

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.
@@ -0,0 +1,121 @@
1
+ import {MikroORM, Options} from "@mikro-orm/core";
2
+ import {Logger} from "@tsed/logger";
3
+ import {anything, instance, mock, reset, verify, when} from "ts-mockito";
4
+
5
+ import {MikroOrmFactory} from "./MikroOrmFactory.js";
6
+ import {MikroOrmRegistry} from "./MikroOrmRegistry.js";
7
+
8
+ const fixtures: {mydb2: Options; none: Options; mydb: Options} = {
9
+ mydb: {
10
+ contextName: "mydb",
11
+ type: "postgresql",
12
+ clientUrl: "postgresql://localhost:5432"
13
+ },
14
+ none: {
15
+ type: "postgresql",
16
+ clientUrl: "postgresql://localhost:5432"
17
+ },
18
+ mydb2: {
19
+ contextName: "mydb2",
20
+ type: "postgresql",
21
+ clientUrl: "postgresql://localhost:5432"
22
+ }
23
+ } as never;
24
+
25
+ describe("MikroOrmRegistry", () => {
26
+ const loggerMock = mock<Logger>();
27
+ const mikroOrmFactoryMock = mock<MikroOrmFactory>();
28
+ const mikroOrm = mock(MikroORM);
29
+
30
+ Object.values(fixtures).forEach((options: Options) => when(mikroOrmFactoryMock.create(options)).thenResolve(instance(mikroOrm)));
31
+
32
+ let mikroOrmRegistry!: MikroOrmRegistry;
33
+
34
+ beforeEach(() => {
35
+ mikroOrmRegistry = new MikroOrmRegistry(instance(loggerMock), instance(mikroOrmFactoryMock));
36
+ });
37
+
38
+ afterEach(() => reset<MikroORM | MikroOrmFactory>(mikroOrmFactoryMock, mikroOrm));
39
+
40
+ describe("register", () => {
41
+ it("should register instances", async () => {
42
+ // arrange
43
+ Object.values(fixtures).forEach((options: Options) => when(mikroOrmFactoryMock.create(options)).thenResolve(instance(mikroOrm)));
44
+
45
+ // act
46
+ const result1 = await mikroOrmRegistry.register(fixtures.mydb);
47
+ const result2 = await mikroOrmRegistry.register(fixtures.mydb);
48
+ const result3 = await mikroOrmRegistry.register(fixtures.none);
49
+ const result4 = await mikroOrmRegistry.register(fixtures.mydb2);
50
+
51
+ // assert
52
+ expect(result1).toEqual(mikroOrmRegistry.get("mydb"));
53
+ expect(result2).toEqual(mikroOrmRegistry.get("mydb"));
54
+ expect(result3).toEqual(mikroOrmRegistry.get("default"));
55
+ expect(result4).toEqual(mikroOrmRegistry.get("mydb2"));
56
+ verify(mikroOrmFactoryMock.create(anything())).thrice();
57
+ });
58
+ });
59
+
60
+ describe("clear", () => {
61
+ it("should dispose all instances", async () => {
62
+ // arrange
63
+ when(mikroOrm.isConnected()).thenResolve(true);
64
+ Object.values(fixtures).forEach((options: Options) => when(mikroOrmFactoryMock.create(options)).thenResolve(instance(mikroOrm)));
65
+ await mikroOrmRegistry.register(fixtures.none);
66
+ await mikroOrmRegistry.register(fixtures.mydb2);
67
+
68
+ // act
69
+ await mikroOrmRegistry.clear();
70
+
71
+ // assert
72
+ verify(mikroOrm.close(anything())).twice();
73
+ });
74
+ });
75
+
76
+ describe("get", () => {
77
+ it("should return corresponded instances", async () => {
78
+ // arrange
79
+ Object.values(fixtures).forEach((options: Options) => when(mikroOrmFactoryMock.create(options)).thenResolve(instance(mikroOrm)));
80
+ const none = await mikroOrmRegistry.register(fixtures.none);
81
+ const mydb = await mikroOrmRegistry.register(fixtures.mydb);
82
+
83
+ // act
84
+ const result1 = mikroOrmRegistry.get();
85
+ const result2 = mikroOrmRegistry.get("default");
86
+ const result3 = mikroOrmRegistry.get("mydb");
87
+
88
+ // assert
89
+ expect(result1).toEqual(none);
90
+ expect(result2).toEqual(none);
91
+ expect(result3).toEqual(mydb);
92
+ });
93
+ });
94
+
95
+ describe("has", () => {
96
+ it("should return corresponded instances existence", async () => {
97
+ // arrange
98
+ Object.values(fixtures).forEach((options: Options) => when(mikroOrmFactoryMock.create(options)).thenResolve(instance(mikroOrm)));
99
+ await mikroOrmRegistry.register(fixtures.none);
100
+ await mikroOrmRegistry.register(fixtures.mydb);
101
+
102
+ // act & assert
103
+ expect(mikroOrmRegistry.has()).toBeTruthy();
104
+ expect(mikroOrmRegistry.has("mydb")).toBeTruthy();
105
+ expect(mikroOrmRegistry.has("default")).toBeTruthy();
106
+ expect(mikroOrmRegistry.has("mydb10")).toBeFalsy();
107
+ });
108
+ });
109
+
110
+ describe("values", () => {
111
+ it("should return corresponded instances", async () => {
112
+ // arrange
113
+ Object.values(fixtures).forEach((options: Options) => when(mikroOrmFactoryMock.create(options)).thenResolve(instance(mikroOrm)));
114
+ await mikroOrmRegistry.register(fixtures.none);
115
+ await mikroOrmRegistry.register(fixtures.mydb);
116
+
117
+ // act & assert
118
+ expect([...mikroOrmRegistry.values()]).toMatchObject([instance(mikroOrm), instance(mikroOrm)]);
119
+ });
120
+ });
121
+ });
@@ -0,0 +1,62 @@
1
+ import {IDatabaseDriver as DatabaseDriver, MikroORM, Options} from "@mikro-orm/core";
2
+ import {catchAsyncError, getValue} from "@tsed/core";
3
+ import {Inject, Injectable} from "@tsed/di";
4
+ import {Logger} from "@tsed/logger";
5
+
6
+ import {MikroOrmFactory} from "./MikroOrmFactory.js";
7
+
8
+ @Injectable()
9
+ export class MikroOrmRegistry {
10
+ private readonly DEFAULT_CONTEXT_NAME = "default";
11
+ private readonly instances = new Map<string, MikroORM>();
12
+
13
+ constructor(
14
+ @Inject() private readonly logger: Logger,
15
+ @Inject() private readonly mikroOrmFactory: MikroOrmFactory
16
+ ) {}
17
+
18
+ public async register<T extends DatabaseDriver>(options: Options<T>): Promise<MikroORM> {
19
+ const contextName = getValue<string>(options, "contextName", this.DEFAULT_CONTEXT_NAME);
20
+
21
+ if (this.has(contextName)) {
22
+ return this.get(contextName)!;
23
+ }
24
+
25
+ this.logger.info(`Create connection with MikroOrm to database: %s`, contextName);
26
+ this.logger.debug(`options: %j`, options);
27
+
28
+ const instance = await this.mikroOrmFactory.create(options);
29
+
30
+ this.instances.set(contextName, instance);
31
+
32
+ this.logger.info(`Connected with MikroOrm to database: %s`, contextName);
33
+
34
+ return instance;
35
+ }
36
+
37
+ public get(contextName?: string): MikroORM | undefined {
38
+ return this.instances.get(contextName ?? this.DEFAULT_CONTEXT_NAME);
39
+ }
40
+
41
+ public has(contextName?: string): boolean {
42
+ return this.instances.has(contextName ?? this.DEFAULT_CONTEXT_NAME);
43
+ }
44
+
45
+ public values(): IterableIterator<MikroORM> {
46
+ return this.instances.values();
47
+ }
48
+
49
+ public async clear(): Promise<void> {
50
+ const instances = [...this.instances.values()];
51
+
52
+ await Promise.all(instances.map((instance: MikroORM) => catchAsyncError(() => this.dispose(instance))));
53
+
54
+ this.instances.clear();
55
+ }
56
+
57
+ private async dispose(instance: MikroORM): Promise<void> {
58
+ if (await instance.isConnected()) {
59
+ await instance.close(false);
60
+ }
61
+ }
62
+ }