Features
- Flexible SQL query builder for multiple databases
- Migration and seed file management
- Connection pooling and transaction support
- Support for PostgreSQL, MySQL, SQLite, Oracle, MSSQL
Pros
- Mature and widely used SQL query builder
- Excellent migration tooling
- Gives full SQL control without raw strings
Cons
- Weak TypeScript support compared to Kysely or Drizzle
- Not a full ORM โ no model layer or relations
- Maintenance has slowed in recent years
Overview
Knex.js is a SQL query builder for Node.js that supports PostgreSQL, MySQL, SQLite, Oracle, and MS SQL Server. It provides a fluent API for building SQL queries programmatically, along with a robust migration system and seed file management for database setup.
Knex.js has been a foundational tool in the Node.js database ecosystem, powering ORMs like Bookshelf.js and Objection.js. While newer tools like Kysely and Drizzle offer better TypeScript support, Knex.js remains widely used in production applications.
When to Use
Knex.js is appropriate for existing projects that depend on it, or when you need a SQL query builder with solid migration tooling and donโt need strong TypeScript types. For new TypeScript projects, Kysely or Drizzle are recommended alternatives.
Getting Started
npm install knex pg
npx knex init
import knex from "knex";
const db = knex({
client: "pg",
connection: "postgres://user:pass@localhost/db",
});
const users = await db("users").select("*").where("active", true);