Skip to content

Instantly share code, notes, and snippets.

@knezdusan
Last active August 13, 2024 14:15
Show Gist options
  • Save knezdusan/42d9a2825a61fd8cc368b654c9243f4a to your computer and use it in GitHub Desktop.
Save knezdusan/42d9a2825a61fd8cc368b654c9243f4a to your computer and use it in GitHub Desktop.
Prisma ORM - seeding data example
import { Prisma } from '@prisma/client';
import { createId } from '@paralleldrive/cuid2';
// Create clients
export const clients: Prisma.ClientUncheckedCreateInput[] = [
{ id: createId(), name: 'Client One', website: 'https://example1.com', status: "ACTIVE" },
{ id: createId(), name: 'Client Two', website: 'https://example2.com', status: "INACTIVE" },
{ id: createId(), name: 'Client Three', website: 'https://example3.com', status: "TRIAL" },
];
// Create users
export const users: Prisma.UserUncheckedCreateInput[] = [
{ id: createId(), email: 'user1@example.com', name: 'User One', role: 'ADMIN', clientId: clients[0].id as string },
{ id: createId(), email: 'user2@example.com', name: 'User Two', role: 'ADMIN', clientId: clients[1].id as string },
{ id: createId(), email: 'user3@example.com', name: 'User Three', role: 'USER', clientId: clients[1].id as string },
{ id: createId(), email: 'user4@example.com', name: 'User Four', role: 'ADMIN', clientId: clients[2].id as string },
];
// Create profiles
export const profiles: Prisma.ProfileUncheckedCreateInput[] = [
{ id: createId(), bio: 'Bio of User One', userId: users[0].id as string },
{ id: createId(), bio: 'Bio of User Two', userId: users[1].id as string },
{ id: createId(), bio: 'Bio of User Three', userId: users[2].id as string },
];
// Create categories
export const categories: Prisma.CategoryCreateInput[] = [
{ id: createId(), name: 'Category One' },
{ id: createId(), name: 'Category Two' },
{ id: createId(), name: 'Category Three' },
];
// Create posts
export const posts: Prisma.PostUncheckedCreateInput[] = [
{ id: createId(), title: 'Post One', published: true, authorId: users[0].id as string, categories: { connect: [{ id: categories[0].id }, { id: categories[1].id }] } },
{ id: createId(), title: 'Post Two', published: false, authorId: users[1].id as string, categories: { connect: [{ id: categories[0].id }] } },
{ id: createId(), title: 'Post Three', published: true, authorId: users[2].id as string, categories: { connect: [{ id: categories[2].id }] } },
];
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment