Multiple searches in same column MySQL Sequelize, - mysql

I am making a search in a database which have a table productDetails.
I want a query something like this in sequelize,
SELECT * FROM productDetails WHERE title LIKE '%search1%' OR title LIKE '%search2%'
currently using
const pd= await ProductDetail.findAll({
where: {
[Op.or]: {
title: { [Op.like]: `%search1%` },
title: { [Op.like]: `%search2%` },
},
},

Just indicate all conditions as an array as a value of Op.or like this:
where: {
[Op.or]: [
{ title: { [Op.like]: `%search1%` } },
{ title: { [Op.like]: `%search2%` } },
],
},
See examples here

Related

Sequelize nodejs : search by column from table columns and its join table columns

I have table (skill_plan) which has foreign key of other table (skill_set). I need to filter findAndCountAll result by search_input if available , by some columns of skill_plan and some columns of skill_set.
I have tried below code.
let search_input = req.body.search_input;
var whereStatement = {};
if (search_input) {
whereStatement = {
[Op.or]: [
{
title: {
[Op.like]: `%${search_input}%`,
},
},
{
year: {
[Op.like]: `%${search_input}%`,
},
},
{
quarter: {
[Op.like]: `%${search_input}%`,
},
},
],
};
}
await SkillPlan.findAndCountAll({
include: [
{
model: SkillSet,
required: true,
attributes: ["id", "skill_name", "status"],
where: search_input
? {
[Op.or]: [
{
skill_name: {
[Op.like]: `%${search_input}%`,
},
},
],
}
: null,
},
],
where: whereStatement,
distinct: true,
})
Above code executing query like below
SELECT
count(DISTINCT(`skill_plan`.`id`)) AS `count`
FROM
`skill_plan` AS `skill_plan`
INNER JOIN
`skill_sets` AS `skill_set`
ON `skill_plan`.`skill_set_id` = `skill_set`.`id`
AND
(
`skill_set`.`deleted_at` IS NULL
AND
(
`skill_set`.`skill_name` LIKE '%test%'
)
)
WHERE
(
`skill_plan`.`deleted_at` IS NULL
AND
(
`skill_plan`.`title` LIKE '%test%'
OR `skill_plan`.`year` LIKE '%test%'
OR `skill_plan`.`quarter` LIKE '%test%'
)
)
;
But I need skill_set.skill_name LIKE '%test%' condition with OR of skill_plan conditions.
How I can get OR of skill_set column in OR conditions of skill_plan? is there any other way of doing this?
Please guide and help. Thanks
You need to move SkillSet conditions to the SkillPlan conditions level:
if (search_input) {
whereStatement = {
[Op.or]: [
{
title: {
[Op.like]: `%${search_input}%`,
},
},
{
year: {
[Op.like]: `%${search_input}%`,
},
},
{
quarter: {
[Op.like]: `%${search_input}%`,
},
},
{
'$SkillSet.skill_name$': {
[Op.like]: `%${search_input}%`,
},
}
],
};
}

is it possible to user `where` and `count` to return the count for a specified field?

I am trying to use Prisma to return a count for a boolean field where it equals 'true'.
To give some context, on the frontend I am trying to calculate the workouts that have been completed by a user as a percentage, so ideally I would like prisma to return a count for the total workouts (which I have successfully done) and the count for the userWorkouts where 'isCompleted' equal true (which I am unable to achieve), currently the count is returning all userWorkouts not just the completed ones.
Here is my current Prisma Query:
const response = await prisma.user.findUnique({
where: {
id: 1,
},
select: {
id: true,
programs: {
select: {
program: {
select: {
name: true,
blocks: {
select: {
id: true,
name: true,
week: {
select: {
id: true,
number: true,
workouts: {
select: {
userWorkouts: {
where: {
isCompleted: true,
},
},
_count: {
select: {
userWorkouts: true,
},
},
},
},
_count: {
select: {
workouts: true,
},
},
},
},
},
},
},
},
},
},
},
});
res.json(response);
};
Is this possible to achieve using Primsa? Or should I just return all userWorkouts and filter for isCompleted: true on the frontend?
I have spoken with the Prisma team and this can't be achieved yet, although there is a feature request open for it.
https://github.com/prisma/prisma/issues/8413
If you would like to help get this feature added please add you +1 to the feature request.

Sequelize - use sequelize functions to convert iLike queries to be MySQL friendly

I generally use PostgreSQL, and this is my first time using MySQL with Sequelize. I learned that MySQL does not support iLike, and I'm trying to convert my iLike to be MySQL friendly (so-to-speak).
I managed to successfully accomplish the below with one of my queries.
where: {
[Op.or]: [
{
name: {
[Op.iLike]: `%${searchTerm || ''}`,
},
},
{
description: {
[Op.iLike]: `%${searchTerm || ''}`,
},
},
],
},
**Converted To**
where: {
[Op.or]: [
Sequelize.where(Sequelize.fn('lower', Sequelize.col('name')), {
[Op.like]: `%${searchTerm || ''}`,
}),
Sequelize.where(Sequelize.fn('lower', Sequelize.col('description')), {
[Op.like]: `%${searchTerm || ''}`,
}),
],
},
What I'm struggling with is converting the following. I'd appreciate any input I could get.
ingredients = ['%salt%', '%pepper%'];
where: {
ingredient: {
[Op.iLike]: {
[Op.any]: ingredients,
},
},
},
SQL - WHERE "Ingredient"."ingredient" ILIKE ANY (ARRAY['%salt%','%pepper%']);
**Attempt to Convert**
where: Sequelize.where(
Sequelize.fn('lower', Sequelize.col('ingredient')),
{
[Op.like]: {
[Op.any]: ingredients,
},
}
),
SQL - WHERE lower(`ingredient`) LIKE ANY ('%salt%', '%pepper%') It seems like ingredients is not passed as an array compared to the above code.
This way worked to me, maybe this way help you.
where: {
title: {
[Op.like]: ['%' + title + '%'],
},
},

how to search by included model

let find_Query = {
attributes: ["id"],
include: [
{
model: Tender_details,
include: [{
model: Categories,attributes: ["id","category_name"],
where:{}
},
{
model: SubCategories,attributes: ["id","sub_category_name"],
where:{}
}
],
where: {}
},
],
where: {} };
switch (key) {
case "search":
find_Query.include[0].where[Op.or] = [
{
"$Categories.category_name$": {
[Op.like]: `%${req.query.search}%`
}
},
{
"$Subcategories.sub_category_name$": {
[Op.like]: `%${req.query.search}%`
}
}
];
break;
default:
break;
I have used this code. In this code, i'm using [Op.or]. but it's not working properly.
In this code, i want to find data by category_name and sub_category_name from included model

Stuck to figure out how to access a certain field to update

I have this JSON FILE
{
"_id": "GgCRguT8Ky8e4zxqF",
"services": {
"emails": [
{
"address": "Abunae#naa.com",
"verified": false,
"verifiedMail": "Toto#hotmail.com"
}
],
"profile": {
"name": "Janis"
},
"pushIds": []
}
I want to update my verifiedMail field but couldn't figure out how to do it in Meteor, it's always returning me an error
let VerifiedEmail = "Exemple1"
await Meteor.users.update({ _id: user._id }, { $set: { 'emails.verifiedEmail': emailRefactor} }, { upsert: true })
Couldn't figure out how to access the emails.verifiedEmail field
Tried this exemlpe worked like a charm
let VerifiedEmail = "Exemple1"
await Meteor.users.update({ _id: user._id }, { $set: { 'profile.name': emailRefactor} }, { upsert: true })
but couldn't figure out how to access emails.verifiedEmail .
Could you please help me ?
Emails is an array, while profile is an object. You have to access the first object of the email array instead
This updates the exact email address from emails
Meteor.users.update({
"emails.address": emailRefactor
}, {
$set: {
"emails.$.verified": true
}
});
Or update the first element
Meteor.users.update({
_id: user._id,
"emails.address": emailRefactor
}, {
$set: {
"emails.0.verified": true
}
});
You're trying to set verifiedEmail while the actual field is verifiedMail.