Seeding NestJs with Prisma And Faker
––– views
•
2 mins
1 Aug 2021
git clone https://github.com/nestjs/typescript-starter.git project cd project yarn yarn add -D prisma npx prisma init
// prisma/scheme.prisma datasource db { provider = "postgresql" url = env("DATABASE_URL") } generator client { provider = "prisma-client-js" } model User { id Int @id @default(autoincrement()) name String email String @unique password String }
yarn prisma generate yarn prisma migate dev init
yarn add -D faker dotenv
// prisma/seed.ts import { PrismaClient } from '@prisma/client'; import * as faker from 'faker'; import * as dotenv from 'dotenv'; const prisma = new PrismaClient(); const fakerUser = (): any => ({ name: faker.name.firstName() + faker.name.lastName(), email: faker.internet.email(), password: faker.internet.password(), }); async function main() { const fakerRounds = 10; dotenv.config(); console.log('Seeding...'); /// --------- Users --------------- for (let i = 0; i < fakerRounds; i++) { await prisma.user.create({ data: fakerUser() }); } }; main() .catch((e) => console.error(e)) .finally(async () => { await prisma.$disconnect(); });
{ ... "scripts":{ ... "seed": "ts-node prisma/seed.ts" } ... }
yarn seed