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

@@ -4,7 +4,10 @@
*/
import type Database from 'better-sqlite3';
import type { Repository, NewRepository, IndexingJob, NewIndexingJob } from '$lib/types';
import { RepositoryMapper } from '$lib/server/mappers/repository.mapper.js';
import { IndexingJobMapper } from '$lib/server/mappers/indexing-job.mapper.js';
import { Repository, RepositoryEntity } from '$lib/server/models/repository.js';
import { IndexingJob, IndexingJobEntity } from '$lib/server/models/indexing-job.js';
import { resolveGitHubId, resolveLocalId } from '$lib/server/utils/id-resolver';
import {
AlreadyExistsError,
@@ -47,16 +50,18 @@ export class RepositoryService {
const offset = options?.offset ?? 0;
if (options?.state) {
return this.db
const rows = this.db
.prepare(
`SELECT * FROM repositories WHERE state = ? ORDER BY created_at DESC LIMIT ? OFFSET ?`
)
.all(options.state, limit, offset) as Repository[];
.all(options.state, limit, offset) as RepositoryEntity[];
return rows.map((row) => RepositoryMapper.fromEntity(new RepositoryEntity(row)));
}
return this.db
const rows = this.db
.prepare(`SELECT * FROM repositories ORDER BY created_at DESC LIMIT ? OFFSET ?`)
.all(limit, offset) as Repository[];
.all(limit, offset) as RepositoryEntity[];
return rows.map((row) => RepositoryMapper.fromEntity(new RepositoryEntity(row)));
}
/**
@@ -79,10 +84,10 @@ export class RepositoryService {
* Get a single repository by ID.
*/
get(id: string): Repository | null {
return (
(this.db.prepare(`SELECT * FROM repositories WHERE id = ?`).get(id) as Repository | undefined) ??
null
);
const row = this.db.prepare(`SELECT * FROM repositories WHERE id = ?`).get(id) as
| RepositoryEntity
| undefined;
return row ? RepositoryMapper.fromEntity(new RepositoryEntity(row)) : null;
}
/**
@@ -126,24 +131,25 @@ export class RepositoryService {
}
const now = Math.floor(Date.now() / 1000);
const repo: Record<string, unknown> = {
const repo = new Repository({
id,
title,
description: input.description ?? null,
source: input.source,
source_url: input.sourceUrl,
sourceUrl: input.sourceUrl,
branch: input.branch ?? 'main',
state: 'pending',
total_snippets: 0,
total_tokens: 0,
trust_score: 0,
benchmark_score: 0,
totalSnippets: 0,
totalTokens: 0,
trustScore: 0,
benchmarkScore: 0,
stars: null,
github_token: input.githubToken ?? null,
last_indexed_at: null,
created_at: now,
updated_at: now
};
githubToken: input.githubToken ?? null,
lastIndexedAt: null,
createdAt: new Date(now * 1000),
updatedAt: new Date(now * 1000)
});
const entity = RepositoryMapper.toEntity(repo);
this.db
.prepare(
@@ -154,22 +160,22 @@ export class RepositoryService {
VALUES (?, ?, ?, ?, ?, ?, ?, ?, ?, ?, ?, ?, ?, ?, ?, ?)`
)
.run(
repo.id,
repo.title,
repo.description,
repo.source,
repo.source_url,
repo.branch,
repo.state,
repo.total_snippets,
repo.total_tokens,
repo.trust_score,
repo.benchmark_score,
repo.stars,
repo.github_token,
repo.last_indexed_at,
repo.created_at,
repo.updated_at
entity.id,
entity.title,
entity.description,
entity.source,
entity.source_url,
entity.branch,
entity.state,
entity.total_snippets,
entity.total_tokens,
entity.trust_score,
entity.benchmark_score,
entity.stars,
entity.github_token,
entity.last_indexed_at,
entity.created_at,
entity.updated_at
);
return this.get(id)!;
@@ -274,24 +280,37 @@ export class RepositoryService {
WHERE repository_id = ? AND status IN ('queued', 'running')
ORDER BY created_at DESC LIMIT 1`
)
.get(repositoryId) as IndexingJob | undefined;
.get(repositoryId) as IndexingJobEntity | undefined;
if (runningJob) return runningJob;
if (runningJob) return IndexingJobMapper.fromEntity(new IndexingJobEntity(runningJob));
const now = Math.floor(Date.now() / 1000);
const job: Record<string, unknown> = {
const job = new IndexingJob({
id: crypto.randomUUID(),
repository_id: repositoryId,
version_id: versionId ?? null,
repositoryId,
versionId: versionId ?? null,
status: 'queued',
progress: 0,
total_files: 0,
processed_files: 0,
totalFiles: 0,
processedFiles: 0,
error: null,
startedAt: null,
completedAt: null,
createdAt: new Date(now * 1000)
});
const entity = new IndexingJobEntity({
id: job.id,
repository_id: job.repositoryId,
version_id: job.versionId,
status: job.status,
progress: job.progress,
total_files: job.totalFiles,
processed_files: job.processedFiles,
error: job.error,
started_at: null,
completed_at: null,
created_at: now
};
});
this.db
.prepare(
@@ -301,21 +320,22 @@ export class RepositoryService {
VALUES (?, ?, ?, ?, ?, ?, ?, ?, ?, ?, ?)`
)
.run(
job.id,
job.repository_id,
job.version_id,
job.status,
job.progress,
job.total_files,
job.processed_files,
job.error,
job.started_at,
job.completed_at,
job.created_at
entity.id,
entity.repository_id,
entity.version_id,
entity.status,
entity.progress,
entity.total_files,
entity.processed_files,
entity.error,
entity.started_at,
entity.completed_at,
entity.created_at
);
return this.db
const created = this.db
.prepare(`SELECT * FROM indexing_jobs WHERE id = ?`)
.get(job.id) as IndexingJob;
.get(job.id) as IndexingJobEntity;
return IndexingJobMapper.fromEntity(new IndexingJobEntity(created));
}
}