0

根据文档 - 您需要 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();
4

1 回答 1

0

如果没有 typegoose 完全重新实现模式和模型在 mongoose 中的制作方式,这是不可能的

于 2020-07-06T08:56:54.180 回答