freshyo/apps/backend/README.md
2026-10-13 19:10:27 +05:30

133 lines
3.6 KiB
Markdown
Executable file

# Health Petal Database
This project contains the database schema, seed data, and related tools for the Health Petal application.
## Setup
1. Make sure PostgreSQL is installed and running on your machine
2. Create a database named `health_petal`
3. Configure your `.env` file with the correct database credentials (or use the default ones)
## Database Schema
The database schema includes the following tables:
- profiles (users)
- role_info
- user_roles
- hospital
- specializations
- hospital_specializations
- hospital_employees
- doctor_info
- doctor_specializations
- doctor_secretaries
- doctor_availability
- token_info
- running_counter
## Available Commands
- `npm run migrate` - Generate SQL migration files
- `npm run db:push` - Push schema changes to the database
- `npm run db:seed` - Seed the database with mock data
- `npm run docker:build` - Build Docker image for the backend
## Create a user (script)
`scripts/create-user.ts` creates a user with a password directly in D1 — without going through the app. It hashes the password with `bcryptjs` (12 rounds, same as the signup flow) and writes through `wrangler d1 execute`, so it targets the same database as `wrangler dev`.
```bash
cd apps/backend
bun run create-user -- \
--mobile 9990001111 \
--password "secret123" \
--name "Test User" \
--email test@freshyo.in
```
Log in with either the email or the mobile as the `identifier` (`trpc.user.auth.login`), plus the password.
Flags:
| Flag | Description |
| --- | --- |
| `--name <string>` | Display name (optional) |
| `--email <string>` / `--mobile <string>` | Login identifier — at least one is required |
| `--password <string>` | Plaintext password (required) |
| `--update` | If the user already exists, reset its password (otherwise it refuses) |
| `--remote` | Write to the remote D1 instead of the local one |
| `--show-sql` | Print the SQL and exit (no write) |
| `--help` | Show usage |
Env overrides: `D1_CONFIG` (default `wrangler.dev.toml`; `wrangler.prod.toml` when `--remote`) and `D1_DB_NAME` (default `freshyo-dev`).
It writes two tables: `users` (identity: name/email/mobile) and `user_creds` (bcrypt password hash). Dry run to preview the SQL:
```bash
bun run create-user -- --mobile 9990001111 --password "secret123" --show-sql
```
## Getting Started
1. First, install dependencies:
```
npm install
```
2. Set up the database:
```
# Create a PostgreSQL database
createdb health_petal
# Generate and push the schema
npm run migrate
npm run db:push
# Seed the database with test data
npm run db:seed
```
3. Verify the data:
```sql
-- Connect to the database
psql health_petal
-- Check the tables
\dt
-- Check users
SELECT * FROM profiles;
-- Check doctors
SELECT p.name, p.email, d.qualifications, d.daily_token_count
FROM doctor_info d
JOIN profiles p ON d.user_id = p.id;
-- Check doctor specializations
SELECT p.name, s.name as specialization
FROM doctor_specializations ds
JOIN doctor_info d ON ds.doctor_id = d.id
JOIN profiles p ON d.user_id = p.id
JOIN specializations s ON ds.specialization_id = s.id;
```
## Docker
This project includes Docker support for easy deployment:
1. Build the Docker image:
```
npm run docker:build
```
2. Run with Docker Compose (includes PostgreSQL database):
```
docker-compose up -d
```
The Docker Compose setup includes:
- PostgreSQL database with the correct schema
- Backend application with all environment variables configured
Note: For production deployment, make sure to update the environment variables in docker-compose.yml with your actual values.