Prisma
This example shows how to set up your Seed workflow with Prisma (opens in a new tab).
Prerequisites
- Set up a new Prisma project by following the Prisma Quickstart (opens in a new tab) guide.
1. Install Seed
Set up Seed in your Prisma project by running the following command:
2. Configure Seed
Using the previous command should have created a seed.config.ts
configuration file.
You can use it as is, or customize it to fit your needs.
3. Customize the Seed Script
The npx @snaplet/seed init
command should also have created a seed script example seed.ts
.
You can move this file into your prisma
directory and customize it to seed your database with the data you need.
4. Hook into Prisma seeding workflow
You can hook into Prisma seeding workflow (opens in a new tab) by specifying a seed
command in your package.json
file:
With this change, the database will be seeded with the data you specified in your seed.ts
file for each of the following commands:
prisma db seed
prisma migrate dev
prisma migrate reset
5. Keep Seed Client in sync with your database
Whenever your database structure changes (e.g after a new migration is applied), @snaplet/seed
will need be regenerated to reflect the new structure. To do this, run the following command:
If you want to automate this process, you can add a post script (opens in a new tab) to your package.json
file:
Recipes
@map and @@map renaming with Prisma in Snaplet Seed
In Prisma, you have the flexibility to rename tables and columns in the database using the @@map
and @map
annotations. This feature allows developers to map their model and field names in Prisma to different database table and column names. However, when using Snaplet Seed, it's important to note that Snaplet Seed operates directly with database table names, not Prisma model names. Below, we illustrate how to align Snaplet Seed's operations with your Prisma schema through alias overrides.
Default Prisma and Database Configuration
Here's how a typical Prisma model might be set up without any renaming:
model UserProfile { id String @id @default(uuid()) createdAt DateTime @default(now())}
This would correspond to the following SQL table creation:
CREATE TABLE "UserProfile" ( "id" TEXT NOT NULL PRIMARY KEY, "createdAt" DATETIME NOT NULL DEFAULT CURRENT_TIMESTAMP);
And it's used in @snaplet/seed
like this:
await seed.UserProfile((x) => x(10, { createdAt: new Date(),}));
Customizing Table and Column Names
You can customize the names in Prisma using @@map
for tables and @map
for columns:
model UserProfile { id String @id @default(uuid()) createdAt DateTime @default(now()) @map("created_at") @@map("user_profile")}
This reflects the following changes in your SQL schema:
CREATE TABLE "user_profile" ( "id" TEXT NOT NULL PRIMARY KEY, "created_at" DATETIME NOT NULL DEFAULT CURRENT_TIMESTAMP);
Subsequently, Snaplet Seed would access this table as:
await seed.user_profile((x) => x(10, { created_at: new Date(),}));
Aligning Snaplet Seed with Prisma Mappings
To use Prisma-style naming in Snaplet Seed, adjust your seed.config.ts
using alias overrides:
export default defineConfig({ alias: { override: { user_profile: { name: 'UserProfile', fields: { created_at: "createdAt" } } } }, adapter: () => { const client = new PrismaClient(); return new SeedPrisma(client); }, select: ["!*_prisma_migrations"],});
After updating your configuration, run npx @snaplet/seed sync
to apply the changes. Now, you can continue using the Prisma model names and conventions in Snaplet Seed:
await seed.UserProfile((x) => x(10, { createdAt: new Date()}))