Knex.js vs Mongoose: What are the differences?
Developers describe Knex.js as "SQL query builder for Postgres, MySQL, MariaDB, SQLite3, and Oracle". Knex.js is a "batteries included" SQL query builder for Postgres, MySQL, MariaDB, SQLite3, and Oracle designed to be flexible, portable, and fun to use. It features both traditional node style callbacks as well as a promise interface for cleaner async flow control, a stream interface, full featured query and schema builders, transaction support (with savepoints), connection pooling and standardized responses between different query clients and dialects. On the other hand, Mongoose is detailed as "MongoDB object modeling designed to work in an asynchronous environment". Let's face it, writing MongoDB validation, casting and business logic boilerplate is a drag. That's why we wrote Mongoose. Mongoose provides a straight-forward, schema-based solution to modeling your application data and includes built-in type casting, validation, query building, business logic hooks and more, out of the box.
Knex.js belongs to "Database Tools" category of the tech stack, while Mongoose can be primarily classified under "Object Document Mapper (ODM)".
"Write once and then connect to almost any sql engine" is the primary reason why developers consider Knex.js over the competitors, whereas "Well documented" was stated as the key factor in picking Mongoose.
Knex.js and Mongoose are both open source tools. It seems that Mongoose with 18.8K GitHub stars and 2.62K forks on GitHub has more adoption than Knex.js with 9.79K GitHub stars and 1.22K GitHub forks.
According to the StackShare community, Mongoose has a broader approval, being mentioned in 85 company stacks & 89 developers stacks; compared to Knex.js, which is listed in 10 company stacks and 9 developer stacks.
What is Knex.js?
What is Mongoose?
Need advice about which tool to choose?Ask the StackShare community!
Sign up to add, upvote and see more prosMake informed product decisions
What are the cons of using Knex.js?
Sign up to get full access to all the companiesMake informed product decisions
Sign up to get full access to all the tool integrationsMake informed product decisions
I inherited a stack where Mongoose is used in the database layer.
It's been several months and it's still the single highest source of daily WT*s in my backend development. The API is full of irregularities and the design is a poor mix of object-orientation and stateful objects with a lot implicit behavior. Mongoose made the choices of taking the worst parts of ORMs and using them in a context where the benefits of ORMs don't apply. The only reason I'm keeping it is its handy
.populate() feature. Expect bad surprises!
Not an ORM but a query builder. So developers are encouraged or sometimes forced to think about SQL and database layer, which is a good thing.