Knex
ELI5 — The Vibe Check
Knex is a SQL query builder for Node.js that's like writing SQL with training wheels. It doesn't try to hide the SQL — it just makes it easier to write and database-agnostic. It's the foundation that many other ORMs (like Objection.js) are built on top of.
Real Talk
Knex.js is a batteries-included SQL query builder for Node.js supporting PostgreSQL, MySQL, SQLite3, Oracle, and MSSQL. It provides a chainable API for building SQL queries, a migration system, and seed files. It operates at a lower level than full ORMs, giving developers more control over generated SQL.
Show Me The Code
const users = await knex('users')
.select('name', 'email')
.where('active', true)
.orderBy('created_at', 'desc')
.limit(10);
When You'll Hear This
"We use Knex for migrations and raw queries where the ORM is overkill." / "Knex lets us switch between Postgres and MySQL without rewriting queries."
Related Terms
Migration
A migration is a versioned script that modifies your database schema — adding a column, creating a table, changing a type.
ORM (Object-Relational Mapper)
An ORM is like a translator between your code and your database. Instead of writing scary SQL, you just write normal code like `User.
Query Builder
A query builder lets you build database queries by chaining methods instead of writing raw SQL. It's like assembling a sentence with Lego blocks — `.
SQL (Structured Query Language)
SQL is the language you use to talk to a database. You ask it things like 'give me all users who signed up this week' and it actually does it.