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>
This commit is contained in:
Giancarmine Salucci
2026-03-25 14:29:49 +01:00
parent 7994254e23
commit 215cadf070
39 changed files with 1339 additions and 562 deletions

View File

@@ -0,0 +1,99 @@
export class LibrarySearchJsonResultDto {
id: string;
title: string;
description: string | null;
branch: string | null;
lastUpdateDate: string | null;
state: 'initial' | 'finalized' | 'error';
totalTokens: number | null;
totalSnippets: number | null;
stars: number | null;
trustScore: number | null;
benchmarkScore: number | null;
versions: string[];
source: string;
constructor(props: LibrarySearchJsonResultDto) {
this.id = props.id;
this.title = props.title;
this.description = props.description;
this.branch = props.branch;
this.lastUpdateDate = props.lastUpdateDate;
this.state = props.state;
this.totalTokens = props.totalTokens;
this.totalSnippets = props.totalSnippets;
this.stars = props.stars;
this.trustScore = props.trustScore;
this.benchmarkScore = props.benchmarkScore;
this.versions = props.versions;
this.source = props.source;
}
}
export class LibrarySearchJsonResponseDto {
results: LibrarySearchJsonResultDto[];
constructor(results: LibrarySearchJsonResultDto[]) {
this.results = results;
}
}
export class CodeListItemDto {
language: string;
code: string;
constructor(props: CodeListItemDto) {
this.language = props.language;
this.code = props.code;
}
}
export class CodeSnippetJsonDto {
type: 'code' = 'code';
title: string | null;
description: string | null;
language: string | null;
codeList: CodeListItemDto[];
id: string;
tokenCount: number | null;
pageTitle: string | null;
constructor(props: Omit<CodeSnippetJsonDto, 'type'>) {
this.title = props.title;
this.description = props.description;
this.language = props.language;
this.codeList = props.codeList;
this.id = props.id;
this.tokenCount = props.tokenCount;
this.pageTitle = props.pageTitle;
}
}
export class InfoSnippetJsonDto {
type: 'info' = 'info';
text: string;
breadcrumb: string | null;
pageId: string;
tokenCount: number | null;
constructor(props: Omit<InfoSnippetJsonDto, 'type'>) {
this.text = props.text;
this.breadcrumb = props.breadcrumb;
this.pageId = props.pageId;
this.tokenCount = props.tokenCount;
}
}
export type SnippetJsonDto = CodeSnippetJsonDto | InfoSnippetJsonDto;
export class ContextJsonResponseDto {
snippets: SnippetJsonDto[];
rules: string[];
totalTokens: number;
constructor(props: ContextJsonResponseDto) {
this.snippets = props.snippets;
this.rules = props.rules;
this.totalTokens = props.totalTokens;
}
}

View File

