Sequelize how to check if entry exists in database

后端 未结 5 600
伪装坚强ぢ
伪装坚强ぢ 2021-01-01 15:33

I need to check if entry with specific ID exists in the database using Sequelize in Node.js

  function isIdUnique (id) {
    db.Profile.count({ where: { id:          


        
5条回答
  •  情书的邮戳
    2021-01-01 16:24

    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);
    

提交回复
热议问题