Files
petfeed/prisma/seed.ts
T

117 lines
2.5 KiB
TypeScript

import { PrismaClient } from "@prisma/client";
import { PrismaPg } from "@prisma/adapter-pg";
import { Pool } from "pg";
/**
* Seed script for the PawFeed database.
* Seeds Species (Dog, Cat, Bird) and Breed taxonomy idempotently.
* Uses upsert so it is safe to run multiple times.
*
* Run: npx tsx prisma/seed.ts
* Or: npx prisma db seed
*/
const databaseUrl = process.env.DATABASE_URL;
if (!databaseUrl) {
throw new Error(
"DATABASE_URL is not set. Please add it to .env.local first."
);
}
const adapter = new PrismaPg(new Pool({ connectionString: databaseUrl }));
const prisma = new PrismaClient({ adapter });
const DOG_BREEDS = [
"Labrador Retriever",
"Golden Retriever",
"German Shepherd",
"French Bulldog",
"Bulldog",
"Poodle",
"Beagle",
"Rottweiler",
"German Shorthaired Pointer",
"Dachshund",
"Pembroke Welsh Corgi",
"Australian Shepherd",
"Yorkshire Terrier",
"Boxer",
"Cavalier King Charles Spaniel",
"Doberman Pinscher",
"Great Dane",
"Miniature Schnauzer",
"Siberian Husky",
"Border Collie",
];
const CAT_BREEDS = [
"Domestic Shorthair",
"Domestic Longhair",
"Maine Coon",
"Ragdoll",
"Siamese",
"Persian",
"Bengal",
"British Shorthair",
"Sphynx",
"Scottish Fold",
"American Shorthair",
"Abyssinian",
"Norwegian Forest Cat",
"Birman",
"Russian Blue",
];
const BIRD_BREEDS = [
"Budgerigar (Budgie)",
"Cockatiel",
"Lovebird",
"African Grey Parrot",
"Cockatoo",
"Conure",
"Macaw",
"Canary",
"Finch",
"Amazon Parrot",
];
async function main() {
console.log("Seeding species...");
const speciesData = [
{ name: "Dog", breeds: DOG_BREEDS },
{ name: "Cat", breeds: CAT_BREEDS },
{ name: "Bird", breeds: BIRD_BREEDS },
];
for (const s of speciesData) {
const species = await prisma.species.upsert({
where: { name: s.name },
update: {},
create: { name: s.name },
});
console.log(` Upserted species: ${species.name} (${species.id})`);
console.log(` Seeding ${s.breeds.length} breeds for ${species.name}...`);
for (const breedName of s.breeds) {
const breed = await prisma.breed.upsert({
where: { speciesId_name: { speciesId: species.id, name: breedName } },
update: {},
create: { speciesId: species.id, name: breedName },
});
console.log(` Upserted breed: ${breed.name}`);
}
}
console.log("Seeding complete.");
}
main()
.catch((e) => {
console.error(e);
process.exit(1);
})
.finally(async () => {
await prisma.$disconnect();
});