Full Stack Development

Seeding and Migrations



by Tomas Trescak t.trescak@westernsydney.edu.au

Prisma

/

Seeding and Migration

  • Empty apps lose users
  • Schema changes break things
  • Misconception: Manual fixes work

Prisma

/

migrate vs db push

  • migrate dev : Versioned updates
  • db push : Instant sync
    // schema.prisma
model Post {
  id        Int      @id @default(autoincrement())
  title     String
  content   String
  createdAt DateTime @default(now())
  tags      String[] // Added later
}
  

Prisma

/

migrate vs db push

Prisma

/

migrate vs db push

Prisma

/

Seeding your App

  • Preload data for users
  • Full control for admins
    import { PrismaClient } from '@prisma/client';

const prisma = new PrismaClient();

async function main() {
  await prisma.post.deleteMany(); // Reset for fresh seed
  await prisma.post.createMany({
    data: [
      { title: "First Post", content: "Hi!", createdAt: new Date("2025-01-01"), tags: ["intro"] },
      { title: "Tech Bit", content: "AI rocks.", createdAt: new Date("2025-02-15"), tags: ["tech"] },
    ],
  });
}

main().then(() => prisma.$disconnect()).catch((e) => { 
  console.error(e); prisma.$disconnect(); process.exit(1); 
});
  

Prisma

/

Seeding and Migrations

  • Key Takeaways : migrate dev for structure, db push for speed, seeding for instant data.
  • Best Practices : Use migrate in teams, test db push carefully, keep seeds realistic.
  • Pitfalls : Don’t mix migrate and push carelessly, avoid untested schema changes.