Skip to content

Commit

Permalink
fix: Ensure dataSourceName used in InjectRepository (#3709)
Browse files Browse the repository at this point in the history
  • Loading branch information
odex21 authored Mar 18, 2024
1 parent 5d2dd74 commit 3eaaaa6
Show file tree
Hide file tree
Showing 4 changed files with 33 additions and 20 deletions.
18 changes: 9 additions & 9 deletions packages/mikro/src/configuration.ts
Original file line number Diff line number Diff line change
Expand Up @@ -50,17 +50,17 @@ export class MikroConfiguration implements ILifeCycle {
connectionName?: string;
}
) => {
if (RequestContext.getEntityManager()) {
return RequestContext.getEntityManager().getRepository(meta.modelKey);
const name =
meta.connectionName ||
this.dataSourceManager.getDefaultDataSourceName();

if (RequestContext.getEntityManager(name)) {
return RequestContext.getEntityManager(name).getRepository(
meta.modelKey
);
} else {
return this.dataSourceManager
.getDataSource(
meta.connectionName ||
this.dataSourceManager.getDataSourceNameByModel(
meta.modelKey
) ||
this.dataSourceManager.getDefaultDataSourceName()
)
.getDataSource(name)
.em.getRepository(meta.modelKey);
}
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -13,8 +13,6 @@ export default (appInfo: MidwayAppInfo): MidwayConfig => {
dbName: join(appInfo.appDir, 'test1.sqlite'),
driver: SqliteDriver, // 这里使用了 sqlite 做示例
// debug: true,
// allowGlobalContext: true,
contextName: 'default1',
timezone: '+08:00',
loadStrategy: LoadStrategy.JOINED,
// forceUtcTimezone: true, //可以强制将日期保存在不带时区的日期
Expand Down
Original file line number Diff line number Diff line change
@@ -1,15 +1,26 @@
import { Controller, Get } from '@midwayjs/core';
import { InjectEntityManager } from '../../../../../../../../src';
import {
InjectEntityManager,
InjectRepository,
} from '../../../../../../../../src';
import { EntityManager } from '@mikro-orm/core';
import { User } from '../entity/user.entity';
import { EntityRepository } from '@mikro-orm/sqlite';
@Controller('/m1')
export class HomeController {

@InjectEntityManager('default1')
em: EntityManager;

@InjectRepository(User, 'default1')
userRepo2: EntityRepository<User>;

@Get('/')
async home() {
return await this.em.findAll(User);
}

@Get('/withEntity')
async withEntity() {
return await this.userRepo2.findAll();
}
}
18 changes: 11 additions & 7 deletions packages/mikro/test/index.test.ts
Original file line number Diff line number Diff line change
@@ -1,6 +1,11 @@
import { join } from 'path';
import { existsSync, unlinkSync } from 'fs';
import { close, createApp, createLightApp, createHttpRequest } from '@midwayjs/mock';
import {
close,
createApp,
createLightApp,
createHttpRequest,
} from '@midwayjs/mock';
import { IMidwayApplication } from '@midwayjs/core';

describe('/test/index.test.ts', () => {
Expand All @@ -24,12 +29,8 @@ describe('/test/index.test.ts', () => {
});

it('test multi entity manager in different component', async () => {
cleanFile(
join(__dirname, 'fixtures/multi-enitymanager', 'test.sqlite')
);
cleanFile(
join(__dirname, 'fixtures/multi-enitymanager', 'test1.sqlite')
);
cleanFile(join(__dirname, 'fixtures/multi-enitymanager', 'test.sqlite'));
cleanFile(join(__dirname, 'fixtures/multi-enitymanager', 'test1.sqlite'));

const app: IMidwayApplication = await createApp(
join(__dirname, 'fixtures/multi-enitymanager'),
Expand All @@ -38,6 +39,9 @@ describe('/test/index.test.ts', () => {
let result = await createHttpRequest(app).get('/m1').expect(200);
expect(result.text).toEqual('[]');

result = await createHttpRequest(app).get('/m1/withEntity').expect(200);
expect(result.text).toEqual('[]');

result = await createHttpRequest(app).get('/').expect(200);
expect(result.text).toEqual('[[],[]]');

Expand Down

0 comments on commit 3eaaaa6

Please sign in to comment.