Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How is Node.js Knex similar/different to Sequelize?

Tags:

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?

like image 345
nawK Avatar asked May 07 '19 18:05

nawK


People also ask

Does Sequelize use KNEX?

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.

What is KNEX in node js?

Knex. js is a “batteries-included” query builder for PostgreSQL, MySQL, SQLite3, Oracle, Amazon Redshift, and many other database drivers. We simply install the Knex library and the appropriate driver to query the database. Primarily made for Node. js, Knex supports both Node-style callbacks and promises.

What is the purpose of KNEX?

Knex is a technique that is used to build queries. It supports various databases like Postgres, MySQL, SQLite, Oracle, and some others as well. It provides both callbacks and promise interface. It provides connection pooling and standardized responses.

What is use of Sequelize in node js?

Sequelize is a Node. js-based Object Relational Mapper that makes it easy to work with MySQL, MariaDB, SQLite, PostgreSQL databases, and more. An Object Relational Mapper performs functions like handling database records by representing the data as objects.


1 Answers

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.

like image 200
Mikael Lepistö Avatar answered Oct 01 '22 09:10

Mikael Lepistö