
Node.js teams that work with a relational database usually end up choosing between two extremes. Raw SQL strings give full control, but they are easy to get wrong, hard to compose and risky when user input slips into them. A full ORM hides SQL behind models and relations, but it also hides what actually runs against the database. Knex.js sits between the two: you build SQL with JavaScript methods, the library handles parameters and dialects, and the query that runs is still the one you wrote.
This article explains what Knex.js is, how it works, where it fits, when an ORM or a newer query builder is the better choice, and how it compares with the alternatives.
What is Knex.js?
Knex.js is an open-source SQL query builder for Node.js. It describes itself as "batteries included": besides building queries, it manages connection pools, transactions, schema changes through migrations, and seed data. It supports PostgreSQL, MySQL and MariaDB, SQLite, Microsoft SQL Server, Oracle, CockroachDB and Amazon Redshift through the same API.
Knex was written by Tim Griesser, who also created the Bookshelf.js ORM on top of it. The first versions were published to npm in May 2013, and the library spent almost nine years in 0.x versions while it was already widely used in production. Knex 1.0 arrived on 16 January 2022, and the 3.x line requires Node.js 16 or later. It is released under the MIT licence.
How Knex.js works
Queries as method chains. Instead of concatenating strings, you write knex('orders').where({ status: 'paid' }).orderBy('created_at', 'desc').limit(20). Knex turns that chain into SQL for the database you configured, so the same code produces correct syntax for PostgreSQL or SQL Server.
Parameters are bound, not pasted. Values are passed to the database as bound parameters, which protects against SQL injection. When you need something the builder does not cover, knex.raw() lets you write SQL directly while still binding values safely.
Schema and migrations in code. A schema builder creates and alters tables, indexes and foreign keys. Migrations are timestamped files with up and down functions, run from the Knex command line, so every environment can be moved to the same schema version and rolled back if needed. Seed files fill development and test databases with known data.
Transactions and pooling. Knex keeps a pool of database connections and exposes transactions as a function: everything inside it commits together or rolls back together.
Key features
- One API for many SQL dialects, which keeps code portable between databases.
- Migrations and seeds with a command-line tool, built in.
- Transactions with automatic commit and rollback.
- Connection pooling handled for you.
- Streaming of large result sets instead of loading them into memory at once.
- TypeScript type definitions included in the package.
- No models or entities: Knex returns plain rows and leaves the domain design to you.
Where Knex.js is used
- Backend APIs and microservices on Node.js that need predictable, hand-tuned SQL.
- Reporting and data-heavy endpoints with complex joins, aggregates and window functions that ORMs express poorly.
- Migration tooling for projects that want versioned schema changes even if queries are written elsewhere.
- As a foundation for ORMs: Bookshelf.js and Objection.js are built on Knex, and MikroORM used Knex as its SQL layer until version 7, when it moved to Kysely.
When to choose Knex.js, and when not to
Choose Knex.js when:
- You want to see and control the SQL that runs, without writing raw strings everywhere.
- Queries are complex, and performance tuning at the SQL level matters.
- The project may need to run on more than one relational database.
- You prefer to design your own repositories and domain layer rather than adopt an ORM's model.
Look elsewhere when:
- The domain has many related entities and you want relations, change tracking and a unit of work handled for you. An ORM such as MikroORM or Prisma saves a lot of code.
- Strict type safety for every query result is a priority in a TypeScript codebase. Kysely and Drizzle infer result types from the schema; Knex types are looser.
- The data is not relational. Knex only speaks SQL.
Knex.js and its alternatives
| Tool | Type | Strength | Best fit |
|---|---|---|---|
| Knex.js | Query builder | Mature, many dialects, built-in migrations | SQL-first Node.js backends |
| Kysely | Type-safe query builder | Result types inferred from the schema | TypeScript projects that want typed SQL |
| Prisma | ORM with its own schema language | Generated, fully typed client | Fast development on typical CRUD domains |
| MikroORM | Data Mapper ORM | Unit of Work, identity map, domain models | Complex domains and DDD-style backends |
Knex and Kysely are the closest pair. Kysely was built for TypeScript from the start and catches mistakes in column names and result shapes at compile time; Knex is older, supports more dialects and ships migrations and seeds in the box. In the .NET world, the nearest counterpart is a micro ORM such as Dapper: thin, SQL-first and close to the database.
How we use Knex.js at Internative
We use Knex in backend systems that need precise control over SQL without the overhead of a full ORM. It lets us write optimised queries, keep the data layer portable across databases such as PostgreSQL, and design clean data access layers of our own.
If you are planning a Node.js backend and weighing a query builder against an ORM, our custom software development team can help you choose the data access approach that fits your system.
Frequently asked questions
What is Knex used for?
Knex is used to build and run SQL queries from Node.js code, manage database schema changes with migrations, fill databases with seed data and run transactions, across several relational databases with one API.
Is Knex an ORM?
No. Knex is a query builder. It has no models, relations or change tracking; it returns plain rows. ORMs such as Bookshelf.js and Objection.js are built on top of it to add those features.
What is the difference between Kysely and Knex?
Both are SQL query builders for Node.js. Kysely is written for TypeScript and infers the type of every query result from your database schema. Knex is older, supports more databases and includes migrations, seeds and a command-line tool. Choose Kysely for strict typing, Knex for breadth and built-in tooling.
Is an ORM faster than SQL?
No. An ORM generates SQL, so at best it matches well-written SQL and sometimes produces less efficient queries. A query builder like Knex stays close to the SQL you would write by hand, which is one reason teams choose it for performance-sensitive code.
Who created Knex?
Knex.js was created by Tim Griesser, who first published it to npm in 2013. Today it is maintained as an open-source project by a team of contributors under the knex organisation on GitHub.
Keep reading
The articles that follow this one.



