0
0
NestJSframework~10 mins

Prisma setup in NestJS - Step-by-Step Execution

Choose your learning style9 modes available
Concept Flow - Prisma setup in NestJS
Install Prisma CLI
Initialize Prisma
Define schema.prisma
Generate Prisma Client
Create PrismaService in NestJS
Inject PrismaService in modules
Use PrismaClient for DB operations
This flow shows the steps to set up Prisma in a NestJS project, from installation to using the Prisma client in services.
Execution Sample
NestJS
npm install @prisma/client prisma
npx prisma init
// edit schema.prisma
npx prisma generate
// create prisma.service.ts
// inject PrismaService in app.module.ts
This code installs Prisma, initializes it, generates the client, and sets up the PrismaService in NestJS.
Execution Table
StepActionResultNotes
1Run npm install @prisma/client prismaPrisma packages installedPackages ready for use
2Run npx prisma initCreates prisma/ folder with schema.prismaSchema file created for DB models
3Edit schema.prismaDefine data modelsModels describe DB tables
4Run npx prisma generateGenerates Prisma ClientClient code to query DB
5Create PrismaService classService wraps PrismaClientAllows injection in NestJS
6Add PrismaService to module providersService available for injectionNestJS DI setup
7Inject PrismaService in other servicesUse PrismaClient methodsPerform DB operations
8Application runs with PrismaDB queries workSetup complete
💡 All steps done, Prisma is set up and ready in NestJS
Variable Tracker
VariableStartAfter Step 2After Step 4After Step 5Final
prismaClientundefinedundefinedGenerated PrismaClient instanceWrapped in PrismaServiceInjected and used in services
schema.prismanoneCreated empty schemaEdited with modelsUsed to generate clientDefines DB structure
Key Moments - 3 Insights
Why do we run 'npx prisma generate' after editing schema.prisma?
Because the Prisma Client code must be regenerated to reflect the updated data models in schema.prisma, as shown in execution_table step 4.
How does PrismaService help in NestJS?
PrismaService wraps the PrismaClient and allows it to be injected via NestJS's dependency injection system, making it easy to use Prisma in other services (see steps 5 and 6).
What happens if you forget to add PrismaService to module providers?
NestJS won't know how to inject PrismaService, causing errors when other services try to use it, as indicated in step 6.
Visual Quiz - 3 Questions
Test your understanding
Look at the execution table, at which step is the Prisma Client generated?
AStep 4
BStep 3
CStep 5
DStep 2
💡 Hint
Check the 'Result' column for 'Generates Prisma Client' in the execution_table.
According to variable_tracker, what is the state of prismaClient after step 5?
AUndefined
BGenerated PrismaClient instance
CWrapped in PrismaService
DInjected and used in services
💡 Hint
Look at the 'prismaClient' row under 'After Step 5' in variable_tracker.
If you skip adding PrismaService to module providers, what will likely happen?
APrismaClient will still work without errors
BNestJS will throw an injection error
Cschema.prisma will not be created
DPrisma Client will not generate
💡 Hint
Refer to key_moments about forgetting to add PrismaService to providers.
Concept Snapshot
Prisma setup in NestJS:
1. Install @prisma/client and prisma packages.
2. Run 'npx prisma init' to create schema.prisma.
3. Define your data models in schema.prisma.
4. Run 'npx prisma generate' to create Prisma Client.
5. Create PrismaService wrapping PrismaClient.
6. Add PrismaService to module providers.
7. Inject PrismaService in services to query DB.
Full Transcript
To set up Prisma in NestJS, first install the Prisma packages. Then initialize Prisma to create the schema.prisma file. Edit this file to define your database models. Next, generate the Prisma Client code that will let you query the database. Create a PrismaService class that wraps the PrismaClient instance, so you can inject it using NestJS's dependency injection system. Add this service to your module's providers array. Finally, inject PrismaService into other services where you want to perform database operations. This setup allows your NestJS app to communicate with the database easily using Prisma.