【问题标题】:What is BuildOptions in sequelizesequelize 中的 BuildOptions 是什么
【发布时间】:2021-11-03 07:20:19
【问题描述】:

我已经阅读了 sequelize 文档并在 Google 上搜索了这个问题。找不到任何解释。为什么需要 BuildOptions 以及它如何影响创建的模型?

import { Sequelize, Model, DataTypes, BuildOptions } from 'sequelize';
// We need to declare an interface for our model that is basically what our class would be
interface MyModel extends Model {
  readonly id: number;
}

// Need to declare the static model so `findOne` etc. use correct types.
type MyModelStatic = typeof Model & {
  new (values?: object, options?: BuildOptions): MyModel;
}

【问题讨论】:

    标签: typescript express sequelize.js


    【解决方案1】:

    在 node_modules/sequelize 文件夹中搜索 BuildOptions 后,这会出现在 node_modules/sequelize/types/lib/model.d.ts 中的第 630-651 行,在 sequelize 的版本 6.6.5 中。

    /**  
      * Options for Model.build method                                                                       
      */  
     export interface BuildOptions {
       /**
        * If set to true, values will ignore field and virtual setters.
        */                      
       raw?: boolean;
                                                                              
       /**
        * Is this record new
        */
       isNewRecord?: boolean;
      
       /**
        * An array of include options. A single option is also supported - Used to build prefetched/included model instances. See `set`
        * 
        * TODO: See set                 
        */
       include?: Includeable | Includeable[];
    }
    

    这似乎与调用Model.build 方法在sequelize 中构造新模型实例时使用的接口相同,然后将其保存到数据库。这是api reference 的链接,它是build 方法的第二个参数。另外,这里是手册中的relevant pages

    这几乎必须是问题中提到的相同接口,因为它是出现在 node_modules/sequelize 文件夹中的BuildOptions 的唯一定义。

    作为旁注,虽然 version 5 of the manual 中提到了它(请参阅原始问题中的代码),但 version 6 的文档中的相同页面中没有它。

    【讨论】:

      猜你喜欢
      • 2016-11-12
      • 2018-12-20
      • 2020-07-22
      • 2016-10-24
      • 2018-10-25
      • 2019-06-08
      • 2016-12-16
      • 1970-01-01
      • 2015-12-31
      相关资源
      最近更新 更多