knex
    Overview
    Insights & Analysis
    Code
    Versions
    Find an Expert
    Documentation
    Playground
    Issues
    Career Opportunities
    Alternatives
    Dependencies

knex

A batteries-included SQL query & schema builder for Postgres, MySQL and SQLite3 and the Browser

0.19.2  •  Published 6 days ago  •  by Tim Griesser  •  MIT License

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')({
  dialect: '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);
  });

Dependencies

bluebird  ·  colorette  ·  commander  ·  debug  ·  getopts  ·  inherits  ·  interpret  ·  liftoff  ·  lodash  ·  mkdirp  ·  pg-connection-string  ·  tarn  ·  tildify  ·  uuid  ·  v8flags  ·  @types/node  ·  JSONStream  ·  chai  ·  chai-subset-in-order  ·  cli-testlab  ·  coveralls  ·  cross-env  ·  dtslint  ·  eslint  ·  eslint-config-prettier  ·  eslint-plugin-import  ·  husky  ·  jake  ·  lint-staged  ·  mocha  ·  mock-fs  ·  mssql  ·  mysql  ·  mysql2  ·  nyc  ·  pg  ·  pg-query-stream  ·  prettier  ·  rimraf  ·  sinon  ·  sinon-chai  ·  source-map-support  ·  sqlite3  ·  tap-spec  ·  tape  ·  toxiproxy-node-client  ·  typescript  ·  webpack-cli

Tags

sql
query
postgresql
mysql
sqlite3
oracle
mssql
Avoid unsafe or low quality packages
Warn or block the installation of vulnerable, badly licensed, deprecated, outdated, unmaintained, or unpopular packages. Integrates with your npm and pre-commit/build scripts
Just $9/month per developer

Popularity

Weekly Downloads
324.9K
Stars
10.2K

Issues and PRs

Activity

Last ver 6 days ago
Created 6 years ago
Last commit 1 month ago
1 day between commits

Sustainability

215 contributors

Technology

Node version: 12.4.0
551.6K unpacked

Legal and Compliance

MIT License
OSI Approved
0 vulnerabilities

Top Experts

Mikael Lepistö
Maintainer, 142 commits, 215 merges, 48 PRs
Works at Vincit
Igor Savin
Maintainer, 121 commits, 145 merges, 95 PRs
Tim Griesser
Maintainer, 82 commits, 163 merges, 29 PRs
Works at cypress-io
wubzz
Maintainer, 170 commits, 59 merges, 83 PRs
Rhys van der Waerden
Maintainer, 89 commits, 74 merges, 8 PRs
Works at usabilityhub
Ben Drucker
Maintainer, 31 merges, 10 PRs
Works at TakeScoop