Mongodb、Typegoose - 通用存储库模式

标签 mongodb typescript mongoose typegoose

@typegoose/typegoose": "^7.2.0"

有没有办法拥有带有 typescirpt 和 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;
}

最佳答案

引用:typegoose/typegoose#303

如果您想要一个“管理器类”而不是通用的实际模型,这很简单,您只需要拥有正确的类型

我建议这样编写类(class):

// NodeJS: 14.4.0
// MongoDB: 4.2-bionic (Docker)
import { getModelForClass, prop, types, ReturnModelType, DocumentType } from "@typegoose/typegoose"; // @typegoose/<a href="https://stackoverflow.com/cdn-cgi/l/email-protection" class="__cf_email__" data-cfemail="12666b6277757d7d617752253c203c22" rel="noreferrer noopener nofollow">[email protected]</a>
import * as mongoose from "mongoose"; // <a href="https://stackoverflow.com/cdn-cgi/l/email-protection" class="__cf_email__" data-cfemail="7c1113121b13130f193c495245524d44" rel="noreferrer noopener nofollow">[email protected]</a> @types/<a href="https://stackoverflow.com/cdn-cgi/l/email-protection" class="__cf_email__" data-cfemail="bbd6d4d5dcd4d4c8defb8e958c95898c" rel="noreferrer noopener nofollow">[email protected]</a>

export class GenericCRUDService<U extends types.AnyParamConstructor<any>> {
  public dataModel: ReturnModelType<U>;

  constructor(cls: U) {
    this.dataModel = getModelForClass(cls);
  }

  public create(data: mongoose.CreateQuery<DocumentType<InstanceType<U>>>) {
    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;
}

(async () => {
  await mongoose.connect(`mongodb://localhost:27017/`, { useNewUrlParser: true, dbName: "verifyMASTER", useCreateIndex: true, useUnifiedTopology: true });

  const CatService = new GenericCRUDService(Cat);

  await CatService.create({}); // with type support (since ~@types/<a href="https://stackoverflow.com/cdn-cgi/l/email-protection" class="__cf_email__" data-cfemail="e08d8f8e878f8f9385a0d5ced7ced2d1" rel="noreferrer noopener nofollow">[email protected]</a>)

  await mongoose.disconnect();
})();

(采用旧示例的问题是,自 7.1.0 以来,“不必要的”T 泛型已被删除)

关于Mongodb、Typegoose - 通用存储库模式,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/62599137/

相关文章:

javascript - 你可以在nodejs Controller 的变量中使用mongodb表名吗?

python - 类型错误 : Object of type function is not JSON serializable when using flask_jwt_extended int RESTful API

node.js - 导出类静态方法

javascript - 使用 formData 上传文件时输入错误

javascript - Typescript,自返回通用

node.js - 为mongoose查找查询结果添加一个字段

Mongodb -Moogose 查询数组字段过滤器

java - MongoDB java API 聚合 $lookup 和管道使用变量

filter - MongoDB - 在结果集中过滤内部数组的内容

javascript - 如何在保存到 Mongoose (ExpressJS) 之前格式化模型中的数据