Home > Net >  How to search with each starting letter of words in sequelize
How to search with each starting letter of words in sequelize

Time:09-28

I want to get search results if my string is "customer details" .I am able to do a search if I type cust will get desired output but if I search using data keyword. I am getting no results. How to achieve with sequelize?

What I have tried -

sequelize.where(sequelize.fn('LOWER', sequelize.col('name')), 'LIKE', queryObj.keyword.toLowerCase()   '\%')

Generated Query

SELECT "Campaign"."id" FROM "Campaigns" AS "Campaign" LEFT OUTER JOIN "EmailCampaigns" AS "EmailCampaigns" ON "Campaign"."id" = "EmailCampaigns"."campaignId" AND ("EmailCampaigns"."deletedAt" IS NULL)
 LEFT OUTER JOIN "CampaignEmailTemplates" AS "CampaignEmailTemplate" ON "Campaign"."id" = "CampaignEmailTemplate"."campaignId" 
 AND ("CampaignEmailTemplate"."deletedAt" IS NULL) 
 LEFT OUTER JOIN "CampaignScheduleRequests" AS "CampaignScheduleRequest" ON "Campaign"."id" = "CampaignScheduleRequest"."campaignId" 
 AND ("CampaignScheduleRequest"."deletedAt" IS NULL) 
 LEFT OUTER JOIN "CampaignStats" AS "CampaignStat" ON "Campaign"."id" = "CampaignStat"."campaignId"  AND ("CampaignStat"."deletedAt" IS NULL) 
 WHERE ("Campaign"."deletedAt" IS NULL AND ("Campaign"."accountId" = 403 
AND LOWER("Campaign"."name") iLIKE 'cust%')) ORDER BY "Campaign"."updatedAt" DESC;

CodePudding user response:

I found the way can be achieved by

sequelize.where(sequelize.fn('LOWER', sequelize.col('name')), {[Op.iRegexp]: `\\y${queryObj.keyword.toLowerCase()}.*\\y`})

CodePudding user response:

sequelize.where(sequelize.fn('LOWER', sequelize.col('name')), {[Op.like]: `%${queryObj.keyword.toLowerCase()}%`}),
  • Related