Files
trueref/src/lib/server/mappers/repository.mapper.ts
Giancarmine Salucci 215cadf070 refactor: introduce domain model classes and mapper layer
Replace ad-hoc inline row casting (snake_case → camelCase) spread across
services, routes, and the indexing pipeline with explicit model classes
(Repository, IndexingJob, RepositoryVersion, Snippet, SearchResult) and
dedicated mapper classes that own the DB → domain conversion.

- Add src/lib/server/models/ with typed model classes for all domain entities
- Add src/lib/server/mappers/ with mapper classes per entity
- Remove duplicated RawRow interfaces and inline map functions from
  job-queue, repository.service, indexing.pipeline, and all API routes
- Add dtoJsonResponse helper to standardise JSON responses via SvelteKit json()
- Add api-contract.integration.test.ts as a regression baseline

Co-Authored-By: Claude Sonnet 4.6 <noreply@anthropic.com>
2026-03-25 14:29:49 +01:00

67 lines
2.1 KiB
TypeScript

import { Repository, RepositoryDto, RepositoryEntity } from '$lib/server/models/repository.js';
export class RepositoryMapper {
static fromEntity(entity: RepositoryEntity): Repository {
return new Repository({
id: entity.id,
title: entity.title,
description: entity.description,
source: entity.source,
sourceUrl: entity.source_url,
branch: entity.branch,
state: entity.state,
totalSnippets: entity.total_snippets ?? 0,
totalTokens: entity.total_tokens ?? 0,
trustScore: entity.trust_score ?? 0,
benchmarkScore: entity.benchmark_score ?? 0,
stars: entity.stars,
githubToken: entity.github_token,
lastIndexedAt:
entity.last_indexed_at != null ? new Date(entity.last_indexed_at * 1000) : null,
createdAt: new Date(entity.created_at * 1000),
updatedAt: new Date(entity.updated_at * 1000)
});
}
static toEntity(domain: Repository): RepositoryEntity {
return new RepositoryEntity({
id: domain.id,
title: domain.title,
description: domain.description,
source: domain.source,
source_url: domain.sourceUrl,
branch: domain.branch,
state: domain.state,
total_snippets: domain.totalSnippets,
total_tokens: domain.totalTokens,
trust_score: domain.trustScore,
benchmark_score: domain.benchmarkScore,
stars: domain.stars,
github_token: domain.githubToken,
last_indexed_at:
domain.lastIndexedAt != null ? Math.floor(domain.lastIndexedAt.getTime() / 1000) : null,
created_at: Math.floor(domain.createdAt.getTime() / 1000),
updated_at: Math.floor(domain.updatedAt.getTime() / 1000)
});
}
static toDto(domain: Repository): RepositoryDto {
return new RepositoryDto({
id: domain.id,
title: domain.title,
description: domain.description,
source: domain.source,
sourceUrl: domain.sourceUrl,
branch: domain.branch,
state: domain.state,
totalSnippets: domain.totalSnippets,
totalTokens: domain.totalTokens,
trustScore: domain.trustScore,
benchmarkScore: domain.benchmarkScore,
stars: domain.stars,
lastIndexedAt: domain.lastIndexedAt,
createdAt: domain.createdAt,
updatedAt: domain.updatedAt
});
}
}