API Reference

Knex

SQL query builder for Node.js with a promise API, migrations, transactions, and support for PostgreSQL, MySQL, and SQLite.

Official Documentation ↗
CompatibilityThis documentation covers version Knex 3.x; behavior and driver support vary by database client. Check the official docs for the latest API surface.

Installation

npmnpm install knex pg
pnpmpnpm add knex pg
yarnyarn add knex pg

Methods

Knex builds SQL with a JavaScript API and manages migrations. It is not an ORM; you still choose joins, indexes, and transaction boundaries. The value is composable queries, parameter binding, and a consistent interface across databases.

Pool and shutdown

Configure a bounded pool and destroy it during shutdown. The pool limit is per process and must fit the database connection budget. A query builder that creates a new connection per call defeats pooling.

Migrations are release artifacts

Version migrations in source control, run them in one controlled job, and provide a tested down migration or a snapshot restore plan. Running migrations at startup from every replica can race. Keep schema changes compatible with the old application version during a rolling deploy.

Errors

The following points are quoted verbatim from the official documentation (https://knexjs.org/guide/). Any error type, throw condition or recovery step not stated there is left unspecified; this page does not infer exception classes or return values.

  • WARNING When using connectionPool , the pool config option (min, max, afterCreate, validate, etc.) is ignored since knex is not creating the pool. (https://knexjs.org/guide/)
  • WARNING The connectionPool option is supported for pg , mysql , mysql2 , and oracledb dialects when passing native driver pools. (https://knexjs.org/guide/)
  • When a DB driver returns an error, this previously captured stack trace is thrown instead of a new one. (https://knexjs.org/guide/)

Security notes

Quoted from the official documentation (https://knexjs.org/guide/); this is not a complete threat model.

  • Composing SQL queries in the browser for execution on the server is highly discouraged, as this can be the cause of serious security vulnerabilities. (https://knexjs.org/guide/)
  • By default Knex adds compiled SQL ( SELECT * FROM users WHERE password = ‘myPassword’ ) to the error message. (https://knexjs.org/guide/)
  • This can be changed to parameterized SQL ( SELECT * FROM users WHERE password = ? (https://knexjs.org/guide/)

FAQ

Is Knex an ORM?
No. It is a query builder with migrations. It does not manage entity identity or relationships; you write queries explicitly.
How do I prevent SQL injection with Knex?
Use the builder methods and bindings. Avoid raw SQL with interpolated user input; raw identifiers and fragments need an allowlist.
Should migrations run at application startup?
Prefer a separate release step. Running migrations from every instance can race and makes rollback harder. Use one controlled migration job.