Prisma is self-described as the next-generation ORM and I think that statement is true. If you haven't heard of Prisma or have doubts about using it in your projects let me give you 3 reasons why Prisma is awesome.
The Prisma Schema
I have worked with other ORMs in the past, and I not going to say that they are incredibly hard or unusable, current ORM solutions are OK, my main complaint is defining clear relations between models and lack of clarity of data types. Here is were Prisma comes in, the Prisma schema is so clear that you will be up and ready in no time.
You want to use SQLite for this project, just define it on the schema, that's not a problem:
datasource db {
provider = "sqlite"
url = "file:./app.db"
}
You want to use PostgreSQL instead, prisma has you covered:
datasource db {
provider = "postgresql"
url = "postgresql://USER:PASSWORD@HOST:PORT/DATABASE"
}
The tedious task of defining relations between models becomes just a breeze using Prisma schema:
- One to many, in this example we are defining a user with many posts
model User {
id Int @default(autoincrement()) @id
email String @unique
posts Post[]
}
model Post {
id Int @default(autoincrement()) @id
title String
author User? @relation(fields: [authorId], references: [id])
authorId Int?
}
- One to One, in this example we are defining a user with a profile
model User {
id Int @id @default(autoincrement())
profile Profile?
}
model Profile {
id Int @id @default(autoincrement())
user User @relation(fields: [userId], references: [id])
userId Int
}
- Many to many, in this example we are defining that an item can have many categories
model Item {
id Int @id @default(autoincrement())
categories Category[]
}
model Category {
id Int @id @default(autoincrement())
items Item[]
}
Prisma is still in the early stages of development, some field types are not yet defined, I suggest you check the documentation to learn more
The Prisma CLI
Prisma has it's own CLI that enables you, the developer, to make hassle-free migrations, and database resets during development. The CLI can also format your prisma.schema file to make it more readable and it has a visual database editor and browser called Prisma Studio that lets you straightforwardly search through your database.
TypeScript support out of the box
Prisma offers you auto-generated types that you can use in your application services, routes, controllers, etc. This greatly improves the development speed, and the developer experience, since all the types autogenerated by Prisma are correlated to the schema file and this allows to make complex queries using all the supporting power of typescript. Let's see an example, let's say that your schema User definition looks something like this:
model User {
id Int @default(autoincrement()) @id
email String @unique
}
Where the ID field is unique as well as the email field, if you import UserWhereUniqueInput
from the @prisma/client, that type will only accept id or/and email as valid properties and you can also import the User type definition directly as a class. Pretty cool, right?
import { PrismaClient, User, UserWhereUniqueInput } from "@prisma/client";
// or import { PrismaClient, User, Prisma } from "@prisma/client";
// on Prisma >= 2.15
const prisma = new PrismaClient();
export const user = async (
userWhereUniqueInput: UserWhereUniqueInput
// or userWhereUniqueInput: Prisma.UserWhereUniqueInput on Prisma >= 2.15
): Promise<User> => {
return prisma.user.findUnique({
where: userWhereUniqueInput,
});
};
Check the docs to see fully functioning examples
- Prisma with Next.js
- Prisma with GraphQL
- Prisma with Apollo
- Prisma with NestJS
- Prisma with Express
- Prisma with hapi
I hope you like to try Prisma, and that you enjoy it. It is a cool piece of software that deserves more love. ❤️
Top comments (0)