Files
adventure/packages/db/src/seed.ts
Zaine 8cff315496
Some checks failed
CI / test (push) Has been cancelled
change
2026-07-03 16:26:54 +01:00

166 lines
5.2 KiB
TypeScript
Executable File

import { db } from "./index";
import {
adventureItems,
adventureTemplates,
spiritualConfig,
userProgress,
users,
settings,
} from "./schema";
const DEFAULT_PRAYER = [
"Morning",
"Midday",
"Evening",
"Rosary",
"Examen",
];
const DEFAULT_LITANIES = [
"Litany 1",
"Litany 2",
"Litany 3",
"Litany 4",
"Litany 5",
"Litany 6",
];
function itemConfig(
label: string,
config: Record<string, unknown> = {}
): Record<string, unknown> {
const semanticByLabel: Record<string, string> = {
Work: "work",
Exercise: "exercise",
Prayer: "prayer",
Litanies: "litanies",
Teaching: "teaching",
"Class 1": "class",
"Class 2": "class",
};
const semanticKey = semanticByLabel[label];
return semanticKey ? { ...config, semanticKey } : config;
}
async function seed() {
const existing = await db.select().from(users).limit(1);
if (existing.length > 0) {
console.log("User already exists, skipping seed.");
return;
}
const [user] = await db
.insert(users)
.values({ displayName: "Traveler" })
.returning();
await db.insert(userProgress).values({ userId: user.id });
await db.insert(spiritualConfig).values({
userId: user.id,
prayerLabels: DEFAULT_PRAYER,
litanyLabels: DEFAULT_LITANIES,
});
await db.insert(settings).values([
{ userId: user.id, key: "weekly_reading_goal", value: 50 },
{ userId: user.id, key: "notifications_enabled", value: false },
{ userId: user.id, key: "sounds_enabled", value: false },
{ userId: user.id, key: "theme", value: "minimal-dark" },
]);
const templates = [
{
name: "Weekday",
daysOfWeek: [1, 2, 3, 4, 5],
isDefault: true,
isSystem: true,
sortPriority: 0,
items: [
{ type: "duration", label: "Work", config: itemConfig("Work", { targetHours: 7.5 }), sortOrder: 0 },
{ type: "checkbox", label: "Exercise", config: itemConfig("Exercise"), sortOrder: 1 },
{ type: "reading", label: "Read", config: {}, sortOrder: 2 },
{ type: "timeblock", label: "Class 1", config: itemConfig("Class 1", { scheduledTime: "18:00" }), sortOrder: 3 },
{ type: "timeblock", label: "Class 2", config: itemConfig("Class 2", { scheduledTime: "20:00" }), sortOrder: 4 },
{ type: "checklist", label: "Prayer", config: itemConfig("Prayer", { checklistSize: 5 }), sortOrder: 5 },
{ type: "checklist", label: "Litanies", config: itemConfig("Litanies", { checklistSize: 6 }), sortOrder: 6 },
{ type: "note", label: "Notes", config: {}, sortOrder: 7 },
],
},
{
name: "Saturday",
daysOfWeek: [6],
isDefault: false,
isSystem: true,
sortPriority: 0,
items: [
{ type: "checkbox", label: "Exercise", config: itemConfig("Exercise"), sortOrder: 0 },
{ type: "reading", label: "Read", config: {}, sortOrder: 1 },
{ type: "checklist", label: "Prayer", config: itemConfig("Prayer", { checklistSize: 5 }), sortOrder: 2 },
{ type: "checklist", label: "Litanies", config: itemConfig("Litanies", { checklistSize: 6 }), sortOrder: 3 },
{ type: "note", label: "Notes", config: {}, sortOrder: 4 },
],
},
{
name: "Sunday",
daysOfWeek: [0],
isDefault: false,
isSystem: true,
sortPriority: 0,
items: [
{ type: "reading", label: "Read", config: {}, sortOrder: 0 },
{ type: "checkbox", label: "Exercise", config: itemConfig("Exercise"), sortOrder: 1 },
{ type: "checklist", label: "Prayer", config: itemConfig("Prayer", { checklistSize: 5 }), sortOrder: 2 },
{ type: "checklist", label: "Litanies", config: itemConfig("Litanies", { checklistSize: 6 }), sortOrder: 3 },
{ type: "note", label: "Notes", config: {}, sortOrder: 4 },
],
},
{
name: "Teaching Day",
daysOfWeek: [2, 4],
isDefault: false,
isSystem: true,
sortPriority: 10,
items: [
{ type: "duration", label: "Work", config: { targetHours: 7.5 }, sortOrder: 0 },
{ type: "checkbox", label: "Teaching", config: itemConfig("Teaching"), sortOrder: 1 },
{ type: "checkbox", label: "Exercise", config: itemConfig("Exercise"), sortOrder: 2 },
{ type: "reading", label: "Read", config: {}, sortOrder: 3 },
{ type: "checklist", label: "Prayer", config: itemConfig("Prayer", { checklistSize: 5 }), sortOrder: 4 },
{ type: "checklist", label: "Litanies", config: itemConfig("Litanies", { checklistSize: 6 }), sortOrder: 5 },
],
},
];
for (const tpl of templates) {
const [template] = await db
.insert(adventureTemplates)
.values({
userId: user.id,
name: tpl.name,
daysOfWeek: tpl.daysOfWeek,
isDefault: tpl.isDefault,
isSystem: tpl.isSystem,
sortPriority: tpl.sortPriority,
})
.returning();
for (const item of tpl.items) {
await db.insert(adventureItems).values({
templateId: template.id,
type: item.type,
label: item.label,
config: item.config,
sortOrder: item.sortOrder,
});
}
}
console.log("Seed complete. User ID:", user.id);
}
seed()
.then(() => process.exit(0))
.catch((err) => {
console.error(err);
process.exit(1);
});