An open API service indexing awesome lists of open source software.

https://github.com/ibnabdullah1/fastify-prisma-starter

Production-ready Node.js Fastify Prisma starter for REST API with authentication, Docker, and TypeScript
https://github.com/ibnabdullah1/fastify-prisma-starter

authentication backend clean-architecture docker fastify fastify-prisma fastify-starter jwt-auth nodejs nodejs-api prisma prisma-starter rest-api scaffold starter starter-template typescript

Last synced: 9 days ago
JSON representation

Production-ready Node.js Fastify Prisma starter for REST API with authentication, Docker, and TypeScript

Awesome Lists containing this project

README

          

# Node.js Fastify Prisma Starter

**Production-ready Node.js Fastify Prisma starter** for building scalable REST APIs with TypeScript, PostgreSQL, and modern development practices. A clean architecture Fastify Prisma boilerplate with JWT authentication, Docker support, and comprehensive documentation.

## Why this Fastify Prisma starter?

This **Node.js Fastify Prisma starter template** provides everything you need to build production-ready REST APIs quickly. Built with Fastify (high-performance web framework), Prisma ORM (type-safe database access), and TypeScript for a robust development experience.

## ๐Ÿš€ Features

This **Fastify Prisma TypeScript starter** includes:

- **๐Ÿ”ง Tech Stack**: Node.js, Fastify, TypeScript, PostgreSQL, Prisma ORM
- **๐Ÿ” Authentication**: JWT-based authentication with refresh tokens (Fastify Prisma authentication starter)
- **๐Ÿ‘ฅ User Management**: Complete user CRUD with role-based access control
- **๐Ÿ“ File Upload**: Cloudinary integration for media management
- **๐Ÿ“ง Email Service**: Nodemailer integration for email functionality
- **๐Ÿ›ก๏ธ Security**: Helmet, CORS, rate limiting, input validation
- **๐Ÿ“Š Database**: PostgreSQL with Prisma ORM for type-safe operations
- **๐Ÿงช Testing**: Jest setup with coverage reporting
- **๐Ÿ“ Code Quality**: ESLint, Prettier, Husky for code standards
- **๐Ÿณ Docker**: Dockerized Fastify Prisma starter with docker-compose
- **๐Ÿ“š Documentation**: Comprehensive documentation and API reference
- **๐Ÿ”„ CI/CD Ready**: GitHub Actions workflow templates
- **โšก Module Generation**: Automated module creation and renaming scripts

## ๐Ÿ“‹ Prerequisites

- **Node.js** (v16 or higher)
- **npm** or **yarn** package manager
- **PostgreSQL** database
- **Git**

## โšก Getting Started with Node.js Fastify Prisma Boilerplate

### 1. Clone the Repository

```bash
git clone https://github.com/ibnabdullah1/nodejs-fastify-prisma-starter.git
cd nodejs-fastify-prisma-starter
```

### 2. Install Dependencies

```bash
npm install
```

### 3. Environment Setup

```bash
# Copy environment template
cp env.example .env

# Edit .env with your configuration
nano .env
```

### 4. Database Setup

```bash
# Generate Prisma client
npm run db:generate

# Run database migrations
npm run migrate

# (Optional) Seed the database
npm run db:seed
```

### 5. Start Development Server

```bash
npm run dev
```

Your server will be running at `http://localhost:5000`

## ๐Ÿ› ๏ธ Available Scripts

### Development
- `npm run dev` - Start development server with hot reload
- `npm run build` - Build the project for production
- `npm run start` - Start production server

### Database
- `npm run migrate` - Run database migrations
- `npm run migrate:deploy` - Deploy migrations to production
- `npm run migrate:reset` - Reset database
- `npm run db:studio` - Open Prisma Studio
- `npm run db:generate` - Generate Prisma client
- `npm run db:seed` - Seed database with sample data

### Code Quality
- `npm run lint` - Run ESLint
- `npm run lint:fix` - Fix ESLint errors
- `npm run format` - Format code with Prettier
- `npm run type-check` - Run TypeScript type checking

### Module Scripts
- `npm run create-module ` - Create a new module with complete structure
- `npm run rename-module ` - Rename an existing module and update all references

### Testing
- `npm test` - Run tests
- `npm run test:watch` - Run tests in watch mode
- `npm run test:coverage` - Run tests with coverage

### Docker
- `npm run docker:build` - Build Docker image
- `npm run docker:run` - Run Docker container
- `npm run docker:compose` - Start with docker-compose

## ๐Ÿ“ Project Structure

```
src/
โ”œโ”€โ”€ controllers/ # Request handlers
โ”œโ”€โ”€ services/ # Business logic
โ”œโ”€โ”€ models/ # Data models
โ”œโ”€โ”€ middleware/ # fastify middleware
โ”œโ”€โ”€ utils/ # Utility functions
โ”œโ”€โ”€ types/ # TypeScript type definitions
โ”œโ”€โ”€ constants/ # Application constants
โ”œโ”€โ”€ validators/ # Request validation schemas
โ”œโ”€โ”€ database/ # Database configuration
โ”œโ”€โ”€ routes/ # Route definitions
โ”œโ”€โ”€ config/ # Configuration files
โ”œโ”€โ”€ scripts/ # Utility scripts
โ”œโ”€โ”€ templates/ # Email templates
โ”œโ”€โ”€ tests/ # Test files
โ”œโ”€โ”€ app.ts # fastify app configuration
โ””โ”€โ”€ server.ts # Server entry point
```

