@midwayjs/mikro 3.15.2 → 3.15.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.
@@ -29,12 +29,13 @@ let MikroConfiguration = class MikroConfiguration {
29
29
  }
30
30
  });
31
31
  this.decoratorService.registerPropertyHandler(decorator_1.ENTITY_MANAGER_KEY, (propertyName, meta) => {
32
- if (core_2.RequestContext.getEntityManager()) {
33
- return core_2.RequestContext.getEntityManager();
32
+ const name = meta.connectionName ||
33
+ this.dataSourceManager.getDefaultDataSourceName();
34
+ if (core_2.RequestContext.getEntityManager(name)) {
35
+ return core_2.RequestContext.getEntityManager(name);
34
36
  }
35
37
  else {
36
- return this.dataSourceManager.getDataSource(meta.connectionName ||
37
- this.dataSourceManager.getDefaultDataSourceName()).em;
38
+ return this.dataSourceManager.getDataSource(name).em;
38
39
  }
39
40
  });
40
41
  this.decoratorService.registerPropertyHandler(decorator_1.DATA_SOURCE_KEY, (propertyName, meta) => {
@@ -44,6 +45,26 @@ let MikroConfiguration = class MikroConfiguration {
44
45
  }
45
46
  async onReady(container) {
46
47
  this.dataSourceManager = await container.getAsync(dataSourceManager_1.MikroDataSourceManager);
48
+ const names = this.dataSourceManager.getDataSourceNames();
49
+ const entityManagers = names.map(name => {
50
+ return this.dataSourceManager.getDataSource(name).em;
51
+ });
52
+ if (names.length > 0) {
53
+ // create mikro request scope
54
+ // https://mikro-orm.io/docs/identity-map
55
+ this.applicationManager.getApplications().forEach(app => {
56
+ app.useMiddleware(async (ctx, next) => {
57
+ if (core_2.RequestContext['createAsync']) {
58
+ // mikro-orm 5.x
59
+ return await core_2.RequestContext['createAsync'](entityManagers, next);
60
+ }
61
+ else {
62
+ // mikro-orm 6.x
63
+ return await core_2.RequestContext.create(entityManagers, next);
64
+ }
65
+ });
66
+ });
67
+ }
47
68
  }
48
69
  async onStop(container) {
49
70
  if (this.dataSourceManager) {
@@ -20,12 +20,18 @@ let MikroDataSourceManager = class MikroDataSourceManager extends core_1.DataSou
20
20
  return 'mikro';
21
21
  }
22
22
  async createDataSource(config, dataSourceName) {
23
+ var _a;
23
24
  if (config.logger && typeof config.logger === 'string') {
24
25
  const logger = this.loggerService.getLogger(config.logger);
25
26
  config.logger = message => {
26
27
  logger.info(message);
27
28
  };
28
29
  }
30
+ // https://mikro-orm.io/docs/usage-with-nestjs#multiple-database-connections
31
+ if (!config.contextName) {
32
+ config.contextName = dataSourceName;
33
+ }
34
+ config.registerRequestContext = (_a = config.registerRequestContext) !== null && _a !== void 0 ? _a : false;
29
35
  return core_2.MikroORM.init(config);
30
36
  }
31
37
  async checkConnected(dataSource) {
package/package.json CHANGED
@@ -1,6 +1,6 @@
1
1
  {
2
2
  "name": "@midwayjs/mikro",
3
- "version": "3.15.2",
3
+ "version": "3.15.3",
4
4
  "main": "dist/index.js",
5
5
  "typings": "index.d.ts",
6
6
  "files": [
@@ -12,9 +12,9 @@
12
12
  "@midwayjs/core": "^3.15.0",
13
13
  "@midwayjs/koa": "^3.15.2",
14
14
  "@midwayjs/mock": "^3.15.2",
15
- "@mikro-orm/core": "6.1.8",
16
- "@mikro-orm/entity-generator": "6.1.8",
17
- "@mikro-orm/sqlite": "6.1.8"
15
+ "@mikro-orm/core": "6.1.10",
16
+ "@mikro-orm/entity-generator": "6.1.10",
17
+ "@mikro-orm/sqlite": "6.1.10"
18
18
  },
19
19
  "author": {
20
20
  "name": "czy88840616",
@@ -33,5 +33,5 @@
33
33
  "type": "git",
34
34
  "url": "https://github.com/midwayjs/midway.git"
35
35
  },
36
- "gitHead": "e8b53689c50aa8c9a691de7d985cafd62f5f70e7"
36
+ "gitHead": "5d2dd74111b1b28d9bbf2cd533306119e3b58dce"
37
37
  }