Returning a single field from Model in Sequelize with findOne/findAll #17270
Unanswered
guenonjerome
asked this question in
Help & Questions
Replies: 1 comment 2 replies
-
That is currently the only way, sorry
Do you have links / examples? I'm curious about what it looks like |
Beta Was this translation helpful? Give feedback.
2 replies
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
-
Hello! Is there a way to retrieve from database, using the last version of Sequelize without the overhead of loading a model(and of course, more elegant than the following)?
const product = await Product.findOne({ where: { id: id }, attributes: ['hash'], transaction: tr }); return product?.hash;
I know that if I pass { raw: true }, the overhead is supposed to be minimal, but even so, the code above is not very idiomatic nor elegant for the purpose of getting a value from just one field, it's too verbose as well.
Please don't tell me to do a raw SQL query, I already know that can be done, but I'm choosing not to, because of reasons I wont go in to details on here.
Look for instance how elegantly Sequelize handles this:
return await Product.max<number, Product>('level', { where: { hash: hash }, transaction: tr } );
I was looking for something similar, but so far have been unsuccessful. In the world of Java that is so simple and beautiful, as well as available since way way way back.
Thanks.
Beta Was this translation helpful? Give feedback.
All reactions