Prisma.sql vs Prisma $queryRaw diff
Can someone guide me what is the main benefit of
Prisma.sql
and main differences with $queryRaw
? I could not find the Prisma.sql in official docs.
Is below approach corrct?
2 Replies
You chose to debug with a human. They'll tinker with your query soon. If you get curious meanwhile, hop into
#ask-ai
for a quick spin!Hi @uralsmh
Prisma.sql
is a helper function provided by Prisma to create SQL query fragments that can be safely interpolated into larger queries. See this section of the documentation. You can also take a look at sql-template-tag repo that Prisma.sql utilizes under the hood.
$queryRaw
is a method to execute raw SQL queries. The main differences are:
1. When used as a tagged template literal prisma.$queryRawSELECT * FROM User WHERE email = ${email}
, it's safe from SQL injections.
When used as a function call (like prisma.$queryRaw("SELECT * FROM User WHERE email = " + email)
), it's susceptible to SQL injections.
Your approach using Prisma.sql
in your code is correct. You're using Prisma.sql
to create a safe SQL fragment that includes other SQL fragments and interpolated values. You're using Prisma.raw
for column names, which is correct as column names can't be parameterized and you are using Prisma.join
to safely join the repository_names array into the query.GitHub
GitHub - blakeembrey/sql-template-tag: ES2015 tagged template strin...
ES2015 tagged template string for preparing SQL statements, works with
pg
, mysql
, sqlite
and oracledb
- blakeembrey/sql-template-tagRaw queries | Prisma Documentation
Learn how you can send raw SQL and MongoDB queries to your database using the raw() methods from the Prisma Client API.