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 1 commit into
base: main
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from all 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
162 changes: 162 additions & 0 deletions hono-prisma-postgres/README.md
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Can you please add the docker configuration for the same as well

Original file line number Diff line number Diff line change
@@ -0,0 +1,162 @@
# 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

## Tech Stack

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

## 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

### 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 the Project

1. **Clone the Repository**

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

2. **Install Dependencies**
Use the following command to install dependencies with Bun:
```bash
bun install
```
3. **Setup `.env`**
Create a .env file and add your Postgres database url. following is an example url:
```bash
DATABASE_URL="postgresql://postgres:password@localhost:5432/my_pgserver?schema=public"
```
4. **Setup Database**
Run the Prisma migration command to initialize the database schema:

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

5. **Run the Application**
Start the application using the following command:

```bash
bun dev
```

The application should now be running on the specified port.

## Keploy Integration

Keploy allows you to record and test API requests and responses. Follow the steps below to use Keploy with this project:

### Recording Test Cases

1. Start the Keploy server if it's not already running.
2. Record test cases using the following command:
```bash
keploy record -c "bun dev"
```
This will record all API interactions.

### Running Test Cases

1. Once test cases are recorded, you can run them using the following command:

```bash
keploy test -c "bun dev" --delay 10
```

The `--delay` flag ensures Keploy has enough time to send API requests after the application starts.

## 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:

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

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.
1 change: 1 addition & 0 deletions hono-prisma-postgres/index.ts
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

is this file needed?

Original file line number Diff line number Diff line change
@@ -0,0 +1 @@
console.log("Hello via Bun!");
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