Skip to content

Commit 4bfd889

Browse files
committed
refacotr: support default config for orm.client
1 parent 3fc2998 commit 4bfd889

File tree

16 files changed

+121
-205
lines changed

16 files changed

+121
-205
lines changed

core/orm-decorator/src/decorator/Model.ts

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -3,6 +3,7 @@ import { ModelInfoUtil } from '../util/ModelInfoUtil';
33

44
export interface ModelParams {
55
tableName?: string;
6+
dataSource?: string;
67
}
78

89
export const MODEL_PROTO_IMPL_TYPE = 'MODEL_PROTO';
@@ -18,6 +19,9 @@ export function Model(param?: ModelParams) {
1819
if (param?.tableName) {
1920
ModelInfoUtil.setTableName(param.tableName, clazz);
2021
}
22+
if (param?.dataSource) {
23+
ModelInfoUtil.setDataSource(param.dataSource, clazz);
24+
}
2125
func(clazz);
2226
};
2327
}

plugin/orm/lib/LeoricRegister.ts

Lines changed: 20 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -17,22 +17,35 @@ export class LeoricRegister extends Base {
1717
this.realmMap = new Map();
1818
}
1919

20-
getOrCreateRealm(datasource: string | undefined): any {
20+
getConfig(datasource?: string) {
2121
let config: OrmConfig | undefined;
2222
if (!datasource) {
2323
config = this.dataSourceManager.getDefaultConfig();
2424
} else {
2525
config = this.dataSourceManager.getConfig(datasource);
2626
}
27-
if (!config) {
28-
throw new Error(`not found datasource for ${datasource}`);
27+
return config;
28+
}
29+
30+
getRealm(config: OrmConfig | undefined): Realm | undefined {
31+
if (!config?.database) {
32+
return undefined;
2933
}
30-
let realm = this.realmMap.get(config.database);
31-
if (realm) {
32-
return realm;
34+
const realm = this.realmMap.get(config.database);
35+
return realm;
36+
}
37+
38+
getOrCreateRealm(datasource: string | undefined): any {
39+
const config = this.getConfig(datasource);
40+
let realm: Realm | undefined;
41+
if (config) {
42+
realm = this.getRealm(config);
43+
if (realm) {
44+
return realm;
45+
}
3346
}
3447
realm = new (Realm as any)({ ...config });
35-
this.realmMap.set(config.database, realm);
48+
this.realmMap.set(config!.database, realm);
3649
return realm;
3750
}
3851

plugin/orm/lib/SingletonORM.ts

Lines changed: 8 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -15,11 +15,16 @@ export class Orm {
1515

1616
// default dataSource
1717
get client(): Realm {
18-
return this.getClient(undefined);
18+
const defaultConfig = this.leoricRegister.getConfig();
19+
return this.leoricRegister.getRealm(defaultConfig)!;
1920
}
2021

21-
getClient(dataSource: string | undefined): Realm {
22-
return this.leoricRegister.getOrCreateRealm(dataSource);
22+
getClient(datasource: string): Realm {
23+
const config = this.leoricRegister.getConfig(datasource);
24+
if (!config) {
25+
throw new Error(`not found ${datasource} datasource`);
26+
}
27+
return this.leoricRegister.getOrCreateRealm(config.database)!;
2328
}
2429

2530
}

plugin/orm/test/fixtures/apps/multi-db-app/app.ts

Lines changed: 0 additions & 24 deletions
This file was deleted.

plugin/orm/test/fixtures/apps/multi-db-app/config/config.default.js

Lines changed: 0 additions & 47 deletions
This file was deleted.

plugin/orm/test/fixtures/apps/multi-db-app/config/module.json

Lines changed: 0 additions & 5 deletions
This file was deleted.

plugin/orm/test/fixtures/apps/multi-db-app/config/plugin.js

Lines changed: 0 additions & 16 deletions
This file was deleted.

plugin/orm/test/fixtures/apps/multi-db-app/modules/multi-db-app/DBService.ts

Lines changed: 0 additions & 17 deletions
This file was deleted.

plugin/orm/test/fixtures/apps/multi-db-app/modules/multi-db-app/package.json

Lines changed: 0 additions & 6 deletions
This file was deleted.

plugin/orm/test/fixtures/apps/multi-db-app/package.json

Lines changed: 0 additions & 3 deletions
This file was deleted.

0 commit comments

Comments
 (0)