Files
gridpilot.gg/apps/api/src/persistence/postgres/PostgresRacingPersistenceModule.ts
2025-12-29 00:24:56 +01:00

201 lines
7.3 KiB
TypeScript

import { Module } from '@nestjs/common';
import { TypeOrmModule, getDataSourceToken } from '@nestjs/typeorm';
import type { DataSource } from 'typeorm';
import { LoggingModule } from '../../domain/logging/LoggingModule';
import {
DRIVER_REPOSITORY_TOKEN,
GAME_REPOSITORY_TOKEN,
LEAGUE_MEMBERSHIP_REPOSITORY_TOKEN,
LEAGUE_REPOSITORY_TOKEN,
LEAGUE_SCORING_CONFIG_REPOSITORY_TOKEN,
LEAGUE_WALLET_REPOSITORY_TOKEN,
PENALTY_REPOSITORY_TOKEN,
PROTEST_REPOSITORY_TOKEN,
RACE_REGISTRATION_REPOSITORY_TOKEN,
RACE_REPOSITORY_TOKEN,
RESULT_REPOSITORY_TOKEN,
SEASON_REPOSITORY_TOKEN,
SEASON_SPONSORSHIP_REPOSITORY_TOKEN,
SPONSOR_REPOSITORY_TOKEN,
SPONSORSHIP_PRICING_REPOSITORY_TOKEN,
SPONSORSHIP_REQUEST_REPOSITORY_TOKEN,
STANDING_REPOSITORY_TOKEN,
TEAM_MEMBERSHIP_REPOSITORY_TOKEN,
TEAM_REPOSITORY_TOKEN,
TRANSACTION_REPOSITORY_TOKEN,
} from '../inmemory/InMemoryRacingPersistenceModule';
import { LeagueOrmEntity } from '@adapters/racing/persistence/typeorm/entities/LeagueOrmEntity';
import { LeagueScoringConfigOrmEntity } from '@adapters/racing/persistence/typeorm/entities/LeagueScoringConfigOrmEntity';
import { RaceOrmEntity } from '@adapters/racing/persistence/typeorm/entities/RaceOrmEntity';
import { SeasonOrmEntity } from '@adapters/racing/persistence/typeorm/entities/SeasonOrmEntity';
import { TypeOrmLeagueRepository } from '@adapters/racing/persistence/typeorm/repositories/TypeOrmLeagueRepository';
import { TypeOrmLeagueScoringConfigRepository } from '@adapters/racing/persistence/typeorm/repositories/TypeOrmLeagueScoringConfigRepository';
import { TypeOrmRaceRepository } from '@adapters/racing/persistence/typeorm/repositories/TypeOrmRaceRepository';
import { TypeOrmSeasonRepository } from '@adapters/racing/persistence/typeorm/repositories/TypeOrmSeasonRepository';
import { LeagueOrmMapper } from '@adapters/racing/persistence/typeorm/mappers/LeagueOrmMapper';
import { RaceOrmMapper } from '@adapters/racing/persistence/typeorm/mappers/RaceOrmMapper';
import { SeasonOrmMapper } from '@adapters/racing/persistence/typeorm/mappers/SeasonOrmMapper';
import { PointsTableJsonMapper } from '@adapters/racing/persistence/typeorm/mappers/PointsTableJsonMapper';
import { ChampionshipConfigJsonMapper } from '@adapters/racing/persistence/typeorm/mappers/ChampionshipConfigJsonMapper';
import { LeagueScoringConfigOrmMapper } from '@adapters/racing/persistence/typeorm/mappers/LeagueScoringConfigOrmMapper';
function makePlaceholder(token: string): unknown {
return Object.freeze({
__token: token,
__kind: 'postgres-placeholder',
__notImplemented(): never {
throw new Error(`[PostgresRacingPersistenceModule] Placeholder provider "${token}" is not implemented yet`);
},
});
}
const typeOrmFeatureImports = [
TypeOrmModule.forFeature([LeagueOrmEntity, SeasonOrmEntity, RaceOrmEntity, LeagueScoringConfigOrmEntity]),
];
@Module({
imports: [LoggingModule, ...typeOrmFeatureImports],
providers: [
{
provide: DRIVER_REPOSITORY_TOKEN,
useFactory: () => makePlaceholder(DRIVER_REPOSITORY_TOKEN),
inject: ['Logger'],
},
{
provide: LEAGUE_REPOSITORY_TOKEN,
useFactory: (dataSource: DataSource) => {
const leagueMapper = new LeagueOrmMapper();
return new TypeOrmLeagueRepository(dataSource, leagueMapper);
},
inject: [getDataSourceToken()],
},
{
provide: RACE_REPOSITORY_TOKEN,
useFactory: (dataSource: DataSource) => {
const raceMapper = new RaceOrmMapper();
return new TypeOrmRaceRepository(dataSource, raceMapper);
},
inject: [getDataSourceToken()],
},
{
provide: RESULT_REPOSITORY_TOKEN,
useFactory: () => makePlaceholder(RESULT_REPOSITORY_TOKEN),
inject: ['Logger'],
},
{
provide: STANDING_REPOSITORY_TOKEN,
useFactory: () => makePlaceholder(STANDING_REPOSITORY_TOKEN),
inject: ['Logger'],
},
{
provide: LEAGUE_MEMBERSHIP_REPOSITORY_TOKEN,
useFactory: () => makePlaceholder(LEAGUE_MEMBERSHIP_REPOSITORY_TOKEN),
inject: ['Logger'],
},
{
provide: RACE_REGISTRATION_REPOSITORY_TOKEN,
useFactory: () => makePlaceholder(RACE_REGISTRATION_REPOSITORY_TOKEN),
inject: ['Logger'],
},
{
provide: TEAM_REPOSITORY_TOKEN,
useFactory: () => makePlaceholder(TEAM_REPOSITORY_TOKEN),
inject: ['Logger'],
},
{
provide: TEAM_MEMBERSHIP_REPOSITORY_TOKEN,
useFactory: () => makePlaceholder(TEAM_MEMBERSHIP_REPOSITORY_TOKEN),
inject: ['Logger'],
},
{
provide: PENALTY_REPOSITORY_TOKEN,
useFactory: () => makePlaceholder(PENALTY_REPOSITORY_TOKEN),
inject: ['Logger'],
},
{
provide: PROTEST_REPOSITORY_TOKEN,
useFactory: () => makePlaceholder(PROTEST_REPOSITORY_TOKEN),
inject: ['Logger'],
},
{
provide: SEASON_REPOSITORY_TOKEN,
useFactory: (dataSource: DataSource) => {
const seasonMapper = new SeasonOrmMapper();
return new TypeOrmSeasonRepository(dataSource, seasonMapper);
},
inject: [getDataSourceToken()],
},
{
provide: SEASON_SPONSORSHIP_REPOSITORY_TOKEN,
useFactory: () => makePlaceholder(SEASON_SPONSORSHIP_REPOSITORY_TOKEN),
inject: ['Logger'],
},
{
provide: LEAGUE_SCORING_CONFIG_REPOSITORY_TOKEN,
useFactory: (dataSource: DataSource) => {
const pointsTableMapper = new PointsTableJsonMapper();
const championshipMapper = new ChampionshipConfigJsonMapper(pointsTableMapper);
const scoringConfigMapper = new LeagueScoringConfigOrmMapper(championshipMapper);
return new TypeOrmLeagueScoringConfigRepository(dataSource, scoringConfigMapper);
},
inject: [getDataSourceToken()],
},
{
provide: GAME_REPOSITORY_TOKEN,
useFactory: () => makePlaceholder(GAME_REPOSITORY_TOKEN),
inject: ['Logger'],
},
{
provide: LEAGUE_WALLET_REPOSITORY_TOKEN,
useFactory: () => makePlaceholder(LEAGUE_WALLET_REPOSITORY_TOKEN),
inject: ['Logger'],
},
{
provide: TRANSACTION_REPOSITORY_TOKEN,
useFactory: () => makePlaceholder(TRANSACTION_REPOSITORY_TOKEN),
inject: ['Logger'],
},
{
provide: SPONSOR_REPOSITORY_TOKEN,
useFactory: () => makePlaceholder(SPONSOR_REPOSITORY_TOKEN),
inject: ['Logger'],
},
{
provide: SPONSORSHIP_PRICING_REPOSITORY_TOKEN,
useFactory: () => makePlaceholder(SPONSORSHIP_PRICING_REPOSITORY_TOKEN),
inject: ['Logger'],
},
{
provide: SPONSORSHIP_REQUEST_REPOSITORY_TOKEN,
useFactory: () => makePlaceholder(SPONSORSHIP_REQUEST_REPOSITORY_TOKEN),
inject: ['Logger'],
},
],
exports: [
DRIVER_REPOSITORY_TOKEN,
LEAGUE_REPOSITORY_TOKEN,
RACE_REPOSITORY_TOKEN,
RESULT_REPOSITORY_TOKEN,
STANDING_REPOSITORY_TOKEN,
LEAGUE_MEMBERSHIP_REPOSITORY_TOKEN,
RACE_REGISTRATION_REPOSITORY_TOKEN,
TEAM_REPOSITORY_TOKEN,
TEAM_MEMBERSHIP_REPOSITORY_TOKEN,
PENALTY_REPOSITORY_TOKEN,
PROTEST_REPOSITORY_TOKEN,
SEASON_REPOSITORY_TOKEN,
SEASON_SPONSORSHIP_REPOSITORY_TOKEN,
LEAGUE_SCORING_CONFIG_REPOSITORY_TOKEN,
GAME_REPOSITORY_TOKEN,
LEAGUE_WALLET_REPOSITORY_TOKEN,
TRANSACTION_REPOSITORY_TOKEN,
SPONSOR_REPOSITORY_TOKEN,
SPONSORSHIP_PRICING_REPOSITORY_TOKEN,
SPONSORSHIP_REQUEST_REPOSITORY_TOKEN,
],
})
export class PostgresRacingPersistenceModule {}