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;