|
| 1 | +# Prisma DB Integration Setup |
| 2 | + |
| 3 | +This project now includes a complete Backend-for-Frontend (BFF) layer with Prisma ORM integration following clean architecture and SOLID principles. |
| 4 | + |
| 5 | +## 🏗️ Architecture Overview |
| 6 | + |
| 7 | +``` |
| 8 | +src/bff/ |
| 9 | +├── db/ |
| 10 | +│ ├── prisma/ |
| 11 | +│ │ └── schema.prisma # Database schema with Users & Roles |
| 12 | +│ ├── client.ts # Prisma client singleton |
| 13 | +│ └── index.ts # DB bootstrap functions |
| 14 | +├── types/ |
| 15 | +│ └── index.ts # TypeScript interfaces |
| 16 | +├── repositories/ |
| 17 | +│ ├── user.repository.ts # User data access layer |
| 18 | +│ ├── role.repository.ts # Role data access layer |
| 19 | +│ └── index.ts # Repository exports |
| 20 | +├── services/ |
| 21 | +│ ├── user.service.ts # User business logic layer |
| 22 | +│ └── index.ts # Service exports |
| 23 | +└── index.ts # Main BFF exports |
| 24 | +``` |
| 25 | + |
| 26 | +## 🚀 Quick Start |
| 27 | + |
| 28 | +### 1. Database Setup |
| 29 | + |
| 30 | +1. Copy the environment template: |
| 31 | + ```bash |
| 32 | + cp .env.example .env |
| 33 | + ``` |
| 34 | + |
| 35 | +2. Update your `.env` file with your MySQL database URL: |
| 36 | + ``` |
| 37 | + DATABASE_URL="mysql://username:password@localhost:3306/database_name" |
| 38 | + ``` |
| 39 | + |
| 40 | +### 2. Generate Prisma Client |
| 41 | + |
| 42 | +```bash |
| 43 | +npm run db:generate |
| 44 | +``` |
| 45 | + |
| 46 | +### 3. Push Schema to Database |
| 47 | + |
| 48 | +```bash |
| 49 | +npm run db:push |
| 50 | +``` |
| 51 | + |
| 52 | +### 4. Run the Development Server |
| 53 | + |
| 54 | +```bash |
| 55 | +npm run dev |
| 56 | +``` |
| 57 | + |
| 58 | +## 📊 Database Schema |
| 59 | + |
| 60 | +### Users Table |
| 61 | +- `id` (number, primary key) |
| 62 | +- `email` (string, unique) |
| 63 | +- `name` (string) |
| 64 | +- `password` (string) |
| 65 | +- `phone` (string, optional) |
| 66 | +- `picture` (string, optional) |
| 67 | +- `role_id` (number, foreign key) |
| 68 | +- `status` (enum: signup, active, disabled) |
| 69 | +- `created_at` (datetime) |
| 70 | + |
| 71 | +### Roles Table |
| 72 | +- `id` (number, primary key) |
| 73 | +- `name` (string) |
| 74 | +- `description` (string) |
| 75 | + |
| 76 | +### Relationships |
| 77 | +- User `belongsTo` Role (via `role_id`) |
| 78 | +- Role `hasMany` Users |
| 79 | + |
| 80 | +## 🔌 API Endpoints |
| 81 | + |
| 82 | +### GET /api/v1/users |
| 83 | +- Get all users or filter by criteria |
| 84 | +- Query params: `id`, `status`, `role_id` |
| 85 | + |
| 86 | +### POST /api/v1/users |
| 87 | +- Create new user |
| 88 | +- Required: `email`, `name`, `password`, `role_id` |
| 89 | + |
| 90 | +### PUT /api/v1/users |
| 91 | +- Update existing user |
| 92 | +- Required: `id` in request body |
| 93 | + |
| 94 | +See [API.md](./API.md) for detailed endpoint documentation. |
| 95 | + |
| 96 | +## 🛠️ Available Scripts |
| 97 | + |
| 98 | +```bash |
| 99 | +# Generate Prisma client |
| 100 | +npm run db:generate |
| 101 | + |
| 102 | +# Push schema changes to database |
| 103 | +npm run db:push |
| 104 | + |
| 105 | +# Open Prisma Studio (database GUI) |
| 106 | +npm run db:studio |
| 107 | + |
| 108 | +# Build project |
| 109 | +npm run build |
| 110 | + |
| 111 | +# Start development server |
| 112 | +npm run dev |
| 113 | +``` |
| 114 | + |
| 115 | +## 🏛️ Architecture Principles |
| 116 | + |
| 117 | +### Clean Architecture |
| 118 | +- **Repository Layer**: Handles data access and database queries |
| 119 | +- **Service Layer**: Contains business logic and validation |
| 120 | +- **API Layer**: HTTP request/response handling |
| 121 | + |
| 122 | +### SOLID Principles Applied |
| 123 | +- **Single Responsibility**: Each class has one reason to change |
| 124 | +- **Open/Closed**: Easily extendable without modification |
| 125 | +- **Liskov Substitution**: Repository interfaces can be swapped |
| 126 | +- **Interface Segregation**: Focused, specific interfaces |
| 127 | +- **Dependency Inversion**: Services depend on abstractions |
| 128 | + |
| 129 | +### Key Features |
| 130 | +- ✅ Strongly typed Prisma client |
| 131 | +- ✅ Testable repository pattern |
| 132 | +- ✅ Business logic separation |
| 133 | +- ✅ Role relationships populated |
| 134 | +- ✅ Input validation |
| 135 | +- ✅ Error handling |
| 136 | +- ✅ Type safety throughout |
| 137 | + |
| 138 | +## 🔧 Customization |
| 139 | + |
| 140 | +### Adding New Entities |
| 141 | +1. Update `schema.prisma` with new model |
| 142 | +2. Run `npm run db:generate` and `npm run db:push` |
| 143 | +3. Create repository in `src/bff/repositories/` |
| 144 | +4. Create service in `src/bff/services/` |
| 145 | +5. Add API routes in `src/pages/api/v1/` |
| 146 | + |
| 147 | +### Authentication (TODO) |
| 148 | +In production, consider adding: |
| 149 | +- JWT authentication |
| 150 | +- Password hashing with bcrypt |
| 151 | +- Role-based authorization |
| 152 | +- Rate limiting |
| 153 | +- Input sanitization |
| 154 | + |
| 155 | +## 🚨 Important Notes |
| 156 | + |
| 157 | +- **Development Only**: Current password handling is not secure |
| 158 | +- **Database Required**: You need a MySQL database to run the API endpoints |
| 159 | +- **SSR Mode**: Project is configured for server-side rendering |
| 160 | +- **Node.js Adapter**: Using @astrojs/node for deployment |
0 commit comments