Loading...
Transforming complex challenges into strategic digital opportunities through innovation and technical excellence.
From custom platforms to automated systems, Code Scrapper turns complex ideas into seamless digital experiences that make a difference.

To create a NestJS application with Prisma, You'll need to follow several steps to set up Prisma, configure your database, and create the necessary modules, services, and controllers to handle blog posts. I'll guide you through the entire process, including setting up Prisma with MongoDB, configuring NestJS modules, and creating a blogging post feature.
1. Install NestJS and Create a New Project
npm install -g @nestjs/cli nest new project-name cd project-name
2. Install Prisma and Initialize Prisma
Using npm:
npm install @prisma/client npm install -D prisma
Using yarn:
yarn add @prisma/client yarn add -D prisma
Initialize Prisma for mongoDB:
npx prisma init --datasource-provider mongodb
This will create a prisma folder with schema.prisma and a .env file.
Open prisma/schema.prisma and update the datasource block to use MongoDB:
datasource db {
provider = "mongodb"
url = env("DATABASE_URL")
}
generator client {
provider = "prisma-client-js"
}
Update your .env file with the MongoDB connection string:
DATABASE_URL="mongodb://localhost:27017/abc"
Make sure your MongoDB server is running locally or replace the connection string with your remote MongoDB server details.
model User {
id String @id @default(auto()) @map("_id") @db.ObjectId
email String @unique
name String?
posts Post[]
}
model Post {
id String @id @default(auto()) @map("_id") @db.ObjectId
title String
content String
published Boolean @default(false)
authorId String @db.ObjectId
author User @relation(fields: [authorId], references: [id])
createdAt DateTime @default(now())
updatedAt DateTime @updatedAt
}
These models define the basic structure of users and posts in your blogging platform.
Generate the Prisma client, which allows you to interact with the database:
npx prisma generate
Inside the src directory, create a new file prisma.service.ts:
import { Injectable, OnModuleInit } from '@nestjs/common';
import { PrismaClient } from '@prisma/client';
@Injectable()
export class PrismaService extends PrismaClient implements OnModuleInit {
async onModuleInit() {
await this.$connect();
}
}
Inside the src directory, create a new file prisma.module.ts:
import { Module } from '@nestjs/common';
import { PrismaService } from './prisma.service';
@Module({
providers: [PrismaService],
exports: [PrismaService],
})
export class PrismaModule {}
import { Module } from '@nestjs/common';
import { AppController } from './app.controller';
import { AppService } from './app.service';
import { PrismaService } from './prisma.service';
@Module({
imports: [],
controllers: [AppController],
providers: [AppService,PrismaService],
})
export class AppModule {}