Is there a way to get the attributes of // associations using a previously defined Sequelize Model?

I need to get some data using a previously defined Sequelize Model.

What I need:

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

For some reason, itโ€™s quite difficult to test the Sequelize models in the console:

 > db.sequelize.models.Payment Payment // <- it 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' }, ... 

As you can see, there is no actual information about the types of fields. The same thing happens with associations.

So, is there any reliable โ€œofficialโ€ way to extract this data from the Model class without digging and reversing the object?

+5
source share
1 answer

Try Payment.rawAttributes , which is an object with the property name as a key and an object with property data. property.type.key is a string with a type.

Payment.associations - association object - the key is the name and each association will have the associationType property - you can also make an association instanceof sequelize.Association.BelongsTo , etc.

+13
source

Source: https://habr.com/ru/post/1236563/


All Articles