|
| 1 | +# REST API Example with Hono & Prisma Postgres |
| 2 | + |
| 3 | +This example shows how to implement a **REST API with TypeScript** using [Hono](https://hono.dev/) and [Prisma Client](https://www.prisma.io/docs/concepts/components/prisma-client). It uses a [Prisma Postgres](https://www.prisma.io/postgres) database. |
| 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 orm/hono --install npm --name hono |
| 13 | +``` |
| 14 | + |
| 15 | +Then, navigate into the project directory: |
| 16 | + |
| 17 | +``` |
| 18 | +cd hono |
| 19 | +``` |
| 20 | + |
| 21 | +<details><summary><strong>Alternative:</strong> Clone the entire repo</summary> |
| 22 | + |
| 23 | +Clone this repository: |
| 24 | + |
| 25 | +``` |
| 26 | +git clone git@github.com:prisma/prisma-examples.git --depth=1 |
| 27 | +``` |
| 28 | + |
| 29 | +Install npm dependencies: |
| 30 | + |
| 31 | +``` |
| 32 | +cd prisma-examples/orm/hono |
| 33 | +npm install |
| 34 | +``` |
| 35 | + |
| 36 | +</details> |
| 37 | + |
| 38 | +### 2. Create and seed the database |
| 39 | + |
| 40 | +Create a new [Prisma Postgres](https://www.prisma.io/docs/postgres/overview) database by executing: |
| 41 | + |
| 42 | +```terminal |
| 43 | +npx prisma init --db |
| 44 | +``` |
| 45 | + |
| 46 | +If you don't have a [Prisma Data Platform](https://console.prisma.io/) account yet, or if you are not logged in, the command will prompt you to log in using one of the available authentication providers. A browser window will open so you can log in or create an account. Return to the CLI after you have completed this step. |
| 47 | + |
| 48 | +Once logged in (or if you were already logged in), the CLI will prompt you to: |
| 49 | +1. Select a **region** (e.g. `us-west-1`) |
| 50 | +1. Enter a **project name** |
| 51 | + |
| 52 | +After successful creation, you will see output similar to the following: |
| 53 | + |
| 54 | +<details> |
| 55 | + |
| 56 | +<summary>CLI output</summary> |
| 57 | + |
| 58 | +```terminal |
| 59 | +Let's set up your Prisma Postgres database! |
| 60 | +? Select your region: ap-northeast-1 - Asia Pacific (Tokyo) |
| 61 | +? Enter a project name: testing-migration |
| 62 | +✔ Success! Your Prisma Postgres database is ready ✅ |
| 63 | +
|
| 64 | +We found an existing schema.prisma file in your current project directory. |
| 65 | +
|
| 66 | +--- Database URL --- |
| 67 | +
|
| 68 | +Connect Prisma ORM to your Prisma Postgres database with this URL: |
| 69 | +
|
| 70 | +prisma+postgres://accelerate.prisma-data.net/?api_key=... |
| 71 | +
|
| 72 | +--- Next steps --- |
| 73 | +
|
| 74 | +Go to https://pris.ly/ppg-init for detailed instructions. |
| 75 | +
|
| 76 | +1. Install and use the Prisma Accelerate extension |
| 77 | +Prisma Postgres requires the Prisma Accelerate extension for querying. If you haven't already installed it, install it in your project: |
| 78 | +npm install @prisma/extension-accelerate |
| 79 | +
|
| 80 | +...and add it to your Prisma Client instance: |
| 81 | +import { withAccelerate } from "@prisma/extension-accelerate" |
| 82 | +
|
| 83 | +const prisma = new PrismaClient().$extends(withAccelerate()) |
| 84 | +
|
| 85 | +2. Apply migrations |
| 86 | +Run the following command to create and apply a migration: |
| 87 | +npx prisma migrate dev |
| 88 | +
|
| 89 | +3. Manage your data |
| 90 | +View and edit your data locally by running this command: |
| 91 | +npx prisma studio |
| 92 | +
|
| 93 | +...or online in Console: |
| 94 | +https://console.prisma.io/{workspaceId}/{projectId}/studio |
| 95 | +
|
| 96 | +4. Send queries from your app |
| 97 | +If you already have an existing app with Prisma ORM, you can now run it and it will send queries against your newly created Prisma Postgres instance. |
| 98 | +
|
| 99 | +5. Learn more |
| 100 | +For more info, visit the Prisma Postgres docs: https://pris.ly/ppg-docs |
| 101 | +``` |
| 102 | + |
| 103 | +</details> |
| 104 | + |
| 105 | +Locate and copy the database URL provided in the CLI output. Then, create a `.env` file in the project root: |
| 106 | + |
| 107 | +```bash |
| 108 | +touch .env |
| 109 | +``` |
| 110 | + |
| 111 | +Now, paste the URL into it as a value for the `DATABASE_URL` environment variable. For example: |
| 112 | + |
| 113 | +```bash |
| 114 | +# .env |
| 115 | +DATABASE_URL=prisma+postgres://accelerate.prisma-data.net/?api_key=ey... |
| 116 | +``` |
| 117 | + |
| 118 | +Run the following command to create tables in your database. This creates the `User` and `Post` tables that are defined in [`prisma/schema.prisma`](./prisma/schema.prisma): |
| 119 | + |
| 120 | +```terminal |
| 121 | +npx prisma migrate dev --name init |
| 122 | +``` |
| 123 | + |
| 124 | +Execute the seed file in [`prisma/seed.ts`](./prisma/seed.ts) to populate your database with some sample data, by running: |
| 125 | + |
| 126 | +```terminal |
| 127 | +npx prisma db seed |
| 128 | +``` |
| 129 | + |
| 130 | +### 3. Start the REST API server |
| 131 | + |
| 132 | +``` |
| 133 | +npm run dev |
| 134 | +``` |
| 135 | + |
| 136 | +The server is now running on `http://localhost:3000`. You can send now the API requests, e.g. [`http://localhost:3000/users`](http://localhost:3000/users). |
| 137 | + |
| 138 | +## Switch to another database |
| 139 | + |
| 140 | +If you want to try this example with another database rather than Prisma Postgres, refer to the [Databases](https://www.prisma.io/docs/orm/overview/databases) section in our documentation |
| 141 | + |
| 142 | +## Next steps |
| 143 | + |
| 144 | +- Check out the [Prisma docs](https://www.prisma.io/docs) |
| 145 | +- Share your feedback on the [Prisma Discord](https://pris.ly/discord/) |
| 146 | +- Create issues and ask questions on [GitHub](https://github.com/prisma/prisma/) |
0 commit comments