-
Notifications
You must be signed in to change notification settings - Fork 46
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
Vibgitcode27
wants to merge
1
commit into
keploy:main
Choose a base branch
from
Vibgitcode27:main
base: main
Could not load branches
Branch not found: {{ refName }}
Loading
Could not load tags
Nothing to show
Loading
Are you sure you want to change the base?
Some commits from the old base branch may be removed from the timeline,
and old review comments may become outdated.
Open
Changes from all commits
Commits
File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,3 @@ | ||
# deps | ||
node_modules/ | ||
.env |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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 not shown.
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. is this file needed? |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1 @@ | ||
console.log("Hello via Bun!"); |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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. |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,2 @@ | ||
|
||
/reports/ |
Oops, something went wrong.
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
There was a problem hiding this comment.
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