【问题标题】:Pass a Sequelize model as property in Node using Typescript使用 Typescript 在 Node 中将 Sequelize 模型作为属性传递
【发布时间】:2019-04-13 02:39:34
【问题描述】:

我正在使用 Node、Typescript 和 Sequelize 构建一个项目。我想定义一个具有以下功能的 GENERAL 控制器:

class Controller {
  Schema: <Sequelize-Model>;

  constructor(schema: <Sequelize-Model>) {
    this.Schema = schema
  }

  find(req: Request, res: Response, next: NextFunction) {
    return this.Schema
      .findWithCount(req.query)
      .then(collection => res.status(200).json(collection))
      .catch(next);
  }
}

问题是,我不知道在 .我尝试过使用 any、Sequelize 的 Model、sequelize-typescript 的 Model 以及其他一些选项。想法?

【问题讨论】:

标签: node.js typescript sequelize.js sequelize-typescript


【解决方案1】:

你可以这样做,

型号:

@Table({
 })

 export class model1 extends Model<model1> {
     @PrimaryKey
     @Column({
         allowNull: false,
         type: DataType.UUID,
     })
     public id: string;

 }

 import { model1 } from '../../model/model1';
 export default class Repository {
     public find(req: any): Promise<any> {
         return new Promise((resolve, reject) => {
             model1.findAndCount({
                 where: req.query
             }).then(
                 (results) => {
                     resolve(results);
                 },
                 err => {
                     reject(err);
                 }
             );
         });
     }
 }

【讨论】:

    猜你喜欢
    • 2019-10-20
    • 1970-01-01
    • 2015-10-02
    • 2019-11-29
    • 1970-01-01
    • 2021-11-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多