Sequelize how to check if entry exists in database

You can count and find.

    Project
  .findAndCountAll({
     where: {
        title: {
          [Op.like]: 'foo%'
        }
     },
     offset: 10,
     limit: 2
  })
  .then(result => {
    console.log(result.count);
    console.log(result.rows);
  });

Doc link, v5 Beta Release


I don't prefer using count to check for record existence. Suppose you have similarity for hundred in million records why to count them all if you want just to get boolean value, true if exists false if not?

findOne will get the job done at the first value when there's matching.

const isIdUnique = id =>
  db.Profile.findOne({ where: { id} })
    .then(token => token !== null)
    .then(isUnique => isUnique);

Update: see the answer which suggests using findOne() below. I personally prefer; this answer though describes an alternative approach.

You are not returning from the isIdUnique function:

function isIdUnique (id) {
    return db.Profile.count({ where: { id: id } })
      .then(count => {
        if (count != 0) {
          return false;
        }
        return true;
    });
}

isIdUnique(id).then(isUnique => {
    if (isUnique) {
        // ...
    }
});

I found the answer by @alecxe to be unreliable in some instances, so I tweaked the logic:

function isIdUnique (id, done) {
  db.Profile.count({ where: { id: id } })
  .then(count => {
    return (count > 0) ? true : false
  });
}