根据文档 - 您需要 1)声明一个类,2)获取类的模型和 3)使用模型创建或检索数据到另一个对象(见下文)
每次通过函数 (getModelForClass) 创建/声明模型似乎是浪费开销。是否可以通过继承或实现在模块中创建它所需的属性来声明它?
class User {
@prop()
public name?: string;
}
const UserModel = getModelForClass(User); // UserModel is a regular Mongoose Model with correct types
(async () => {
await mongoose.connect('mongodb://localhost:27017/', { useNewUrlParser: true, useUnifiedTopology: true, dbName: "test" });
const { _id: id } = await UserModel.create({ name: 'JohnDoe' } as User);
// an "as" assertion, to have types for all properties
const user = await UserModel.findById(id).exec();