Mongoose 在为服务扩展类时,如何处理NestJS依赖项注入?
我正在尝试根据我的Mongoose 在为服务扩展类时,如何处理NestJS依赖项注入?,mongoose,dependency-injection,subclass,extends,nestjs,Mongoose,Dependency Injection,Subclass,Extends,Nestjs,我正在尝试根据我的ConfigService中的值提供不同的服务 我遇到的问题是,在执行诸如findOne()(结果为null)或countDocuments()(结果为0)之类的查询方法时,被注入的mongoose模型不会返回任何值 我的服务类别定义如下: 导出类基本服务{ 构造函数(@InjectModel('Cat')公共只读catModel:Model){} createService(选项:字符串){ 如果(选项=='其他'){ 返回新的OtherService(this.catMod
ConfigService
中的值提供不同的服务
我遇到的问题是,在执行诸如findOne()
(结果为null
)或countDocuments()
(结果为0
)之类的查询方法时,被注入的mongoose模型不会返回任何值
我的服务类别定义如下:
导出类基本服务{
构造函数(@InjectModel('Cat')公共只读catModel:Model){}
createService(选项:字符串){
如果(选项=='其他'){
返回新的OtherService(this.catModel);
}else if(选项===“另一个”){
返回新的其他服务(this.catModel);
}否则{
返回新的BaseService(this.catModel);
}
}
异步findOne(id:string):承诺{
返回等待this.catModel.findOne({u id:id});
}
异步计数():承诺{
return等待这个.catModel.countDocuments();
}
testClass(){
log(“使用的基本服务类”);
}
}
@可注射()
导出类OtherService扩展BaseService{
构造函数(@InjectModel('Cat')公共只读catModel:Model){
超级(catModel);
}
testClass(){
log(“使用的其他服务类”);
}
}
@可注射()
导出类AnotherService扩展BaseService{
构造函数(@InjectModel('Cat')公共只读catModel:Model){
超级(catModel);
}
testClass(){
log(“使用了另一个服务类”);
}
}
这允许我从我的提供者那里获得正确的服务(testClass()
打印期望的字符串)。我的提供者如下所示:
导出常量catProviders=[
{
提供:“CatModelToken”,
useFactory:(连接:连接)=>connection.model('CAT',CatSchema),
注入:['DbConnectionToken'],
},
{
提供:“BaseService”,
useFactory:(配置服务:配置服务,连接:连接)=>{
const options=ConfigService.get('SERVICE_TYPE');
让model=connection.model('CAT',CatSchema);
返回新的BaseService(模型).createService(选项);
},
inject:[ConfigService,'CatModelToken','DbConnectionToken'],
}
];
因此,我的问题分为两部分:
- 是否有更好的方法来处理正确类和
为了避免为sole创建
实例 调用BaseService
的目的createService()
- 将mongoose模型注入新创建的服务的正确方法是什么
我也不能使用文档中的
useClass
示例,因为我需要能够注入ConfigService
您可以使用工厂方法解决此问题,请尝试以下方法:
确定服务“形状”的接口:
export interface IDatabaseService {
findOne(id: string): Promise<Cat>;
count(): Promise<number>;
testClass(): void;
}
工厂类是被注入的对象:
@Injectable()
export class DatabaseServiceFactory {
constructor(@InjectModel('Cat') private readonly catModel: Model<Cat>) {}
createService(name: string) : IDatabaseService {
switch(name) {
case 'other': return new OtherService(this.catModel);
case 'another': return new AnotherService(this.catModel);
default: throw new Error(`No service has been implemented for the name "${name}"`);
}
}
}
虽然此链接可以回答问题,但最好在此处包含答案的基本部分,并提供链接供参考。如果链接页面发生更改,则仅链接的答案可能无效-
export class OtherService extends BaseService {
constructor(@InjectModel('Cat') public readonly catModel: Model<Cat>) {
super(catModel);
}
testClass() {
console.log('OTHER SERVICE CLASS USED');
}
}
export class AnotherService extends BaseService {
constructor(@InjectModel('Cat') public readonly catModel: Model<Cat>) {
super(catModel);
}
testClass() {
console.log('ANOTHER SERVICE CLASS USED');
}
}
@Injectable()
export class DatabaseServiceFactory {
constructor(@InjectModel('Cat') private readonly catModel: Model<Cat>) {}
createService(name: string) : IDatabaseService {
switch(name) {
case 'other': return new OtherService(this.catModel);
case 'another': return new AnotherService(this.catModel);
default: throw new Error(`No service has been implemented for the name "${name}"`);
}
}
}
export const catProviders = [
{
provide: 'CatModelToken',
useFactory: (connection: Connection) => connection.model('CAT', CatSchema),
inject: ['DbConnectionToken'],
},
{
provide: 'BaseService',
useFactory: (ConfigService: ConfigService, connection: Connection, dbFactory: DatabaseServiceFactory) => {
const options = ConfigService.get('SERVICE_TYPE');
let model = connection.model('CAT', CatSchema);
//return new BaseService(model).createService(options);
return dbFactory.createService(options);
},
inject: [
ConfigService,
'CatModelToken',
'DbConnectionToken',
DatabaseServiceFactory
],
}
];