Anas Badran
Anas Badran
Explore posts from servers
DTDrizzle Team
Created by chris_st on 10/19/2024 in #help
Problem with findMany with many-to-many relations
I made something like this, but I think there is a better solution..
with: {
tags: {
with: {
tag: true,
},
},
},
with: {
tags: {
with: {
tag: true,
},
},
},
6 replies
DTDrizzle Team
Created by chris_st on 10/19/2024 in #help
Problem with findMany with many-to-many relations
did you figure it out?
6 replies
DTDrizzle Team
Created by Anas Badran on 12/14/2024 in #help
Build a complex query
this sql query works fine, but is there equivalent in drizzle?
SELECT p.*
FROM posts p
JOIN post_extra_data ped1 ON p.id = ped1.post_id AND ped1.key = 'surah' AND ped1.value = '1'
JOIN post_extra_data ped2 ON p.id = ped2.post_id AND ped2.key = 'verse' AND ped2.value = '3'
SELECT p.*
FROM posts p
JOIN post_extra_data ped1 ON p.id = ped1.post_id AND ped1.key = 'surah' AND ped1.value = '1'
JOIN post_extra_data ped2 ON p.id = ped2.post_id AND ped2.key = 'verse' AND ped2.value = '3'
4 replies
DTDrizzle Team
Created by Anas Badran on 12/14/2024 in #help
Build a complex query
How to write a query to select the post where it's have a two extraData records as follows: - key => 'surah', value => '1' - key => 'verse', value => '3'
4 replies
PPrisma
Created by Anas Badran on 12/11/2024 in #help-and-questions
Fix a query
FOR SIMPLIFIYNG The code below works, when commenting either part of AND, but when putting them together doesn't work
const result = await prisma.post.findMany({
where: {
extension: 'SURAH_WITH_VERSE',

extraData: {
some: {
AND: [
{
key: 'surah',
value: '2',
},
// {
// key: 'verse',
// value: '283',
// },
],
},
},
},
select: {
title: true,
extraData: true,
},
});
const result = await prisma.post.findMany({
where: {
extension: 'SURAH_WITH_VERSE',

extraData: {
some: {
AND: [
{
key: 'surah',
value: '2',
},
// {
// key: 'verse',
// value: '283',
// },
],
},
},
},
select: {
title: true,
extraData: true,
},
});
23 replies
PPrisma
Created by Anas Badran on 12/11/2024 in #help-and-questions
Fix a query
Surah or verse alone works fine
23 replies
PPrisma
Created by Anas Badran on 12/11/2024 in #help-and-questions
Fix a query
Verse and surah together
23 replies
PPrisma
Created by Anas Badran on 12/11/2024 in #help-and-questions
Fix a query
it didn't work
23 replies
PPrisma
Created by Anas Badran on 12/11/2024 in #help-and-questions
Fix a query
I tried this also and it didn't work:
await prisma.collection.findFirst({
where: {
id: collectionId,
},
include: {
posts: {
take: consts.ITEMS_PER_PAGE,
skip: (currentPage - 1) * consts.ITEMS_PER_PAGE,
include: {
collection: true,
extraData: true,
},
where: {
extraData: {
some: {
...(verse && surah
? {
AND: [
{ key: { equals: 'verse' }, value: { equals: verse } },
{ key: { equals: 'surah' }, value: { equals: surah } },
],
}
: surah
? { key: 'surah', value: surah }
: verse
? { key: 'verse', value: verse }
: {}),
},
},
},
},
},
})
await prisma.collection.findFirst({
where: {
id: collectionId,
},
include: {
posts: {
take: consts.ITEMS_PER_PAGE,
skip: (currentPage - 1) * consts.ITEMS_PER_PAGE,
include: {
collection: true,
extraData: true,
},
where: {
extraData: {
some: {
...(verse && surah
? {
AND: [
{ key: { equals: 'verse' }, value: { equals: verse } },
{ key: { equals: 'surah' }, value: { equals: surah } },
],
}
: surah
? { key: 'surah', value: surah }
: verse
? { key: 'verse', value: verse }
: {}),
},
},
},
},
},
})
23 replies
PPrisma
Created by Anas Badran on 12/11/2024 in #help-and-questions
Fix a query
I'm using nextjs, and apply the filters based on what I recieve in searchParams
23 replies
PPrisma
Created by Anas Badran on 12/11/2024 in #help-and-questions
Fix a query
Yes, you are right
23 replies
PPrisma
Created by Anas Badran on 12/11/2024 in #help-and-questions
Fix a query
it works for surah alone, or verse alone, but not both together.
23 replies
PPrisma
Created by Anas Badran on 12/11/2024 in #help-and-questions
Fix a query
The updated query:
await prisma.collection.findFirst({
where: {
id: collectionId,
},
include: {
posts: {
take: consts.ITEMS_PER_PAGE,
skip: (currentPage - 1) * consts.ITEMS_PER_PAGE,
include: {
collection: true,
extraData: true,
},
where: {
extraData: {
some: {
...(verse && surah
? {
AND: [
{
AND: [
{
key: {
equals: 'verse',
},
value: {
equals: verse,
},
},
],
},

{
AND: [
{
key: {
equals: 'surah',
},
value: {
equals: surah,
},
},
],
},
],
}
: surah
? { key: 'surah', value: surah }
: verse
? { key: 'verse', value: verse }
: {}),
},
},
},
},
},
})
await prisma.collection.findFirst({
where: {
id: collectionId,
},
include: {
posts: {
take: consts.ITEMS_PER_PAGE,
skip: (currentPage - 1) * consts.ITEMS_PER_PAGE,
include: {
collection: true,
extraData: true,
},
where: {
extraData: {
some: {
...(verse && surah
? {
AND: [
{
AND: [
{
key: {
equals: 'verse',
},
value: {
equals: verse,
},
},
],
},

{
AND: [
{
key: {
equals: 'surah',
},
value: {
equals: surah,
},
},
],
},
],
}
: surah
? { key: 'surah', value: surah }
: verse
? { key: 'verse', value: verse }
: {}),
},
},
},
},
},
})
23 replies
PPrisma
Created by Anas Badran on 12/11/2024 in #help-and-questions
Fix a query
the schema:
model Post {
id String @id @default(cuid())
createdAt DateTime @default(now())
updatedAt DateTime @updatedAt
title String
title_ Json? @db.JsonB
content String
content_ Json? @db.JsonB
collectionId String
collection Collection @relation(fields: [collectionId], references: [id], onDelete: Cascade)
metadata PostMetadata? @relation(name: "PostMetadata")
// extraData Json? @db.JsonB
extraData PostExtraData[]
extension PostExtensionEnum @default(BASE)
tags Tag[] @relation(name: "PostTags")
order Int?
deletedAt DateTime?
}

