RTFM: https://www.prisma.io/docs/concepts/components/prisma-client/raw-database-access#tagged-template-helpers
import { Prisma } from "@prisma/client";
const ids = [1, 3, 5, 10, 20];
const result = await prisma.$queryRaw`SELECT * FROM User WHERE id IN (${Prisma.join(
ids
)})`;
Answer from Michael Dausmann on Stack OverflowRTFM: https://www.prisma.io/docs/concepts/components/prisma-client/raw-database-access#tagged-template-helpers
import { Prisma } from "@prisma/client";
const ids = [1, 3, 5, 10, 20];
const result = await prisma.$queryRaw`SELECT * FROM User WHERE id IN (${Prisma.join(
ids
)})`;
As @MichaelDausmann has mentioned above, there is a Prisma function that joins array items and formats the SQL by the types of parameters. But when you use uuid type, Prisma's join function formats the parameters adding double apostrophe. So you can take error like 'type mismatch' in PostreSQL. Alternatively, you can use native Array.join function to build a sql string and send it to Prisma raw query function using Prisma.raw function.
const sql = `select * from table where id in (${idArray.map(v => `'${v}'::uuid`).join(",")})`
result = await prisma.$queryRaw(Prisma.raw(sql))
dynamic WHERE clause field not working in raw queries
Type inference in raw queries is broken for arrays containing single null element
queryRaw returns empty array
How to get the size of an array type column in Prisma postgresql?
export async function userExists({ field, val} : {field: string, val: string | null | undefined}) {
if(!field || !val) return;
console.log(field, val);
const user: any[] = await prisma.$queryRaw`SELECT id from next_auth."User" WHERE ${field} = ${val};`
console.log(user);
if(user.length == 0) return false;
return true;
}I have this simple function and it was working as expected, but when I made the filed in the where clause dynamic it started to always return an empty array. I've tried to change the field name, but didn't work. And also when I try to give it a field name that doesn't exist, it also returns an empty array.
Is there any feature like cardinality function of postgresql in prisma? Or I would have to use raw query or get the the whole array from table and then get the no. of elements in it?