I need to find the latest entry in a table. What is the best way to do this? The table has Sequelize's default createdAt
field.
To use Sequelize findOne to find the latest entry, we can use the findOne method with the order property to order items by createdAt descending. model. findOne({ where: { key }, order: [ ['createdAt', 'DESC'] ], });
To set the Sequelize findAll sort order in Node. js, we can set the order property. const getStaticCompanies = () => { return Company. findAll({ where: { //... }, order: [ ['id', 'DESC'], ['name', 'ASC'], ], attributes: ['id', 'logo_version', 'logo_content_type', 'name', 'updated_at'] }); };
model.findOne({ where: { key }, order: [ [ 'createdAt', 'DESC' ]], });
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With