Home > Software design >  How would I compare the two column in Sequelize Postgres Database using node.js
How would I compare the two column in Sequelize Postgres Database using node.js

Time:12-28

In my Postgres DB, there are two columns, one is for email and another one is for storing the roles, the roles are customer and engineer. Here, some emails are containing the customer as a role and some are the engineer as a role in DB. When I retrieve the data from DB, I need to verify that the email is matching to that particular role(customer/engineer). How to write the condition using nodejs.

id | role     | email
--- ---------- ------------------------
1  | engineer | [email protected]
--- ---------- ------------------------
2  | customer | [email protected]
--- ---------- ------------------------
3  | engineer | [email protected]
---------------------------------------

while retrieving I need to compare the [email protected] is engineer or not

CodePudding user response:

If you just need to make sure there is a record with specified email and role then you can use count method and simple where condition like this:

const roleCount = await Model.count({
  where: {
    email: '[email protected]',
    role: 'engineer'
  }
})
const isEngineer = roleCount > 0;
  • Related