31

The answer I got from an IRC channel:

Sequelize is an ORM that includes some query builder stuff; Knex is just a query builder, not an ORM.

ORMs don't actually fit very well in many use cases, it's easy to run up against the limits of what they can express, and end up needing to break your way out of them.

But that doesn't really explain the pros and cons of each. I am looking for an explanation, and possibly a simple example (use case) highlighting those similarities / differences.

Why would one use one over the other?

Community
  • 1
  • 1
nawK
  • 693
  • 1
  • 7
  • 13

2 Answers2

40

Sequelize is full blown ORM forcing you to hide SQL behind object representation. Knex is plain query builder, which is way too low level tool for application development.

Better to use objection.js it combines good parts of ORMs without compromising power of writing any kind of SQL queries.

Here is good article about it from the author of objection.js https://www.jakso.me/blog/objection-to-orm-hatred

Disclaimer: I'm knex maintainer and been also involved in development of objection.js.

Mikael Lepistö
  • 18,909
  • 3
  • 68
  • 70
  • 10
    Interesting that you say Knex is way too low level for application development. I've used it without an ORM before, it works fine. I find it easier to see what is going on in the database without the abstraction of an ORM. – Matt West Nov 02 '20 at 19:21
  • 1
    Good for you, I suppose you haven't then encountered the same kind of problems that we did. `Objection.js` was developed exactly because we were using earlier `knex` directly and working with complex relations with it was really tedious and time consuming. The linked blog post has all the reasoning behind my opinion. – Mikael Lepistö Nov 03 '20 at 15:23
  • 1
    Fair enough, I'll be sure to check out Objection if I start running into problems. – Matt West Nov 11 '20 at 03:30
  • @MattWest think I agree with you. Was just going through a lot of "ORM" libraries for Rust and they seemed very popular. Cycle back to Node.js on which our company built a lot of 80% of our services initially (over 4 years), we have never used anything except Knex. It has served us very well. The only issue that we have ever faced comes from complex DB schema which leads to a lot of joins. I had no idea that ORMs were that popular in Node.js as well! Well you learn something new everyday. I think inferring types probably reduced the need of an ORM, but I'll definitely use one in a new service! – coder123 Aug 29 '22 at 10:07
  • Maybe what would be really interesting to learn is what kind of problems you faced @MikaelLepistö? If you could expand on them – coder123 Aug 29 '22 at 10:09
  • 1
    This answer is significantly weakened by the opinion-based statement _"way too low level tool for application development"_. While a full ORM is often useful, in other applications I have found that even just Knex is sometimes too _high_ level, because--for example--it doesn't provide an interface to use prepared statements in MySQL. Certainly I would not have benefitted from objectionjs in those situations. – GrandOpener Aug 14 '23 at 14:44
  • @GrandOpener if you read the article I linked, you should also understand the context of that statement. Also even if you use Knex it does not prevent you from accessing even more low level DB functionality if you need it, neither does Objection.js. I still fully stand behind that statement :) – Mikael Lepistö Aug 23 '23 at 07:34
2

Think of it like this which is the better performance and which is easier to learn.

As low level Database driver

For postgresql you can use pg as a query builder

As intermediate level you can use knex

As high level you can use ORM like sequelize, bookshelf, objection which is based on knex

Now low level doesn’t mean a bad thing. It’s the best performance you can get but the down side is you need to learn queries of the database you are using

Now knex is the same as a query builder the same cost operation

Now the highest level have the highest cost But it’s easy to learn but the down size if you learn sequelize and decided to use objection they are different so you will need to learn another ORM

My suggestion if you want the best performance for a scalable complex backend server you can use query builder or knex

If you want to feel like dealing with objects instances like mongoose you can use Sequelize.

The only difference is the cost operating and it’s not large.

But ORMs have more functionality.

Of course you can refer to this article to understand more About ORM

https://blog.logrocket.com/why-you-should-avoid-orms-with-examples-in-node-js-e0baab73fa5/

DevHub
  • 522
  • 6
  • 15