How to Use Prisma in Node.js: Simple Guide with Example
To use
Prisma in Node.js, first install the Prisma client and initialize it with your database schema. Then import PrismaClient in your code, create an instance, and use its methods to query or modify your database easily.Syntax
Here is the basic syntax to use Prisma in Node.js:
import { PrismaClient } from '@prisma/client': Imports the Prisma client class.const prisma = new PrismaClient(): Creates a new Prisma client instance to interact with the database.await prisma.modelName.method(): Calls methods on your database models likefindMany,create,update, etc.await prisma.$disconnect(): Closes the database connection when done.
javascript
import { PrismaClient } from '@prisma/client'; const prisma = new PrismaClient(); async function main() { const allRecords = await prisma.modelName.findMany(); console.log(allRecords); } main() .catch(e => console.error(e)) .finally(async () => { await prisma.$disconnect(); });
Example
This example shows how to use Prisma to fetch all users from a database. It demonstrates connecting, querying, and disconnecting properly.
javascript
import { PrismaClient } from '@prisma/client'; const prisma = new PrismaClient(); async function main() { const users = await prisma.user.findMany(); console.log('All users:', users); } main() .catch(e => { console.error(e); process.exit(1); }) .finally(async () => { await prisma.$disconnect(); });
Output
All users: []
Common Pitfalls
Common mistakes when using Prisma in Node.js include:
- Not running
prisma generateafter changing the schema, so the client is outdated. - Forgetting to
await prisma.$disconnect(), which can leave connections open. - Using incorrect model names or methods that don't exist in your schema.
- Not handling async errors properly, causing unhandled promise rejections.
javascript
/* Wrong: Missing prisma.$disconnect() */ import { PrismaClient } from '@prisma/client'; const prisma = new PrismaClient(); async function main() { const users = await prisma.user.findMany(); console.log(users); } main(); /* Right: Proper disconnect and error handling */ async function mainSafe() { try { const users = await prisma.user.findMany(); console.log(users); } catch (e) { console.error(e); } finally { await prisma.$disconnect(); } } mainSafe();
Quick Reference
Here is a quick cheat sheet for common Prisma client methods:
| Method | Description |
|---|---|
| findMany() | Get multiple records from a model |
| findUnique() | Get a single record by unique field |
| create() | Add a new record |
| update() | Modify an existing record |
| delete() | Remove a record |
| $disconnect() | Close the database connection |
Key Takeaways
Install and generate Prisma client after defining your schema.
Create a PrismaClient instance to interact with your database.
Always handle async errors and disconnect the client properly.
Use Prisma methods like findMany, create, update to work with data.
Keep your Prisma schema and client in sync by running prisma generate.