Rewrite the data access layer onto Prisma Client

This commit is contained in:
2026-07-31 01:27:18 +02:00
parent 439453be38
commit 3a04da3180
4 changed files with 119 additions and 147 deletions
+10 -10
View File
@@ -1,5 +1,5 @@
import { describe, it, expect, afterAll } from 'vitest';
import { getPool, closePool, buildDatabaseUrl } from '../src/db.js';
import { getPrismaClient, closePrismaClient, buildDatabaseUrl } from '../src/db.js';
import { loadConfig } from '../src/config.js';
describe('buildDatabaseUrl', () => {
@@ -20,25 +20,25 @@ describe('buildDatabaseUrl', () => {
});
});
describe('getPool', () => {
describe('getPrismaClient', () => {
afterAll(async () => {
await closePool();
await closePrismaClient();
});
it('returns a working pool that can run a query', async () => {
it('returns a working client that can run a query', async () => {
const config = loadConfig();
const pool = getPool(config);
const prisma = getPrismaClient(config);
const rows = await pool.query('SELECT 1 AS value');
const rows = await prisma.$queryRaw`SELECT 1 AS value`;
expect(Number(rows[0].value)).toBe(1);
});
it('returns the same pool instance on repeated calls', () => {
it('returns the same client instance on repeated calls', () => {
const config = loadConfig();
const poolA = getPool(config);
const poolB = getPool(config);
const clientA = getPrismaClient(config);
const clientB = getPrismaClient(config);
expect(poolA).toBe(poolB);
expect(clientA).toBe(clientB);
});
});
+29 -29
View File
@@ -1,5 +1,5 @@
import { describe, it, expect, beforeAll, afterAll, beforeEach } from 'vitest';
import { getPool, closePool } from '../../src/db.js';
import { getPrismaClient, closePrismaClient } from '../../src/db.js';
import { loadConfig } from '../../src/config.js';
import {
createJob,
@@ -13,18 +13,18 @@ import {
markCleaned,
} from '../../src/jobs/jobRepository.js';
let pool;
let prisma;
beforeAll(() => {
pool = getPool(loadConfig());
prisma = getPrismaClient(loadConfig());
});
afterAll(async () => {
await closePool();
await closePrismaClient();
});
beforeEach(async () => {
await pool.query('DELETE FROM conversion_jobs');
await prisma.conversionJob.deleteMany();
});
function baseJob(overrides = {}) {
@@ -44,9 +44,9 @@ function baseJob(overrides = {}) {
describe('jobRepository', () => {
it('creates and retrieves a pending job', async () => {
await createJob(pool, baseJob());
await createJob(prisma, baseJob());
const job = await getJobByUuid(pool, '11111111-1111-4111-8111-111111111111');
const job = await getJobByUuid(prisma, '11111111-1111-4111-8111-111111111111');
expect(job.id).toEqual(expect.any(Number));
expect(job.uuid).toBe('11111111-1111-4111-8111-111111111111');
@@ -66,31 +66,31 @@ describe('jobRepository', () => {
});
it('stores and retrieves a numeric quality value', async () => {
await createJob(pool, baseJob({ uuid: '66666666-6666-4666-8666-666666666666', quality: 82 }));
await createJob(prisma, baseJob({ uuid: '66666666-6666-4666-8666-666666666666', quality: 82 }));
const job = await getJobByUuid(pool, '66666666-6666-4666-8666-666666666666');
const job = await getJobByUuid(prisma, '66666666-6666-4666-8666-666666666666');
expect(job.quality).toBe(82);
});
it('returns null for an unknown uuid', async () => {
const job = await getJobByUuid(pool, '22222222-2222-4222-8222-222222222222');
const job = await getJobByUuid(prisma, '22222222-2222-4222-8222-222222222222');
expect(job).toBeNull();
});
it('transitions a job through processing to done', async () => {
await createJob(pool, baseJob());
await createJob(prisma, baseJob());
await markProcessing(pool, (await getJobByUuid(pool, '11111111-1111-4111-8111-111111111111')).id);
const processing = await getJobByUuid(pool, '11111111-1111-4111-8111-111111111111');
await markProcessing(prisma, (await getJobByUuid(prisma, '11111111-1111-4111-8111-111111111111')).id);
const processing = await getJobByUuid(prisma, '11111111-1111-4111-8111-111111111111');
expect(processing.status).toBe('processing');
await markDone(pool, processing.id, {
await markDone(prisma, processing.id, {
outputPath: '11111111-1111-4111-8111-111111111111.webp',
outputMimeType: 'image/webp',
outputSizeBytes: 2048,
conversionDurationSeconds: 1.5,
});
const done = await getJobByUuid(pool, '11111111-1111-4111-8111-111111111111');
const done = await getJobByUuid(prisma, '11111111-1111-4111-8111-111111111111');
expect(done.status).toBe('done');
expect(done.outputPath).toBe('11111111-1111-4111-8111-111111111111.webp');
expect(done.outputMimeType).toBe('image/webp');
@@ -99,29 +99,29 @@ describe('jobRepository', () => {
});
it('marks a job failed with a short message and a separate detailed log', async () => {
await createJob(pool, baseJob());
const created = await getJobByUuid(pool, '11111111-1111-4111-8111-111111111111');
await createJob(prisma, baseJob());
const created = await getJobByUuid(prisma, '11111111-1111-4111-8111-111111111111');
await markFailed(pool, created.id, {
await markFailed(prisma, created.id, {
errorMessage: 'Conversion failed, please try again',
errorLog: 'Error: sharp threw at line 42\n at convert (image.js:10:5)',
});
const job = await getJobByUuid(pool, '11111111-1111-4111-8111-111111111111');
const job = await getJobByUuid(prisma, '11111111-1111-4111-8111-111111111111');
expect(job.status).toBe('failed');
expect(job.errorMessage).toBe('Conversion failed, please try again');
expect(job.errorLog).toBeUndefined();
const errorLog = await getJobErrorLog(pool, created.id);
const errorLog = await getJobErrorLog(prisma, created.id);
expect(errorLog).toBe('Error: sharp threw at line 42\n at convert (image.js:10:5)');
});
it('finds pending jobs oldest first, up to a limit', async () => {
await createJob(pool, baseJob({ uuid: '33333333-3333-4333-8333-333333333333' }));
await createJob(pool, baseJob({ uuid: '44444444-4444-4444-8444-444444444444' }));
await createJob(pool, baseJob({ uuid: '55555555-5555-4555-8555-555555555555' }));
await createJob(prisma, baseJob({ uuid: '33333333-3333-4333-8333-333333333333' }));
await createJob(prisma, baseJob({ uuid: '44444444-4444-4444-8444-444444444444' }));
await createJob(prisma, baseJob({ uuid: '55555555-5555-4555-8555-555555555555' }));
const jobs = await findPendingJobs(pool, 2);
const jobs = await findPendingJobs(prisma, 2);
expect(jobs).toHaveLength(2);
expect(jobs[0].uuid).toBe('33333333-3333-4333-8333-333333333333');
@@ -129,19 +129,19 @@ describe('jobRepository', () => {
});
it('finds expired jobs and allows marking them cleaned without deleting the row', async () => {
await createJob(pool, baseJob({ expiresAt: new Date(Date.now() - 1000) }));
await createJob(prisma, baseJob({ expiresAt: new Date(Date.now() - 1000) }));
const expired = await findExpiredJobs(pool);
const expired = await findExpiredJobs(prisma);
expect(expired).toHaveLength(1);
expect(expired[0].uuid).toBe('11111111-1111-4111-8111-111111111111');
await markCleaned(pool, expired[0].id);
await markCleaned(prisma, expired[0].id);
const stillPresent = await getJobByUuid(pool, '11111111-1111-4111-8111-111111111111');
const stillPresent = await getJobByUuid(prisma, '11111111-1111-4111-8111-111111111111');
expect(stillPresent).not.toBeNull();
expect(stillPresent.cleanedAt).not.toBeNull();
const afterCleaning = await findExpiredJobs(pool);
const afterCleaning = await findExpiredJobs(prisma);
expect(afterCleaning).toHaveLength(0);
});
});