Seed files
What is a seed file? ๐ฑ
Section titled โWhat is a seed file? ๐ฑโSeed files are a way to quickly generate data for your database.
In Prisma, the seed file is located in prisma/seed.ts
. Open this now in your code editor.
Adding categories.
Section titled โAdding categories.โInside your new prisma/seed.js
file, add the following code:
import { PrismaClient } from "@prisma/client";
const prisma = new PrismaClient();
async function seed() { const games = [76 collapsed lines
{ title: "The Legend of Zelda: Breath of the Wild", description: "An open-world adventure game set in the kingdom of Hyrule.", price: 59.99, rating: 4.9, releaseDate: new Date("2017-03-03"), }, { title: "The Witcher 3: Wild Hunt", description: "An action RPG set in a fantasy world full of monsters and magic.", price: 39.99, rating: 4.8, releaseDate: new Date("2015-05-19"), }, { title: "Red Dead Redemption 2", description: "An open-world western adventure game.", price: 59.99, rating: 4.7, releaseDate: new Date("2018-10-26"), }, { title: "God of War", description: "An action-adventure game based on Norse mythology.", price: 49.99, rating: 4.9, releaseDate: new Date("2018-04-20"), }, { title: "Minecraft", description: "A sandbox game where players can build and explore their own worlds.", price: 26.95, rating: 4.8, releaseDate: new Date("2011-11-18"), }, { title: "Fortnite", description: "A battle royale game where players fight to be the last one standing.", price: 0.0, rating: 4.5, releaseDate: new Date("2017-07-25"), }, { title: "Cyberpunk 2077", description: "An open-world RPG set in a dystopian future.", price: 59.99, rating: 4.0, releaseDate: new Date("2020-12-10"), }, { title: "Among Us", description: "A multiplayer game where players work together to complete tasks on a spaceship.", price: 4.99, rating: 4.4, releaseDate: new Date("2018-06-15"), }, { title: "Animal Crossing: New Horizons", description: "A life simulation game where players can build and manage their own island.", price: 59.99, rating: 4.9, releaseDate: new Date("2020-03-20"), }, { title: "Hades", description: "A rogue-like dungeon crawler where players fight their way out of the Underworld.", price: 24.99, rating: 4.9, releaseDate: new Date("2020-09-17"), }, ];
for (const game of games) { await prisma.game.create({ data: game }); }
console.log("๐พ Games created successfully");
const categories = [ { title: "Action", description: "Games that require quick reflexes and hand-eye coordination.", }, { title: "Adventure", description: "Games that involve exploration and puzzle-solving.", }, { title: "RPG", description: "Games that focus on character development and story.", }, { title: "Simulation", description: "Games that simulate real-world activities or systems.", }, { title: "Strategy", description: "Games that require strategic thinking and planning.", }, { title: "Puzzle", description: "Games that challenge players with logic and problem-solving.", }, { title: "Sports", description: "Games that simulate sports or physical activities.", }, { title: "Multiplayer", description: "Games that can be played with multiple players.", }, { title: "Indie", description: "Games developed by independent studios or individuals.", }, { title: "Horror", description: "Games that aim to scare or unsettle players.", }, ];
for (const category of categories) { await prisma.category.create({ data: category }); }
console.log("๐ฎ Categories created successfully");
console.log("Seed data created successfully");}
seed() .catch((e) => { console.error(e); process.exit(1); }) .finally(async () => { await prisma.$disconnect(); });
Linking games to categories
Section titled โLinking games to categoriesโNow that we have our categories in the database, letโs link them to our games.
Add the code below to your prisma/seed.ts
file:
import { PrismaClient } from "@prisma/client";
const prisma = new PrismaClient();
async function seed() {133 collapsed lines
const games = [ { title: "The Legend of Zelda: Breath of the Wild", description: "An open-world adventure game set in the kingdom of Hyrule.", price: 59.99, rating: 4.9, releaseDate: new Date("2017-03-03"), }, { title: "The Witcher 3: Wild Hunt", description: "An action RPG set in a fantasy world full of monsters and magic.", price: 39.99, rating: 4.8, releaseDate: new Date("2015-05-19"), }, { title: "Red Dead Redemption 2", description: "An open-world western adventure game.", price: 59.99, rating: 4.7, releaseDate: new Date("2018-10-26"), }, { title: "God of War", description: "An action-adventure game based on Norse mythology.", price: 49.99, rating: 4.9, releaseDate: new Date("2018-04-20"), }, { title: "Minecraft", description: "A sandbox game where players can build and explore their own worlds.", price: 26.95, rating: 4.8, releaseDate: new Date("2011-11-18"), }, { title: "Fortnite", description: "A battle royale game where players fight to be the last one standing.", price: 0.0, rating: 4.5, releaseDate: new Date("2017-07-25"), }, { title: "Cyberpunk 2077", description: "An open-world RPG set in a dystopian future.", price: 59.99, rating: 4.0, releaseDate: new Date("2020-12-10"), }, { title: "Among Us", description: "A multiplayer game where players work together to complete tasks on a spaceship.", price: 4.99, rating: 4.4, releaseDate: new Date("2018-06-15"), }, { title: "Animal Crossing: New Horizons", description: "A life simulation game where players can build and manage their own island.", price: 59.99, rating: 4.9, releaseDate: new Date("2020-03-20"), }, { title: "Hades", description: "A rogue-like dungeon crawler where players fight their way out of the Underworld.", price: 24.99, rating: 4.9, releaseDate: new Date("2020-09-17"), }, ];
for (const game of games) { await prisma.game.create({ data: game }); }
console.log("๐พ Games created successfully");
const categories = [ { title: "Action", description: "Games that require quick reflexes and hand-eye coordination.", }, { title: "Adventure", description: "Games that involve exploration and puzzle-solving.", }, { title: "RPG", description: "Games that focus on character development and story.", }, { title: "Simulation", description: "Games that simulate real-world activities or systems.", }, { title: "Strategy", description: "Games that require strategic thinking and planning.", }, { title: "Puzzle", description: "Games that challenge players with logic and problem-solving.", }, { title: "Sports", description: "Games that simulate sports or physical activities.", }, { title: "Multiplayer", description: "Games that can be played with multiple players.", }, { title: "Indie", description: "Games developed by independent studios or individuals.", }, { title: "Horror", description: "Games that aim to scare or unsettle players.", }, ];
for (const category of categories) { await prisma.category.create({ data: category }); }
console.log("๐ฎ Categories created successfully");
console.log("๐ Linking each game to a category");
// Fetch games from the database const dbGames = await prisma.game.findMany();
// Fetch categories from the database const dbCategories = await prisma.category.findMany();
// Link each game to a category for (const game of dbGames) { const category = dbCategories[Math.floor(Math.random() * dbCategories.length)]; await prisma.game.update({ where: { id: game.id }, data: { categoryId: category.id }, }); }
console.log("๐ Games and categories linked successfully");
console.log("Seed data created successfully");}
seed() .catch((e) => { console.error(e); process.exit(1); }) .finally(async () => { await prisma.$disconnect(); });
With this in place, we can run the seed file to populate our database with games and categories:
npm run setup
Checking the database
Section titled โChecking the databaseโTo verify that the seed file worked correctly, open the Prisma Studio:
npx prisma studio
You should see a list of games and categories in the database:
Furthermore, if you click into the โGameโ model and scroll to the far right-hand side of the table, you should see the โCategoryโ column populated with the category each game belongs to:
Now that weโve got some data in our database, we can move on to displaying it in our Remix app. ๐