Understanding Prisma, a beginners guide

Understanding Prisma, a beginners guide

To somebody who is fairly new to programming, the term ORM or Object Relational Mapper can sound quite confusing. I have used Mongoose in my earlier projects, but never really got around to fully understanding what ORM is and how it really fits in the scheme of things.

And amidst all these, you come across a tool like Prisma and see that it's been used all around so much so that it is used commonly as part of mentioning a new tech stack. I must admit that this by far is the only reason that piqued my interest in exploring what Prisma really is and why should I even care to learn. So, in this article, I’m going to help you understand what Prisma is and why you would want to consider using it in your future projects. But before we even start talking about Prisma, let’s try and understand what ORM really is and why it exists.

What is an ORM?

ORM in its simplest terms means software tools that we use to bridge the gap between the programs that we write(using our language of choice) and the underlying database. But then the natural question would be, what is even the need for it? Well, if you choose not to use it, it is still fair but using it makes your life a whole lot easier. why? because it abstracts away a lot of database-related intricacies which otherwise you need to handle yourself, (you see an extra burden).

If you have been programming for a while, you must surely know what SQL statements are, for example below is a SQL statement to create a table.

CREATE TABLE "User" ( id SERIAL PRIMARY KEY);

Now the above statement is simple but in most applications, these statements can get complicated very quickly. So we make use of ORMs to simplify this process of having to write complex queries with our own language of choice utilizing the object-oriented paradigms. In other words, we use our language of choice to interact with our database than using SQL. Moreover, these interactions with the database are not just limited to writing SQL queries for your application, there are numerous other database-related operations that you would have to write on your own. So this is where ORMs come into the picture and help simplify the process.

Pros and Cons of ORM

Is ORM a silver bullet, something that can be utilized everywhere? The answer perhaps is NO and IT DEPENDS. Following are some of the advantages and disadvantages of using ORMs.

Pros

  1. The leverage that ORMs give to developers to be able to avoid writing SQL and still perform queries using their language.
  2. Less context switching having to toggle between your programming language and SQL statements.
  3. It speeds up development time and decreases development costs.
  4. Offers the right match of developer productivity and control.
  5. With most ORM libraries, you get some additional features such as migrations, SEEDS, etc. These additional features provide great developer experience and can manage changes in your data structure which otherwise done manually can be cumbersome and time-consuming.

Cons

  1. There is a steep learning curve when it comes to using ORMs. It might be easy to get off using it quickly but as the application grows you may have to dig deep into using specific constructs.
  2. Depending on certain situations, sometimes you are better off writing complex SQL queries which are more performant than using ORMs.
  3. Sometimes the level of abstraction introduced by an ORM can make debugging your application difficult.
  4. There are also times when the representation the ORM uses to translate between the database and your application might not be completely accurate.
  5. These abstractions that ORMs provide may keep you from getting into the weeds of how the application is actually implemented.

Now that we have a basic understanding of what an ORM is, how it relates to working with databases, and having a brief look at its trade-offs, let's now look at what Prisma is and whether you should consider using it in your future projects.

Refer here to learn about ORMS patterns and more.

What is Prisma ?

Prisma is an open next-generation ORM. Like any other ORM library, it provides a set of tools that enable you to access and interact with your database. Additionally, it provides tons of great new features that offer a great developer experience. Prisma makes it easy for developers to think about their data as plain old objects by offering easy-to-use type-safe APIs.

So how is Prisma different that other ORMs and Query builders ? … well, long story short, it eliminates the major trade-offs that developers face between Productivity and Control(i.e the extend to which developers are in charge of fine-grained intricacies over their database) when working with their database.

Finally, we discuss a high-level overview of various basic Prisma components that will help you quickly grasp the important concepts.

  • Prisma CLI : The Prisma command line interface (CLI) is the primary way to interact with your Prisma project from the command line. It can initialize new project assets, generate Prisma Client, and analyze existing database structures through introspection to automatically create your application models.

  • Prisma schema : This is the main configuration file for your Prisma setup. Following are the three things that you would setup

    1. Data source: Specifies your database connection (via an environment variable)
    2. Generator: Indicates that you want to generate Prisma Client
    3. Data model: Defines your application models
  • Prisma Client : An auto-generated and type-safe query builder. we run the following command to install prisma client.

npm install @prisma/client

Installing the @prisma/client package invokes the prisma generate command, which reads the Prisma schema and generates the Prisma Client code. The code is generated into the node_modules/.prisma/client folder by default.

  • Prisma Migrate : It is a database schema migration tool that enables you to keep the database schema with your Prisma Schema and maintain existing data in your database.

  • Prisma Studio : A visual editor for the data in your database

Closing thoughts

There are times when developing applications, we tend to start using tools that get thrown out at us or we get fascinated with those marketing buzzwords that we rarely step back and try to understand the reason behind why these tools exist and whether at all it is worth dabbling with. My main goal in writing this article is to put the missing pieces together, especially for someone new to databases, and help them provide a high-level overview of concepts behind ORMs, query builders, and of course Prisma.