| .. | ||
| .wrangler | ||
| assets | ||
| creds | ||
| dumps | ||
| scripts | ||
| src | ||
| tests | ||
| .dockerignore | ||
| .env.example | ||
| .envz | ||
| demo.json | ||
| docker-compose.yml | ||
| Dockerfile | ||
| index.ts | ||
| migrated.sql | ||
| package.json | ||
| README.md | ||
| reset-remote-db.sh | ||
| test-case-info.md | ||
| tsconfig.json | ||
| worker.ts | ||
| wrangler-commands.md | ||
| wrangler.dev.toml | ||
| wrangler.prod.toml | ||
Health Petal Database
This project contains the database schema, seed data, and related tools for the Health Petal application.
Setup
- Make sure PostgreSQL is installed and running on your machine
- Create a database named
health_petal - Configure your
.envfile 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 filesnpm run db:push- Push schema changes to the databasenpm run db:seed- Seed the database with mock datanpm 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.
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:
bun run create-user -- --mobile 9990001111 --password "secret123" --show-sql
Getting Started
-
First, install dependencies:
npm install -
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 -
Verify the data:
-- 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:
-
Build the Docker image:
npm run docker:build -
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.