I would like to construct a sequelize query that returns rows in ascending order with NULL values first.

I have a timestamp when an email is sent, which is NULL if an email has never been sent.

Here's the Postgres query that does what I want:

SELECT quote, quote_id
FROM quotes
WHERE book_id = '${book_id}'
ORDER BY last_emailed ASC NULLS FIRST
LIMIT 5

The sequelize query I have is:

const res = await Quote.findAll({
  attributes: ['quote', 'quote_id'],
  where: { book_id },
  order: [
    ['last_emailed', 'ASC']
  ],
  limit: 5,
})

But this returns all NULL values last. It otherwise does what I want

0 Answers