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

@@ -6,7 +6,11 @@
*/
import type Database from 'better-sqlite3';
import type { RepositoryVersion } from '$lib/types';
import { RepositoryVersionMapper } from '$lib/server/mappers/repository-version.mapper.js';
import {
RepositoryVersion,
RepositoryVersionEntity
} from '$lib/server/models/repository-version.js';
import { AlreadyExistsError, NotFoundError } from '$lib/server/utils/validation';
export class VersionService {
@@ -16,13 +20,14 @@ export class VersionService {
* List all versions for a repository, newest first.
*/
list(repositoryId: string): RepositoryVersion[] {
return this.db
const rows = this.db
.prepare(
`SELECT * FROM repository_versions
WHERE repository_id = ?
ORDER BY created_at DESC`
)
.all(repositoryId) as RepositoryVersion[];
.all(repositoryId) as RepositoryVersionEntity[];
return rows.map((row) => RepositoryVersionMapper.fromEntity(new RepositoryVersionEntity(row)));
}
/**
@@ -60,9 +65,10 @@ export class VersionService {
)
.run(id, repositoryId, tag, title ?? null, now);
return this.db
const row = this.db
.prepare(`SELECT * FROM repository_versions WHERE id = ?`)
.get(id) as RepositoryVersion;
.get(id) as RepositoryVersionEntity;
return RepositoryVersionMapper.fromEntity(new RepositoryVersionEntity(row));
}
/**
@@ -86,13 +92,12 @@ export class VersionService {
* Returns `null` when not found.
*/
getByTag(repositoryId: string, tag: string): RepositoryVersion | null {
return (
(this.db
.prepare(
`SELECT * FROM repository_versions WHERE repository_id = ? AND tag = ?`
)
.get(repositoryId, tag) as RepositoryVersion | undefined) ?? null
);
const row = this.db
.prepare(
`SELECT * FROM repository_versions WHERE repository_id = ? AND tag = ?`
)
.get(repositoryId, tag) as RepositoryVersionEntity | undefined;
return row ? RepositoryVersionMapper.fromEntity(new RepositoryVersionEntity(row)) : null;
}
/**