Post .findAll({where: {tag: 'news'}, limit: 10}) .success(function(result) { ... })
How to insert the condition of sorting by date in my query with not using sequelize.query
like
.findAll({ limit: 10, sort: [updatedAt, descending]})
You need to specify the column name and the order of the sort as a single Array unit inside the order array. If you only need to order by one column, you still need to put the ordering array inside the order array as follows: Cities. findAll({ order: [["population", "DESC"]], }).
const TODAY = new Date(); const SUM = await OrdersModel. sum('price', { where: { created: Sequelize. DATE(TODAY), }, }); console. log(SUM);
findByPk The findByPk method obtains only a single entry from the table, using the provided primary key. const project = await Project. findByPk(123); if (project === null) {
Sequelize instance comes with the query() method which you can use to run a raw query. The syntax of the method is as shown below: const [results, metadata] = await sequelize. query( "Your query here", { options } );
@dankohn is correct and that will work but it is safer to use the following:
Post.findAll({ limit: 10, order: [['updatedAt', 'DESC']]});
as this
will escape updatedAt and validate DESC against a list of valid direction parameters
Here's the syntax:
Post.findAll({ limit: 10, order: '"updatedAt" DESC' })
Here are more examples from the official documentation.
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