URL Shortener Backend
The backend service for the URL Shortener application, built with Bun, Prisma, and SQLite.
Tech Stack
- Runtime: Bun - A fast all-in-one JavaScript runtime.
- Language: TypeScript
- ORM: Prisma - Next-generation Node.js and TypeScript ORM.
- Database: SQLite (via Prisma)
- Validation: Zod - TypeScript-first schema declaration and validation.
- Authentication: Jose - JSON Web Almost Everything (JWT, JWS, JWE, JWK, JWA).
- ID Generation: Nanoid - A tiny, secure, URL-friendly, unique string ID generator.
- Tooling: Biome - Fast formatter and linter.
Architecture
The backend follows a layered architecture to separate concerns and ensure maintainability:
src/
├── config/ # Configuration files
├── controllers/ # Request handlers (input validation, response formatting)
├── db/ # Database connection initialization
├── generated/ # Generated Prisma client
├── lib/ # Shared utilities and helpers
├── routes/ # API route definitions
├── schemas/ # Zod schemas for validation
├── services/ # Business logic
└── tests/ # Test files
Request Flow
- Routes: Define the API endpoints and map them to controllers.
- Controllers: Validate the request input using Zod schemas and call the appropriate service.
- Services: Execute the business logic (e.g., creating a short URL, hashing passwords) and interact with the database via Prisma.
- Database: SQLite stores the data.
Getting Started
Prerequisites
- Bun (latest version)
Installation
Install the dependencies:
bun install
Environment Setup
Ensure you have a .env file in the project root (or backend root if running standalone) with the necessary variables (see root README.md).
Database Setup
Initialize the database and generate the Prisma client:
# Run migrations
bun run db:migrate
# Generate Prisma client
bun run db:generate
Running the Server
Start the development server with hot reloading:
bun run dev
The server will start on http://0.0.0.0:3000 (default).
To run in production mode:
bun start
Scripts
bun run dev: Start development server.bun start: Start production server.bun run format: Format code using Biome.bun run lint: Lint code using Biome.bun run check: Check code for formatting and linting errors.bun run db:studio: Open Prisma Studio to view/edit data.bun run db:generate: Generate Prisma client.bun run db:migrate: Run database migrations.bun run db:deploy: Deploy migrations (for production).
API Documentation
The API specification is available in openapi.json.