Darkyne
01/26/2022, 10:01 PMconst name = _input_.name ?? undefined;
const categories = _input_.categories ?? undefined;
const games = await prisma.game.findMany({
where: {
AND: {
name: { contains: name, mode: 'insensitive' },
categories: {
some: {
}
}
}
},
I have this query and I'm trying to to check if any category matches one of the categories array (input) how do I map this?Sabin Adams
01/26/2022, 11:15 PMgame
matching the provided name and where it has a category in a list of categories provided?
If so, give something like this a try:
const name = input.name ?? undefined;
const categories = input.categories ?? undefined;
const games = await prisma.game.findMany({
where: {
AND: [
{ name: { contains: name, mode: "insensitive" }},
{ categories: {
some: {
name: { in: categories.map( category => category.name) },
},
}}
]
},
});
Darkyne
01/27/2022, 8:25 PMSabin Adams
01/27/2022, 8:32 PMconst name = input.name ?? undefined;
const categories = input.categories ?? undefined;
const games = await prisma.game.findMany({
where: {
AND: [
{ name: { contains: name, mode: "insensitive" }},
{ categories: {
every: {
name: { in: categories.map( category => category.name) },
},
}}
]
},
});
So this should work by just switching to that every
key. This is saying "give me all the games where every category in the provided list is associated with the game and where the name matches"Sabin Adams
01/27/2022, 8:33 PMevery
key isn't saying the game has to only have the provided categories. It's saying every category provided should exist on the game. There could be more potentially and it should still matchDarkyne
01/27/2022, 8:43 PMconst games = await prisma.game.findMany({
where: {
AND: [
{ name: { contains: name, mode: 'insensitive' } },
{ categories: {
every: {
description: { in: categories.map( (category) => category) },
},
}},
]
},
take: limit + 1,
cursor: cursor ? { id: cursor } : undefined,
include: {
categories: true,
genres: true
},
orderBy: {
id: 'asc'
}
});
And I only get games where the input and the games categories exactly align so if my input is Single-player but the game itself has more categories it's not getting returnedSabin Adams
01/27/2022, 8:44 PMconst name = input.name ?? undefined;
const categories = input.categories ?? undefined;
const games = await prisma.game.findMany({
where: {
AND: [
{ name: { contains: name, mode: 'insensitive' } },
{
categories: {
name: {
some: {
OR: categories.map(category => ({ name: category.name })),
},
},
},
},
],
},
});
Could you try this?Darkyne
01/27/2022, 8:45 PMDarkyne
01/27/2022, 8:45 PMmodel Game {
id String @id
name String
is_free Boolean
short_description String
header_image String
website String
developers String[]
publishers String[]
windows Boolean
mac Boolean
linux Boolean
metacriticScore Int
metacriticUrl String
categories Category[]
genres Genre[]
background String
storeUrl String
}
model Category {
id String @id
description String
games Game[]
}
model Genre {
id String @id
description String
games Game[]
}
that's the modelSabin Adams
01/27/2022, 8:48 PMconst games = await prisma.game.findMany({
where: {
AND: [
{ name: { contains: name, mode: 'insensitive' } },
{
categories: {
some: {
OR: categories.map(category => ({ description: category })),
},
},
},
],
},
});
Ah okay, sorry working without your schema generated so I think I messed up the syntaxSabin Adams
01/27/2022, 8:49 PMDarkyne
01/27/2022, 8:52 PMSabin Adams
01/27/2022, 8:53 PMDarkyne
01/27/2022, 8:53 PMDarkyne
01/27/2022, 8:54 PMSabin Adams
01/27/2022, 8:57 PMwhere: {
AND: [
...categories!.map((value) => ({ categories: { some: { description: { contains: value } } } })),
]
},
Saw this in the main chatDarkyne
01/27/2022, 9:00 PMSabin Adams
01/27/2022, 9:02 PMwhere: {
AND: categories.map(description => ({ categories: { some: { description: { contains:description } } } }))
}
Darkyne
01/27/2022, 9:03 PMDarkyne
01/27/2022, 9:06 PMSabin Adams
01/27/2022, 9:08 PMSabin Adams
01/27/2022, 9:08 PMSabin Adams
01/27/2022, 9:10 PMconst generateDescriptionFilter = descriptions =>
descriptions.map(description => ({ categories: { some: { description: { contains:description } } } }))
This obviously doesn't change the funcionality, just cleans up the actual query if you prefer it that waySabin Adams
01/27/2022, 9:10 PMDarkyne
01/27/2022, 9:14 PMSabin Adams
01/27/2022, 9:15 PMPrisma
namespace to pull out your required typesDarkyne
01/27/2022, 9:36 PMSabin Adams
01/27/2022, 9:36 PMDarkyne
01/27/2022, 9:36 PMSabin Adams
01/27/2022, 9:38 PMconst generateRelationSomeFilter = (list, relationName, column) =>
list.map(value => ({ [relationName]: { some: { [column]: { contains: value } } } }))
Of course you could play around with it and make the search shape dynamic tooDarkyne
01/27/2022, 9:47 PMconst generateRelationFilter = (list: string[], relationName: string, column: string) => list.map((value) => ({ [relationName]: { some: { [column]: { contains: value, mode: 'insensitive' } } } }));
running like this:
{ AND: generateRelationFilter(categories, 'categories', 'description') },
and it works like a charmDarkyne
01/27/2022, 9:47 PMSabin Adams
01/27/2022, 9:49 PMhasMany
function in relation filters would be 👌 But for now that looks pretty good 🙂Darkyne
01/27/2022, 9:53 PM