@@ -0,0 +1,125 @@
export interface IndexingJobEntityProps {
id: string;
repository_id: string;
version_id: string | null;
status: 'queued' | 'running' | 'done' | 'failed';
progress: number;
total_files: number;
processed_files: number;
error: string | null;
started_at: number | null;
completed_at: number | null;
created_at: number;
}
export class IndexingJobEntity {
id: string;
repository_id: string;
version_id: string | null;
status: 'queued' | 'running' | 'done' | 'failed';
progress: number;
total_files: number;
processed_files: number;
error: string | null;
started_at: number | null;
completed_at: number | null;
created_at: number;
constructor(props: IndexingJobEntityProps) {
this.id = props.id;
this.repository_id = props.repository_id;
this.version_id = props.version_id;
this.status = props.status;
this.progress = props.progress;
this.total_files = props.total_files;
this.processed_files = props.processed_files;
this.error = props.error;
this.started_at = props.started_at;
this.completed_at = props.completed_at;
this.created_at = props.created_at;
}
}
export interface IndexingJobProps {
id: string;
repositoryId: string;
versionId: string | null;
status: 'queued' | 'running' | 'done' | 'failed';
progress: number;
totalFiles: number;
processedFiles: number;
error: string | null;
startedAt: Date | null;
completedAt: Date | null;
createdAt: Date;
}
export class IndexingJob {
id: string;
repositoryId: string;
versionId: string | null;
status: 'queued' | 'running' | 'done' | 'failed';
progress: number;
totalFiles: number;
processedFiles: number;
error: string | null;
startedAt: Date | null;
completedAt: Date | null;
createdAt: Date;
constructor(props: IndexingJobProps) {
this.id = props.id;
this.repositoryId = props.repositoryId;
this.versionId = props.versionId;
this.status = props.status;
this.progress = props.progress;
this.totalFiles = props.totalFiles;
this.processedFiles = props.processedFiles;
this.error = props.error;
this.startedAt = props.startedAt;
this.completedAt = props.completedAt;
this.createdAt = props.createdAt;
}
}
export interface IndexingJobDtoProps {
id: string;
repositoryId: string;
versionId: string | null;
status: 'queued' | 'running' | 'done' | 'failed';
progress: number;
totalFiles: number;
processedFiles: number;
error: string | null;
startedAt: Date | null;
completedAt: Date | null;
createdAt: Date;
}
export class IndexingJobDto {
id: string;
repositoryId: string;
versionId: string | null;
status: 'queued' | 'running' | 'done' | 'failed';
progress: number;
totalFiles: number;
processedFiles: number;
error: string | null;
startedAt: Date | null;
completedAt: Date | null;
createdAt: Date;
constructor(props: IndexingJobDtoProps) {
this.id = props.id;
this.repositoryId = props.repositoryId;
this.versionId = props.versionId;
this.status = props.status;
this.progress = props.progress;
this.totalFiles = props.totalFiles;
this.processedFiles = props.processedFiles;
this.error = props.error;
this.startedAt = props.startedAt;
this.completedAt = props.completedAt;
this.createdAt = props.createdAt;
}
}

View File

@@ -0,0 +1,98 @@
export interface RepositoryVersionEntityProps {
id: string;
repository_id: string;
tag: string;
title: string | null;
state: 'pending' | 'indexing' | 'indexed' | 'error';
total_snippets: number | null;
indexed_at: number | null;
created_at: number;
}
export class RepositoryVersionEntity {
id: string;
repository_id: string;
tag: string;
title: string | null;
state: 'pending' | 'indexing' | 'indexed' | 'error';
total_snippets: number | null;
indexed_at: number | null;
created_at: number;
constructor(props: RepositoryVersionEntityProps) {
this.id = props.id;
this.repository_id = props.repository_id;
this.tag = props.tag;
this.title = props.title;
this.state = props.state;
this.total_snippets = props.total_snippets;
this.indexed_at = props.indexed_at;
this.created_at = props.created_at;
}
}
export interface RepositoryVersionProps {
id: string;
repositoryId: string;
tag: string;
title: string | null;
state: 'pending' | 'indexing' | 'indexed' | 'error';
totalSnippets: number;
indexedAt: Date | null;
createdAt: Date;
}
export class RepositoryVersion {
id: string;
repositoryId: string;
tag: string;
title: string | null;
state: 'pending' | 'indexing' | 'indexed' | 'error';
totalSnippets: number;
indexedAt: Date | null;
createdAt: Date;
constructor(props: RepositoryVersionProps) {
this.id = props.id;
this.repositoryId = props.repositoryId;
this.tag = props.tag;
this.title = props.title;
this.state = props.state;
this.totalSnippets = props.totalSnippets;
this.indexedAt = props.indexedAt;
this.createdAt = props.createdAt;
}
}
export interface RepositoryVersionDtoProps {
id: string;
repositoryId: string;
tag: string;
title: string | null;
state: 'pending' | 'indexing' | 'indexed' | 'error';
totalSnippets: number;
indexedAt: Date | null;
createdAt: Date;
}
export class RepositoryVersionDto {
id: string;
repositoryId: string;
tag: string;
title: string | null;
state: 'pending' | 'indexing' | 'indexed' | 'error';
totalSnippets: number;
indexedAt: Date | null;
createdAt: Date;
constructor(props: RepositoryVersionDtoProps) {
this.id = props.id;
this.repositoryId = props.repositoryId;
this.tag = props.tag;
this.title = props.title;
this.state = props.state;
this.totalSnippets = props.totalSnippets;
this.indexedAt = props.indexedAt;
this.createdAt = props.createdAt;
}
}

