|
1 | 1 | --- |
2 | | -title: 'How to use Prisma ORM with Hono' |
| 2 | +title: 'How to use Prisma with Hono' |
3 | 3 | metaTitle: 'How to use Prisma ORM and Prisma Postgres with Hono' |
4 | 4 | description: 'Learn how to use Prisma ORM in a Hono app' |
5 | 5 | sidebar_label: 'Hono' |
@@ -33,7 +33,7 @@ npm create hono@latest |
33 | 33 | - *Which package manager do you want to use?* `npm` |
34 | 34 | ::: |
35 | 35 |
|
36 | | -## 2. Install and Configure Prisma |
| 36 | +## 2. Install and configure Prisma |
37 | 37 |
|
38 | 38 | ### 2.1. Install dependencies |
39 | 39 |
|
@@ -209,9 +209,9 @@ And open Prisma Studio to inspect your data: |
209 | 209 | npx prisma studio |
210 | 210 | ``` |
211 | 211 |
|
212 | | -## 3. Integrate Prisma Into Hono |
| 212 | +## 3. Integrate Prisma into Hono |
213 | 213 |
|
214 | | -### 3.1. Create a Prisma Middleware |
| 214 | +### 3.1. Create a Prisma middleware |
215 | 215 |
|
216 | 216 | Inside of `/src`, create a `lib` directory and a `prisma.ts` file inside it. This file will be used to create and export your Prisma Client instance. Set up the Prisma client like this: |
217 | 217 |
|
@@ -283,20 +283,42 @@ import * as dotenv from 'dotenv'; |
283 | 283 | dotenv.config(); |
284 | 284 | ``` |
285 | 285 |
|
286 | | -Next, Hono needs additional types to to know that the `withPrisma` middleware will set a `prsima` |
| 286 | +Next, Hono needs additional types to to know that the `withPrisma` middleware will set a `prisma` |
287 | 287 | key on the Hono Context |
288 | 288 |
|
289 | 289 | ```ts file=src/index.ts |
290 | | -import type { PrismaClient } from './generated/prisma/client.js'; |
| 290 | +import { Hono } from "hono"; |
| 291 | +import { serve } from "@hono/node-server"; |
| 292 | +// add-next-line |
| 293 | +import type { PrismaClient } from "./generated/prisma/client.js"; |
291 | 294 |
|
| 295 | +import * as dotenv from "dotenv"; |
| 296 | +dotenv.config(); |
| 297 | + |
| 298 | +// add-start |
292 | 299 | type ContextWithPrisma = { |
293 | 300 | Variables: { |
294 | 301 | prisma: PrismaClient; |
295 | 302 | }; |
296 | 303 | }; |
| 304 | +// add-end |
297 | 305 |
|
| 306 | +// edit-next-line |
298 | 307 | const app = new Hono<ContextWithPrisma>(); |
299 | | -``` |
| 308 | + |
| 309 | +app.get("/", (c) => { |
| 310 | + return c.text("Hello Hono!"); |
| 311 | +}); |
| 312 | + |
| 313 | +serve( |
| 314 | + { |
| 315 | + fetch: app.fetch, |
| 316 | + port: 3000, |
| 317 | + }, |
| 318 | + (info) => { |
| 319 | + console.log(`Server is running on http://localhost:${info.port}`); |
| 320 | + } |
| 321 | +); |
300 | 322 |
|
301 | 323 |
|
302 | 324 | ### 3.3. Create A GET Route |
|
0 commit comments