Skills2026年4月16日·1 分钟阅读

Knex.js — Flexible SQL Query Builder for Node.js

A batteries-included SQL query builder for PostgreSQL, MySQL, SQLite, and Oracle with a fluent API, migration framework, and connection pooling.

Agent 就绪

Agent 可直接安装

这个资产可安装;Agent 先选择当前运行时、检查安装计划,再运行匹配命令。

Native · 98/100策略:允许
Agent 入口
任意 MCP/CLI Agent
类型
Skill
安装
Single
信任
信任等级:Established
入口
Knex.js Overview
直接安装命令
npx -y tokrepo@latest install 2460ef98-39e1-11f1-9bc6-00163e2b0d79 --target codex

先 dry-run 确认安装计划,再运行此命令。

TL;DR
Knex.js provides a fluent JavaScript API to build parameterized SQL queries across PostgreSQL, MySQL, SQLite, and Oracle.
§01

What it is

Knex.js is one of the most widely used SQL query builders for Node.js. It provides a chainable, fluent JavaScript API for constructing parameterized SQL queries that work across PostgreSQL, MySQL, MariaDB, SQLite3, Oracle, and Amazon Redshift. Rather than writing raw SQL strings, developers compose queries programmatically while Knex handles dialect-specific differences.

Knex serves as the foundation for higher-level ORMs like Objection.js and Bookshelf. It is well suited for backend developers who want more control than a full ORM provides but still want protection against SQL injection and cross-dialect portability.

§02

How it saves time or tokens

Knex eliminates the need to manually write dialect-specific SQL for each database. A single query definition works across PostgreSQL, MySQL, and SQLite without modification. The built-in migration framework handles schema versioning, and connection pooling via tarn.js manages database connections automatically. This reduces boilerplate code and context-switching between SQL dialects when working with AI coding assistants.

§03

How to use

  1. Install Knex and your database driver: npm install knex pg (or mysql2, sqlite3).
  2. Initialize configuration: npx knex init creates a knexfile.js with connection settings.
  3. Create and run migrations: npx knex migrate:make create_users then npx knex migrate:latest.
  4. Use the fluent API to build queries in your application code.
§04

Example

const knex = require('knex')({
  client: 'pg',
  connection: 'postgres://localhost/mydb'
});

// Select with joins
const users = await knex('users')
  .join('orders', 'users.id', 'orders.user_id')
  .select('users.name', knex.raw('SUM(orders.total) as total_spent'))
  .groupBy('users.id')
  .having('total_spent', '>', 100);

// Migration example
exports.up = function(knex) {
  return knex.schema.createTable('users', (table) => {
    table.increments('id');
    table.string('name').notNullable();
    table.string('email').unique();
    table.timestamps(true, true);
  });
};
§05

Related on TokRepo

§06

Common pitfalls

  • Forgetting to return the promise from migration functions causes silent failures. Always return knex.schema... in up and down.
  • Connection pool exhaustion happens when queries are not properly awaited or connections are not released. Set pool.min and pool.max in your knexfile.
  • Knex does not validate your SQL logic. A query that runs on PostgreSQL may fail on SQLite due to unsupported features like RETURNING.

常见问题

What databases does Knex.js support?+

Knex.js supports PostgreSQL, MySQL, MariaDB, SQLite3, Oracle, and Amazon Redshift. Each dialect has its own driver that Knex uses to translate the fluent API into the correct SQL syntax.

How does Knex.js differ from Prisma or Sequelize?+

Knex is a query builder, not a full ORM. It does not define models or relationships. Instead it provides a programmatic way to write SQL. Prisma and Sequelize add schema modeling, validation, and relationship management on top of query construction.

Can Knex.js handle database migrations?+

Yes. Knex includes a built-in migration framework. You create migration files with `knex migrate:make`, define up and down functions for schema changes, and run them with `knex migrate:latest`. Migrations execute inside transactions for atomicity.

Is Knex.js suitable for production applications?+

Yes. Knex uses tarn.js for connection pooling and supports transactions, streaming, and parameterized queries to prevent SQL injection. It is used in production by many Node.js applications and serves as the query layer for ORMs like Objection.js.

How do I use Knex.js with TypeScript?+

Knex provides built-in TypeScript type definitions. You can type your table interfaces and pass them as generics to queries: `knex<User>('users').where('id', 1)`. The knexfile can also be written in TypeScript with ts-node.

引用来源 (3)

讨论

登录后参与讨论。
还没有评论,来写第一条吧。

相关资产