knex

A flexible, portable SQL query builder for Node.js supporting PostgreSQL, MySQL, CockroachDB, SQL Server, SQLite3, and Oracle.

Library
npm
v3.3.0
20,341 stars
MIT License

Repository Health

Pre-computed score based on development activity, maintenance, community, maturity, and trend momentum. How we score it →
66 /100 Good
Development Activity 36
Maintenance 52
Community 76
Maturity 60
Momentum 40

Technical Analysis

AI-assessed by reading the actual repository — architecture, code quality, innovation, and documentation. How we score it →
80 /100 Excellent
Architecture 85
Code Quality 88
Innovation 65
Learning Curve 80

Knex.js is a batteries-included SQL query builder for Node.js that provides a unified, promise-based API across PostgreSQL, MySQL, MariaDB, CockroachDB, SQL Server, SQLite3, and Oracle. Rather than hiding SQL behind an opinionated abstraction, it exposes a chainable builder that compiles directly to dialect-correct SQL, alongside a schema builder for migrations, a connection-pooling layer built on tarn, and both promise and callback interfaces for streaming and transactional workflows.

With over 20,000 GitHub stars and more than 5 million weekly npm downloads, Knex is one of the most widely deployed query builders in the Node.js ecosystem and serves as the SQL foundation underneath higher-level ORMs like Objection.js and MikroORM. Its CLI (knex migrate, knex seed) and dialect-specific compiler architecture make it a common choice for teams that want fine-grained control over generated SQL without writing raw query strings by hand.

What You Get

  • A chainable query builder covering selects, joins, subqueries, and raw SQL escape hatches
  • A schema builder and CLI-driven migration/seed system for versioned database changes
  • Built-in connection pooling (via tarn) and first-class transaction support
  • TypeScript typings with generic row typing for query results
  • Dialect adapters for PostgreSQL, MySQL, MariaDB, CockroachDB, MSSQL, SQLite3, and Oracle

Common Use Cases

  • Building a typed data-access layer for a Node.js API without adopting a full ORM
  • Powering higher-level ORMs (Objection.js, MikroORM) as the underlying SQL compilation engine
  • Managing versioned schema migrations and seed data across environments
  • Supporting multiple database backends from a single codebase

Under The Hood

Architecture The core Client class (lib/client.js) extends EventEmitter and acts as the central coordination point, holding config, a Logger, a connection pool (via tarn’s KnexPool from lib/pool.js), and factory methods that wire together QueryBuilder/QueryCompiler and SchemaBuilder/SchemaCompiler/TableBuilder/TableCompiler/ColumnBuilder/ColumnCompiler pairs. Each dialect under lib/dialects/ (postgres, mysql, mysql2, mariadb, mssql, sqlite3, better-sqlite3, oracledb, cockroachdb, pgnative, redshift) subclasses Client and overrides the compiler classes to emit dialect-correct SQL, so the builder/compiler split cleanly separates what query is being built from how it is rendered as SQL text, a layered strategy-pattern architecture. Execution flows through lib/execution/runner.js and lib/execution/transaction.js, which wrap the pool-acquired connection and run the compiled SQL, with streaming handed off through the formatter layer. Migrations (lib/migrations/) and the CLI (bin/cli.js) sit as a separate concern layered on top of the same Client and QueryBuilder primitives, so changing the core Client contract would ripple through every dialect subclass and the migration runner.

Tech Stack Knex is plain JavaScript (the large majority of the codebase) with a hand-written but extensive TypeScript declaration file rather than being TypeScript-authored; dialect drivers such as pg, mysql, mysql2, mariadb, tedious, sqlite3/better-sqlite3, and oracledb are peer/optional dependencies rather than bundled. Runtime dependencies are deliberately minimal: tarn for connection pooling, lodash for utilities, commander/getopts for the CLI, interpret/rechoir/escalade for loading user config and migration files, and colorette for terminal output. The build pipeline uses tsc for type declarations and Prettier/ESLint for linting, tests run under Mocha with nyc for coverage plus a secondary tape suite, and tsd/tstyche provide type-level testing. CI spins up real database containers via docker-compose for every supported dialect rather than relying only on mocks.

