Sequelize: IS NULL / IS NOT NULL

Sequelize has a neat / bizarre syntax for making parts of WHERE clauses into JSON objects. For “IS NULL”, try this:

 db.Alert.find(
    {where: {
      UserId: req.params.user_id,
      url: {
        $ne: null
      }
    }})
    .then(
      (alert) => {
        // handle result
      }
    );

This renders the following SQL (formatted for readability):

SELECT 
   "id", "title", "text", "domain", 
   "lastRun", "lastSeen", "url", 
    "createdAt", "updatedAt", "UserId" 
FROM "Alerts" AS "Alert" 
WHERE "Alert"."UserId" = '1' 
AND "Alert"."url" IS NOT NULL 
LIMIT 1;

Note that this forces everything to be case sensitive, if you’re using Postgres. It’s kind of irritating actually, because this forces you to quote things when you query your own database.

You can also do “is null”, like so:

db.Alert.find(
  {where: {
    UserId: req.params.user_id,
    url: null
  }})
  .then(
    (alerts) => {
      res.render('embed', { alerts: alerts });
    }
  );