
knex
A JavaScript library for building SQL queries across several databases with one API.
- Stars
- 20.3k
- Forks
- 2.2k
- Licence
- MIT
- Language
- JavaScript
- Last update
- 4 months ago
- Type
- Library
What it is
Knex is a query builder for Node.js. You write database queries as JavaScript code instead of raw SQL strings, and the same code works across PostgreSQL, MySQL, MariaDB, CockroachDB, SQL Server, SQLite3 and Oracle. It also provides transactions, connection pooling and streaming queries.
- Create tables and run schema changes from JavaScript code
- Insert, select and join rows across related tables
- Wrap several queries in a transaction
- Stream large query results instead of loading them all into memory
- Check the SQL that a query would generate using the linked Knex Query Lab
Great for
- Node.js developers who need to talk to a SQL database from application code
- Teams that may switch between database engines and want portable query code
- Projects that want a query builder rather than a full ORM
- Developers who want both promise and callback APIs
Not ideal for
- People who are not writing Node.js code, since the library is for JavaScript applications
- Projects that want an ORM with models and relationships; the README points to separate ORMs such as Objection.js, MikroORM and Bookshelf for that
- Beginners with no SQL knowledge, because the query builder still assumes you understand how SQL works
How to get it
- 1
Install with a package manager
You'll need: Node.js 16+ · Python 3.x with setuptools (for native dependencies such as better-sqlite3) · Visual Studio Build Tools with C++ workload (Windows only)
Install dependencies in a cloned repo
npm install - 2
Run it
- Install the knex package and a database driver for your chosen database
- Create a knex instance with the client and connection settings for your database
- Run schema commands such as createTable to set up tables
- Run insert and select queries using the knex instance
- Call destroy on the instance when finished
- 3
Get the most out of it
- Use the TypeScript typings with the Knex.Config type to get type-checked configuration
- Use the migrate commands to manage schema changes over time, as shown in the README's last example
- Browse ECOSYSTEM.md in the repository for plugins and tools built for knex
- Consult UPGRADING.md when moving from an older version
- Use the recipes wiki for solutions to specific problems
Downloads
Strengths
- Supports many SQL dialects behind one query-building interface
- Includes transactions, connection pooling and streaming in the core library
- Has TypeScript typings and a documented ecosystem of plugins and tools
Trade-offs
- Requires Node.js 16 or newer and, for some SQLite drivers, Python and a C++ build toolchain
- Bringing your own knowledge of SQL is still needed; the abstraction does not remove database-specific differences entirely
- README does not detail performance characteristics or the exact behaviour differences between dialects
How it compares
| Project | Stars | Type | Level | Updated | Best for |
|---|---|---|---|---|---|
| knex (this) | 20.3k | Library | intermediate | 4 months ago | Node.js developers who need to talk to a SQL database from application code |
| sequelize | 30.3k | Library | intermediate | 5 days ago | Node.js backend developers who use a relational SQL database |
| kysely | 14.3k | Library | intermediate | 4 days ago | TypeScript developers who want SQL control without losing compile-time type checks |
| usql | 10.2k | CLI | beginner | 4 days ago | Developers who use several different databases and want one tool for all of them |
| sqlc | 18.4k | Dev tool | intermediate | 4 days ago | Go developers who prefer writing plain SQL over using an ORM |
| gogs | 47.9k | App | intermediate | 5 days ago | Small teams that want a private Git server on their own hardware |
Stats synced from GitHub. The assessment above was drafted with AI from the project's README on 2026-10-29 and may miss recent changes — check the README before relying on a command. MIT licence: free to use, modify and distribute, including commercially, as long as you keep the copyright notice.