Search code examples
node.jssequelize.js

Sequelize find. how to include constant value attributes in a find query


¿How can I retrieve all attributes from a table, plus a new column/attribute with a constant value for every row? As I saw on the docs, it shuld be something similar to this:

my_model.findAll({
    attributes:{
        include:[['constant_value','new_attribute_name']]
    }
}).then(...);

Any clue?

Thank you!


Solution

  • The attributes object is to specifically select attributes that are already on the model.

    From what I can gather you can't add attributes to models but you can however add methods to the model.

    See the documentation for those methods here

    For example you could create a simple class/instance method that simply returns the constant.

    You could also create a hook that adds the constant to the model after it has been retrieved.