KaroKar Backend persists all data in a PostgreSQL database accessed through TypeORM 0.3.x. The ORM is wired into NestJS viaDocumentation Index
Fetch the complete documentation index at: https://mintlify.com/Codefied-CodePix/Karokar-backend/llms.txt
Use this file to discover all available pages before exploring further.
TypeOrmModule.forRootAsync, which defers connection setup until the dependency-injection container has resolved ConfigService — ensuring the DATABASE_URL environment variable is present before the first connection attempt. Schema changes are managed exclusively through TypeORM migrations; the synchronize flag is permanently disabled to prevent accidental schema drift in shared and production databases.
Runtime Connection (AppModule)
The TypeORM integration is configured asynchronously insidesrc/app.module.ts:
| Option | Value | Notes |
|---|---|---|
type | 'postgres' | Selects the pg driver (declared as a dependency in package.json). |
url | DATABASE_URL env var | Uses getOrThrow — the process exits immediately at startup if the variable is missing. |
autoLoadEntities | true | Any entity registered with TypeOrmModule.forFeature(...) in a feature module is automatically included in the connection without manually listing it here. |
synchronize | false | TypeORM will not alter the database schema automatically. All schema changes must go through migrations. |
CLI DataSource (src/database/data-source.ts)
The TypeORM CLI commands need a standaloneDataSource that can be loaded outside the NestJS application context. This file is the entry point for all migration tooling:
config() from the dotenv package is called at the top of the file so that
process.env.DATABASE_URL is populated from .env when the CLI is invoked
directly with ts-node — no NestJS bootstrap is involved.
entities— glob that discovers every*.entity.ts/*.entity.jsfile anywhere undersrc/, matching the module-per-domain folder structure.migrations— points tosrc/database/migrations/, the canonical location for all generated and hand-authored migration files.
The
typeorm npm script in package.json runs TypeORM through
typeorm-ts-node-commonjs, which lets the CLI consume TypeScript source
directly without a prior compilation step:Migration Commands
All migration operations are available as npm scripts. The underlying command always uses the DataSource defined insrc/database/data-source.ts.
Make your entity changes
Add, modify, or remove columns and relations in the relevant
*.entity.ts
file inside the appropriate domain module.Generate the migration
Run
npm run migration:generate -- src/database/migrations/DescriptiveName.
TypeORM connects to the database defined by DATABASE_URL, compares the
current schema to your updated entities, and writes a timestamped migration
file containing the forward (up) and rollback (down) SQL.Review the generated file
Open the new file in
src/database/migrations/ and verify that the
generated SQL matches your intent. Pay particular attention to any DROP
statements.Entity Conventions
All domain entities in the codebase extend the sharedBaseEntity class defined in src/shared/domain/base.entity.ts:
| Column | TypeORM decorator | Type | Notes |
|---|---|---|---|
id | @PrimaryGeneratedColumn('uuid') | string (UUID) | Primary key. TypeORM generates the value using the database’s uuid_generate_v4() function. |
created_at | @CreateDateColumn | Date | Set automatically by TypeORM on INSERT; never updated. |
updated_at | @UpdateDateColumn | Date | Updated automatically by TypeORM on every UPDATE. |
The
uuid package (v11, listed in package.json) is available for
application-layer UUID generation when you need to assign an ID before
persisting — for example when publishing a domain event that must carry the
future entity’s ID. Use import { v4 as uuidv4 } from 'uuid' in those cases.- Column names use
snake_casevia thenameoption on@Column: - TypeScript property names use
camelCase— TypeORM maps between the two automatically at query time. - Repositories are injected with
@InjectRepository()inside service constructors. Direct use ofEntityManageris avoided to keep data-access logic scoped to the owning module. - Entities are registered per feature module using
TypeOrmModule.forFeature([EntityClass]), which works in combination withautoLoadEntities: trueon the root connection.
Concrete Example
src/booking/domain/entities/booking.entity.ts illustrates the full pattern:
Connection String Reference
TheDATABASE_URL follows the standard PostgreSQL URI format. Common patterns:
When connecting to a cloud-hosted PostgreSQL instance that enforces SSL, append
?sslmode=require (or ?sslmode=verify-full with a CA certificate) to the
connection string. The pg driver respects these query parameters without any
additional TypeORM configuration.