【发布时间】:2020-10-17 07:33:27
【问题描述】:
@typegoose/typegoose": "^7.2.0"
有什么方法可以使用 typecirpt 和 typegoose 获得通用 CRUD 存储库?
特别是,如何让getModelForClass() 方法与泛型一起工作?
类似于从question 246 借来的这段代码。
如果是这样,我错过了什么?
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;
}
【问题讨论】:
标签: mongodb typescript mongoose typegoose