// The database seed: makes sure the admin account exists, and nothing else. // // npx prisma db seed create the admin account if it is missing // npx prisma db seed -- --reset DESTRUCTIVE: delete every user, campaign, // character, note, etc. first // // It only ever adds, so it is safe against a live database: an existing // account, and its password, is left alone. SEED_ADMIN_EMAIL / // SEED_ADMIN_PASSWORD / SEED_ADMIN_NAME override the account that is created. // // Worlds are not seeded here. A campaign is created in the app, blank or from // one of the world seeds in /backend/seeds by its code (docs/SEEDS.md). // // Species/classes/skills are not rows either: they are fixed game-rule tables // in src/game/data/*.js, as is the library DMs pull weapons, items, spells // and monsters from. const bcrypt = require('bcrypt'); const prisma = require('../src/db'); const { loadSeeds } = require('../src/seeds'); const ADMIN_EMAIL = process.env.SEED_ADMIN_EMAIL || 'joshua@crosscode.co.za'; const ADMIN_PASSWORD = process.env.SEED_ADMIN_PASSWORD || '12345678'; const ADMIN_NAME = process.env.SEED_ADMIN_NAME || 'Joshua'; async function seed({ reset = false } = {}) { if (reset) { // Truncating the two roots cascades through every table that references them. await prisma.$executeRawUnsafe('TRUNCATE TABLE "users", "campaigns" RESTART IDENTITY CASCADE'); console.log('Database reset: every user and campaign was deleted.'); } if (await prisma.user.findUnique({ where: { email: ADMIN_EMAIL } })) { console.log(`Admin account ${ADMIN_EMAIL} already exists — left as it is.`); } else { await prisma.user.create({ data: { email: ADMIN_EMAIL, passwordHash: await bcrypt.hash(ADMIN_PASSWORD, 12), displayName: ADMIN_NAME }, }); console.log(`Admin/DM login created: ${ADMIN_EMAIL} / ${ADMIN_PASSWORD}`); console.log(' Change that password from the account page ("Account" in the navigation) once you are logged in.'); } const worlds = loadSeeds().filter((world) => !world.error); if (worlds.length) { console.log('Worlds to start a campaign from (Campaigns → New Campaign → world code):'); for (const { code, seed: world } of worlds) console.log(` ${code} ${world.campaign.name}`); } } seed({ reset: process.argv.includes('--reset') }) .catch((err) => { console.error(err); process.exitCode = 1; }) .finally(() => prisma.$disconnect());