我需要通过之前定义的Sequelize Model获得一些数据。
我需要什么:
* 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' },
...
如您所见,没有关于字段类型的实际信息。关联也是如此。
那么,是否有任何可靠的官员&#34;从Model类中提取这些数据而不挖掘和反转对象的方法?
答案 0 :(得分:29)
尝试Payment.rawAttributes
,它是一个具有属性名称作为键的对象,以及一个具有属性详细信息的对象。 property.type.key
是一个类型为字符串的字符串。
Payment.associations
是关联对象 - 关键是名称,每个关联都有associationType
属性 - 您也可以association instanceof sequelize.Association.BelongsTo
等。