Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Add Hono-Prisma-Postgres Todo Application Example (#2395) #69

Open
wants to merge 6 commits into
base: main
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from 5 commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
3 changes: 3 additions & 0 deletions hono-prisma-postgres/.gitignore
Original file line number Diff line number Diff line change
@@ -0,0 +1,3 @@
# deps
node_modules/
.env
20 changes: 20 additions & 0 deletions hono-prisma-postgres/Dockerfile
Original file line number Diff line number Diff line change
@@ -0,0 +1,20 @@
# Start with a lightweight Linux base image with Node.js and Bun installed
FROM oven/bun:latest

# Set the working directory inside the container
WORKDIR /app

# Copy package.json, bun.lockb, and the Prisma schema to the container
COPY package.json bun.lockb prisma ./

# Copy the rest of the application files
COPY . .

# Install dependencies using Bun
RUN bun install

# Expose the port your application will run on
EXPOSE 3000

# Command to start the application
CMD ["bun", "dev"]
193 changes: 193 additions & 0 deletions hono-prisma-postgres/README.md
Vibgitcode27 marked this conversation as resolved.
Show resolved Hide resolved
Original file line number Diff line number Diff line change
@@ -0,0 +1,193 @@
# Hono - Prisma - Postgres Sample Application

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**.

## Features

- User authentication with JWT
- CRUD operations for Todo items
- API testing with Keploy
- Docker support for containerized deployment

## Tech Stack

- **Framework**: Hono
- **Database**: PostgreSQL
- **ORM**: Prisma
- **Authentication**: JWT
- **Language**: TypeScript
- **Package Manager**: Bun
- **Containerization**: Docker

## API Endpoints

### Auth Routes

- GET / - Test response
- POST /register - Register a new user
- POST /login - Log in a user

### Protected Todo Routes (require authentication)

- POST /todos - Create a new todo
- GET /todos - Retrieve all todos
- PUT /todos - Update a todo
- DELETE /todos - Delete a todo

## Setup Instructions

There are two ways to run this application:

### Method 1: Local Development

#### Prerequisites

- Node.js (with Bun installed)
- PostgreSQL installed and running
- Prisma CLI installed globally (npm install -g prisma)
- Keploy installed (Follow [documentation](https://keploy.io/docs/server/installation/) for installation)

#### Steps to Run

1. **Clone the Repository**

```bash
git clone <repository-url>
cd <repository-folder>
```

2. **Install Dependencies**

```bash
bun install
```

3. **Setup .env**
Create a .env file and add your Postgres database url:

```bash
DATABASE_URL="postgresql://postgres:password@localhost:5432/my_pgserver?schema=public"
```

4. **Setup Database**

```bash
npx prisma migrate dev --name init
```

5. **Run the Application**
```bash
bun dev
```

### Method 2: Docker Deployment

#### Prerequisites

- Docker and Docker Compose installed
- Keploy installed

#### Steps to Run

1. **Create Docker Network**

```bash
docker network create keploy-network
```

> **Note:** While this network should be created automatically during installation, this command ensures it exists.

```

```

2. **Start the Application**
```bash
docker-compose up --build
```

## Keploy Integration

Keploy allows you to record and test API requests and responses. There are two methods to use Keploy with this project:

### Method 1: Local Testing

1. **Recording Test Cases**

```bash
keploy record -c "bun dev"
```

2. **Running Test Cases**
```bash
keploy test -c "bun dev" --delay 10
```

### Method 2: Docker-based Testing

1. **Recording Test Cases**

```bash
keploy record -c "docker compose up" --container-name "hono-prisma-postgres" -n "keploy-network"
```

2. **Running Test Cases**
```bash
keploy test -c "docker compose up" --container-name "hono-prisma-postgres" -n "keploy-network"
```

## Notes

- Ensure your database is properly configured in the prisma.schema file and the environment variables are set.
- Use the authMiddleware to protect routes requiring user authentication.
- For CORS support, the application includes:
```typescript
app.use("/*", cors());
```

## Common Issues

### PrismaClientInitializationError with Keploy Recording

When running Keploy record command with database interactions, you might encounter a PrismaClientInitializationError. This often occurs due to SSL connection issues with PostgreSQL.

#### Symptoms

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:

```
2024-12-25 13:42:23.035 IST [123887] [unknown]@[unknown] LOG: could not accept SSL connection: EOF detected
2024-12-25 14:41:45.859 IST [172605] [unknown]@[unknown] LOG: could not accept SSL connection: EOF detected
```

#### Resolution (Ubuntu/Linux)

1. **Access PostgreSQL Configuration**

```bash
sudo nano /etc/postgresql/12/main/postgresql.conf
```

2. **Modify SSL Settings**

- Locate the SSL configuration line
- Change ssl = on to ssl = off

3. **Restart PostgreSQL**
```bash
sudo service postgresql restart
```

#### Important Security Note

⚠️ Disabling SSL should only be done in development environments. For production deployments:

- Keep SSL enabled
- Properly configure SSL certificates
- Follow security best practices for database connections

#### Additional Considerations

- Make sure your database connection string in .env is properly configured
- If you're using a different PostgreSQL version, the configuration file path might vary
- Always backup your configuration files before making changes
Binary file added hono-prisma-postgres/bun.lockb
Binary file not shown.
35 changes: 35 additions & 0 deletions hono-prisma-postgres/docker-compose.yml
Original file line number Diff line number Diff line change
@@ -0,0 +1,35 @@
version: '3.8'

services:
app:
build:
context: .
dockerfile: Dockerfile
container_name: todo-app
ports:
- "3000:3000"
environment:
DATABASE_URL: "postgresql://postgres:postgres@db:5432/my_pgserver"
depends_on:
- db
command: >
sh -c "bunx prisma migrate dev --name init && bun dev"
networks:
- keploy-network

db:
image: postgres:15
container_name: postgres-db
restart: always
environment:
POSTGRES_USER: postgres
POSTGRES_PASSWORD: postgres
POSTGRES_DB: my_pgserver
ports:
- "5432:5432"
networks:
- keploy-network

networks:
keploy-network:
external: true
61 changes: 61 additions & 0 deletions hono-prisma-postgres/keploy.yml
Original file line number Diff line number Diff line change
@@ -0,0 +1,61 @@
path: ""
appId: 0
appName: hono-prisma-postgres
command: bun dev
templatize:
testSets: []
port: 0
dnsPort: 26789
proxyPort: 16789
debug: false
disableTele: false
disableANSI: false
containerName: ""
networkName: ""
buildDelay: 30
test:
selectedTests: {}
globalNoise:
global: {}
test-sets: {}
delay: 5
host: ""
port: 0
apiTimeout: 5
skipCoverage: false
coverageReportPath: ""
ignoreOrdering: true
mongoPassword: default@123
language: ""
removeUnusedMocks: false
fallBackOnMiss: false
jacocoAgentPath: ""
basePath: ""
mocking: true
ignoredTests: {}
disableLineCoverage: false
disableMockUpload: true
useLocalMock: false
updateTemplate: false
record:
filters: []
recordTimer: 0s
configPath: ""
bypassRules: []
generateGithubActions: false
keployContainer: keploy-v2
keployNetwork: keploy-network
cmdType: native
contract:
services: []
tests: []
path: ""
download: false
generate: false
driven: consumer
mappings:
servicesMapping: {}
self: ""
inCi: false

# Visit [https://keploy.io/docs/running-keploy/configuration-file/] to learn about using keploy through configration file.
2 changes: 2 additions & 0 deletions hono-prisma-postgres/keploy/.gitignore
Original file line number Diff line number Diff line change
@@ -0,0 +1,2 @@

/reports/
Loading