knex
Knex.js is a batteries-included SQL query builder for Node.js that supports PostgreSQL, MySQL, MariaDB, SQLite3, Oracle, and Amazon Redshift with a flexible, po…
Installation
npm install knex
yarn add knex
pnpm add knex
Import
import Knex from 'knex';
Quick Example
import Knex from 'knex';
const knex = Knex({ client: 'pg', connection: process.env.DATABASE_URL });
const users = await knex('users')
.where('active', true)
.orderBy('created_at', 'desc')
.limit(10);
await knex('users').insert({ name: 'Alice', email: '[email protected]' });About knex
Knex.js is a batteries-included SQL query builder for Node.js that supports PostgreSQL, MySQL, MariaDB, SQLite3, Oracle, and Amazon Redshift with a flexible, portable, and fun-to-use API. Knex provides a chainable query builder interface for constructing SQL queries programmatically — knex('users').where('age', '>', 18).orderBy('name').limit(10) — along with schema building methods for creating and modifying tables, a migration system for versioned schema changes, and a seed system for populating databases with test data. The query builder generates dialect-specific SQL automatically while providing raw() for injecting custom SQL fragments safely with parameter binding. Knex supports transactions, connection pooling, streaming large result sets, and both promise and callback APIs. While not a full ORM, Knex provides the query building layer that ORMs like Bookshelf and Objection.js build upon. Knex is an excellent choice when you want fine-grained control over SQL queries without the overhead of a full ORM but still want protection from SQL injection and a portable query API. The migration system tracks applied migrations in a database table and supports rollbacks.
Quick Facts
| Package | knex |
| Category | Database |
| Weekly Downloads | 2M+ |
| License | MIT |
| Install | npm install knex |
Related Packages
Objection.js is an SQL-friendly ORM for Node.js built on Knex.js that provides a powerful relation m…
Bookshelf is a JavaScript ORM for Node.js built on top of the Knex.js query builder, providing an Ac…
Prisma is a next-generation Node.js and TypeScript ORM that provides a declarative data modeling lan…
pg (node-postgres) is the standard PostgreSQL client for Node.js, providing low-level access to Post…
mysql2 is a high-performance MySQL client for Node.js that provides prepared statements, non-UTF-8 e…
Browse npm Packages by Category
Explore our reference of 200 popular npm packages with install commands, examples, and quick-start guides.