有没有办法通过先前定义的 Sequelize 模型获取属性 // 关联?

Is there a way to get attributes // associations by previously defined Sequelize Model?

我需要通过之前定义的 Sequelize 模型获取一些数据。

我需要的:

* attributes list
  * attribute name
  * attribute type (INTEGER, STRING,...)
  * was it generated by association method?
* list of associations
  * association type (belongsTo, hasMany, ...)

由于某些原因,很难在控制台中检查 Sequelize 模型:

> db.sequelize.models.Payment
Payment // <- it's valid Sequelize Model {Object}, however its not inspectable

> db.sequelize.models.Payment.attributes
...
type:
 { type: { values: [Object] },
   values: [ 'cash', 'account', 'transfer' ],
   Model: Payment,
   fieldName: 'type',
   _modelAttribute: true,
   field: 'type' },
sum: 
 { type: 
    { options: [Object],
      _length: undefined,
      _zerofill: undefined,
      _decimals: undefined,
      _precision: undefined,
      _scale: undefined,
      _unsigned: undefined },
   Model: Payment,
   fieldName: 'sum',
   _modelAttribute: true,
   field: 'sum' },
 ...

如您所见,没有关于字段类型的实际信息。协会也是如此。

那么,有没有可靠的"official" 方法可以从模型 class 中提取这些数据,而无需挖掘和反转对象?

尝试 Payment.rawAttributes,这是一个以 属性 名称作为键的对象,以及一个具有 属性 详细信息的对象。 property.type.key 是一个类型为.

的字符串

Payment.associations 是关联对象——键是名称,每个关联都会有一个 associationType 属性——你也可以做 association instanceof sequelize.Association.BelongsTo 等。