Skip to main content

Raw database access

While Prisma ORM aims to make all your database queries intuitive, type-safe, and convenient, there may be situations where you need to drop down to raw queries.

This may happen for several reasons, e.g., because you need to optimize the performance of a specific query or because your data requirements can't be expressed by Prisma Client's query API.

In that case, you can use one of Prisma Client's escape hatches, such as $queryRaw or $executeRaw, to send raw queries to the database.

When using relational databases, you can use community tools that can still vastly improve the developer experience writing raw SQL, e.g., by enabling type safety or providing auto-completion for your SQL queries:

  • prisma-extension-kysely: A Client extension that uses the Kysely SQL query builder API to compose SQL queries in TypeScript, with auto-completion and type safety, and submits them through Prisma Client's connection pool.
  • SafeQL: An ESLint plugin enabling syntax highlighting and type safety for SQL strings inside Prisma ORM's raw SQL escape hatches. For a tutorial on using SafeQL, visit this page.

In this section