P
Prisma•6d ago
Supernova

help contructing a filter query

I'm receiving filter query params as a prop to my server action, every filter param can either be an array of values or just a single value. I can't figure out how to go on creating this filter query
5 Replies
Prisma AI Help
Prisma AI Help•6d ago
You decided to hold for human wisdom. We'll chime in soon! Meanwhile, #ask-ai is there if you need a quick second opinion.
Supernova
SupernovaOP•6d ago
here's my server action for now:
export async function getTeachersSubjects(
subject: string,
page: number,
queries: {
subject?: string | string[];
language?: string | string[];
studentLevel?: string | string[];
educationLevel?: string | string[];
},
): Promise<Teacher[]> {
const teachers = await prisma.user.findMany({
where: {
role: Role.TEACHER,
subjects: {
some: { name: subject },
},
},
select: {
teacher: true,
educations: true,
languages: true,
subjects: true,
},
take: PERPAGE,
skip: (page - 1) * PERPAGE,
});


return JSON.parse(
JSON.stringify(
teachers.map((user) => ({
...toCamelCase(user.teacher),
educations: user.educations,
languages: user.languages,
subjects: user.subjects,
})),
),
);
}
export async function getTeachersSubjects(
subject: string,
page: number,
queries: {
subject?: string | string[];
language?: string | string[];
studentLevel?: string | string[];
educationLevel?: string | string[];
},
): Promise<Teacher[]> {
const teachers = await prisma.user.findMany({
where: {
role: Role.TEACHER,
subjects: {
some: { name: subject },
},
},
select: {
teacher: true,
educations: true,
languages: true,
subjects: true,
},
take: PERPAGE,
skip: (page - 1) * PERPAGE,
});


return JSON.parse(
JSON.stringify(
teachers.map((user) => ({
...toCamelCase(user.teacher),
educations: user.educations,
languages: user.languages,
subjects: user.subjects,
})),
),
);
}
Supernova
SupernovaOP•6d ago
example of filter params
No description
Supernova
SupernovaOP•6d ago
here's the related schemas
model User {
id Int @id @default(autoincrement())
email String @unique
password String
role Role
profile_completed Boolean @default(false)

teacher Teacher?
student Student?
subjects Subject[]
languages Language[]
educations Education[]

@@map("users")
}
model Language {
id Int @id @default(autoincrement())
name String
users User[]

@@map("languages")
}

model Education {
id Int @id @default(autoincrement())
name String
users User[]

@@map("education_levels")
}

model Subject {
id Int @id @default(autoincrement())
name String

users User[]

@@map("subjects")
}
model User {
id Int @id @default(autoincrement())
email String @unique
password String
role Role
profile_completed Boolean @default(false)

teacher Teacher?
student Student?
subjects Subject[]
languages Language[]
educations Education[]

@@map("users")
}
model Language {
id Int @id @default(autoincrement())
name String
users User[]

@@map("languages")
}

model Education {
id Int @id @default(autoincrement())
name String
users User[]

@@map("education_levels")
}

model Subject {
id Int @id @default(autoincrement())
name String

users User[]

@@map("subjects")
}
Nurul
Nurul•5d ago
Hey 👋 Would doing something like this work for your case?
// Add dynamic filters
if (queries.subject) {
whereClause.subjects = {
some: {
name: Array.isArray(queries.subject) ? { in: queries.subject } : queries.subject,
},
};
}

if (queries.language) {
whereClause.languages = {
some: {
name: Array.isArray(queries.language) ? { in: queries.language } : queries.language,
},
};
}

if (queries.educationLevel) {
whereClause.educations = {
some: {
name: Array.isArray(queries.educationLevel) ? { in: queries.educationLevel } : queries.educationLevel,
},
};
}

// Note: studentLevel is not directly related to the User model in the provided schema

const teachers = await prisma.user.findMany({
where: whereClause,
select: {
teacher: true,
educations: true,
languages: true,
subjects: true,
},
take: PERPAGE,
skip: (page - 1) * PERPAGE,
});
// Add dynamic filters
if (queries.subject) {
whereClause.subjects = {
some: {
name: Array.isArray(queries.subject) ? { in: queries.subject } : queries.subject,
},
};
}

if (queries.language) {
whereClause.languages = {
some: {
name: Array.isArray(queries.language) ? { in: queries.language } : queries.language,
},
};
}

if (queries.educationLevel) {
whereClause.educations = {
some: {
name: Array.isArray(queries.educationLevel) ? { in: queries.educationLevel } : queries.educationLevel,
},
};
}

// Note: studentLevel is not directly related to the User model in the provided schema

const teachers = await prisma.user.findMany({
where: whereClause,
select: {
teacher: true,
educations: true,
languages: true,
subjects: true,
},
take: PERPAGE,
skip: (page - 1) * PERPAGE,
});

Did you find this page helpful?