SQL to Prisma
Unanswered
Russian Blue posted this in #help-forum
Russian BlueOP
I want to implement search functionality using prisma.
3 Replies
Russian BlueOP
SQL query from nextjs org tutorial// const invoices = await sql<InvoicesTable>
// SELECT
// invoices.id,
// invoices.amount,
// invoices.date,
// invoices.status,
// customers.name,
// customers.email,
// customers.image_url
// FROM invoices
// JOIN customers ON invoices.customer_id = customers.id
// WHERE
// customers.name ILIKE ${
%${query}%} OR
// customers.email ILIKE ${
%${query}%} OR
// invoices.amount::text ILIKE ${
%${query}%} OR
// invoices.date::text ILIKE ${
%${query}%} OR
// invoices.status ILIKE ${
%${query}%}
// ORDER BY invoices.date DESC
// LIMIT ${ITEMS_PER_PAGE} OFFSET ${offset}
//
;const invoices = await prisma.invoices.findMany({
select: {
id: true,
amount: true,
date: true,
status: true,
customers: {
select: {
name: true,
email: true,
image_url: true
},
where: {
OR: [
{ name: { contains: query, mode: 'insensitive' } },
{ email: { contains: query, mode: 'insensitive' } }
]
}
}
},
where: {
OR: [
{ amount: { contains: query, mode: 'insensitive' } },
{ date: { contains: query, mode: 'insensitive' } },
{ status: { contains: query, mode: 'insensitive' } }
]
},
orderBy: {
date: 'desc'
}
});