-
Notifications
You must be signed in to change notification settings - Fork 1.5k
Add elysia example #8393
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
Merged
Merged
Add elysia example #8393
Changes from all commits
Commits
Show all changes
2 commits
Select commit
Hold shift + click to select a range
File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
There are no files selected for viewing
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,45 @@ | ||
| # See https://help.github.com/articles/ignoring-files/ for more about ignoring files. | ||
|
|
||
| # dependencies | ||
| /node_modules | ||
| /.pnp | ||
| .pnp.js | ||
|
|
||
| # testing | ||
| /coverage | ||
|
|
||
| # next.js | ||
| /.next/ | ||
| /out/ | ||
|
|
||
| # production | ||
| /build | ||
|
|
||
| # misc | ||
| .DS_Store | ||
| *.pem | ||
|
|
||
| # debug | ||
| npm-debug.log* | ||
| yarn-debug.log* | ||
| yarn-error.log* | ||
|
|
||
| # local env files | ||
| .env | ||
| .env.local | ||
| .env.development.local | ||
| .env.test.local | ||
| .env.production.local | ||
|
|
||
| # vercel | ||
| .vercel | ||
|
|
||
| **/*.trace | ||
| **/*.zip | ||
| **/*.tar.gz | ||
| **/*.tgz | ||
| **/*.log | ||
| package-lock.json | ||
| **/*.bun | ||
| /src/generated/prisma | ||
| /src/generated/prismabox |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,123 @@ | ||
| # REST API Example with Elysia & Prisma | ||
|
|
||
| This example shows how to implement a **simple Todo REST API with TypeScript** using [Elysia](https://elysiajs.com/) and [Prisma Client](https://www.prisma.io/docs/concepts/components/prisma-client). It connects to a Postgres database via the [`@prisma/adapter-pg`](https://www.prisma.io/docs/orm/overview/databases/postgresql/pg-driver-adapter) driver adapter. | ||
|
|
||
| This example uses [Prismabox](https://github.com/m1212e/prismabox) to automatically generate Elysia validation models from the Prisma schema, ensuring type safety and eliminating the need to manually define validation schemas. | ||
|
|
||
| ## Project structure | ||
|
|
||
| - `src/index.ts` – defines the Elysia server and the REST routes for todos. | ||
| - `src/lib/prisma.ts` – creates a Prisma Client using `@prisma/adapter-pg`. | ||
| - `prisma/schema.prisma` – Prisma schema with the `Todo` model. | ||
| - `prisma/seed.ts` – seeds the database with sample todos. | ||
|
|
||
| ## Getting started | ||
|
|
||
| ### 1. Download the example and install dependencies | ||
|
|
||
| ```bash | ||
| npx try-prisma@latest --template orm/elysia --name elysia | ||
| cd elysia | ||
| bun install | ||
| ``` | ||
|
|
||
| > Alternatively clone this repo and run `bun install` inside `prisma-examples/orm/elysia`. | ||
|
|
||
| ### 2. Configure `DATABASE_URL` | ||
|
|
||
| Create a Postgres database (Prisma Postgres, Supabase, Railway, Docker, etc.) and copy the direct connection string: | ||
|
|
||
| ``` | ||
| postgresql://USER:PASSWORD@HOST:PORT/DATABASE?sslmode=require | ||
| ``` | ||
|
|
||
| Create a `.env` file in the project root and add the URL: | ||
|
|
||
| ```bash | ||
| touch .env | ||
|
|
||
| # .env | ||
| DATABASE_URL="postgresql://USER:PASSWORD@HOST:5432/DATABASE?sslmode=require" | ||
| ``` | ||
|
|
||
| ### 3. Generate Prisma Client & Prismabox models, migrate & seed the database | ||
|
|
||
| ```bash | ||
| bunx prisma generate | ||
| bunx prisma migrate dev --name init | ||
| bunx prisma db seed | ||
| ``` | ||
|
|
||
| This generates: | ||
| - **Prisma Client** - for database operations | ||
| - **Prismabox models** - Elysia validation schemas (in `src/generated/prismabox/`) | ||
| - Creates the tables defined in [`prisma/schema.prisma`](./prisma/schema.prisma) | ||
| - Runs [`prisma/seed.ts`](./prisma/seed.ts) to insert demo data | ||
|
|
||
| ### 4. Start the REST API server | ||
|
|
||
| ```bash | ||
| bun run dev | ||
| ``` | ||
|
|
||
| The server listens on `http://localhost:3000`. | ||
|
|
||
| ## API Endpoints | ||
|
|
||
| - `GET /todos` – get all todos | ||
| - `GET /todos/:id` – get a single todo by ID | ||
| - `POST /todos` – create a new todo | ||
| - `PUT /todos/:id` – update a todo | ||
| - `PATCH /todos/:id/toggle` – toggle todo completion status | ||
| - `DELETE /todos/:id` – delete a todo | ||
|
|
||
| ## Example API requests | ||
|
|
||
| ### Get all todos | ||
|
|
||
| ```bash | ||
| curl http://localhost:3000/todos | ||
| ``` | ||
|
|
||
| ### Create a todo | ||
|
|
||
| ```bash | ||
| curl -X POST http://localhost:3000/todos \ | ||
| -H "Content-Type: application/json" \ | ||
| -d '{"title": "Buy groceries"}' | ||
| ``` | ||
|
|
||
| ### Update a todo | ||
|
|
||
| ```bash | ||
| curl -X PUT http://localhost:3000/todos/1 \ | ||
| -H "Content-Type: application/json" \ | ||
| -d '{"title": "Buy groceries and cook dinner", "completed": true}' | ||
| ``` | ||
|
|
||
| ### Toggle todo completion | ||
|
|
||
| ```bash | ||
| curl -X PATCH http://localhost:3000/todos/1/toggle | ||
| ``` | ||
|
|
||
| ### Delete a todo | ||
|
|
||
| ```bash | ||
| curl -X DELETE http://localhost:3000/todos/1 | ||
| ``` | ||
|
|
||
| ## Features | ||
|
|
||
| - **Type-safe API**: Elysia's built-in schema validation with `Elysia.t` ensures type safety at both runtime and compile time. | ||
| - **Prismabox Integration**: Automatically generates Elysia validation models from Prisma schema, eliminating manual schema definitions. | ||
| - **Prisma ORM**: Full type safety with Prisma Client and automatic type inference. | ||
| - **PostgreSQL**: Uses `@prisma/adapter-pg` for direct database connections. | ||
| - **Bun runtime**: Optimized for Bun's fast JavaScript runtime. | ||
|
|
||
| ## Learn more | ||
|
|
||
| - [Elysia Documentation](https://elysiajs.com/) | ||
| - [Prisma Documentation](https://www.prisma.io/docs/) | ||
| - [Prismabox Documentation](https://github.com/m1212e/prismabox) | ||
| - [Prisma Client API Reference](https://www.prisma.io/docs/reference/api-reference/prisma-client-reference) |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,24 @@ | ||
| { | ||
| "name": "elysia", | ||
| "version": "1.0.0", | ||
| "type": "module", | ||
| "scripts": { | ||
| "dev": "bun run --watch src/index.ts", | ||
| "start": "bun run src/index.ts", | ||
| "db:seed": "bun run prisma/seed.ts" | ||
| }, | ||
| "dependencies": { | ||
| "@prisma/adapter-pg": "^7.1.0", | ||
| "@prisma/client": "^7.1.0", | ||
| "elysia": "latest", | ||
| "pg": "^8.16.3", | ||
| "prismabox": "^1.1.25" | ||
| }, | ||
| "devDependencies": { | ||
| "bun-types": "latest", | ||
AmanVarshney01 marked this conversation as resolved.
Show resolved
Hide resolved
|
||
| "prisma": "^7.1.0" | ||
| }, | ||
| "prisma": { | ||
| "seed": "bun run prisma/seed.ts" | ||
| } | ||
| } | ||
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,13 @@ | ||
| import "dotenv/config"; | ||
| import { defineConfig, env } from "prisma/config"; | ||
|
|
||
| export default defineConfig({ | ||
| schema: "prisma/schema.prisma", | ||
| migrations: { | ||
| path: "prisma/migrations", | ||
| seed: "bun run prisma/seed.ts", | ||
| }, | ||
| datasource: { | ||
| url: env("DATABASE_URL"), | ||
| }, | ||
| }); |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,27 @@ | ||
| // This is your Prisma schema file, | ||
| // learn more about it in the docs: https://pris.ly/d/prisma-schema | ||
|
|
||
| generator client { | ||
| provider = "prisma-client" | ||
| output = "../src/generated/prisma" | ||
| } | ||
|
|
||
| generator prismabox { | ||
| provider = "prismabox" | ||
| typeboxImportDependencyName = "elysia" | ||
| typeboxImportVariableName = "t" | ||
| inputModel = true | ||
| output = "../src/generated/prismabox" | ||
| } | ||
|
|
||
| datasource db { | ||
| provider = "postgresql" | ||
| } | ||
|
|
||
| model Todo { | ||
| id Int @id @default(autoincrement()) | ||
| title String | ||
| completed Boolean @default(false) | ||
| createdAt DateTime @default(now()) | ||
| updatedAt DateTime @updatedAt | ||
| } |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,36 @@ | ||
| import { PrismaClient } from '../src/generated/prisma/client.js' | ||
| import { PrismaPg } from '@prisma/adapter-pg' | ||
|
|
||
| if (!process.env.DATABASE_URL) { | ||
| throw new Error('DATABASE_URL is not set') | ||
| } | ||
|
|
||
| const adapter = new PrismaPg({ connectionString: process.env.DATABASE_URL }) | ||
| const prisma = new PrismaClient({ adapter }) | ||
|
|
||
| const todoData = [ | ||
| { title: 'Learn Elysia' }, | ||
| { title: 'Learn Prisma' }, | ||
| { title: 'Build something awesome', completed: true }, | ||
| ] | ||
|
|
||
| async function main() { | ||
| console.log('Start seeding...') | ||
| for (const todo of todoData) { | ||
| const created = await prisma.todo.create({ | ||
| data: todo, | ||
| }) | ||
| console.log(`Created todo with id: ${created.id}`) | ||
| } | ||
| console.log('Seeding finished.') | ||
| } | ||
|
|
||
| main() | ||
| .then(async () => { | ||
| await prisma.$disconnect() | ||
| }) | ||
| .catch(async (e) => { | ||
| console.error(e) | ||
| await prisma.$disconnect() | ||
| process.exit(1) | ||
| }) |
Oops, something went wrong.
Oops, something went wrong.
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
Uh oh!
There was an error while loading. Please reload this page.