【问题标题】:How to Declare Typegoose Models instead of instantiate如何声明 Typegoose 模型而不是实例化
【发布时间】:2020-07-06 16:56:54
【问题描述】:

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

【问题讨论】:

    标签: typegoose


    【解决方案1】:

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

    【讨论】:

      猜你喜欢
      • 2016-10-19
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2011-08-11
      • 1970-01-01
      • 1970-01-01
      • 2011-09-19
      • 1970-01-01
      相关资源
      最近更新 更多