|
| 1 | +# Hono - Prisma - Postgres Sample Application |
| 2 | + |
| 3 | +This is a simple Todo application built using **Hono**, **Prisma**, **PostgreSQL**, **JWT**, and **TypeScript**. The project also integrates with **Keploy** for recording and running test cases. The package manager used is **Bun**. |
| 4 | + |
| 5 | +## Features |
| 6 | + |
| 7 | +- User authentication with JWT |
| 8 | +- CRUD operations for Todo items |
| 9 | +- API testing with Keploy |
| 10 | +- Docker support for containerized deployment |
| 11 | + |
| 12 | +## Tech Stack |
| 13 | + |
| 14 | +- **Framework**: Hono |
| 15 | +- **Database**: PostgreSQL |
| 16 | +- **ORM**: Prisma |
| 17 | +- **Authentication**: JWT |
| 18 | +- **Language**: TypeScript |
| 19 | +- **Package Manager**: Bun |
| 20 | +- **Containerization**: Docker |
| 21 | + |
| 22 | +## API Endpoints |
| 23 | + |
| 24 | +### Auth Routes |
| 25 | + |
| 26 | +- GET / - Test response |
| 27 | +- POST /register - Register a new user |
| 28 | +- POST /login - Log in a user |
| 29 | + |
| 30 | +### Protected Todo Routes (require authentication) |
| 31 | + |
| 32 | +- POST /todos - Create a new todo |
| 33 | +- GET /todos - Retrieve all todos |
| 34 | +- PUT /todos - Update a todo |
| 35 | +- DELETE /todos - Delete a todo |
| 36 | + |
| 37 | +## Setup Instructions |
| 38 | + |
| 39 | +There are two ways to run this application: |
| 40 | + |
| 41 | +### Method 1: Local Development |
| 42 | + |
| 43 | +#### Prerequisites |
| 44 | + |
| 45 | +- Node.js (with Bun installed) |
| 46 | +- PostgreSQL installed and running |
| 47 | +- Prisma CLI installed globally (npm install -g prisma) |
| 48 | +- Keploy installed (Follow [documentation](https://keploy.io/docs/server/installation/) for installation) |
| 49 | + |
| 50 | +#### Steps to Run |
| 51 | + |
| 52 | +1. **Clone the Repository** |
| 53 | + |
| 54 | + ```bash |
| 55 | + git clone <repository-url> |
| 56 | + cd <repository-folder> |
| 57 | + ``` |
| 58 | + |
| 59 | +2. **Install Dependencies** |
| 60 | + |
| 61 | + ```bash |
| 62 | + bun install |
| 63 | + ``` |
| 64 | + |
| 65 | +3. **Setup .env** |
| 66 | + Create a .env file and add your Postgres database url: |
| 67 | + |
| 68 | + ```bash |
| 69 | + DATABASE_URL="postgresql://postgres:password@localhost:5432/my_pgserver?schema=public" |
| 70 | + ``` |
| 71 | + |
| 72 | +4. **Setup Database** |
| 73 | + |
| 74 | + ```bash |
| 75 | + npx prisma migrate dev --name init |
| 76 | + ``` |
| 77 | + |
| 78 | +5. **Run the Application** |
| 79 | + ```bash |
| 80 | + bun dev |
| 81 | + ``` |
| 82 | + |
| 83 | +### Method 2: Docker Deployment |
| 84 | + |
| 85 | +#### Prerequisites |
| 86 | + |
| 87 | +- Docker and Docker Compose installed |
| 88 | +- Keploy installed |
| 89 | + |
| 90 | +#### Steps to Run |
| 91 | + |
| 92 | +1. **Create Docker Network** |
| 93 | + |
| 94 | + ```bash |
| 95 | + docker network create keploy-network |
| 96 | + ``` |
| 97 | + |
| 98 | + > **Note:** While this network should be created automatically during installation, this command ensures it exists. |
| 99 | +
|
| 100 | + ``` |
| 101 | +
|
| 102 | + ``` |
| 103 | + |
| 104 | +2. **Start the Application** |
| 105 | + ```bash |
| 106 | + docker-compose up --build |
| 107 | + ``` |
| 108 | + |
| 109 | +## Keploy Integration |
| 110 | + |
| 111 | +Keploy allows you to record and test API requests and responses. There are two methods to use Keploy with this project: |
| 112 | + |
| 113 | +### Method 1: Local Testing |
| 114 | + |
| 115 | +1. **Recording Test Cases** |
| 116 | + |
| 117 | + ```bash |
| 118 | + keploy record -c "bun dev" |
| 119 | + ``` |
| 120 | + |
| 121 | +2. **Running Test Cases** |
| 122 | + ```bash |
| 123 | + keploy test -c "bun dev" --delay 10 |
| 124 | + ``` |
| 125 | + |
| 126 | +### Method 2: Docker-based Testing |
| 127 | + |
| 128 | +1. **Recording Test Cases** |
| 129 | + |
| 130 | + ```bash |
| 131 | + keploy record -c "docker compose up" --container-name "hono-prisma-postgres" -n "keploy-network" |
| 132 | + ``` |
| 133 | + |
| 134 | +2. **Running Test Cases** |
| 135 | + ```bash |
| 136 | + keploy test -c "docker compose up" --container-name "hono-prisma-postgres" -n "keploy-network" |
| 137 | + ``` |
| 138 | + |
| 139 | +## Notes |
| 140 | + |
| 141 | +- Ensure your database is properly configured in the prisma.schema file and the environment variables are set. |
| 142 | +- Use the authMiddleware to protect routes requiring user authentication. |
| 143 | +- For CORS support, the application includes: |
| 144 | + ```typescript |
| 145 | + app.use("/*", cors()); |
| 146 | + ``` |
| 147 | + |
| 148 | +## Common Issues |
| 149 | + |
| 150 | +### PrismaClientInitializationError with Keploy Recording |
| 151 | + |
| 152 | +When running Keploy record command with database interactions, you might encounter a PrismaClientInitializationError. This often occurs due to SSL connection issues with PostgreSQL. |
| 153 | + |
| 154 | +#### Symptoms |
| 155 | + |
| 156 | +When executing the Keploy record command, you might encounter database connectivity issues, particularly when making API calls that interact with the database. The PostgreSQL logs typically show SSL-related errors like: |
| 157 | + |
| 158 | +``` |
| 159 | +2024-12-25 13:42:23.035 IST [123887] [unknown]@[unknown] LOG: could not accept SSL connection: EOF detected |
| 160 | +2024-12-25 14:41:45.859 IST [172605] [unknown]@[unknown] LOG: could not accept SSL connection: EOF detected |
| 161 | +``` |
| 162 | + |
| 163 | +#### Resolution (Ubuntu/Linux) |
| 164 | + |
| 165 | +1. **Access PostgreSQL Configuration** |
| 166 | + |
| 167 | + ```bash |
| 168 | + sudo nano /etc/postgresql/12/main/postgresql.conf |
| 169 | + ``` |
| 170 | + |
| 171 | +2. **Modify SSL Settings** |
| 172 | + |
| 173 | + - Locate the SSL configuration line |
| 174 | + - Change ssl = on to ssl = off |
| 175 | + |
| 176 | +3. **Restart PostgreSQL** |
| 177 | + ```bash |
| 178 | + sudo service postgresql restart |
| 179 | + ``` |
| 180 | + |
| 181 | +#### Important Security Note |
| 182 | + |
| 183 | +⚠️ Disabling SSL should only be done in development environments. For production deployments: |
| 184 | + |
| 185 | +- Keep SSL enabled |
| 186 | +- Properly configure SSL certificates |
| 187 | +- Follow security best practices for database connections |
| 188 | + |
| 189 | +#### Additional Considerations |
| 190 | + |
| 191 | +- Make sure your database connection string in .env is properly configured |
| 192 | +- If you're using a different PostgreSQL version, the configuration file path might vary |
| 193 | +- Always backup your configuration files before making changes |
0 commit comments