This directory contains database configuration, migrations, and seeders for Stellar Uzima Backend.
- Node.js >= 18.x
- PostgreSQL 12+ (or 15+ via Docker)
- npm (or yarn/pnpm)
- Docker & Docker Compose (optional, for containerized development)
Follow these steps to get the database running locally:
git clone https://github.com/Stellar-Uzima/Uzima-Backend.git
cd Uzima-Backend
npm installCreate a .env file at the project root with the required database variables:
# Database (REQUIRED)
DATABASE_HOST=localhost
DATABASE_PORT=5432
DATABASE_USERNAME=postgres
DATABASE_PASSWORD=postgres
DATABASE_NAME=uzima
# Alternative variable names (also supported)
# DB_HOST=localhost
# DB_PORT=5432
# DB_USERNAME=postgres
# DB_PASSWORD=postgres
# DB_NAME=uzima
# SSL (set to 'true' if your PostgreSQL requires SSL)
DATABASE_SSL=false
# For SQLite testing only (optional)
# DATABASE_TYPE=sqliteNote: The application accepts both DATABASE_* and DB_* prefixed variables. data-source.ts (used for CLI migrations/seeding) checks DB_* first, then falls back to DATABASE_*. The NestJS module (typeorm.config.ts) uses DATABASE_* first, then DB_*.
Option A — Docker (recommended):
docker compose up -d postgres
# Wait for healthy status:
docker compose psThis starts a PostgreSQL 15 container with:
- User:
postgres(or whateverDB_USERNAMEis set to) - Password:
postgres(or whateverDB_PASSWORDis set to) - Database:
stellar_uzima_dev(or whateverDB_NAMEis set to) - Port:
5432mapped to localhost
Option B — Local PostgreSQL:
Ensure PostgreSQL is installed and running, then create the database:
createdb uzima
# or via psql:
# psql -U postgres -c "CREATE DATABASE uzima;"Apply all pending migrations to set up the database schema:
npm run migrate
# or
npm run migration:runExpected output: A list of executed migrations with no errors.
Populate the database with initial data (users, categories, tasks):
npm run seed
# or
npm run seed:dbSeeders run in this order:
UserSeeder— creates admin, healer, and test usersTaskCategorySeeder— creates task categories (Nutrition, Exercise, Mental Health, etc.)HealthTaskSeeder— creates health tasks linked to categories
Start the development server:
npm run start:devThe API should be available at http://localhost:3000 (or the port set in APP_PORT).
If you need to reset the database completely:
npm run seed:refreshThis rolls back all migrations, re-applies them, and re-seeds the data.
database/
├── migrations/ # Canonical TypeORM migrations folder (single source of truth)
├── seeders/ # TypeORM seeder classes (canonical seeding mechanism)
├── entities/ # Shared database entities
├── services/ # Transaction services
├── data-source.ts # DataSource config for TypeORM CLI (migrations & seeding)
├── database.module.ts # Database module
├── typeorm.config.ts # TypeORM config for NestJS module
└── README.md
All migrations are consolidated in src/database/migrations/ — this is the canonical migrations folder.
npm run migrate:create -- -n MigrationNameThe new migration file is created in the current working directory. Move it to src/database/migrations/ and add the Unix timestamp prefix followed by a descriptive name.
npm run migrate
# or
npm run migration:runBoth commands use the data source defined in src/database/data-source.ts.
npm run migrate:rollbackMigrations use Unix timestamp prefixes (milliseconds since epoch) to ensure deterministic ordering. Example:
1700000000000-InitialSchema.ts1700000000001-AddReferralFields.ts
| Issue | Solution |
|---|---|
| "relation already exists" | The migration was already applied. Check migrations table in the database. |
| "relation does not exist" | Run npm run migrate to apply pending migrations. |
| Connection refused | Ensure PostgreSQL is running. Check DATABASE_HOST and DATABASE_PORT in .env. |
| Authentication failed | Verify DATABASE_USERNAME and DATABASE_PASSWORD in .env. |
| Database does not exist | Create the database manually: createdb uzima |
The canonical seeding mechanism is src/database/seeders/, using TypeORM-based seeder classes.
To seed the database:
npm run seedSeeders run in this order:
UserSeeder— creates admin, healer, and test usersTaskCategorySeeder— creates task categories (Nutrition, Exercise, Mental Health, etc.)HealthTaskSeeder— creates health tasks linked to categories
Seeders use the same environment variables as migrations (DATABASE_* or DB_*). They connect via src/database/data-source.ts.
- Create a new class in
src/database/seeders/that implements aseed()method - Add it to the seeders array in
src/database/seeders/run-seeders.ts - Ensure it handles duplicate data gracefully (use
INSERT ... ON CONFLICT DO NOTHINGor check existence before inserting)
Entity files are located in their respective module directories:
src/modules/users/entities/user.entity.tssrc/modules/health-tasks/entities/task.entity.tssrc/modules/wallet/entities/wallet.entity.ts- etc.
- Ensure PostgreSQL is running
- Create the database specified in
.env - Run
npm run migrateto apply migrations - Run
npm run seedto populate seed data
| Variable | Required | Default | Description |
|---|---|---|---|
DATABASE_HOST / DB_HOST |
Yes | localhost |
PostgreSQL host |
DATABASE_PORT / DB_PORT |
No | 5432 |
PostgreSQL port |
DATABASE_USERNAME / DB_USERNAME |
Yes | postgres |
PostgreSQL user |
DATABASE_PASSWORD / DB_PASSWORD |
Yes | postgres |
PostgreSQL password |
DATABASE_NAME / DB_NAME |
Yes | uzima |
Database name |
DATABASE_SSL |
No | false |
Enable SSL (true / false) |
DATABASE_TYPE |
No | postgres |
Set to sqlite for in-memory testing |
NODE_ENV |
No | development |
When test, SQLite may be auto-selected |
- Create entities in their respective modules
- Use TypeORM decorators for all database-related metadata
- Always create migrations for schema changes
- Add proper indexes and constraints
- Use migrations for production deployments
- Always add migrations to
src/database/migrations/(the canonical folder) - Use
src/database/seeders/for all seeding needs - Never run
synchronize: truein production - Test migrations locally before deploying