Sequelize: IS NULL / IS NOT NULL

Sequelize has a neat syntax for turning SQL WHERE clauses into JSON objects. The following shows how to create IS NULL and IS NOT NULL queries.

IS NOT NULL

For “IS NOT 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: Quoting the column names forces everything to be case sensitive in Postgres.

This will force you to quote columns and tables when you query your own database.

IS NULL

You can also query for “is null”, like so:

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

And there you have it!