https://github.com/awebcode/next-express-rest-api
https://github.com/awebcode/next-express-rest-api
Last synced: 10 months ago
JSON representation
- Host: GitHub
- URL: https://github.com/awebcode/next-express-rest-api
- Owner: awebcode
- Created: 2024-11-27T14:01:54.000Z (about 1 year ago)
- Default Branch: master
- Last Pushed: 2024-11-28T00:54:06.000Z (about 1 year ago)
- Last Synced: 2025-02-07T06:15:02.445Z (12 months ago)
- Language: TypeScript
- Size: 289 KB
- Stars: 0
- Watchers: 1
- Forks: 0
- Open Issues: 0
-
Metadata Files:
- Readme: README.md
Awesome Lists containing this project
README
# Server: Backend API with Prisma
This directory contains the server-side implementation of the application. It uses **Prisma** as the ORM to interact with the database, providing a type-safe and efficient way to handle database operations.
## Features
- **Prisma ORM**: Type-safe database queries with auto-generated types.
- **CRUD Operations**: Fully implemented CRUD functionality for managing products.
- **REST API**: Endpoints to create, read, update, and delete resources.
- **MONGODB Support**: Configured for a MONGODB database but easily adaptable to other relational databases.
- **Environment Variables**: Centralized configuration for secure and flexible setup.
## Prerequisites
Before starting, ensure you have the following installed:
- **BUN** (v16 or later)
- **MONGODB** (or your preferred relational database)
- **Prisma CLI** (installed as a development dependency)
## Getting Started
Follow these steps to set up and run the server:
### 1. Install Dependencies
Run the following command in the `/server` directory to install all required dependencies:
```bash
bun install
npx prisma init
npx prisma generate
npx prisma migrate dev --name init
/server
├── prisma/
│ ├── schema.prisma # Prisma schema defining the database models
├── src/
│ ├── routes/ # API route handlers
│ ├── controllers/ # Business logic
│ ├── middlewares/ # Middleware for validation, error handling, etc.
│ ├── index.ts # Server entry point
├── .env # Environment variables
├── package.json # Node.js dependencies and scripts
API Endpoints
1. Create Product
POST /products
Request Body:
json
{
"name": "Product Name",
"description": "Product Description",
"price": 100
}
Response:
json
{
"product": {
"id": "1",
"name": "Product Name",
"description": "Product Description",
"price": 100
}
}
2. Get All Products
GET /products
Response:
json
{
"products": [
{
"id": "1",
"name": "Product Name",
"description": "Product Description",
"price": 100
}
]
}
3. Update Product
PUT /products/:id
Request Body:
json
{
"name": "Updated Name",
"description": "Updated Description",
"price": 200
}
Response:
json
{
"product": {
"id": "1",
"name": "Updated Name",
"description": "Updated Description",
"price": 200
}
}
4. Delete Product
DELETE /products/:id
Response:
json
{
"message": "Product deleted successfully"
}
Project Structure
/server
├── prisma/
│ ├── schema.prisma # Prisma schema defining the database models
├── src/
│ ├── routes/ # API route handlers
│ ├── controllers/ # Business logic
│ ├── middlewares/ # Middleware for validation, error handling, etc.
│ ├── index.ts # Server entry point
├── .env # Environment variables
├── package.json # Node.js dependencies and scripts
Technologies Used
Prisma: ORM for database interaction
Express.js: Minimalist web framework
PostgreSQL: Database management system
TypeScript: Type-safe programming
Additional Commands
Format Prisma Schema:
npx prisma format
View Database in Prisma Studio:
npx prisma studio
Contributing
Feel free to contribute to this project by submitting issues or pull requests.
License
This project is licensed under the MIT License.
css