knex.js

一个针对 PostgreSQL、MySQL 和 SQLite3 的查询生成器,设计灵活、可移植、使用有趣。「A query builder for PostgreSQL, MySQL and SQLite3, designed to be flexible, portable, and fun to use.」

Github stars Tracking Chart

knex.js

一个灵活、便携、有趣的 SQL 查询构建器!

一个功能齐全的、多语言(MSSQL、MySQL、PostgreSQL、SQLite3、Oracle(包括 Oracle Wallet 认证))的 Node.js 查询构建器,其特点是:

支持Node.js 10+版本。

阅读完整的文档以开始使用!
或者查看我们的 Recipes wiki 来搜索一些特定问题的解决方案。
如果从旧版本升级,请参见 升级说明

如需支持和提问,请加入 freenode IRC 的 #bookshelf 频道。

关于对象关系映射器,请看。

要查看 Knex 将为给定查询生成的 SQL,请看:Knex 查询实验室

例子

我们在网站上有几个例子。这里是第一个让你入门的例子:

const knex = require('knex')({
  client: 'sqlite3',
  connection: {
    filename: './data.db',
  },
});

// Create a table
knex.schema
  .createTable('users', table => {
    table.increments('id');
    table.string('user_name');
  })

  // ...and another
  .createTable('accounts', table => {
    table.increments('id');
    table.string('account_name');
    table
      .integer('user_id')
      .unsigned()
      .references('users.id');
  })

  // Then query the table...
  .then(() =>
    knex('users').insert({ user_name: 'Tim' })
  )

  // ...and using the insert id, insert into the other table.
  .then(rows => 
    knex('accounts').insert({ account_name: 'knex', user_id: rows[0] })
  )

  // Query both of the rows.
  .then(() => 
    knex('users')
      .join('accounts', 'users.id', 'accounts.user_id')
      .select('users.user_name as user', 'accounts.account_name as account')
  )

  // map over the results
  .then(rows =>
    rows.map(row => {
      console.log(row)
    })
  )

  // Finally, add a .catch handler for the promise chain
  .catch(e => {
    console.error(e);
  });


Main metrics

Overview
Name With Ownerknex/knex
Primary LanguageJavaScript
Program languageJavaScript (Language Count: 3)
PlatformLinux, Mac, Windows
License:MIT License
所有者活动
Created At2012-12-29 05:18:25
Pushed At2025-05-19 12:22:42
Last Commit At2025-02-28 09:52:04
Release Count183
Last Release Name3.1.0 (Posted on )
First Release Name0.1.0 (Posted on )
用户参与
Stargazers Count19.8k
Watchers Count204
Fork Count2.2k
Commits Count3.1k
Has Issues Enabled
Issues Count3992
Issue Open Count1056
Pull Requests Count1470
Pull Requests Open Count145
Pull Requests Close Count595
项目设置
Has Wiki Enabled
Is Archived
Is Fork
Is Locked
Is Mirror
Is Private

knex.js

npm version
npm downloads
Build Status
Coverage Status
Dependencies Status
Gitter chat
Language Grade: JavaScript

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

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

Node.js versions 8+ are supported.

Read the full documentation to get started!
Or check out our Recipes wiki to search for solutions to some specific problems
If upgrading from older version, see Upgrading instructions

For support and questions, join the #bookshelf channel on freenode IRC

For an Object Relational Mapper, see:

To see the SQL that Knex will generate for a given query, see: Knex Query Lab

Examples

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

const knex = require('knex')({
  client: 'sqlite3',
  connection: {
    filename: './data.db',
  },
});

// Create a table
knex.schema
  .createTable('users', function(table) {
    table.increments('id');
    table.string('user_name');
  })

  // ...and another
  .createTable('accounts', function(table) {
    table.increments('id');
    table.string('account_name');
    table
      .integer('user_id')
      .unsigned()
      .references('users.id');
  })

  // Then query the table...
  .then(function() {
    return knex('users').insert({ user_name: 'Tim' });
  })

  // ...and using the insert id, insert into the other table.
  .then(function(rows) {
    return knex('accounts').insert({ account_name: 'knex', user_id: rows[0] });
  })

  // Query both of the rows.
  .then(function() {
    return knex('users')
      .join('accounts', 'users.id', 'accounts.user_id')
      .select('users.user_name as user', 'accounts.account_name as account');
  })

  // .map over the results
  .map(function(row) {
    console.log(row);
  })

  // Finally, add a .catch handler for the promise chain
  .catch(function(e) {
    console.error(e);
  });