Seeding NestJs with Prisma And Faker

  • Seeding NestJs with Prisma And Faker

I’ve been working on this college project and I chose NestJs for the backend. You could just Hasura or other BaaS platforms for small projects. But I wanted to learn NestJs.

Note: Usage with other ORMs might differ but will be almost the same because we’ll be using a script.

What you’ll need:

  • NestJs template for existing project setup with prisma as the default ORM

As someone once said.

ez commands got brrrr.

  • For NestJs.
git clone https://github.com/nestjs/typescript-starter.git project
cd project
yarn
yarn add -D prisma
npx prisma init

prisma/schema.prisma

// 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
}
  • env(“DATABASE_URL”) is set in .env file

After that run :

yarn prisma generate
yarn prisma migate dev init
After seeding

Now: onto the actual seeder using faker for generating random data and dotenv to initialize environment variables :

yarn add -D faker dotenv

Now create the script:

// 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();
});
  • Add the seeder to our package.json for ease of use:
{
...
"scripts":{
...
"seed": "ts-node prisma/seed.ts"
}
...
}
  • And then we run the seeder:
yarn seed
seed.ts

✨ That’s it ✨

The preview of the database:

https://imgur.com/pT77pr7.png

Source code : https://github.com/100lvlmaster/nestjs-prisma-seeder-template

(If you’re a beginner) don’t be alarmed at the number of packages because devDependencies aren’t bundled in the production build :]

You can find me at:

https://www.100lvlmaster.in

--

--

--

onw to 10x developer

Love podcasts or audiobooks? Learn on the go with our new app.

Recommended from Medium

What’s New in Angular 10

A group of Developers — Photo by Alex Kotliarskyi on Unsplash

What is Server-Side Rendering?

How recursion works in JavaScript

Server Backend Concepts (NodeJS, ExpressJS)

How to build a Node.js API using Postgres, Lambda and API Gateway

Learn Angular for Back-End Developer (Part 3)-Angular Fundamentals

The main Angular building blocks explained with simple examples

Blog Article 03 — NodeJS

Navigating React Router

Get the Medium app

A button that says 'Download on the App Store', and if clicked it will lead you to the iOS App store
A button that says 'Get it on, Google Play', and if clicked it will lead you to the Google Play store
Navin Kodag

Navin Kodag

onw to 10x developer

More from Medium

Debugging TypeScript using Replay Node

Single repository, multiple NestJS projects

How to use oak_decorators to implement a Nest.js-like Deno application

Web Application Firewall rate-limiting in TypeScript CDK