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.
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
:
bun run migrate-init
When bun run migrate
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.
bun run generate
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
.
bun 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.
This example project is structured similarly to the starter example with a key difference:
prisma/sql/
: Contains SQL query files that are type-checked by Prisma and then included in the generated Prisma Client.prisma/sql/conversionByVariant.sql
: Example SQL query used in the project.
Key areas to look at:
- Database schema:
prisma/schema.prisma
- Example SQL query:
prisma/sql/conversionByVariant.sql
- Query execution:
src/index.ts
- Data seeding:
prisma/seed.ts
- Build and run scripts:
package.json
- Check out the Prisma docs
- Share your feedback in the Prisma Discord