Skip to content

Database & TypeORM

DailyDesk uses PostgreSQL as its relational database and TypeORM as the Object-Relational Mapper (ORM).

TypeORM Configuration

The application interfaces with the database using standard NestJS TypeORM integration.

Connection Setup

The database connection is defined in src/config/typeorm.config.ts. It loads connection parameters from environment variables (DB_HOST, DB_PORT, DB_USERNAME, etc.).

// Example Config Pattern
export const typeOrmConfig: TypeOrmModuleOptions = {
  type: 'postgres',
  host: process.env.DB_HOST,
  port: parseInt(process.env.DB_PORT, 10),
  username: process.env.DB_USERNAME,
  password: process.env.DB_PASSWORD,
  database: process.env.DB_NAME,
  entities: [__dirname + '/../**/*.entity.{js,ts}'],
  synchronize: false, // Always false in production!
  migrations: [__dirname + '/../migrations/*.{js,ts}'],
};

Pattern: Data Mapper

DailyDesk follows the Data Mapper pattern (standard in NestJS). * Entities: Define the data structure (e.g., UserEntity). * Repositories: Handle data access logic. We inject Repository<Entity> into our Services.

// Example Service Injection
constructor(
  @InjectRepository(UserEntity)
  private readonly userRepository: Repository<UserEntity>,
) {}

Migrations

Database schema changes are managed via TypeORM Migrations, not synchronize: true (except potentially in local dev if configured, but production relies on migrations).

  • Generate: Create a new migration file based on entity changes.
  • Run: Execute pending migrations to update the database schema.
  • Revert: Undo the last migration.

In the CI/CD pipeline, the CdkBackendStack ensures migrations are run (via a CodeBuild project) before the new application code is fully deployed to ECS.