|
| 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) |
0 commit comments