feat: implement blood glucose tracking form and API with MariaDB database support

This commit is contained in:
jeremy bayse
2026-04-27 11:59:25 +02:00
parent 8df7a5ace8
commit f0ff0ce268
9 changed files with 185 additions and 471 deletions

Binary file not shown.

View File

@@ -4,7 +4,7 @@ generator client {
}
datasource db {
provider = "sqlite"
provider = "mysql"
}
// ─── Auth ────────────────────────────────────────────────────────────────────
@@ -45,7 +45,7 @@ model Reading {
measuredAt DateTime
moment String
value Float
notes String?
notes String? @db.Text
createdAt DateTime @default(now())
@@index([userId, measuredAt])
@@ -71,7 +71,7 @@ model DailyAnalysis {
id String @id @default(cuid())
userId String @unique
user User @relation(fields: [userId], references: [id], onDelete: Cascade)
content String
content String @db.Text
generatedAt DateTime @default(now())
}

View File

@@ -1,19 +1,26 @@
import path from "node:path";
import { PrismaClient } from "../src/generated/prisma/client";
import { PrismaBetterSqlite3 } from "@prisma/adapter-better-sqlite3";
import { PrismaClient } from "../src/generated/prisma";
import { PrismaMariaDb } from "@prisma/adapter-mariadb";
import bcrypt from "bcryptjs";
import "dotenv/config";
const databaseUrl =
process.env.DATABASE_URL ?? `file:${path.resolve("prisma/dev.db")}`;
const prisma = new PrismaClient({
adapter: new PrismaBetterSqlite3({ url: databaseUrl }),
// Configuration de l'adaptateur pour le script
const adapter = new PrismaMariaDb({
host: "localhost",
port: 3306,
user: "root",
password: "",
database: "diabetix_001",
});
const prisma = new PrismaClient({ adapter });
const moments = [
{ key: "FASTING", hour: 7, baseMin: 0.85, baseMax: 1.25 },
{ key: "LUNCH", hour: 14, baseMin: 1.05, baseMax: 1.75 },
{ key: "DINNER", hour: 21, baseMin: 1.0, baseMax: 1.7 },
{ key: "BEFORE_LUNCH", hour: 11, baseMin: 0.8, baseMax: 1.2 },
{ key: "AFTER_LUNCH", hour: 14, baseMin: 1.1, baseMax: 1.8 },
{ key: "BEFORE_DINNER", hour: 18, baseMin: 0.8, baseMax: 1.2 },
{ key: "AFTER_DINNER", hour: 21, baseMin: 1.1, baseMax: 1.8 },
{ key: "BEDTIME", hour: 23, baseMin: 1.0, baseMax: 1.4 },
] as const;
function rand(min: number, max: number) {
@@ -21,31 +28,74 @@ function rand(min: number, max: number) {
}
async function main() {
await prisma.reading.deleteMany();
console.log("🌱 Début du seeding avec nouveaux moments...");
// 1. Nettoyage
await prisma.reading.deleteMany();
await prisma.patient.deleteMany();
await prisma.user.deleteMany();
// 2. Création de l'utilisateur de test
const passwordHash = await bcrypt.hash("password123", 10);
const user = await prisma.user.create({
data: {
email: "jeremy@test.com",
name: "Jeremy",
passwordHash,
plan: "PREMIUM",
},
});
await prisma.patient.create({
data: {
userId: user.id,
firstName: "Jeremy",
lastName: "Test",
diabetesType: "TYPE1",
treatment: "Insuline",
birthDate: new Date("1990-01-01"),
},
});
// 3. Génération de 30 jours de relevés réalistes
const today = new Date();
today.setHours(0, 0, 0, 0);
const data: { measuredAt: Date; moment: string; value: number; notes: string | null }[] = [];
const data: { userId: string; measuredAt: Date; moment: string; value: number; notes: string | null }[] = [];
for (let day = 29; day >= 0; day--) {
for (const m of moments) {
// Pour simuler la vie réelle, on ne prend pas tous les relevés chaque jour
if (m.key !== "FASTING" && Math.random() < 0.3) continue;
const d = new Date(today.getTime() - day * 86_400_000);
d.setHours(m.hour, Math.floor(Math.random() * 30), 0, 0);
d.setHours(m.hour, Math.floor(Math.random() * 45), 0, 0);
let value = rand(m.baseMin, m.baseMax);
// Quelques valeurs aberrantes pour montrer hypo / hyper
if (Math.random() < 0.05) value = rand(0.5, 0.68);
if (Math.random() < 0.05) value = rand(1.85, 2.4);
// Quelques valeurs aberrantes
if (Math.random() < 0.04) value = rand(0.5, 0.68);
if (Math.random() < 0.04) value = rand(1.85, 2.3);
data.push({
userId: user.id,
measuredAt: d,
moment: m.key,
value: Math.round(value * 100) / 100,
notes: Math.random() < 0.15 ? "Bonne forme" : null,
notes: null,
});
}
}
await prisma.reading.createMany({ data });
console.log(`✓ Base MySQL synchronisée avec les nouveaux moments.`);
console.log(`${data.length} relevés générés.`);
console.log("✅ Seeding terminé !");
}
main().finally(() => prisma.$disconnect());
main()
.catch((e) => {
console.error(e);
process.exit(1);
})
.finally(async () => {
await prisma.$disconnect();
});