Throughout my NodeJS journey as a full-stack developer, I've been in contact with many ORMs and Query builders, dealing with databases was always a real pain for me, especially when the actual tooling around most of the ORMs is not clear enough.
And for almost a year now, I've been using what I think is the best ORM available for NodeJS which is Prisma. It is one of the most complete tools for database manipulation that I have ever seen, and I'll be listing some things about it, and hopefully, encourage people to try it out.
Modeling your database with code
Prisma uses a schema file to define your database, so you can write your entire database out of it, without needing to use an abstraction of some kind like classes or decorators, just a simples "model" and Prisma can generate your database with ease. It can also read your already-built database, and extract the models from it, build a schema file that matches the database.
An example would be something like this:
model User {
id Int @id @default(autoincrement())
email String @unique
name String?
posts Post[]
}
model Post {
id Int @id @default(autoincrement())
title String
content String?
published Boolean @default(false)
author User @relation(fields: [authorId], references[id])
authorId Int
}
With this model, you can easily generate a database with two tables, that have relations between them, and in a more "code" fashion, easily understandable.
Automatic client and type generation
Prisma uses a custom client to manage your database queries, which means that each Prisma project has its client built in differently, the client is generated after every database migration that you do, keeping its types matching the schema file.
Prisma also generates a base type definition for every table, so you also don't need to create different type files like DTOs for your application, and use it directly imported from Prisma.
Query Batching
Prisma automatically batches its queries, making it easy to deal with N+1 problems in graphql for example, without the need for additional tools or building custom dataloaders.
Prisma studio
This one is what I think is the coolest feature among the others, and it differentiates Prisma from other libraries.
When using Prisma, you can easily run a CLI command called "prisma studio", and it will read your schema file, and spin up a browser-based database managing toll, making it possible to read all your tables, add and remove content from them, and watch over them in real-time when testing.
For more information and very good documentation about it, just visit prisma.io and try it yourself, Prisma also has many implementations and automation which I didn't mention in this post.
If you like this type of content, feel free to follow me on social media and here on Dev.to, I always try to write about a cool thing that I'm using at the moment on my projects.
Top comments (0)