If it is possible to fetch some particular field all the time #14074
-
I have a doubt If it is possible to fetch some particular field all the time or in all the fetch without mentioning the attribute name in all the fetch. If there is any way to fetch automatically that field all the time? Example: In this, if I put member.findAll({ attributes: ['id','name'] }) alone then the "email" field all should be fetched. |
Beta Was this translation helpful? Give feedback.
Replies: 1 comment
-
I don't think scopes would work here because setting the I think it would look like this, but this code hasn't been tested: YourModel.beforeFind((options) => {
if (Array.isArray(options.attributes) && !options.attributes.includes('email')) {
options.attributes = [...options.attributes, 'email'];
}
}); |
Beta Was this translation helpful? Give feedback.
I don't think scopes would work here because setting the
attributes
option field would override the one from the scope but you should be able to achieve what you're trying to do using thebeforeFind
hook: It'll receive the options and you can add the attributes you want to them.I think it would look like this, but this code hasn't been tested: