🗄️
Database2M+/wkMIT

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
npm install knex
yarn
yarn add knex
pnpm
pnpm add knex

Import

ESM
import Knex from 'knex';

Quick Example

usage
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

Packageknex
CategoryDatabase
Weekly Downloads2M+
LicenseMIT
Installnpm install knex

Related Packages

Browse npm Packages by Category

Explore our reference of 200 popular npm packages with install commands, examples, and quick-start guides.