knex.js

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

Github星跟蹤圖

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);
  });


主要指標

概覽
名稱與所有者knex/knex
主編程語言JavaScript
編程語言JavaScript (語言數: 3)
平台Linux, Mac, Windows
許可證MIT License
所有者活动
創建於2012-12-29 05:18:25
推送於2025-05-19 12:22:42
最后一次提交2025-02-28 09:52:04
發布數183
最新版本名稱3.1.0 (發布於 )
第一版名稱0.1.0 (發布於 )
用户参与
星數19.8k
關注者數204
派生數2.2k
提交數3.1k
已啟用問題?
問題數3992
打開的問題數1056
拉請求數1470
打開的拉請求數145
關閉的拉請求數595
项目设置
已啟用Wiki?
已存檔?
是復刻?
已鎖定?
是鏡像?
是私有?

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);
  });