Skip to content

Commit ee465e2

Browse files
Add elysia example (#8393)
* add elysia example * remove comment
1 parent 8190625 commit ee465e2

File tree

9 files changed

+561
-0
lines changed

9 files changed

+561
-0
lines changed

orm/elysia/.gitignore

Lines changed: 45 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,45 @@
1+
# See https://help.github.com/articles/ignoring-files/ for more about ignoring files.
2+
3+
# dependencies
4+
/node_modules
5+
/.pnp
6+
.pnp.js
7+
8+
# testing
9+
/coverage
10+
11+
# next.js
12+
/.next/
13+
/out/
14+
15+
# production
16+
/build
17+
18+
# misc
19+
.DS_Store
20+
*.pem
21+
22+
# debug
23+
npm-debug.log*
24+
yarn-debug.log*
25+
yarn-error.log*
26+
27+
# local env files
28+
.env
29+
.env.local
30+
.env.development.local
31+
.env.test.local
32+
.env.production.local
33+
34+
# vercel
35+
.vercel
36+
37+
**/*.trace
38+
**/*.zip
39+
**/*.tar.gz
40+
**/*.tgz
41+
**/*.log
42+
package-lock.json
43+
**/*.bun
44+
/src/generated/prisma
45+
/src/generated/prismabox

orm/elysia/README.md

Lines changed: 123 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,123 @@
1+
# REST API Example with Elysia & Prisma
2+
3+
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.
4+
5+
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.
6+
7+
## Project structure
8+
9+
- `src/index.ts` – defines the Elysia server and the REST routes for todos.
10+
- `src/lib/prisma.ts` – creates a Prisma Client using `@prisma/adapter-pg`.
11+
- `prisma/schema.prisma` – Prisma schema with the `Todo` model.
12+
- `prisma/seed.ts` – seeds the database with sample todos.
13+
14+
## Getting started
15+
16+
### 1. Download the example and install dependencies
17+
18+
```bash
19+
npx try-prisma@latest --template orm/elysia --name elysia
20+
cd elysia
21+
bun install
22+
```
23+
24+
> Alternatively clone this repo and run `bun install` inside `prisma-examples/orm/elysia`.
25+
26+
### 2. Configure `DATABASE_URL`
27+
28+
Create a Postgres database (Prisma Postgres, Supabase, Railway, Docker, etc.) and copy the direct connection string:
29+
30+
```
31+
postgresql://USER:PASSWORD@HOST:PORT/DATABASE?sslmode=require
32+
```
33+
34+
Create a `.env` file in the project root and add the URL:
35+
36+
```bash
37+
touch .env
38+
39+
# .env
40+
DATABASE_URL="postgresql://USER:PASSWORD@HOST:5432/DATABASE?sslmode=require"
41+
```
42+
43+
### 3. Generate Prisma Client & Prismabox models, migrate & seed the database
44+
45+
```bash
46+
bunx prisma generate
47+
bunx prisma migrate dev --name init
48+
bunx prisma db seed
49+
```
50+
51+
This generates:
52+
- **Prisma Client** - for database operations
53+
- **Prismabox models** - Elysia validation schemas (in `src/generated/prismabox/`)
54+
- Creates the tables defined in [`prisma/schema.prisma`](./prisma/schema.prisma)
55+
- Runs [`prisma/seed.ts`](./prisma/seed.ts) to insert demo data
56+
57+
### 4. Start the REST API server
58+
59+
```bash
60+
bun run dev
61+
```
62+
63+
The server listens on `http://localhost:3000`.
64+
65+
## API Endpoints
66+
67+
- `GET /todos` – get all todos
68+
- `GET /todos/:id` – get a single todo by ID
69+
- `POST /todos` – create a new todo
70+
- `PUT /todos/:id` – update a todo
71+
- `PATCH /todos/:id/toggle` – toggle todo completion status
72+
- `DELETE /todos/:id` – delete a todo
73+
74+
## Example API requests
75+
76+
### Get all todos
77+
78+
```bash
79+
curl http://localhost:3000/todos
80+
```
81+
82+
### Create a todo
83+
84+
```bash
85+
curl -X POST http://localhost:3000/todos \
86+
-H "Content-Type: application/json" \
87+
-d '{"title": "Buy groceries"}'
88+
```
89+
90+
### Update a todo
91+
92+
```bash
93+
curl -X PUT http://localhost:3000/todos/1 \
94+
-H "Content-Type: application/json" \
95+
-d '{"title": "Buy groceries and cook dinner", "completed": true}'
96+
```
97+
98+
### Toggle todo completion
99+
100+
```bash
101+
curl -X PATCH http://localhost:3000/todos/1/toggle
102+
```
103+
104+
### Delete a todo
105+
106+
```bash
107+
curl -X DELETE http://localhost:3000/todos/1
108+
```
109+
110+
## Features
111+
112+
- **Type-safe API**: Elysia's built-in schema validation with `Elysia.t` ensures type safety at both runtime and compile time.
113+
- **Prismabox Integration**: Automatically generates Elysia validation models from Prisma schema, eliminating manual schema definitions.
114+
- **Prisma ORM**: Full type safety with Prisma Client and automatic type inference.
115+
- **PostgreSQL**: Uses `@prisma/adapter-pg` for direct database connections.
116+
- **Bun runtime**: Optimized for Bun's fast JavaScript runtime.
117+
118+
## Learn more
119+
120+
- [Elysia Documentation](https://elysiajs.com/)
121+
- [Prisma Documentation](https://www.prisma.io/docs/)
122+
- [Prismabox Documentation](https://github.com/m1212e/prismabox)
123+
- [Prisma Client API Reference](https://www.prisma.io/docs/reference/api-reference/prisma-client-reference)

orm/elysia/package.json

Lines changed: 24 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,24 @@
1+
{
2+
"name": "elysia",
3+
"version": "1.0.0",
4+
"type": "module",
5+
"scripts": {
6+
"dev": "bun run --watch src/index.ts",
7+
"start": "bun run src/index.ts",
8+
"db:seed": "bun run prisma/seed.ts"
9+
},
10+
"dependencies": {
11+
"@prisma/adapter-pg": "^7.1.0",
12+
"@prisma/client": "^7.1.0",
13+
"elysia": "latest",
14+
"pg": "^8.16.3",
15+
"prismabox": "^1.1.25"
16+
},
17+
"devDependencies": {
18+
"bun-types": "latest",
19+
"prisma": "^7.1.0"
20+
},
21+
"prisma": {
22+
"seed": "bun run prisma/seed.ts"
23+
}
24+
}

orm/elysia/prisma.config.ts

Lines changed: 13 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,13 @@
1+
import "dotenv/config";
2+
import { defineConfig, env } from "prisma/config";
3+
4+
export default defineConfig({
5+
schema: "prisma/schema.prisma",
6+
migrations: {
7+
path: "prisma/migrations",
8+
seed: "bun run prisma/seed.ts",
9+
},
10+
datasource: {
11+
url: env("DATABASE_URL"),
12+
},
13+
});

orm/elysia/prisma/schema.prisma

Lines changed: 27 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,27 @@
1+
// This is your Prisma schema file,
2+
// learn more about it in the docs: https://pris.ly/d/prisma-schema
3+
4+
generator client {
5+
provider = "prisma-client"
6+
output = "../src/generated/prisma"
7+
}
8+
9+
generator prismabox {
10+
provider = "prismabox"
11+
typeboxImportDependencyName = "elysia"
12+
typeboxImportVariableName = "t"
13+
inputModel = true
14+
output = "../src/generated/prismabox"
15+
}
16+
17+
datasource db {
18+
provider = "postgresql"
19+
}
20+
21+
model Todo {
22+
id Int @id @default(autoincrement())
23+
title String
24+
completed Boolean @default(false)
25+
createdAt DateTime @default(now())
26+
updatedAt DateTime @updatedAt
27+
}

orm/elysia/prisma/seed.ts

Lines changed: 36 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,36 @@
1+
import { PrismaClient } from '../src/generated/prisma/client.js'
2+
import { PrismaPg } from '@prisma/adapter-pg'
3+
4+
if (!process.env.DATABASE_URL) {
5+
throw new Error('DATABASE_URL is not set')
6+
}
7+
8+
const adapter = new PrismaPg({ connectionString: process.env.DATABASE_URL })
9+
const prisma = new PrismaClient({ adapter })
10+
11+
const todoData = [
12+
{ title: 'Learn Elysia' },
13+
{ title: 'Learn Prisma' },
14+
{ title: 'Build something awesome', completed: true },
15+
]
16+
17+
async function main() {
18+
console.log('Start seeding...')
19+
for (const todo of todoData) {
20+
const created = await prisma.todo.create({
21+
data: todo,
22+
})
23+
console.log(`Created todo with id: ${created.id}`)
24+
}
25+
console.log('Seeding finished.')
26+
}
27+
28+
main()
29+
.then(async () => {
30+
await prisma.$disconnect()
31+
})
32+
.catch(async (e) => {
33+
console.error(e)
34+
await prisma.$disconnect()
35+
process.exit(1)
36+
})

0 commit comments

Comments
 (0)