Sequelize Vs Prisma
Start
Currently at eBay, I am migrating our internal tools from using MongoDB to Postgres. A project that is very difficult but I’m excited to take head-on. While researching, I determined that for our codebase, using an ORM would be best since the codebase was already designed to use Mongoose. But now the challenge is, which ORM would be best for our codebase? In this blog, I will dive into that question. Please note that I can’t disclose everything in this blog due to the nature of my contact with my employer.
Requirements
Based on the requirements for this project, here are my requirements for what this ORM must be/do:
- Be a JavaScript package; since most of the code is written in JavaScript
- Must support Postgres and most of its features
- Its performance must be at least on par or better than Mongoose
- Must be open source and maintained
ORMs
After doing a lot of research, I determined the top three ORMs that fit with the requirements are:
I ultimately decided to just focus on Sequelize and Prisma because I did not have time to fully test three different ORMs due to deadlines.
Testing Environment
For my testing environment, I ran Postgres through Docker. I grabbed our largest and more complex dataset, converted it from a document structure to a table structure, and added it to my local Postgres instance. I converted the dataset by using columns, one-to-one relationships, one-to-many relationships, and some JSONB columns.
From there, I would write JavaScript code that used that specific ORM and measure:
- How long it would take to create an entry
- How long it would take to update an entry
- How long it would take to update a nested entry (relationship and/or key-value in a JSON)
- How long it would take to delete an entry
- How long it would take to query/get an entry
I created repos for each ORM test environment. I would love to share these repos but they are technically owned by eBay, so I can’t share them.
Results
Around May 15, 2023, I decided that Sequelize was the better ORM for our use case and that it would be the ORM used for our migration from MongoDB to Postgres. Ultimately, I picked Sequelize because:
- It was “truly open-source” and not maintained by a funded startup.
- Supported most of Postgres’s features.
- Had good performance, especially compared to Prisma.
- Has good documentation, though not as well documented as Prisma’s docs.
I settled on Sequelize, but I also contracted a pros and cons list for each ORM I tested in the hopes that it will help people better determine if Sequelize or Prisma would work for your use case.
Sequelize Pros & Cons
Pros:
- Has a sync() function which automatically creates and handles tables for you
- Can handle complex joins (nested data)
- Supports a lot of filtering options (such as Regex)
- Model/schema representation is done in raw JavaScript using classes which are highly customizable.
- Sequelize handles connections for your selected database(s).
- Sequelize supports multiple read-connections
- Supports raw SQL queries.
- As of May 15, 2023:
- On NPM, Sequelize was last updated “14 days ago” and gets 1,505,835 weekly downloads.
- On GitHub, Sequelize was last updated “yesterday”, has 4.2k Forks, and 27.9k Stars.
- Sequelize has an MIT license and has been open source for over 10 years. So it will most likely stay open source for the foreseeable future.
Cons
- Model/schema representation can get very complex and bloated. For example, the Mongoose representation of our large dataset was about 262 lines (including spaces). The same dataset represented through Sequelize is 564 lines (including spaces).
- The syntax for Sequelize gets really confusing and complicated in certain cases.
- Migrating your database (editing it) is very bothersome. You can use sequelize-cli to generate a JavaScript migration template scripts. BUT, this seems to be the same case across nearly all ORMs. Migrating in a relational database is not easy in most cases.
- The documentation is not that great. It has gotten better but still needs work. But with tools like ChatGPT, this is not as big of an issue as it used to be sense ChatGPT has a very good understanding on Sequelize most likely due to Sequelize existing for a decade.
- Sequelize is not as type-sensitive compared to Prisma, which could lead to issues.
- It does not support TypeScript well, which for my project is not a problem but for many other projects could be a big issue.
Prisma Pros and Cons
Pros:
- Has its own schema language. This means that you can more easily and cleanly make your schema/model. For example, the Mongoose representation of our large dataset was about 262 lines (including spaces). But the same dataset represented by Prisma was only 221 lines (including spaces).
- Prisma has a CLI tool that makes it easier in the creation and migration (modification) of your database. Which is very convenient. It’s not a silver bullet but it’s the best thing I’ve seen so far from an ORM.
- Supports raw SQL queries.
- The code for Prisma is clean and simple. You still have to learn and wrap yourself around Prisma’s syntax but it’s way easier to understand than Sequelize’s syntax.
- Prisma has a client that automatically generates query builders for Node.js & TypeScript.
- Documentation is very very good and clean. ChatGPT can still help but it’s not as up to date about Prisma as it is with Sequelize.
- As of May 15, 2023:
- On NPM, Prisma was last updated “6 days ago” and gets 1,344,705 weekly downloads.
- On GitHub, Prisma was last updated “3 hours ago”, has 1.1k Forks, and 31.3k Stars.
Cons
- Does not support Regex filtering for Postgres but does have “contains”, “includes”, and “startsWith” filter options.
- From my testing, Prisma is significantly slower at creating our large dataset’s entries in Postgres. Sequelize created those large entries at a rate of about 2.26 seconds per entry (JSON file). While Prisma had a rate of about 11.21 seconds per entry (JSON file). Based on these results, Prisma is around 5x slower than Sequelize at this task.
- Also, deleting one entry from the large dataset resulted in a wait time of nearly 4 minutes, which is very very bad.
- Sequelize was way faster at deleting an entry in the largest and most complex dataset in my tests. Sequelize did not have an advantage over Prisma when it came to the dataset’s construction. Both had to deal with a three-layer deep relationship for this large dataset, so I would say it was a fair comparison.
- Prisma is a startup, meaning Prisma is a for-profit company with $56.5 million in funding. Knowing this, Prisma’s main ORM code/package is open source with an Apache-2.0 license. This is nice, but since Prisma has investors, I would not be surprised if they pull a MongoDB when it comes to their license.
Sources
- https://www.bitovi.com/blog/battle-of-the-node.js-orms-objection-prisma-sequelize
- https://www.prisma.io/docs/orm/more/comparisons/prisma-and-sequelize
- https://github.com/prisma/prisma
- https://github.com/sequelize/sequelize
- https://www.youtube.com/watch?v=rLRIB6AF2Dg
- https://www.youtube.com/watch?v=RebA5J-rlwg
- https://www.youtube.com/watch?v=a5Wh_LDXtLc
- https://www.youtube.com/watch?v=4QN1BzxF8wM