{"id":26574185,"url":"https://github.com/aashari/boilerplate-bun-restapi","last_synced_at":"2025-03-23T01:38:08.920Z","repository":{"id":215389047,"uuid":"738811596","full_name":"aashari/boilerplate-bun-restapi","owner":"aashari","description":"A modern REST API boilerplate built with Bun runtime and Elysia framework. Offers superior performance over Node.js with MongoDB integration, request validation, authentication middleware, comprehensive error handling, and Docker deployment configurations.","archived":false,"fork":false,"pushed_at":"2025-03-21T15:22:20.000Z","size":45,"stargazers_count":5,"open_issues_count":1,"forks_count":2,"subscribers_count":1,"default_branch":"main","last_synced_at":"2025-03-21T16:25:23.372Z","etag":null,"topics":["api","api-development","boilerplate","bun","docker","elysia","elysiajs","mongodb","mongoose","rest-api","restapi","typescript","web-server"],"latest_commit_sha":null,"homepage":"https://aashari.github.io/boilerplate-rest-api-bun","language":"TypeScript","has_issues":true,"has_wiki":null,"has_pages":null,"mirror_url":null,"source_name":null,"license":null,"status":null,"scm":"git","pull_requests_enabled":true,"icon_url":"https://github.com/aashari.png","metadata":{"files":{"readme":"README.md","changelog":null,"contributing":null,"funding":null,"license":null,"code_of_conduct":null,"threat_model":null,"audit":null,"citation":null,"codeowners":null,"security":null,"support":null,"governance":null,"roadmap":null,"authors":null,"dei":null,"publiccode":null,"codemeta":null}},"created_at":"2024-01-04T05:20:05.000Z","updated_at":"2025-03-21T15:22:23.000Z","dependencies_parsed_at":"2024-01-05T17:29:14.788Z","dependency_job_id":"3ef36e8b-566f-4031-9768-ddfaa85e2de4","html_url":"https://github.com/aashari/boilerplate-bun-restapi","commit_stats":null,"previous_names":["aashari/boilerplate-rest-api-bun","aashari/boilerplate-bun-restapi"],"tags_count":2,"template":false,"template_full_name":null,"repository_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories/aashari%2Fboilerplate-bun-restapi","tags_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories/aashari%2Fboilerplate-bun-restapi/tags","releases_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories/aashari%2Fboilerplate-bun-restapi/releases","manifests_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories/aashari%2Fboilerplate-bun-restapi/manifests","owner_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/owners/aashari","download_url":"https://codeload.github.com/aashari/boilerplate-bun-restapi/tar.gz/refs/heads/main","host":{"name":"GitHub","url":"https://github.com","kind":"github","repositories_count":245044499,"owners_count":20551898,"icon_url":"https://github.com/github.png","version":null,"created_at":"2022-05-30T11:31:42.601Z","updated_at":"2022-07-04T15:15:14.044Z","host_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub","repositories_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories","repository_names_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repository_names","owners_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/owners"}},"keywords":["api","api-development","boilerplate","bun","docker","elysia","elysiajs","mongodb","mongoose","rest-api","restapi","typescript","web-server"],"created_at":"2025-03-23T01:38:08.201Z","updated_at":"2025-03-23T01:38:08.908Z","avatar_url":"https://github.com/aashari.png","language":"TypeScript","funding_links":[],"categories":[],"sub_categories":[],"readme":"# Boilerplate REST API using Bun\n\nThis repository contains a professional boilerplate for building a REST API using the [Bun](https://bun.sh/) runtime,\nan alternative to Node.js designed to be faster and more efficient. The project is structured using `Elysia`,\na minimalist web framework for Bun, and `mongoose` for MongoDB object modeling.\n\n## Overview\n\nThis project provides a complete, production-ready REST API that manages authors and their blog posts. It demonstrates:\n\n- Modern REST API architecture with TypeScript\n- MongoDB integration with Mongoose\n- Clean project structure with separation of concerns\n- Robust error handling and response standardization\n\nSuitable for both learning purposes and as a foundation for production applications.\n\n## Features\n\n- **Bun Runtime**: Ultra-fast JavaScript runtime as an alternative to Node.js\n- **Elysia Framework**: Lightweight and fast web framework for handling HTTP requests\n- **MongoDB Integration**: Complete setup with Mongoose models for authors and posts\n- **Docker Support**: Ready-to-use Docker Compose setup for MongoDB and Mongo Express\n- **TypeScript**: Full TypeScript support for better developer experience\n- **Error Handling**: Built-in middleware for error handling and response standardization\n- **Environment Variables**: Configured to use environment variables for easy configuration\n- **Helper Scripts**: Includes a convenient script for managing the API and MongoDB\n- **Detailed Guide**: Comprehensive guide for running and testing the API\n\n## Prerequisites\n\nBefore you begin, ensure you have the following installed:\n\n- [Bun](https://bun.sh/) runtime (v1.0.0 or newer)\n  - Installation: `curl -fsSL https://bun.sh/install | bash`\n- [Docker](https://www.docker.com/) and [Docker Compose](https://docs.docker.com/compose/) (for MongoDB)\n  - Follow the official Docker installation guide for your operating system\n- [curl](https://curl.se/) or [Postman](https://www.postman.com/) for testing API endpoints (optional)\n\n## Quick Start\n\n### Step 1: Clone the repository\n\n```sh\ngit clone https://github.com/aashari/boilerplate-rest-api-bun.git\ncd boilerplate-rest-api-bun\n```\n\n### Step 2: Set up environment variables\n\n```sh\n# Copy the example environment file\ncp .env.example .env\n```\n\nNote: The default values in .env should work out of the box for local development.\n\n### Step 3: Start MongoDB using Docker Compose\n\n```sh\n# Start MongoDB and Mongo Express\n./api.sh mongo-up\n\n# You can access Mongo Express at http://localhost:8081\n```\n\nNote: Mongo Express is a web-based MongoDB admin interface that lets you view and modify your database.\n\n### Step 4: Install dependencies\n\n```sh\nbun install\n```\n\n### Step 5: Start the development server\n\n```sh\n# Start the API in the background\n./api.sh start\n\n# Check the logs\n./api.sh logs\n```\n\nThe server will start at http://localhost:3000 by default.\n\nNote: If you want to see the server logs in real-time, press `Ctrl+C` to exit the logs view while keeping the server running.\n\n## Using the Helper Script\n\nThis project includes a helper script (`api.sh`) to make it easier to manage the API and MongoDB:\n\n```sh\n# Show available commands\n./api.sh help\n\n# Start the API\n./api.sh start\n\n# Check if the API is running\n./api.sh status\n\n# View API logs\n./api.sh logs\n\n# Stop the API\n./api.sh stop\n\n# Start MongoDB containers\n./api.sh mongo-up\n\n# Stop MongoDB containers\n./api.sh mongo-down\n```\n\n## API Endpoints\n\n### Posts\n\n- `GET /posts` - Get all posts\n- `POST /posts` - Create a new post\n- `GET /posts/:id` - Get a post by ID\n- `PUT /posts/:id` - Update a post by ID\n- `DELETE /posts/:id` - Delete a post by ID\n\n### Authors\n\n- `GET /authors` - Get all authors\n- `POST /authors` - Create a new author\n- `GET /authors/:id` - Get an author by ID\n- `PUT /authors/:id` - Update an author by ID\n- `DELETE /authors/:id` - Delete an author by ID\n\n### Root\n\n- `GET /` - Returns a simple \"hello world\" message\n\n## Project Structure\n\n```\n.\n├── src/\n│   ├── _mongo/              # MongoDB connection and models\n│   │   ├── index.ts         # MongoDB connection setup\n│   │   ├── model.author.ts  # Author model\n│   │   └── model.post.ts    # Post model\n│   ├── authors/             # Author-related components\n│   │   ├── dto.ts           # Author Data Transfer Object\n│   │   ├── index.ts         # Author routes\n│   │   └── service.ts       # Author service functions\n│   ├── posts/               # Post-related components\n│   │   ├── dto.ts           # Post Data Transfer Object\n│   │   ├── index.ts         # Post routes\n│   │   └── service.ts       # Post service functions\n│   ├── index.ts             # Main application entry point\n│   └── type.ts              # Common types and interfaces\n├── .env.example             # Example environment variables\n├── api.sh                   # Helper script for managing the API\n├── docker-compose.yml       # Docker Compose configuration\n├── GUIDE.md                 # Comprehensive guide for running and testing\n├── package.json             # Project dependencies\n└── tsconfig.json            # TypeScript configuration\n```\n\n## Architecture Overview\n\n### Key Components\n\n1. **Entry Point (`src/index.ts`)**\n\n   - Sets up the Elysia server\n   - Registers middleware for logging and error handling\n   - Registers routes for authors and posts\n   - Connects to MongoDB\n\n2. **MongoDB Connection (`src/_mongo/index.ts`)**\n\n   - Establishes a connection to MongoDB\n   - Configures connection options based on environment variables\n\n3. **Models (`src/_mongo/model.author.ts` and `src/_mongo/model.post.ts`)**\n\n   - Define the schema for authors and posts\n   - Create indexes for optimized queries\n   - Establish relationships between models\n\n4. **Routes and Controllers**\n\n   - Each resource (authors, posts) has its own module with routes and service functions\n   - Routes define the API endpoints\n   - Service functions implement the business logic\n\n5. **Data Transfer Objects (DTOs)**\n   - Handle data transformations between the API and the database\n   - Ensure data consistency and validation\n\n## Development\n\n### Running in Development Mode\n\n```sh\nbun run dev\n```\n\nThis will start the server with hot reloading enabled, which means changes to your code will automatically restart the server.\n\n### Environment Variables\n\nThe following environment variables can be configured in the `.env` file:\n\n- `PORT`: The port on which the server will run (default: 3000)\n- `SERVER_NAME`: The name of the server (default: boilerplate-rest-api-bun)\n- `MONGODB_URI`: The MongoDB connection URI (default: mongodb://localhost:27017)\n- `MONGODB_DATABASE`: The MongoDB database name (default: boilerplate_db)\n- `NODE_ENV`: The environment mode (development, production, etc.)\n\n## MongoDB Management\n\nThis project includes [Mongo Express](https://github.com/mongo-express/mongo-express), a web-based MongoDB admin interface. When you start the Docker Compose setup, you can access Mongo Express at http://localhost:8081.\n\nYou can use Mongo Express to:\n\n- Browse and query collections\n- View, add, delete, and update documents\n- Create and delete indexes\n- Execute MongoDB commands\n\n## Troubleshooting\n\n### MongoDB Connection Issues\n\n- Ensure Docker is running\n- Check if the MongoDB container is running with `docker ps`\n- Verify that the MongoDB URI in your `.env` file is correct\n- Try restarting the MongoDB container with `./api.sh mongo-down` followed by `./api.sh mongo-up`\n\n### API Startup Issues\n\n- Check the logs with `./api.sh logs` or `cat app.log`\n- Verify Bun is installed correctly\n- Ensure all dependencies are installed with `bun install`\n- Verify that nothing else is running on the configured port (default: 3000)\n\n### Authentication Issues with MongoDB\n\n- If using authentication, ensure the username and password in `.env` match those in `docker-compose.yml`\n- If not using authentication, comment out or remove `MONGODB_USER` and `MONGODB_PASSWORD` from `.env`\n\n## Additional Documentation\n\nFor a comprehensive guide on running and testing the API, see the [GUIDE.md](GUIDE.md) file. This guide includes:\n\n- Detailed setup instructions\n- Example API calls with `curl`\n- Troubleshooting tips\n- Next steps for enhancing the project\n\n## License\n\nDistributed under the MIT License. See `LICENSE` for more information.\n","project_url":"https://awesome.ecosyste.ms/api/v1/projects/github.com%2Faashari%2Fboilerplate-bun-restapi","html_url":"https://awesome.ecosyste.ms/projects/github.com%2Faashari%2Fboilerplate-bun-restapi","lists_url":"https://awesome.ecosyste.ms/api/v1/projects/github.com%2Faashari%2Fboilerplate-bun-restapi/lists"}