View File

@@ -0,0 +1,167 @@
export interface RepositoryEntityProps {
id: string;
title: string;
description: string | null;
source: 'github' | 'local';
source_url: string;
branch: string | null;
state: 'pending' | 'indexing' | 'indexed' | 'error';
total_snippets: number | null;
total_tokens: number | null;
trust_score: number | null;
benchmark_score: number | null;
stars: number | null;
github_token: string | null;
last_indexed_at: number | null;
created_at: number;
updated_at: number;
}
export class RepositoryEntity {
id: string;
title: string;
description: string | null;
source: 'github' | 'local';
source_url: string;
branch: string | null;
state: 'pending' | 'indexing' | 'indexed' | 'error';
total_snippets: number | null;
total_tokens: number | null;
trust_score: number | null;
benchmark_score: number | null;
stars: number | null;
github_token: string | null;
last_indexed_at: number | null;
created_at: number;
updated_at: number;
constructor(props: RepositoryEntityProps) {
this.id = props.id;
this.title = props.title;
this.description = props.description;
this.source = props.source;
this.source_url = props.source_url;
this.branch = props.branch;
this.state = props.state;
this.total_snippets = props.total_snippets;
this.total_tokens = props.total_tokens;
this.trust_score = props.trust_score;
this.benchmark_score = props.benchmark_score;
this.stars = props.stars;
this.github_token = props.github_token;
this.last_indexed_at = props.last_indexed_at;
this.created_at = props.created_at;
this.updated_at = props.updated_at;
}
}
export interface RepositoryProps {
id: string;
title: string;
description: string | null;
source: 'github' | 'local';
sourceUrl: string;
branch: string | null;
state: 'pending' | 'indexing' | 'indexed' | 'error';
totalSnippets: number;
totalTokens: number;
trustScore: number;
benchmarkScore: number;
stars: number | null;
githubToken: string | null;
lastIndexedAt: Date | null;
createdAt: Date;
updatedAt: Date;
}
export class Repository {
id: string;
title: string;
description: string | null;
source: 'github' | 'local';
sourceUrl: string;
branch: string | null;
state: 'pending' | 'indexing' | 'indexed' | 'error';
totalSnippets: number;
totalTokens: number;
trustScore: number;
benchmarkScore: number;
stars: number | null;
githubToken: string | null;
lastIndexedAt: Date | null;
createdAt: Date;
updatedAt: Date;
constructor(props: RepositoryProps) {
this.id = props.id;
this.title = props.title;
this.description = props.description;
this.source = props.source;
this.sourceUrl = props.sourceUrl;
this.branch = props.branch;
this.state = props.state;
this.totalSnippets = props.totalSnippets;
this.totalTokens = props.totalTokens;
this.trustScore = props.trustScore;
this.benchmarkScore = props.benchmarkScore;
this.stars = props.stars;
this.githubToken = props.githubToken;
this.lastIndexedAt = props.lastIndexedAt;
this.createdAt = props.createdAt;
this.updatedAt = props.updatedAt;
}
}
export interface RepositoryDtoProps {
id: string;
title: string;
description: string | null;
source: 'github' | 'local';
sourceUrl: string;
branch: string | null;
state: 'pending' | 'indexing' | 'indexed' | 'error';
totalSnippets: number;
totalTokens: number;
trustScore: number;
benchmarkScore: number;
stars: number | null;
lastIndexedAt: Date | null;
createdAt: Date;
updatedAt: Date;
}
export class RepositoryDto {
id: string;
title: string;
description: string | null;
source: 'github' | 'local';
sourceUrl: string;
branch: string | null;
state: 'pending' | 'indexing' | 'indexed' | 'error';
totalSnippets: number;
totalTokens: number;
trustScore: number;
benchmarkScore: number;
stars: number | null;
lastIndexedAt: Date | null;
createdAt: Date;
updatedAt: Date;
constructor(props: RepositoryDtoProps) {
this.id = props.id;
this.title = props.title;
this.description = props.description;
this.source = props.source;
this.sourceUrl = props.sourceUrl;
this.branch = props.branch;
this.state = props.state;
this.totalSnippets = props.totalSnippets;
this.totalTokens = props.totalTokens;
this.trustScore = props.trustScore;
this.benchmarkScore = props.benchmarkScore;
this.stars = props.stars;
this.lastIndexedAt = props.lastIndexedAt;
this.createdAt = props.createdAt;
this.updatedAt = props.updatedAt;
}
}

