Experiencing Prisma

Ricardo Almeida
3 min readOct 18, 2020

Creating a new project is the opportunity of evaluating options and exploring different technologies. In Software Development there will be always a gama of tools, languages, frameworks, IDEs, etc. It varies from the database to the frontend, what can be scaring for a developer the idea of picking something new. Decisions are made considering the learning curve and deadlines and usually we stick with the current technology. Going against the flow, the decision favored Prisma.

What is Prisma?

Prisma makes it easy for developers to reason about their database queries by providing a clean and type-safe API for submitting database queries which returns plain old JavaScript objects.

TLDR Documentation

Meeting Prisma

This year I decided to focus my skills on statically typed languages, especially Go and Rust. The first time I heard about Prisma was listening to a Podcast and I got interested knowing that Prisma Client is written in Rust. Rust is very promising because of the performance and memory-safe, build using modern compiler concepts that will be hard to implement in C or C++ due to the legacy existing in these languages. For this reason, we see big companies creating or migrating projects in Rust, and others using Rust internally targeting on performance gains. As examples of this are:

Adoption

To start a discussion with co-workers about a new tool, it’s essential good documentation showing what would be the benefits of choosing the different. The comparison was made between Prisma, TypeORM and Sequelize, which led me to find two docs, helping the comparison.

https://www.prisma.io/docs/understand-prisma/api-comparisons

https://www.prisma.io/dataguide/database-tools/evaluating-type-safety-in-the-top-8-typescript-orms

The most relevant advantages I saw was the following:

  1. Very low learning curve.
  2. Type-safe API.
  3. Easy abstraction. Just JSON, no classes to learn.
  4. Database Migrations.
  5. Database Studio (IDE for the database).

Experience

As the Prisma doc says, it’s not an ORM because it doesn’t provide classes to wrap database operations. Prisma has a simple abstraction to write queries in pure JSON and returns JSON, which is very familiar for Javascript/Typescript developers. Combining it with Typescript type-safe, we have auto-complete and API documentation all-time during development, writing SQL queries in JSON syntax, using Prisma client o call database operations, and so on. There is also a VSCode plugin that colorizes and formats Prisma Schema files and generates code for you when creating associations between models.

Prisma modeling and migration is a fantastic productivity experience. Prisma models are defined in the Prisma Schema file and models are mapped to database tables. The interesting thing is that in your models you define the relations between models, which is not a column in the database but used by Prisma Client instead, giving us the fluent interface in code to access association records. Here we go with an example:

...model Playlist {
id Int @id @default(autoincrement())
description String
tracks PlaylistTrack[]
}
model PlaylistTrack {
id Int @id @default(autoincrement())
addedAt DateTime
playlistId Int
playlist Playlist @relation(fields: [playlistId], references: [id])
trackId Int
}
model Track {
id Int @id @default(autoincrement())
name String
}

The entire file you can find here: Federation Nexus Prisma example.

The Prisma Migrate was also good experience in terms of productivity. Changing tables are as easy as changing this file: the Prisma Schema. After changing it, you need to run Prisma commands, that can be easily configured in your Continuous Delivery and Deployment script:

$ yarn prisma migrate dev
$ yarn prisma migrate deploy

Another powerful tool for development is Prisma Studio, a database IDE. It has action buttons to add, delete, and update records and you can navigate between relations defined in your Prisma Schema file.

Integration Test

It’s always a discussion regarding tests of whether or not to mock the database. I like testing GraphQL endpoints integrated with the database, in order to avoid “false positives” in the test. Using Prisma Migrate script, you can easily configure your CI to create the database and update migrations. See this example using Github Actions.

- run: yarn install
- run: yarn build
- run: yarn prisma migrate deploy
- run: yarn test

Conclusion

You definitely should try Prisma and experience a modern toolkit for databases. It has a low learning curve and productivity is higher combined with end-to-end Typescript type-safe. The community is working hard and you can contribute via Slack Group, attending in Meetups, and contributing to Open Source. You can see my codebase using GraphQL Nexus Prisma for example.

Community Links:

--

--