model PostExtraData {
id String @id @default(cuid())
key String
value String
postId String
post Post @relation(fields: [postId], references: [id], onDelete: Cascade)
}
model Post {
id String @id @default(cuid())
createdAt DateTime @default(now())
updatedAt DateTime @updatedAt
title String
title_ Json? @db.JsonB
content String
content_ Json? @db.JsonB
collectionId String
collection Collection @relation(fields: [collectionId], references: [id], onDelete: Cascade)
metadata PostMetadata? @relation(name: "PostMetadata")
// extraData Json? @db.JsonB
extraData PostExtraData[]
extension PostExtensionEnum @default(BASE)
tags Tag[] @relation(name: "PostTags")
order Int?
deletedAt DateTime?
}

model PostExtraData {
id String @id @default(cuid())
key String
value String
postId String
post Post @relation(fields: [postId], references: [id], onDelete: Cascade)
}
23 replies
PPrisma
Created by Anas Badran on 12/11/2024 in #help-and-questions
Fix a query
I'm about to change the schema
23 replies
PPrisma
Created by Anas Badran on 12/11/2024 in #help-and-questions
Fix a query
No, it doesn't
23 replies
PPrisma
Created by Anas Badran on 12/11/2024 in #help-and-questions
Fix a query
surah conditional works
23 replies