This is a sample project demonstrating how to build a RESTful API using Hono (a fast and lightweight web framework) and Supabase (a backend-as-a-service platform). The project uses Drizzle ORM for database interactions and Zod for schema validation.
- CRUD API: Create, Read, Update, and Delete operations for a
products
table. - Pagination and Filtering: Supports pagination and filtering for the
GET /products/all
endpoint. - Validation: Uses Zod for request validation.
- Database Migrations: Uses Drizzle ORM for database schema management.
- Environment Variables: Uses
dotenv
for managing environment variables.
- Hono: A fast and lightweight web framework.
- Supabase: A backend-as-a-service platform.
- Drizzle ORM: A TypeScript ORM for SQL databases.
- Zod: A TypeScript-first schema validation library.
- Faker.js: A library for generating fake data (used for seeding).
Before running the project, ensure you have the following installed:
- Bun (latest version)
- PostgreSQL (or a Supabase database)
- Supabase CLI (optional, for local Supabase setup)
-
Clone the Repository
git clone https://github.com/LaithMahdi/example_hono_drizzle_orm_supabase.git cd example_hono_drizzle_orm_supabase
-
Install Dependencies
bun install
-
Set Up Environment Variables
Create a
.env
file in the root directory and add the following variables:DATABASE_URL="postgres://user:password@localhost:5432/dbname" SUPABASE_URL="https://your-supabase-url.supabase.co" SUPABASE_KEY="your-supabase-anon-key"
Replace the placeholders with your actual database and Supabase credentials.
-
Run Database Migrations
Apply the database schema using Drizzle ORM:
bun run db:push
Alternatively, you can generate and run migrations:
bun run db:generate bun run db:migrate
-
Seed the Database (Optional)
If you want to seed the database with sample data, run:
bun run db:seed
-
Start the Development Server
bun run dev
The server will start at
http://localhost:3000
. -
Access the API
You can interact with the API using tools like Postman or
curl
.
-
Endpoint:
GET /products/all
-
Query Parameters:
page
: Page number (default:1
).limit
: Number of items per page (default:10
).isActive
: Filter byisActive
status (optional).
-
Example:
curl "http://localhost:3000/api/v1/products/all?page=1&limit=10&isActive=true"
-
Response:
{ "data": [ { "id": 1, "name": "Product 1", "description": "Description for Product 1", "price": 19.99, "isActive": true } ], "totalItems": 100, "pageInfo": { "hasPreviousPage": false, "hasNextPage": true } }
-
Endpoint:
GET /products/:id
-
Example:
curl "http://localhost:3000/api/v1/products/1"
-
Response:
{ "id": 1, "name": "Product 1", "description": "Description for Product 1", "price": 19.99, "isActive": true }
-
Endpoint:
POST /products
-
Request Body:
{ "name": "New Product", "description": "Product description", "price": 29.99, "isActive": true }
-
Response:
{ "id": 2, "name": "New Product", "description": "Product description", "price": 29.99, "isActive": true }
-
Endpoint:
PUT /products/:id
-
Request Body:
{ "name": "Updated Product", "price": 39.99 }
-
Response:
{ "id": 1, "name": "Updated Product", "description": "Description for Product 1", "price": 39.99, "isActive": true }
-
Endpoint:
DELETE /products/:id
-
Example:
curl -X DELETE "http://localhost:3000/api/v1/products/1"
-
Response:
{ "message": "Product deleted successfully" }
- Authentication Middleware: Ensures API access is restricted to authenticated users.
- Error Handling Middleware: Catches and formats API errors.
- Request Validation Middleware: Uses Zod to validate request payloads.
A Postman collection for testing the API is available here.
Contributions are welcome! Please open an issue or submit a pull request.
This project is licensed under the MIT License. See the LICENSE file for details.
- Hono for the lightweight web framework.
- Supabase for the backend-as-a-service platform.
- Drizzle ORM for the TypeScript ORM.
- Zod for schema validation.
Enjoy building with Hono and Supabase! 🚀