@typegoose/typegoose": "^7.2.0"
Is there any way to have generic CRUD repository with typescirpt and typegoose?
In particular, how to get the method getModelForClass() work with generics?
Something similar to this code borrowed from the question 246 .
If so, what am I missing?
import { prop, getModelForClass } from '@typegoose/typegoose';
import { AnyParamConstructor, ReturnModelType } from '@typegoose/typegoose/lib/types';
export class GenericCRUDService<T, U extends AnyParamConstructor<T> = AnyParamConstructor<T>> {
dataModel: ReturnModelType<U, T>;
constructor(cls: U) {
this.dataModel = getModelForClass<T, U>(cls);
}
public create(data: T) {
this.dataModel.create(data);
}
}
export class Cat {
@prop()
public age: number;
@prop()
public color: string;
}
export class Dog {
@prop()
public isBarking: boolean;
@prop()
public race: string;
}