Skip to content

Files

Latest commit

fa1f829 · Jul 3, 2025

History

History

typedsql

README.md

TypedSQL Example

This example shows how to use the TypedSQL feature of Prisma ORM in a TypeScript project. TypedSQL allows you to write fully type-safe SQL queries and then run them via Prisma Client.

Getting started

1. Download example and navigate into the project directory

Download this example:

npx try-prisma@latest --template orm/typedsql --install npm --name typedsql

Then, navigate into the project directory:

cd typedsql
Alternative: Clone the entire repo

Clone this repository:

git clone git@github.com:prisma/prisma-examples.git --depth=1

Install npm dependencies:

cd prisma-examples/orm/typedsql
npm install

2. Create and seed the database

Run the following command to create your SQLite database file. This also creates the User and Post tables that are defined in prisma/schema.prisma:

npx prisma migrate dev --name init

When npx prisma migrate dev is executed against a newly created database, seeding is also triggered. The seed file in prisma/seed.ts will be executed and your database will be populated with the sample data.

3. Generate Prisma Client with SQL

npx prisma generate --sql

This command runs prisma generate --sql, which will generate the Prisma Client and also check any SQL files in the prisma/sql directory. After type-checking the SQL files, they are compiled into JavaScript and added to the Prisma Client.

Tip

This also works with the --watch flag! If you want to automatically generate and watch for changes, you can use npx prisma generate --sql --watch.

4. Run the example

npm run dev

This command will run index.ts, which will execute the SQL query defined in prisma/sql/conversionByVariant.sql and print the results to the console.

Project Structure

This example project is structured similarly to the starter example with a key difference:

Key areas to look at:

  1. Database schema: prisma/schema.prisma
  2. Example SQL query: prisma/sql/conversionByVariant.sql
  3. Query execution: src/index.ts
  4. Data seeding: prisma/seed.ts
  5. Build and run scripts: package.json

Next steps