This series of articles focuses on building a full-stack app with the following technology stack: PlanetScale - Prisma - tRPC - React. The project name is mythica and it will allow user to collect random mythical creatures. Here is the full list of the articles:
Table of contents
Open Table of contents
Adding Seed Data
This tutorial is going to be a short one. We will be seeding our PlanetScale database with the data necessary to develop our React application
1. Creating and exporting our data
Under server/prisma
folder, let’s create two new files data.ts
.
The data.ts
file will export our seeding json, and will look like the following:
const { Prisma } = require('@prisma/client');
export const creatures = [
...
];
You can find the list of creatures here
2. Creating seeding script
Under server/prisma
, we can create a script inside seed.ts
to populate our MySQL database with our data using Prisma ORM like so:
import { PrismaClient } from "@prisma/client";
import { creatures } from "./data";
const prisma = new PrismaClient();
const seed = async () => {
try {
const result = await prisma.creature.createMany({
data: creatures,
});
} catch (e) {
} finally {
await prisma.$disconnect();
}
};
seed();
3. Adding seed command
Finally, we will need to add another script to our package.json
in our server
directory
"scripts": {
"test": "echo \"Error: no test specified\" && exit 1",
"dev": "npx ts-node-dev ./src/main.ts",
"seed": "ts-node prisma/seed.ts"
},
Now we can execute this script by running
npm run seed --workspace=server
and that is it we can now check our database to find out that it has been seeded.
Conclusion
In this tutorial, we have learned how to quickly seed our database. Next, we will be working on building the frontend using React and we will learn how to use React and React Query to work with trpc
.