Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

feat: support multi-datasource for typegoose #3761

Open
wants to merge 2 commits into
base: main
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
42 changes: 15 additions & 27 deletions packages/typegoose/src/configuration.ts
Original file line number Diff line number Diff line change
@@ -1,14 +1,14 @@
import {
Config,
Configuration,
getClassMetadata,
Init,
Inject,
listModule,
App,
IMidwayApplication,
IMidwayContainer,
MidwayDecoratorService,
MidwayCommonError,
} from '@midwayjs/core';
import * as mongoose from '@midwayjs/mongoose';
import { ENTITY_MODEL_KEY } from './interface';
Expand All @@ -31,7 +31,7 @@ export class TypegooseConfiguration {
@Inject()
decoratorService: MidwayDecoratorService;

modelMap = new WeakMap();
dataSourceManager: mongoose.MongooseDataSourceManager;

@Init()
async init() {
Expand All @@ -44,39 +44,27 @@ export class TypegooseConfiguration {
connectionName: string;
}
) => {
return this.modelMap.get(meta.modelKey);
// get connection
const conn = this.dataSourceManager.getDataSource(
meta.connectionName ||
this.dataSourceManager.getDataSourceNameByModel(meta.modelKey) ||
this.dataSourceManager.getDefaultDataSourceName()
);
if (!conn) {
throw new MidwayCommonError(
`DataSource ${meta.connectionName} not found with current model ${meta.modelKey}, please check it.`
);
}
return getModelForClass(meta.modelKey, { existingConnection: conn });
}
);
}

async onReady(container: IMidwayContainer) {
const connectionFactory = await container.getAsync(
this.dataSourceManager = await container.getAsync(
mongoose.MongooseDataSourceManager
);

for (const dataSourceName of connectionFactory.getDataSourceNames()) {
const conn = connectionFactory.getDataSource(dataSourceName);
if (conn && (conn as any).entities) {
for (const Model of (conn as any).entities) {
const model = getModelForClass(Model, { existingConnection: conn });
this.modelMap.set(Model, model);
}
}
}

const Models = listModule(ENTITY_MODEL_KEY);
// 兼容老代码
for (const Model of Models) {
const metadata = getClassMetadata(ENTITY_MODEL_KEY, Model) ?? {};
const connectionName = metadata.connectionName ?? 'default';
const conn = connectionFactory.getDataSource(connectionName);
if (conn) {
const model = getModelForClass(Model, { existingConnection: conn });
this.modelMap.set(Model, model);
} else {
throw new Error(`connection name ${metadata.connectionName} not found`);
}
}

// 兼容老代码
if (Models.length === 0 && this.oldMongooseConfig['uri']) {
Expand Down
3 changes: 2 additions & 1 deletion packages/typegoose/src/decorator/injectEntityModel.ts
Original file line number Diff line number Diff line change
@@ -1,8 +1,9 @@
import { createCustomPropertyDecorator } from '@midwayjs/core';
import { ENTITY_MODEL_KEY } from '../interface';

export function InjectEntityModel(modelKey: any) {
export function InjectEntityModel(modelKey: any, connectionName?: string) {
return createCustomPropertyDecorator(ENTITY_MODEL_KEY, {
modelKey,
connectionName,
});
}
Loading