Code Quality The test suite is extensive and multi-layered: unit tests, full integration tests against real databases per dialect, CLI tests driven by Jake, and type-level tests that assert the exported TypeScript types compile correctly, a level of rigor uncommon even in mature libraries. Error handling favors explicit, typed error classes over generic throws, and the codebase enforces ESLint and Prettier via Husky pre-commit hooks and lint-staged. Naming is consistent and descriptive, with mirrored Builder/Compiler pairs per concern. CI enforces linting, unit tests, real-database integration tests, and static analysis on every change, with coverage tracked via nyc and Codecov.

API Design Knex’s core design choice, one chainable builder API that compiles to dialect-correct SQL for seven different databases, lets application code stay portable across databases without hiding SQL semantics behind an ORM’s object model, a genuinely different trade-off from schema-first ORMs. The library requires almost no boilerplate to get started, and the same builder object works identically whether awaited as a promise, used as a subquery, or streamed. Its long-standing role as the SQL layer underneath other ORMs is evidence of a well-designed, embeddable query-compilation API, a battle-tested, pragmatic design more than a novel technical mechanism.

Used by 29 apps in this directory

JavaScript
100%
Other

Automatisch

Automation · No Code Platforms

13,969

Self-hosted, no-code workflow automation that keeps your data on your own servers—a privacy-first alternative to Zapier with 90+ integrations.

View details
49
Repo Health
78
Technical
63
Dependency
Built with
JavaScript 100%
Updated 7 months ago
TypeScript
97%
AGPL 3.0

Bigcapital

Invoicing Finance

3,893

Self-hostable double-entry accounting platform with invoicing, inventory, multi-currency, and real-time financial reporting for small and medium businesses.

View details
91
Repo Health
77
Technical
61
Dependency
Built with
TypeScript 97%
Updated 1 weeks ago
TypeScript
69%
Other

Budibase

Low Code Platforms · No Code Platforms

28,279

Build AI agents, automations, and internal apps on a single open-source platform with full self-hosting control.

View details
92
Repo Health
81
Technical
63
Dependency
Built with
TypeScript 69%
Svelte 26%
Updated 1 weeks ago
TypeScript
81%
Other

Directus

CMS · Low Code Platforms

37,911

Connect any SQL database and get instant REST and GraphQL APIs, a visual management Studio, and a native MCP server for AI agents — free for most organizations.

View details
93
Repo Health
82
Technical
76
Dependency
Built with
TypeScript 81%
Vue 18%
Updated 1 weeks ago
Go
79%
Apache 2.0

Dolt

Data Engineering · Databases · Developer Tools

24,440

The SQL database you can branch, merge, diff, and clone — Git for your data, MySQL-compatible and ready for multi-agent AI workflows.

View details
91
Repo Health
9
Technical
65
Dependency
Built with
Go 79%
Shell 19%
Updated 1 weeks ago
TypeScript
58%
AGPL 3.0

Frappe Books

Invoicing Finance

4,955

Offline desktop accounting with double-entry, POS, and custom invoice templates

View details
88
Repo Health
77
Technical
73
Dependency
Built with
TypeScript 58%
Vue 39%
Updated 2 weeks ago
TypeScript
50%
MIT

Ghost

Blogging · CMS

55,383

Open source headless Node.js CMS for professional publishing, paid memberships, and newsletters with a fully owned audience.

View details
96
Repo Health
85
Technical
67
Dependency
Built with
TypeScript 50%
JavaScript 45%
Updated yesterday
TypeScript
97%
Other

Infisical

Devops · Security

29,257

The open-source platform for secrets, certificates, privileged access, and AI agent security — all in one self-hostable system.

View details
91
Repo Health
84
Technical
65
Dependency
Built with
TypeScript 97%
Updated 1 weeks ago
TypeScript
76%
Other

Joplin

Note Taking

56,365

The privacy-first, open-source note-taking app with end-to-end encrypted sync, AI assistance, and a powerful plugin ecosystem across every platform.

View details
93
Repo Health
87
Technical
61
Dependency
Built with
TypeScript 76%
JavaScript 14%
Updated 1 weeks ago

Join founders buildingwith open source

Opinionated takes, migration guides, cost-saving tips, and insights from the open source ecosystem.

Subscribe on Substack
Join 750+ subscribers