Repository Pattern Prisma ORM

Muhammad Firdaus Sati
4 min readApr 6, 2022

Object Relational-Mapping (ORM) is a tool where you can interact with your databases by removing the need to use SQL directly. People prefer to use ORM because they doesn’t need to change lot of codes when they need to do some query to their databases. By using ORM, you kinda have more structural code for querying to you database and ’cause of that you have much more cleaner code compare with using SQL directly. Since the way you interact to your databases is being process by your ORM, you as a developer can focus more into the functionality of your back-end and doesn’t need to worry about your query might break randomly. The easier you access your database ’cause of using an ORM, most likely you don’t really know how the stuff is work behind the scene. Of course, it depends on what you want to do on your end, but still knowing the basic thing might help you when you need to use the SQL when your ORM of choice can’t help you out for specific cases.

Prisma ORM

Prisma is an ORM that has a built-in TypeScript support and I’m a TypeScript guy, of course I’m going to use it since I can use TypeScript with it. TypeScript is JavaScript but with types and other features that can help you out or it can’t drive you mad since it kinda strict compare with JavaScript. Having an ORM that support TypeScript will help you out and can reduce the chances that you shipping in production is a broken code. Prisma has a feature where when you create a migration for you database it will auto generate a typescript declaration for your model. Another cool thing is that you can use the Prisma studio to manage your database in your browser. Despite the cool features that Prisma had, there is one problem that most of developer out there doesn’t like, repetitive code.

Credit : u/Kidplayer_666

Prisma is cool but it has a problem or two where the developer itself doesn’t want us the developer to use the repository pattern. In fact, you don’t even need to use the repository pattern if you want your code looks simple and has less “magic” compare with when you use the repository pattern. Not everyone like to use repository pattern some might prefer to use another pattern, but for me I like to use it and here’s how you use repository pattern in Prisma.

Define your model

To start with Prisma, you could read the docs for yourself since we gonna focusing in creating the repository pattern itself.

Simple User Prisma Schema

From the gist above, we define a Prisma model or aTable (if you want to call it that way) that has few properties. Keep that in mind that this properties that we define here will be generated automatically when we create our migrations.

You can rename table to other name if you prefer to hyphenated your model name using the “@@map(name)” decorator, but your model name definition/type in Prisma will still use the name that you define after the word “model” in your prisma schema.

Creating a Repository

You might thinking it’s a bad thing to use “@ts-ignore” in your code and the short answer is yes, it’s a bad thing since its going ignore any kind of error in the line after it. But, we do need to use it since TypeScript will mad at us if you have 2 or more model in your schema, ’cause it’s not compatible with each other. It’s gonna be fine when we start to use it in our repository since we clearly said that what is the model that the we going to use for our repository.

Keep that in mind that every time you add a new model, import the model definition to your base repository file and make sure to add it into the ModelStructure type with a lowercase key. We need to do this so we could use the base repository to any other repository that we will create while working on our projects.

Now, what we had is a model and its schema and a base repository. In our base repository, we have some basic function that mostly we will use when interacting with our models. So we have create, findAll and findOne which just get the data from our specific model, update which just update our rows, and lastly the delete function that just delete an entire rows that meet the condition. If you from Sequelize user, you could see that I use some terms and some action that Sequelize had, but I just use the name in this base repository since the function name itself is much more self explanatory.

User Repository

After we have our base repository, we need to use that base repository for our specific model, so we gonna use that repository for our User model.

From the gist above, we extend the resourceToModel and modelToResource function to suit with our need, in this case is extending it to accept a resource for our User model and exporting the selected rows by removing some column that we doesn’t want to expose.

Congratulation

You just create a repository for your Prisma project. For the filterQueryParams in our base repository, it will accept some sort of filter and I recommend you to use a package that I made on top filter-query-parser that works for Prisma, @krsbx/prisma-fqp.

--

--