Sequelize findOne latest entry
model.findOne({
where: { key },
order: [ [ 'createdAt', 'DESC' ]],
});
If someone has turned timestamps false, you can do it with id too. This one worked for me.
model.findOne({
order: [ [ 'id', 'DESC' ]],
});
Method findOne
is wrapper for findAll
method. Therefore you can simply use findAll
with limit 1 and order by id descending.
Example:
YourModel.findAll({
limit: 1,
where: {
//your where conditions, or without them if you need ANY entry
},
order: [ [ 'createdAt', 'DESC' ]]
}).then(function(entries){
//only difference is that you get users list limited to 1
//entries[0]
});