|
| 1 | +# TypedSQL Example |
| 2 | + |
| 3 | +This example shows how to use the TypedSQL feature of [Prisma ORM](https://www.prisma.io/docs/orm/overview/introduction/what-is-prisma) in a TypeScript project along with the [`prisma-client`](https://www.prisma.io/docs/orm/prisma-schema/overview/generators#prisma-client-preview) generator. TypedSQL allows you to write fully type-safe SQL queries and then run them via Prisma Client. |
| 4 | + |
| 5 | +## Getting started |
| 6 | + |
| 7 | +### 1. Download example and navigate into the project directory |
| 8 | + |
| 9 | +Download this example: |
| 10 | + |
| 11 | +``` |
| 12 | +npx try-prisma@latest --template generator-prisma-client/basic-typedsql --install npm --name basic-typedsql |
| 13 | +``` |
| 14 | + |
| 15 | +Then, navigate into the project directory: |
| 16 | + |
| 17 | +``` |
| 18 | +cd basic-typedsql |
| 19 | +``` |
| 20 | + |
| 21 | +<details><summary><strong>Alternative:</strong> Clone the entire repo</summary> |
| 22 | + |
| 23 | +Clone this repository: |
| 24 | + |
| 25 | +``` |
| 26 | +git clone [email protected]:prisma/prisma-examples.git --depth=1 |
| 27 | +``` |
| 28 | + |
| 29 | +Install npm dependencies: |
| 30 | + |
| 31 | +``` |
| 32 | +cd prisma-examples/generator-prisma-client/basic-typedsql |
| 33 | +pnpm install |
| 34 | +``` |
| 35 | + |
| 36 | +</details> |
| 37 | + |
| 38 | +### 2. Create and seed the database |
| 39 | + |
| 40 | +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`](./prisma/schema.prisma): |
| 41 | + |
| 42 | +``` |
| 43 | +pnpm prisma migrate dev --name init |
| 44 | +``` |
| 45 | + |
| 46 | +When `pnpm prisma migrate dev` is executed against a newly created database, seeding is also triggered. The seed file in [`prisma/seed.ts`](./prisma/seed.ts) will be executed and your database will be populated with the sample data. |
| 47 | + |
| 48 | +### 3. Generate Prisma Client with SQL |
| 49 | + |
| 50 | +``` |
| 51 | +pnpm prisma generate --sql |
| 52 | +``` |
| 53 | + |
| 54 | +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. |
| 55 | + |
| 56 | +> [!TIP] |
| 57 | +> This also works with the `--watch` flag! If you want to automatically generate and watch for changes, you can use `pnpm prisma generate --sql --watch`. |
| 58 | +
|
| 59 | +### 4. Run the example |
| 60 | + |
| 61 | +``` |
| 62 | +npm run dev |
| 63 | +``` |
| 64 | + |
| 65 | +This command will run [`index.ts`](./index.ts), which will execute the SQL query defined in [`prisma/sql/conversionByVariant.sql`](./prisma/sql/conversionByVariant.sql) and print the results to the console. |
| 66 | + |
| 67 | +## Project Structure |
| 68 | + |
| 69 | +This example project is structured similarly to the [starter example](https://github.com/prisma/prisma-examples/tree/latest/orm/starter) with a key difference: |
| 70 | + |
| 71 | +- [`prisma/sql/`](./prisma/sql/): Contains SQL query files that are type-checked by Prisma and then included in the generated Prisma Client. |
| 72 | + - [`prisma/sql/conversionByVariant.sql`](./prisma/sql/conversionByVariant.sql): Example SQL query used in the project. |
| 73 | + |
| 74 | +Key areas to look at: |
| 75 | +1. Database schema: [`prisma/schema.prisma`](./prisma/schema.prisma) |
| 76 | +2. Example SQL query: [`prisma/sql/conversionByVariant.sql`](./prisma/sql/conversionByVariant.sql) |
| 77 | +3. Query execution: [`src/index.ts`](./src/index.ts) |
| 78 | +4. Data seeding: [`prisma/seed.ts`](./prisma/seed.ts) |
| 79 | +5. Build and run scripts: [`package.json`](./package.json) |
| 80 | +6. Configure the Prisma CLI: [`prisma.config.ts`](./prisma.config.ts) |
| 81 | + |
| 82 | +## Next steps |
| 83 | + |
| 84 | +- Check out the [Prisma docs](https://www.prisma.io/docs) |
| 85 | +- [Join our community on Discord](https://pris.ly/discord?utm_source=github&utm_medium=prisma_examples&utm_content=next_steps_section) to share feedback and interact with other users. |
| 86 | +- [Subscribe to our YouTube channel](https://pris.ly/youtube?utm_source=github&utm_medium=prisma_examples&utm_content=next_steps_section) for live demos and video tutorials. |
| 87 | +- [Follow us on X](https://pris.ly/x?utm_source=github&utm_medium=prisma_examples&utm_content=next_steps_section) for the latest updates. |
| 88 | +- Report issues or ask [questions on GitHub](https://pris.ly/github?utm_source=github&utm_medium=prisma_examples&utm_content=next_steps_section). |
0 commit comments