Skip to content

Commit f63a20e

Browse files
authored
chore:add sample app using Hono-Prisma-Postgres (#2395) (#69)
* added hono-prisma-postgres sample project with auto-generated testcases and mocks from keploy Signed-off-by: Vibhor Phalke <vibgitcode27@gmail.com> * removed default index.ts file Signed-off-by: Vibhor Phalke <vibgitcode27@gmail.com> * added Dockefile Signed-off-by: Vibhor Phalke <vibgitcode27@gmail.com> * added docker-compose.yml Signed-off-by: Vibhor Phalke <vibgitcode27@gmail.com> * update README.md Signed-off-by: Vibhor Phalke <vibgitcode27@gmail.com> * remove ./keploy Signed-off-by: Vibhor Phalke <vibgitcode27@gmail.com> --------- Signed-off-by: Vibhor Phalke <vibgitcode27@gmail.com>
1 parent 033fe86 commit f63a20e

File tree

13 files changed

+593
-0
lines changed

13 files changed

+593
-0
lines changed

hono-prisma-postgres/.gitignore

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,3 @@
1+
# deps
2+
node_modules/
3+
.env

hono-prisma-postgres/Dockerfile

Lines changed: 20 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,20 @@
1+
# Start with a lightweight Linux base image with Node.js and Bun installed
2+
FROM oven/bun:latest
3+
4+
# Set the working directory inside the container
5+
WORKDIR /app
6+
7+
# Copy package.json, bun.lockb, and the Prisma schema to the container
8+
COPY package.json bun.lockb prisma ./
9+
10+
# Copy the rest of the application files
11+
COPY . .
12+
13+
# Install dependencies using Bun
14+
RUN bun install
15+
16+
# Expose the port your application will run on
17+
EXPOSE 3000
18+
19+
# Command to start the application
20+
CMD ["bun", "dev"]

hono-prisma-postgres/README.md

Lines changed: 193 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,193 @@
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

hono-prisma-postgres/bun.lockb

51.5 KB
Binary file not shown.
Lines changed: 35 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,35 @@
1+
version: '3.8'
2+
3+
services:
4+
app:
5+
build:
6+
context: .
7+
dockerfile: Dockerfile
8+
container_name: todo-app
9+
ports:
10+
- "3000:3000"
11+
environment:
12+
DATABASE_URL: "postgresql://postgres:postgres@db:5432/my_pgserver"
13+
depends_on:
14+
- db
15+
command: >
16+
sh -c "bunx prisma migrate dev --name init && bun dev"
17+
networks:
18+
- keploy-network
19+
20+
db:
21+
image: postgres:15
22+
container_name: postgres-db
23+
restart: always
24+
environment:
25+
POSTGRES_USER: postgres
26+
POSTGRES_PASSWORD: postgres
27+
POSTGRES_DB: my_pgserver
28+
ports:
29+
- "5432:5432"
30+
networks:
31+
- keploy-network
32+
33+
networks:
34+
keploy-network:
35+
external: true

hono-prisma-postgres/keploy.yml

Lines changed: 61 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,61 @@
1+
path: ""
2+
appId: 0
3+
appName: hono-prisma-postgres
4+
command: bun dev
5+
templatize:
6+
testSets: []
7+
port: 0
8+
dnsPort: 26789
9+
proxyPort: 16789
10+
debug: false
11+
disableTele: false
12+
disableANSI: false
13+
containerName: ""
14+
networkName: ""
15+
buildDelay: 30
16+
test:
17+
selectedTests: {}
18+
globalNoise:
19+
global: {}
20+
test-sets: {}
21+
delay: 5
22+
host: ""
23+
port: 0
24+
apiTimeout: 5
25+
skipCoverage: false
26+
coverageReportPath: ""
27+
ignoreOrdering: true
28+
mongoPassword: default@123
29+
language: ""
30+
removeUnusedMocks: false
31+
fallBackOnMiss: false
32+
jacocoAgentPath: ""
33+
basePath: ""
34+
mocking: true
35+
ignoredTests: {}
36+
disableLineCoverage: false
37+
disableMockUpload: true
38+
useLocalMock: false
39+
updateTemplate: false
40+
record:
41+
filters: []
42+
recordTimer: 0s
43+
configPath: ""
44+
bypassRules: []
45+
generateGithubActions: false
46+
keployContainer: keploy-v2
47+
keployNetwork: keploy-network
48+
cmdType: native
49+
contract:
50+
services: []
51+
tests: []
52+
path: ""
53+
download: false
54+
generate: false
55+
driven: consumer
56+
mappings:
57+
servicesMapping: {}
58+
self: ""
59+
inCi: false
60+
61+
# Visit [https://keploy.io/docs/running-keploy/configuration-file/] to learn about using keploy through configration file.

hono-prisma-postgres/package.json

Lines changed: 28 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,28 @@
1+
{
2+
"name": "ts-hono-postgres",
3+
"scripts": {
4+
"dev": "bun run --hot src/index.ts",
5+
"build": "bun build src/index.ts"
6+
},
7+
"dependencies": {
8+
"@prisma/client": "^6.1.0",
9+
"@types/bcrypt": "^5.0.2",
10+
"@types/jsonwebtoken": "^9.0.7",
11+
"@types/pg": "^8.11.10",
12+
"bcrypt": "^5.1.1",
13+
"hono": "^4.6.14",
14+
"jsonwebtoken": "^9.0.2",
15+
"pg": "^8.13.1"
16+
},
17+
"devDependencies": {
18+
"@types/bun": "latest",
19+
"@types/node": "^22.10.2",
20+
"prisma": "^6.1.0",
21+
"tsx": "^4.19.2"
22+
},
23+
"module": "index.ts",
24+
"type": "module",
25+
"peerDependencies": {
26+
"typescript": "^5.7.2"
27+
}
28+
}
Lines changed: 28 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,28 @@
1+
-- CreateTable
2+
CREATE TABLE "User" (
3+
"id" TEXT NOT NULL,
4+
"email" TEXT NOT NULL,
5+
"password" TEXT NOT NULL,
6+
"createdAt" TIMESTAMP(3) NOT NULL DEFAULT CURRENT_TIMESTAMP,
7+
"updatedAt" TIMESTAMP(3) NOT NULL,
8+
9+
CONSTRAINT "User_pkey" PRIMARY KEY ("id")
10+
);
11+
12+
-- CreateTable
13+
CREATE TABLE "Todo" (
14+
"id" TEXT NOT NULL,
15+
"title" TEXT NOT NULL,
16+
"completed" BOOLEAN NOT NULL DEFAULT false,
17+
"userId" TEXT NOT NULL,
18+
"createdAt" TIMESTAMP(3) NOT NULL DEFAULT CURRENT_TIMESTAMP,
19+
"updatedAt" TIMESTAMP(3) NOT NULL,
20+
21+
CONSTRAINT "Todo_pkey" PRIMARY KEY ("id")
22+
);
23+
24+
-- CreateIndex
25+
CREATE UNIQUE INDEX "User_email_key" ON "User"("email");
26+
27+
-- AddForeignKey
28+
ALTER TABLE "Todo" ADD CONSTRAINT "Todo_userId_fkey" FOREIGN KEY ("userId") REFERENCES "User"("id") ON DELETE RESTRICT ON UPDATE CASCADE;
Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,3 @@
1+
# Please do not edit this file manually
2+
# It should be added in your version-control system (e.g., Git)
3+
provider = "postgresql"

0 commit comments

Comments
 (0)