feat: add Prisma schema, initial migration and idempotent seed

Model the whole domain: settings, the reference week, dated exceptions,
vacation periods, the public-holiday cache, display messages, the
translation cache, the audit log, and the device tables the BYOS server
needs (Device, DeviceLog, SyncState).

Two modelling decisions are load-bearing and documented in the schema:

- Opening times are wall-clock "HH:mm" strings in the shop timezone,
  never instants. Nothing is stored in UTC, which turns the March and
  October daylight-saving switches into non-events instead of edge cases.
- VacationPeriod is a source of truth, never expanded into
  ScheduleException rows. The resolver reads it directly at priority
  rank 2, so a holiday sync can never overwrite a manual exception and
  editing a period leaves no orphans behind.

Device access tokens are stored only as SHA-256 digests, and the image
filename column holds a content hash: the firmware skips the redraw when
the filename is unchanged, which is where the battery life comes from.

Prisma 7 no longer accepts the connection URL in the schema file, so it
moves to prisma.config.ts with the pg driver adapter.

The dev Postgres service lands here rather than with the rest of the
Docker work, because the migration needs a database to run against.

Co-Authored-By: Claude Opus 5 <noreply@anthropic.com>
Claude-Session: https://claude.ai/code/session_012cSY9pVhZmJUKNN7wf1Myd
This commit is contained in:
2026-09-20 17:33:13 +02:00
co-authored by Claude Opus 5
parent a9809f83ca
commit 42ce09100e
12 changed files with 807 additions and 9 deletions
+75
View File
@@ -0,0 +1,75 @@
import 'dotenv/config';
import { PrismaPg } from '@prisma/adapter-pg';
import { PrismaClient } from '../lib/generated/prisma/client';
const connectionString = process.env.DATABASE_URL;
if (!connectionString) {
throw new Error('DATABASE_URL is not set');
}
const prisma = new PrismaClient({ adapter: new PrismaPg({ connectionString }) });
/**
* Placeholder reference week, to be corrected from /admin/horaires on first use.
* Wednesday carries a lunch break on purpose: it exercises the multi-slot path
* of the resolver from the very first run, rather than leaving it untested
* against real data until someone happens to need it.
*
* dayOfWeek follows JS getDay(): 0 = Sunday .. 6 = Saturday.
*/
const WEEK: { dayOfWeek: number; isClosed: boolean; slots: { open: string; close: string }[] }[] = [
{ dayOfWeek: 0, isClosed: true, slots: [] },
{ dayOfWeek: 1, isClosed: true, slots: [] },
{ dayOfWeek: 2, isClosed: false, slots: [{ open: '10:00', close: '18:30' }] },
{
dayOfWeek: 3,
isClosed: false,
slots: [
{ open: '10:00', close: '13:00' },
{ open: '14:00', close: '18:30' },
],
},
{ dayOfWeek: 4, isClosed: false, slots: [{ open: '10:00', close: '18:30' }] },
{ dayOfWeek: 5, isClosed: false, slots: [{ open: '10:00', close: '18:30' }] },
{ dayOfWeek: 6, isClosed: false, slots: [{ open: '10:00', close: '18:30' }] },
];
async function main(): Promise<void> {
// Every write below is an upsert: running the seed twice must leave the
// database exactly as running it once did.
const settings = await prisma.settings.upsert({
where: { id: 'singleton' },
update: {},
create: { id: 'singleton' },
});
for (const day of WEEK) {
await prisma.weeklySchedule.upsert({
where: { dayOfWeek: day.dayOfWeek },
update: {},
create: { dayOfWeek: day.dayOfWeek, isClosed: day.isClosed, slots: day.slots },
});
}
await prisma.syncState.upsert({
where: { key: 'holidays' },
update: {},
create: { key: 'holidays' },
});
const openDays = WEEK.filter((day) => !day.isClosed).length;
console.log(`Seeded settings for "${settings.shopName}" and ${openDays} open days.`);
console.log('Adjust the reference week from /admin/horaires.');
}
main()
.catch((error: unknown) => {
console.error(error);
process.exitCode = 1;
})
.finally(() => {
void prisma.$disconnect();
});