-
Notifications
You must be signed in to change notification settings - Fork 1.5k
feat(): add hono example #8317
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
feat(): add hono example #8317
Changes from all commits
File filter
Filter by extension
Conversations
Jump to
Diff view
Diff view
There are no files selected for viewing
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,74 @@ | ||
| #!/bin/bash | ||
|
|
||
| set -eu | ||
|
|
||
| echo "🔍 Starting test setup for hono..." | ||
|
|
||
| echo "📂 Current working directory before REPO_ROOT: $(pwd)" | ||
| echo "📁 Listing contents:" | ||
| ls -la | ||
|
|
||
| REPO_ROOT="$(git rev-parse --show-toplevel)" | ||
| echo "📌 Detected repo root: $REPO_ROOT" | ||
|
|
||
| cd "$REPO_ROOT/orm/hono" | ||
| echo "📂 Changed directory to: $(pwd)" | ||
|
|
||
| echo "📦 Installing test deps..." | ||
| npm install | ||
|
|
||
mhartington marked this conversation as resolved.
Show resolved
Hide resolved
|
||
| # Go to Node script dir and install its deps | ||
| NODE_SCRIPT_DIR="../../.github/get-ppg-dev" | ||
| pushd "$NODE_SCRIPT_DIR" > /dev/null | ||
| npm install | ||
|
|
||
| # Start Prisma Dev server | ||
| LOG_FILE="./ppg-dev-url.log" | ||
| rm -f "$LOG_FILE" | ||
| touch "$LOG_FILE" | ||
|
|
||
| echo "🚀 Starting Prisma Dev in background..." | ||
| node index.js >"$LOG_FILE" & | ||
| NODE_PID=$! | ||
|
|
||
| # Wait for DATABASE_URL | ||
| echo "🔎 Waiting for Prisma Dev to emit DATABASE_URL..." | ||
| MAX_WAIT=60 | ||
| WAITED=0 | ||
| until grep -q '^prisma+postgres://' "$LOG_FILE"; do | ||
| sleep 1 | ||
| WAITED=$((WAITED + 1)) | ||
| if [ "$WAITED" -ge "$MAX_WAIT" ]; then | ||
| echo "❌ Timeout waiting for DATABASE_URL" | ||
| cat "$LOG_FILE" | ||
| kill "$NODE_PID" || true | ||
| exit 1 | ||
| fi | ||
| done | ||
|
|
||
| DB_URL=$(grep '^prisma+postgres://' "$LOG_FILE" | tail -1) | ||
| export DATABASE_URL="$DB_URL" | ||
| echo "✅ DATABASE_URL: $DATABASE_URL" | ||
|
|
||
mhartington marked this conversation as resolved.
Show resolved
Hide resolved
|
||
| popd > /dev/null # Back to orm/hono | ||
|
|
||
| # Run migrations and seed | ||
| npx prisma migrate dev --name init | ||
| npx prisma db seed | ||
mhartington marked this conversation as resolved.
Show resolved
Hide resolved
|
||
|
|
||
| # Start the app | ||
| echo "🚀 Starting Hono app..." | ||
| npm run dev & | ||
| pid=$! | ||
|
|
||
mhartington marked this conversation as resolved.
Show resolved
Hide resolved
|
||
| sleep 15 | ||
|
|
||
| # Check frontend | ||
| echo "🔎 Verifying root frontend route..." | ||
| curl --fail 'http://localhost:3000/' | ||
|
|
||
| # Cleanup | ||
| echo "🛑 Shutting down Hono app (PID $pid) and Prisma Dev (PID $NODE_PID)..." | ||
| kill "$pid" | ||
| kill "$NODE_PID" | ||
| wait "$NODE_PID" || true | ||
mhartington marked this conversation as resolved.
Show resolved
Hide resolved
|
||
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,2 @@ | ||
| # Prisma | ||
| DATABASE_URL="" | ||
mhartington marked this conversation as resolved.
Show resolved
Hide resolved
|
||
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,146 @@ | ||
| # REST API Example with Hono & Prisma Postgres | ||
|
|
||
| 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. | ||
|
|
||
| ## Getting started | ||
|
|
||
| ### 1. Download example and navigate into the project directory | ||
|
|
||
| Download this example: | ||
|
|
||
| ``` | ||
mhartington marked this conversation as resolved.
Show resolved
Hide resolved
|
||
| npx try-prisma@latest --template orm/hono --install npm --name hono | ||
| ``` | ||
|
|
||
| Then, navigate into the project directory: | ||
|
|
||
| ``` | ||
| cd hono | ||
| ``` | ||
|
|
||
| <details><summary><strong>Alternative:</strong> Clone the entire repo</summary> | ||
|
|
||
| Clone this repository: | ||
|
|
||
| ``` | ||
| git clone git@github.com:prisma/prisma-examples.git --depth=1 | ||
| ``` | ||
|
|
||
| Install npm dependencies: | ||
|
|
||
| ``` | ||
| cd prisma-examples/orm/hono | ||
| npm install | ||
| ``` | ||
|
|
||
| </details> | ||
|
|
||
| ### 2. Create and seed the database | ||
|
|
||
| Create a new [Prisma Postgres](https://www.prisma.io/docs/postgres/overview) database by executing: | ||
|
|
||
| ```terminal | ||
| npx prisma init --db | ||
| ``` | ||
|
|
||
| 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. | ||
|
|
||
| Once logged in (or if you were already logged in), the CLI will prompt you to: | ||
| 1. Select a **region** (e.g. `us-west-1`) | ||
| 1. Enter a **project name** | ||
|
|
||
| After successful creation, you will see output similar to the following: | ||
|
|
||
| <details> | ||
|
|
||
| <summary>CLI output</summary> | ||
|
|
||
| ```terminal | ||
| Let's set up your Prisma Postgres database! | ||
| ? Select your region: ap-northeast-1 - Asia Pacific (Tokyo) | ||
| ? Enter a project name: testing-migration | ||
| ✔ Success! Your Prisma Postgres database is ready ✅ | ||
|
|
||
| We found an existing schema.prisma file in your current project directory. | ||
|
|
||
| --- Database URL --- | ||
|
|
||
| Connect Prisma ORM to your Prisma Postgres database with this URL: | ||
|
|
||
| prisma+postgres://accelerate.prisma-data.net/?api_key=... | ||
|
|
||
| --- Next steps --- | ||
|
|
||
| Go to https://pris.ly/ppg-init for detailed instructions. | ||
|
|
||
| 1. Install and use the Prisma Accelerate extension | ||
| Prisma Postgres requires the Prisma Accelerate extension for querying. If you haven't already installed it, install it in your project: | ||
| npm install @prisma/extension-accelerate | ||
|
|
||
| ...and add it to your Prisma Client instance: | ||
| import { withAccelerate } from "@prisma/extension-accelerate" | ||
|
|
||
| const prisma = new PrismaClient().$extends(withAccelerate()) | ||
|
|
||
| 2. Apply migrations | ||
| Run the following command to create and apply a migration: | ||
| npx prisma migrate dev | ||
|
|
||
| 3. Manage your data | ||
| View and edit your data locally by running this command: | ||
| npx prisma studio | ||
|
|
||
| ...or online in Console: | ||
| https://console.prisma.io/{workspaceId}/{projectId}/studio | ||
|
|
||
| 4. Send queries from your app | ||
| 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. | ||
|
|
||
| 5. Learn more | ||
| For more info, visit the Prisma Postgres docs: https://pris.ly/ppg-docs | ||
| ``` | ||
|
|
||
| </details> | ||
|
|
||
| Locate and copy the database URL provided in the CLI output. Then, create a `.env` file in the project root: | ||
|
|
||
| ```bash | ||
| touch .env | ||
| ``` | ||
|
|
||
| Now, paste the URL into it as a value for the `DATABASE_URL` environment variable. For example: | ||
|
|
||
| ```bash | ||
| # .env | ||
| DATABASE_URL=prisma+postgres://accelerate.prisma-data.net/?api_key=ey... | ||
| ``` | ||
|
|
||
| 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): | ||
|
|
||
| ```terminal | ||
| npx prisma migrate dev --name init | ||
| ``` | ||
|
|
||
| Execute the seed file in [`prisma/seed.ts`](./prisma/seed.ts) to populate your database with some sample data, by running: | ||
|
|
||
| ```terminal | ||
| npx prisma db seed | ||
| ``` | ||
|
|
||
| ### 3. Start the REST API server | ||
|
|
||
| ``` | ||
| npm run dev | ||
| ``` | ||
|
|
||
| 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). | ||
|
|
||
| ## Switch to another database | ||
|
|
||
| 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 | ||
|
|
||
| ## Next steps | ||
|
|
||
| - Check out the [Prisma docs](https://www.prisma.io/docs) | ||
| - Share your feedback on the [Prisma Discord](https://pris.ly/discord/) | ||
| - Create issues and ask questions on [GitHub](https://github.com/prisma/prisma/) | ||
| Original file line number | Diff line number | Diff line change | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|
| @@ -0,0 +1,25 @@ | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| { | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| "name": "my-app", | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| "type": "module", | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| "scripts": { | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| "dev": "tsx watch src/index.ts", | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| "build": "tsc", | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| "start": "node dist/index.js" | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| }, | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| "prisma": { | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| "seed": "tsx prisma/seed.ts" | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| }, | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| "dependencies": { | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| "@hono/node-server": "^1.19.5", | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| "@prisma/client": "^6.16.3", | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| "@prisma/extension-accelerate": "^2.0.2", | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| "dotenv": "^17.2.3", | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| "hono": "^4.9.9" | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| }, | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| "devDependencies": { | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| "@types/node": "^20.11.17", | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| "prisma": "^6.16.3", | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| "tsx": "^4.20.6", | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| "typescript": "^5.8.3" | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| } | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| } | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
|
Comment on lines
+1
to
+25
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. 🧹 Nitpick | 🔵 Trivial LGTM! Dependencies are current and properly configured. The package configuration correctly sets up:
Optionally, consider updating Hono from 4.9.9 to 4.9.10 to get the latest fixes (based on learnings). - "hono": "^4.9.9"
+ "hono": "^4.9.10"📝 Committable suggestion
Suggested change
🤖 Prompt for AI Agents |
||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,35 @@ | ||
| // This is your Prisma schema file, | ||
| // learn more about it in the docs: https://pris.ly/d/prisma-schema | ||
|
|
||
| // Looking for ways to speed up your queries, or scale easily with your serverless or edge functions? | ||
| // Try Prisma Accelerate: https://pris.ly/cli/accelerate-init | ||
|
|
||
| generator client { | ||
| provider = "prisma-client" | ||
| output = "../src/generated/prisma" | ||
| } | ||
|
|
||
| datasource db { | ||
| provider = "postgresql" | ||
| url = env("DATABASE_URL") | ||
| } | ||
|
|
||
|
|
||
| model User { | ||
| id Int @id @default(autoincrement()) | ||
| email String @unique | ||
| name String? | ||
| posts Post[] | ||
| } | ||
|
|
||
| model Post { | ||
| id Int @id @default(autoincrement()) | ||
| createdAt DateTime @default(now()) | ||
| updatedAt DateTime @updatedAt | ||
| title String | ||
| content String? | ||
| published Boolean @default(false) | ||
| viewCount Int @default(0) | ||
| author User? @relation(fields: [authorId], references: [id]) | ||
| authorId Int? | ||
| } |
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,57 @@ | ||
| import { PrismaClient, Prisma } from "../src/generated/prisma/client.js"; | ||
|
|
||
| const prisma = new PrismaClient(); | ||
|
|
||
| const userData: Prisma.UserCreateInput[] = [ | ||
| { | ||
| name: 'Alice', | ||
| email: 'alice@prisma.io', | ||
| posts: { | ||
| create: [ | ||
| { | ||
| title: 'Join the Prisma Discord', | ||
| content: 'https://pris.ly/discord', | ||
| published: true, | ||
| }, | ||
| ], | ||
| }, | ||
| }, | ||
| { | ||
| name: 'Nilu', | ||
| email: 'nilu@prisma.io', | ||
| posts: { | ||
| create: [ | ||
| { | ||
| title: 'Follow Prisma on Twitter', | ||
| content: 'https://www.twitter.com/prisma', | ||
| published: true, | ||
| }, | ||
| ], | ||
| }, | ||
| }, | ||
| { | ||
| name: 'Mahmoud', | ||
| email: 'mahmoud@prisma.io', | ||
| posts: { | ||
| create: [ | ||
| { | ||
| title: 'Ask a question about Prisma on GitHub', | ||
| content: 'https://www.github.com/prisma/prisma/discussions', | ||
| published: true, | ||
| }, | ||
| { | ||
| title: 'Prisma on YouTube', | ||
| content: 'https://pris.ly/youtube', | ||
| }, | ||
| ], | ||
| }, | ||
| }, | ||
| ] | ||
|
|
||
| export async function main() { | ||
| for (const u of userData) { | ||
| await prisma.user.create({ data: u }); | ||
| } | ||
| } | ||
|
|
||
| main(); | ||
mhartington marked this conversation as resolved.
Show resolved
Hide resolved
|
||
Uh oh!
There was an error while loading. Please reload this page.