I am trying to provide a different service based on a value from my ConfigService
.
The problem I am running into is that the mongoose model that gets injected does not return any values when executing query methods such as findOne()
(result is null
) or countDocuments()
(result is 0
).
My service classes are defined as follows:
export class BaseService {
constructor(@InjectModel('Cat') public readonly catModel: Model<Cat>) {}
createService(option: string) {
if (option === 'OTHER') {
return new OtherService(this.catModel);
} else if (option === 'ANOTHER') {
return new AnotherService(this.catModel);
} else {
return new BaseService(this.catModel);
}
}
async findOne(id: string): Promise<Cat> {
return await this.catModel.findOne({_id: id});
}
async count(): Promise<number> {
return await this.catModel.countDocuments();
}
testClass() {
console.log('BASE SERVICE CLASS USED');
}
}
@Injectable()
export class OtherService extends BaseService {
constructor(@InjectModel('Cat') public readonly catModel: Model<Cat>) {
super(catModel);
}
testClass() {
console.log('OTHER SERVICE CLASS USED');
}
}
@Injectable()
export class AnotherService extends BaseService {
constructor(@InjectModel('Cat') public readonly catModel: Model<Cat>) {
super(catModel);
}
testClass() {
console.log('ANOTHER SERVICE CLASS USED');
}
}
This allows me to get the correct service from my provider (testClass()
prints the expected string). My provider looks like this:
export const catProviders = [
{
provide: 'CatModelToken',
useFactory: (connection: Connection) => connection.model('CAT', CatSchema),
inject: ['DbConnectionToken'],
},
{
provide: 'BaseService',
useFactory: (ConfigService: ConfigService, connection: Connection) => {
const options = ConfigService.get('SERVICE_TYPE');
let model = connection.model('CAT', CatSchema);
return new BaseService(model).createService(options);
},
inject: [ConfigService, 'CatModelToken', 'DbConnectionToken'],
}
];
So my question is in two parts:
- Is there a better way to handle the creation of the correct class and
to avoid having to create a
BaseService
instance for the sole purpose of callingcreateService()
? - What is the proper way to inject the mongoose model into the newly created service?
I also cannot use the useClass
example from the documentation, since I need to be able to inject the ConfigService
.