Skip to content

Commit 4c03bd1

Browse files
authored
feat(): add hono example (#8317)
1 parent e212116 commit 4c03bd1

File tree

9 files changed

+568
-0
lines changed

9 files changed

+568
-0
lines changed

.github/tests/orm/hono/run.sh

Lines changed: 74 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,74 @@
1+
#!/bin/bash
2+
3+
set -eu
4+
5+
echo "🔍 Starting test setup for hono..."
6+
7+
echo "📂 Current working directory before REPO_ROOT: $(pwd)"
8+
echo "📁 Listing contents:"
9+
ls -la
10+
11+
REPO_ROOT="$(git rev-parse --show-toplevel)"
12+
echo "📌 Detected repo root: $REPO_ROOT"
13+
14+
cd "$REPO_ROOT/orm/hono"
15+
echo "📂 Changed directory to: $(pwd)"
16+
17+
echo "📦 Installing test deps..."
18+
npm install
19+
20+
# Go to Node script dir and install its deps
21+
NODE_SCRIPT_DIR="../../.github/get-ppg-dev"
22+
pushd "$NODE_SCRIPT_DIR" > /dev/null
23+
npm install
24+
25+
# Start Prisma Dev server
26+
LOG_FILE="./ppg-dev-url.log"
27+
rm -f "$LOG_FILE"
28+
touch "$LOG_FILE"
29+
30+
echo "🚀 Starting Prisma Dev in background..."
31+
node index.js >"$LOG_FILE" &
32+
NODE_PID=$!
33+
34+
# Wait for DATABASE_URL
35+
echo "🔎 Waiting for Prisma Dev to emit DATABASE_URL..."
36+
MAX_WAIT=60
37+
WAITED=0
38+
until grep -q '^prisma+postgres://' "$LOG_FILE"; do
39+
sleep 1
40+
WAITED=$((WAITED + 1))
41+
if [ "$WAITED" -ge "$MAX_WAIT" ]; then
42+
echo "❌ Timeout waiting for DATABASE_URL"
43+
cat "$LOG_FILE"
44+
kill "$NODE_PID" || true
45+
exit 1
46+
fi
47+
done
48+
49+
DB_URL=$(grep '^prisma+postgres://' "$LOG_FILE" | tail -1)
50+
export DATABASE_URL="$DB_URL"
51+
echo "✅ DATABASE_URL: $DATABASE_URL"
52+
53+
popd > /dev/null # Back to orm/hono
54+
55+
# Run migrations and seed
56+
npx prisma migrate dev --name init
57+
npx prisma db seed
58+
59+
# Start the app
60+
echo "🚀 Starting Hono app..."
61+
npm run dev &
62+
pid=$!
63+
64+
sleep 15
65+
66+
# Check frontend
67+
echo "🔎 Verifying root frontend route..."
68+
curl --fail 'http://localhost:3000/'
69+
70+
# Cleanup
71+
echo "🛑 Shutting down Hono app (PID $pid) and Prisma Dev (PID $NODE_PID)..."
72+
kill "$pid"
73+
kill "$NODE_PID"
74+
wait "$NODE_PID" || true

orm/hono/.env.example

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,2 @@
1+
# Prisma
2+
DATABASE_URL=""

orm/hono/README.md

Lines changed: 146 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,146 @@
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/)

orm/hono/package.json

Lines changed: 25 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,25 @@
1+
{
2+
"name": "my-app",
3+
"type": "module",
4+
"scripts": {
5+
"dev": "tsx watch src/index.ts",
6+
"build": "tsc",
7+
"start": "node dist/index.js"
8+
},
9+
"prisma": {
10+
"seed": "tsx prisma/seed.ts"
11+
},
12+
"dependencies": {
13+
"@hono/node-server": "^1.19.5",
14+
"@prisma/client": "^6.16.3",
15+
"@prisma/extension-accelerate": "^2.0.2",
16+
"dotenv": "^17.2.3",
17+
"hono": "^4.9.9"
18+
},
19+
"devDependencies": {
20+
"@types/node": "^20.11.17",
21+
"prisma": "^6.16.3",
22+
"tsx": "^4.20.6",
23+
"typescript": "^5.8.3"
24+
}
25+
}

orm/hono/prisma/schema.prisma

Lines changed: 35 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,35 @@
1+
// This is your Prisma schema file,
2+
// learn more about it in the docs: https://pris.ly/d/prisma-schema
3+
4+
// Looking for ways to speed up your queries, or scale easily with your serverless or edge functions?
5+
// Try Prisma Accelerate: https://pris.ly/cli/accelerate-init
6+
7+
generator client {
8+
provider = "prisma-client"
9+
output = "../src/generated/prisma"
10+
}
11+
12+
datasource db {
13+
provider = "postgresql"
14+
url = env("DATABASE_URL")
15+
}
16+
17+
18+
model User {
19+
id Int @id @default(autoincrement())
20+
email String @unique
21+
name String?
22+
posts Post[]
23+
}
24+
25+
model Post {
26+
id Int @id @default(autoincrement())
27+
createdAt DateTime @default(now())
28+
updatedAt DateTime @updatedAt
29+
title String
30+
content String?
31+
published Boolean @default(false)
32+
viewCount Int @default(0)
33+
author User? @relation(fields: [authorId], references: [id])
34+
authorId Int?
35+
}

orm/hono/prisma/seed.ts

Lines changed: 57 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,57 @@
1+
import { PrismaClient, Prisma } from "../src/generated/prisma/client.js";
2+
3+
const prisma = new PrismaClient();
4+
5+
const userData: Prisma.UserCreateInput[] = [
6+
{
7+
name: 'Alice',
8+
email: 'alice@prisma.io',
9+
posts: {
10+
create: [
11+
{
12+
title: 'Join the Prisma Discord',
13+
content: 'https://pris.ly/discord',
14+
published: true,
15+
},
16+
],
17+
},
18+
},
19+
{
20+
name: 'Nilu',
21+
email: 'nilu@prisma.io',
22+
posts: {
23+
create: [
24+
{
25+
title: 'Follow Prisma on Twitter',
26+
content: 'https://www.twitter.com/prisma',
27+
published: true,
28+
},
29+
],
30+
},
31+
},
32+
{
33+
name: 'Mahmoud',
34+
email: 'mahmoud@prisma.io',
35+
posts: {
36+
create: [
37+
{
38+
title: 'Ask a question about Prisma on GitHub',
39+
content: 'https://www.github.com/prisma/prisma/discussions',
40+
published: true,
41+
},
42+
{
43+
title: 'Prisma on YouTube',
44+
content: 'https://pris.ly/youtube',
45+
},
46+
],
47+
},
48+
},
49+
]
50+
51+
export async function main() {
52+
for (const u of userData) {
53+
await prisma.user.create({ data: u });
54+
}
55+
}
56+
57+
main();

0 commit comments

Comments
 (0)