Database

TypeORM configuration with PostgreSQL via DatabaseModule and DataSource factory.

The database infrastructure uses TypeORM with PostgreSQL. The connection is managed by a singleton DataSource injected via a custom token.

typescript
@Module({
  providers: [
    EnvService,
    {
      provide: DATA_SOURCE_PROVIDER_TOKEN,
      useFactory: dataSourceFactory,
      inject: [EnvService],
    },
  ],
  exports: [EnvService, DATA_SOURCE_PROVIDER_TOKEN],
})
export class DatabaseModule {}
typescript
export const DATA_SOURCE_PROVIDER_TOKEN = 'DATA_SOURCE';

export async function dataSourceFactory(env: EnvService) {
  const dataSource = new DataSource({
    type: 'postgres',
    url: env.get('DATABASE_URL'),
    entities: [Person, PersonAddress, PersonContact],
    invalidWhereValuesBehavior: {
      undefined: 'ignore',
    },
  });

  await dataSource.initialize();

  return dataSource;
}

Repositories receive the DataSource via token, not the class directly:

typescript
constructor(@Inject(DATA_SOURCE_PROVIDER_TOKEN) dataSource: DataSource) {
  super(dataSource, Person);
}

The connection URL comes from DATABASE_URL, validated in the Zod schema (format from .env.example):

env
DATABASE_URL=postgresql://postgres:root@localhost:5432/koala_nest
  1. Create the entity in src/domain/entities/.
  2. Include it in the entities array of dataSourceFactory.
  3. Generate and apply the migration.

migration-datasource.ts discovers entities automatically via glob — no need to register them manually there.

InfraModule aggregates repositories and exports RepositoryModule:

typescript
@Module({
  imports: [RepositoryModule],
  exports: [RepositoryModule],
})
export class InfraModule {}

Koala Nest

A facilitator for building NestJS APIs with DDD architecture. Code copied into your repository — readable, adaptable, and under your control.

Creator

igordrangel.com.br

Design, back-end, and product strategy.

Quick Commands

Global CLI and scripts in the generated project

  • bun install -g @koalarx/nest
  • kl-nest new
  • kl-nest add cache
  • bun run migration:run # CRUD template
  • kl-nest --help
© 2026 Koala NestBuilt for NestJS developers and AI-assisted workflows.