## ๐Ÿ”ง Configuration

### Environment Variables

See `env.example` for all available environment variables:

- **Database**: PostgreSQL connection string
- **JWT**: Authentication secrets and expiration times
- **Email**: SMTP configuration for email sending
- **Cloudinary**: File upload and storage
- **Security**: CORS, rate limiting, and other security settings

### Database Schema

The template includes the following models:

- **User**: User authentication and profile management
- **Media**: File upload and metadata storage

## ๐Ÿ› ๏ธ Module Generation

This **Fastify Prisma starter template for Node.js** includes powerful scripts to quickly generate and manage modules with clean architecture patterns.

### Create a New Module

Generate a complete module structure with all necessary files:

```bash
npm run create-module

# Examples:
npm run create-module product
npm run create-module order
npm run create-module category
```

**Generated Files:**
- `module.routes.ts` - Fastify plugin routes with authentication
- `module.controller.ts` - Request handlers (FastifyRequest/FastifyReply)
- `module.service.ts` - Business logic with Prisma
- `module.interface.ts` - TypeScript interfaces
- `module.validation.ts` - Zod validation schemas
- `module.constant.ts` - Module constants

**After creating a module:**

1. **Register routes** in `src/app/routes/index.ts`:
```typescript
import { ProductRoutes } from "../modules/product/product.routes";

const router = async (fastify: FastifyInstance) => {
fastify.register(ProductRoutes, { prefix: "/product" });
};
```

2. **Update Prisma schema** (`prisma/schema.prisma`):
```prisma
model Product {
id Int @id @default(autoincrement())
name String
createdAt DateTime @default(now())
updatedAt DateTime @updatedAt
}
```

3. **Run migrations**:
```bash
npm run migrate
```

### Rename a Module

Rename an existing module and update all references:

```bash
npm run rename-module

# Examples:
npm run rename-module product item
npm run rename-module order purchase
```

**What it does:**
- Renames the module directory
- Updates all file contents (lowercase and capitalized names)
- Renames files if they contain the module name
- Updates all references throughout the codebase

**Important:** After renaming, manually update:
- Route registrations in `src/app/routes/index.ts`
- Prisma schema if needed
- Any other external references

## ๐Ÿš€ Deployment

### Docker Deployment

```bash
# Build and start with docker-compose
npm run docker:compose

# Or build and run manually
npm run docker:build
npm run docker:run
```

### Manual Deployment

1. **Build the application**:
```bash
npm run build
```

2. **Set up production environment**:
```bash
cp env.example .env
# Configure production environment variables
```

3. **Run database migrations**:
```bash
npm run migrate:deploy
```

4. **Start the application**:
```bash
npm start
```

## ๐Ÿ“š Documentation

Comprehensive documentation is available in the `DOCS/` directory:

- **[Quick Start Guide](./DOCS/QUICK_START.md)** - Get started in 5 minutes
- **[API Documentation](./DOCS/API_DOCUMENTATION.md)** - Complete API reference
- **[Project Structure](./DOCS/PROJECT_STRUCTURE.md)** - Detailed project organization
- **[Deployment Guide](./DOCS/DEPLOYMENT_GUIDE.md)** - Production deployment
- **[Troubleshooting](./DOCS/TROUBLESHOOTING.md)** - Common issues and solutions

## ๐Ÿงช Testing

```bash
# Run all tests
npm test

# Run tests in watch mode
npm run test:watch

# Run tests with coverage
npm run test:coverage
```

## ๐Ÿ” Code Quality

The project includes:

- **ESLint** for code linting
- **Prettier** for code formatting
- **Husky** for git hooks
- **lint-staged** for pre-commit checks

## ๐Ÿค Contributing

1. Fork the repository
2. Create a feature branch
3. Make your changes
4. Add tests if applicable
5. Submit a pull request

See [Contributing Guide](./DOCS/CONTRIBUTING.md) for detailed information.

## ๐Ÿ“„ License

This project is licensed under the MIT License - see the [LICENSE](LICENSE) file for details.

## ๐Ÿ†˜ Support

- **Documentation**: Check the `DOCS/` directory
- **Issues**: Create an issue on GitHub
- **Discussions**: Use GitHub Discussions for questions

## ๐Ÿ™ Acknowledgments

- [Fastify](https://fastify.dev/) - Web framework
- [Prisma](https://www.prisma.io/) - Database ORM
- [TypeScript](https://www.typescriptlang.org/) - Type safety
- [PostgreSQL](https://www.postgresql.org/) - Database

---

**Happy Coding! ๐ŸŽ‰**