Sign Up for Free

RunKit +

Try any Node.js package right in your browser

This is a playground to test code. It runs a full Node.js environment and already has all of npm’s 400,000 packages pre-installed, including knex with all npm packages installed. Try it out:

require('sqlite3'); const Knex = require('knex'); const knexSqlite = Knex({ client: 'sqlite', connection: ':memory:', }); const knexMysql = Knex({ client: 'mysql2', }); const knexPg = Knex({ client: 'pg', }); (async function run() { await knexSqlite.schema.createTable('test', (t) => { t.increments('id').primary(); t.string('data'); }); await knexSqlite('test').insert([{ data: 'foo' }, { data: 'bar' }]); console.log('test table data:', await knexSqlite('test')); console.log( knexPg({ f: 'foo', b: 'bar' }) .select('foo.*') .where('f.name', knexPg.raw('??', ['b.name'])) .whereIn('something', knexPg('bar').select('id')) .toSQL().sql ); })();

This service is provided by RunKit and is not affiliated with npm, Inc or the package authors.

knex v0.21.2

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

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 10+ 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:

  • http://bookshelfjs.org
  • https://github.com/Vincit/objection.js

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

Metadata

RunKit is a free, in-browser JavaScript dev environment for prototyping Node.js code, with every npm package installed. Sign up to share your code.
Sign Up for Free