Baike.dev
All toolsOnline toolsAI codingTrendingOpen sourceNewsSubmit
Log in
< Back to tools
K

knex

> 数据库
Open source

A query builder for PostgreSQL, MySQL, CockroachDB, SQL Server, SQLite3 and Oracle, designed to be flexible, portable, and fun to use.

20.3K stars0 likes2 views
WebsiteGitHub

About

A query builder for PostgreSQL, MySQL, CockroachDB, SQL Server, SQLite3 and Oracle, designed to be flexible, portable, and fun to use.

knex.js

A SQL query builder that is flexible, portable, and fun to use!

A batteries-included, multi-dialect (PostgreSQL, MariaDB, MySQL, CockroachDB, MSSQL, SQLite3, Oracle (including Oracle Wallet Authentication)) query builder for Node.js, featuring:

  • transactions
  • connection pooling
  • streaming queries
  • both a promise and callback API
  • a thorough test suite

Node.js versions 16+ are supported.

  • Take a look at the full documentation to get started!
  • Browse the list of plugins and tools built for knex
  • Check out our recipes wiki to search for solutions to some specific problems
  • In case of upgrading from an older version, see migration guide

You can report bugs and discuss features on the GitHub issues page or send tweets to @kibertoad.

For support and questions, join our Gitter channel.

For knex-based Object Relational Mapper, see:

  • https://github.com/Vincit/objection.js
  • https://github.com/mikro-orm/mikro-orm
  • https://bookshelfjs.org

To see the SQL that Knex will generate for a given query, you can use Knex Query Lab

Local Development Setup

Prerequisites

  • Node.js 16+

  • Python 3.x with setuptools installed (required for building native dependencies like better-sqlite3)

    Python 3.12+ removed the built-in distutils module. If you encounter a ModuleNotFoundError: No module named 'distutils' error during npm install, install setuptools for the Python version used by node-gyp:

    bash
    pip install setuptools
  • Windows only: Visual Studio Build Tools with the "Desktop development with C++" workload

Install dependencies

bash
npm install

Examples

We have several examples on the website. Here is the first one to get you started:

…

TypeScript example

typescript
import { Knex, knex } from 'knex';

interface User {
  id: number;
  age: number;
  name: string;
  active: boolean;
  departmentId: number;
}

const config: Knex.Config = {
  client: 'sqlite3',
  connection: {
    filename: './data.db',
  },
  useNullAsDefault: true,
};

const knexInstance = knex(config);

knexInstance<User>('users')
  .select()
  .then((users) => {
    console.log(users);
  })
  .catch((err) => {
    console.error(err);
  })
  .finally(() => {
    knexInstance.destroy();
  });

Usage as ESM module

If you are launching your Node application with --experimental-modules, knex.mjs should be picked up automatically and named ESM import should work out-of-the-box. Otherwise, if you want to use named imports, you'll have to import knex like this:

javascript
import { knex } from 'knex/knex.mjs';

You can also just do the default import:

javascript
import knex from 'knex';

If you are not using TypeScript and would like the IntelliSense of your IDE to work correctly, it is recommended to set the type explicitly:

javascript
/**
 * @type {Knex}
 */
const database = knex({
  client: 'mysql',
  connection: {
    host: '127.0.0.1',
    user: 'your_database_user',
    password: 'your_database_password',
    database: 'myapp_test',
  },
});
database.migrate.latest();

Issues· 754 open

View all issuesOpen on GitHub
  • #6539

    [Security] SQL Injection in Oracle Dialect JSON Path Handling (_jsonPathWrap())

    Updated Sep 24, 2026
  • #3176

    knex silently broken with sqlite3 and config.pool.max > 1

    NG_feature_requestNG_difficulty_easyUpdated Sep 23, 2026
  • #4943

    MySQL alter schema increments first ignored

    NG_bugUpdated Sep 23, 2026
  • #6114

    Overwriting nullable option on column fails for OracleDB

    NG_bugUpdated Sep 23, 2026
  • #5435

    Postgres hasTable does not exist on withSchema

    NG_bugUpdated Sep 23, 2026
  • #2156

    Oracle autoinc_trg causing error: ORA-04091: table xxx is mutating, trigger/function may not see it

    NG_bugUpdated Sep 23, 2026
  • #2155

    TypeError: Cannot read property 'Type' of undefined in tablecompiler.js

    NG_bugUpdated Sep 23, 2026
  • #6146

    [Potential bug] undefined database on table.renameColumn

    NG_bugNG_difficulty_mediumUpdated Sep 23, 2026
  • #3538

    creation of rows in knex_migration_lock table is not safe concurrently

    NG_meta_childUpdated Sep 23, 2026
  • #5150

    `Migrator.rollback` may throw

    NG_bugUpdated Sep 23, 2026

> Tags

JavaScriptjavascriptknexmysqlpostgresql

No comments yet. Be the first to share.

> Details

PublishedAug 1, 2026
UpdatedOct 1, 2026
Category数据库
PricingOpen source

> Related tools

P
PostgreSQL
功能强大的开源关系型数据库
R
Redis
内存数据结构存储,常用作缓存与队列
M
MySQL
广泛使用的开源关系型数据库