Files
scouthub/scouthub-attivita-be/prisma/seed.ts
T
2026-07-26 03:19:11 +02:00

129 lines
4.2 KiB
TypeScript

import { PrismaClient } from "@prisma/client";
const prisma = new PrismaClient();
const UTENTE_MODIFICA = "MANUALE";
async function main() {
await Promise.all(
[
{ id: "PU", nome: "Pubblicato" },
{ id: "BO", nome: "Bozza" },
{ id: "PR", nome: "Privato" },
{ id: "IA", nome: "In attesa di approvazione" },
].map((stato) =>
prisma.statoAttivita.upsert({
where: { id: stato.id },
update: { nome: stato.nome, utenteModifica: UTENTE_MODIFICA },
create: { ...stato, utenteModifica: UTENTE_MODIFICA },
})
)
);
await Promise.all(
[
{ id: "A", nome: "Attività" },
{ id: "M", nome: "Materiale" },
].map((tipo) =>
prisma.tipoCategoria.upsert({
where: { id: tipo.id },
update: { nome: tipo.nome, utenteModifica: UTENTE_MODIFICA },
create: { ...tipo, utenteModifica: UTENTE_MODIFICA },
})
)
);
await Promise.all(
[
{ id: "TITOLO", nome: "Titolo attività" },
{ id: "PARAGRAFO", nome: "Paragrafo attività" },
].map((tipo) =>
prisma.tipoParagrafo.upsert({
where: { id: tipo.id },
update: { nome: tipo.nome, utenteModifica: UTENTE_MODIFICA },
create: { ...tipo, utenteModifica: UTENTE_MODIFICA },
})
)
);
await Promise.all(
[
{ id: 1, nome: "L/C", inizioEta: 8, fineEta: 10, colore: "#FDD835" },
{ id: 2, nome: "E/G", inizioEta: 11, fineEta: 16, colore: "#43A047" },
{ id: 3, nome: "R/S", inizioEta: 17, fineEta: 21, colore: "#E53935" },
{ id: 4, nome: "Co.Ca.", inizioEta: 22, fineEta: 99, colore: "#8E24AA" },
].map((branca) =>
prisma.branca.upsert({
where: { id: branca.id },
update: { ...branca, utenteModifica: UTENTE_MODIFICA },
create: { ...branca, utenteModifica: UTENTE_MODIFICA },
})
)
);
await Promise.all(
[
{ id: 1, nome: "promessa", inizioMese: 1, fineMese: 3 },
{ id: 2, nome: "campo estivo", inizioMese: 6, fineMese: 9 },
{ id: 3, nome: "campo invernale", inizioMese: 12, fineMese: 1 },
].map((periodo) =>
prisma.periodoAnno.upsert({
where: { id: periodo.id },
update: { ...periodo, utenteModifica: UTENTE_MODIFICA },
create: { ...periodo, utenteModifica: UTENTE_MODIFICA },
})
)
);
const categorieSenzaPadre = [
{ id: 1, nome: "attività", tipoId: "A" },
{ id: 2, nome: "gioco", tipoId: "A" },
{ id: 3, nome: "danza", tipoId: "A" },
];
for (const categoria of categorieSenzaPadre) {
await prisma.categoria.upsert({
where: { id: categoria.id },
update: { ...categoria, padreId: null, utenteModifica: UTENTE_MODIFICA },
create: { ...categoria, utenteModifica: UTENTE_MODIFICA },
});
}
const categorieConPadre = [
{ id: 4, nome: "gioco d'acqua", tipoId: "A", padreId: 2 },
{ id: 5, nome: "gioco notturno", tipoId: "A", padreId: 2 },
{ id: 6, nome: "grande gioco", tipoId: "A", padreId: 2 },
{ id: 7, nome: "torneo", tipoId: "A", padreId: 2 },
{ id: 8, nome: "olimpiadi", tipoId: "A", padreId: 2 },
{ id: 9, nome: "gioco giungla", tipoId: "A", padreId: 2 },
];
for (const categoria of categorieConPadre) {
await prisma.categoria.upsert({
where: { id: categoria.id },
update: { ...categoria, utenteModifica: UTENTE_MODIFICA },
create: { ...categoria, utenteModifica: UTENTE_MODIFICA },
});
}
// Branca/Categoria/PeriodoAnno sopra sono inserite con id espliciti: l'upsert non passa
// mai dal DEFAULT della colonna, quindi la sequence Postgres dell'id non avanza e resta
// disallineata rispetto al MAX(id) reale. Se non la si risincronizza qui, la prima riga
// creata in seguito con id auto-generato (es. una tassonomia proposta da un utente) va in
// conflitto su un id già esistente.
await Promise.all(
["branca", "categoria", "periodo_anno", "materiale"].map((tabella) =>
prisma.$executeRawUnsafe(
`SELECT setval(pg_get_serial_sequence('"${tabella}"', 'id'), COALESCE((SELECT MAX(id) FROM "${tabella}"), 1))`
)
)
);
}
main()
.catch((error) => {
console.error(error);
process.exitCode = 1;
})
.finally(async () => {
await prisma.$disconnect();
});