Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Sequelize findOne latest entry

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.

like image 718
Noah Avatar asked Feb 17 '16 00:02

Noah


People also ask

How do I get last data in Sequelize?

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'] ], });

How do I sort data in Sequelize?

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'] }); };


1 Answers

model.findOne({     where: { key },     order: [ [ 'createdAt', 'DESC' ]], }); 
like image 68
szym Avatar answered Sep 30 '22 05:09

szym