View File

@@ -0,0 +1,54 @@
import { Repository } from '$lib/server/models/repository.js';
import { RepositoryVersion } from '$lib/server/models/repository-version.js';
import { Snippet } from '$lib/server/models/snippet.js';
export interface SnippetRepositoryRefProps {
id: string;
title: string;
}
export class SnippetRepositoryRef {
id: string;
title: string;
constructor(props: SnippetRepositoryRefProps) {
this.id = props.id;
this.title = props.title;
}
}
export interface SnippetSearchResultProps {
snippet: Snippet;
score: number;
repository: SnippetRepositoryRef;
}
export class SnippetSearchResult {
snippet: Snippet;
score: number;
repository: SnippetRepositoryRef;
constructor(props: SnippetSearchResultProps) {
this.snippet = props.snippet;
this.score = props.score;
this.repository = props.repository;
}
}
export interface LibrarySearchResultProps {
repository: Repository;
versions: RepositoryVersion[];
score: number;
}
export class LibrarySearchResult {
repository: Repository;
versions: RepositoryVersion[];
score: number;
constructor(props: LibrarySearchResultProps) {
this.repository = props.repository;
this.versions = props.versions;
this.score = props.score;
}
}

View File

@@ -0,0 +1,83 @@
export interface SnippetEntityProps {
id: string;
document_id: string;
repository_id: string;
version_id: string | null;
type: 'code' | 'info';
title: string | null;
content: string;
language: string | null;
breadcrumb: string | null;
token_count: number | null;
created_at: number;
}
export class SnippetEntity {
id: string;
document_id: string;
repository_id: string;
version_id: string | null;
type: 'code' | 'info';
title: string | null;
content: string;
language: string | null;
breadcrumb: string | null;
token_count: number | null;
created_at: number;
constructor(props: SnippetEntityProps) {
this.id = props.id;
this.document_id = props.document_id;
this.repository_id = props.repository_id;
this.version_id = props.version_id;
this.type = props.type;
this.title = props.title;
this.content = props.content;
this.language = props.language;
this.breadcrumb = props.breadcrumb;
this.token_count = props.token_count;
this.created_at = props.created_at;
}
}
export interface SnippetProps {
id: string;
documentId: string;
repositoryId: string;
versionId: string | null;
type: 'code' | 'info';
title: string | null;
content: string;
language: string | null;
breadcrumb: string | null;
tokenCount: number | null;
createdAt: Date;
}
export class Snippet {
id: string;
documentId: string;
repositoryId: string;
versionId: string | null;
type: 'code' | 'info';
title: string | null;
content: string;
language: string | null;
breadcrumb: string | null;
tokenCount: number | null;
createdAt: Date;
constructor(props: SnippetProps) {
this.id = props.id;
this.documentId = props.documentId;
this.repositoryId = props.repositoryId;
this.versionId = props.versionId;
this.type = props.type;
this.title = props.title;
this.content = props.content;
this.language = props.language;
this.breadcrumb = props.breadcrumb;
this.tokenCount = props.tokenCount;
this.createdAt = props.createdAt;
}
}