knex
A batteries-included SQL query & schema builder for Postgres, MySQL and SQLite3 and the Browser
Last updated 10 years ago by tgriesser .
MIT · Repository · Bugs · Original npm · Tarball · package.json
$ npm install knex 
SYNC missed versions from official npm registry.

knex.js

npm version npm downloads Coverage Status Dependencies Status Gitter chat

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

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

Node.js versions 12+ are supported.

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:

To see the SQL that Knex will generate for a given query, you can use 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',
  },
});

try {
  // Create a table
  await 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...
  const insertedRows = await knex('users').insert({ user_name: 'Tim' });

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

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

  // map over the results
  const enrichedRows = selectedRows.map((row) => ({ ...row, active: true }));

  // Finally, add a catch statement
} catch (e) {
  console.error(e);
}

TypeScript example

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

const knexInstance = knex(config);

try {
  const users = await knex<User>('users').select('id', 'age');
} catch (err) {
  // error handling
}

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:

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

You can also just do the default import:

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:

/**
 * @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();

Current Tags

  • 0.21.5-next1                                ...           beta (4 years ago)
  • 3.1.0                                ...           latest (a year ago)
  • 2.5.1                                ...           next (a year ago)
  • 0.16.6-oracle-fix                                ...           oracle-fix (6 years ago)

252 Versions

  • 3.1.0                                ...           a year ago
  • 3.0.1                                ...           a year ago
  • 3.0.0                                ...           a year ago
  • 2.5.1                                ...           a year ago
  • 2.5.0                                ...           a year ago
  • 2.4.2                                ...           2 years ago
  • 2.4.1                                ...           2 years ago
  • 2.4.0                                ...           2 years ago
  • 2.3.0                                ...           2 years ago
  • 2.2.0                                ...           2 years ago
  • 2.1.0                                ...           2 years ago
  • 2.0.0                                ...           3 years ago
  • 1.0.7                                ...           3 years ago
  • 1.0.6                                ...           3 years ago
  • 1.0.5                                ...           3 years ago
  • 1.0.4                                ...           3 years ago
  • 1.0.3                                ...           3 years ago
  • 1.0.2                                ...           3 years ago
  • 1.0.1                                ...           3 years ago
  • 1.0.0                                ...           3 years ago
  • 0.95.15                                ...           3 years ago
  • 0.95.14                                ...           3 years ago
  • 0.95.13                                ...           3 years ago
  • 0.95.12                                ...           3 years ago
  • 0.95.12-rc6                                ...           3 years ago
  • 0.95.12-rc5                                ...           3 years ago
  • 0.95.12-rc4                                ...           3 years ago
  • 0.95.12-rc3                                ...           3 years ago
  • 0.95.12-rc2                                ...           3 years ago
  • 0.95.12-rc1                                ...           3 years ago
  • 0.95.11                                ...           3 years ago
  • 0.95.10                                ...           3 years ago
  • 0.21.21                                ...           3 years ago
  • 0.21.20 [deprecated]           ...           3 years ago
  • 0.95.9                                ...           3 years ago
  • 0.95.8                                ...           3 years ago
  • 0.95.7                                ...           3 years ago
  • 0.95.6                                ...           3 years ago
  • 0.95.5                                ...           4 years ago
  • 0.95.4                                ...           4 years ago
  • 0.95.3                                ...           4 years ago
  • 0.95.2                                ...           4 years ago
  • 0.95.1                                ...           4 years ago
  • 0.95.0                                ...           4 years ago
  • 0.21.19                                ...           4 years ago
  • 0.21.18                                ...           4 years ago
  • 0.95.0-next3                                ...           4 years ago
  • 0.95.0-next2                                ...           4 years ago
  • 0.95.0-next1                                ...           4 years ago
  • 0.21.17                                ...           4 years ago
  • 0.21.16                                ...           4 years ago
  • 0.21.15                                ...           4 years ago
  • 0.21.14                                ...           4 years ago
  • 0.21.13                                ...           4 years ago
  • 0.21.12                                ...           4 years ago
  • 0.21.11                                ...           4 years ago
  • 0.21.10                                ...           4 years ago
  • 0.21.9                                ...           4 years ago
  • 0.21.8                                ...           4 years ago
  • 0.21.7                                ...           4 years ago
  • 0.21.6                                ...           4 years ago
  • 0.21.5                                ...           4 years ago
  • 0.21.5-next2                                ...           4 years ago
  • 0.21.5-next1                                ...           4 years ago
  • 0.21.4                                ...           4 years ago
  • 0.21.3                                ...           4 years ago
  • 0.21.2                                ...           4 years ago
  • 0.21.1                                ...           5 years ago
  • 0.21.0                                ...           5 years ago
  • 0.20.15                                ...           5 years ago
  • 0.20.14                                ...           5 years ago
  • 0.20.13                                ...           5 years ago
  • 0.20.12                                ...           5 years ago
  • 0.20.11                                ...           5 years ago
  • 0.20.10                                ...           5 years ago
  • 0.20.9                                ...           5 years ago
  • 0.20.8                                ...           5 years ago
  • 0.20.7                                ...           5 years ago
  • 0.20.6                                ...           5 years ago
  • 0.20.4                                ...           5 years ago
  • 0.20.3                                ...           5 years ago
  • 0.20.2                                ...           5 years ago
  • 0.20.1                                ...           5 years ago
  • 0.20.0                                ...           5 years ago
  • 0.19.5                                ...           5 years ago
  • 0.19.4                                ...           5 years ago
  • 0.19.3                                ...           5 years ago
  • 0.19.2                                ...           5 years ago
  • 0.19.1                                ...           5 years ago
  • 0.19.0                                ...           5 years ago
  • 0.18.4                                ...           5 years ago
  • 0.18.3                                ...           5 years ago
  • 0.18.2                                ...           5 years ago
  • 0.18.1                                ...           5 years ago
  • 0.18.0                                ...           5 years ago
  • 0.18.0-next4                                ...           5 years ago
  • 0.18.0-next2                                ...           5 years ago
  • 0.18.0-next1                                ...           5 years ago
  • 0.17.6                                ...           5 years ago
  • 0.17.5                                ...           5 years ago
  • 0.17.4                                ...           5 years ago
  • 0.17.3                                ...           5 years ago
  • 0.17.2                                ...           5 years ago
  • 0.17.1                                ...           5 years ago
  • 0.17.1-next                                ...           5 years ago
  • 0.17.0                                ...           5 years ago
  • 0.17.0-next6                                ...           5 years ago
  • 0.17.0-next5                                ...           5 years ago
  • 0.17.0-next4                                ...           5 years ago
  • 0.17.0-next2                                ...           5 years ago
  • 0.17.0-next                                ...           6 years ago
  • 0.16.6-oracle-fix [deprecated]           ...           6 years ago
  • 0.16.5                                ...           6 years ago
  • 0.16.4                                ...           6 years ago
  • 0.16.4-next2                                ...           6 years ago
  • 0.16.4-next1                                ...           6 years ago
  • 0.16.3                                ...           6 years ago
  • 0.16.2                                ...           6 years ago
  • 0.16.1                                ...           6 years ago
  • 0.16.1-next2                                ...           6 years ago
  • 0.16.1-next1                                ...           6 years ago
  • 0.16.0-next5                                ...           6 years ago
  • 0.16.0-next4                                ...           6 years ago
  • 0.16.0-next3                                ...           6 years ago
  • 0.16.0-next2                                ...           6 years ago
  • 0.16.0-next1                                ...           6 years ago
  • 0.15.2                                ...           6 years ago
  • 0.15.1                                ...           6 years ago
  • 0.15.0                                ...           6 years ago
  • 0.14.6                                ...           7 years ago
  • 0.14.5                                ...           7 years ago
  • 0.14.4                                ...           7 years ago
  • 0.14.3                                ...           7 years ago
  • 0.14.2                                ...           7 years ago
  • 0.14.1                                ...           7 years ago
  • 0.14.0                                ...           7 years ago
  • 0.13.0                                ...           8 years ago
  • 0.12.9                                ...           8 years ago
  • 0.12.8                                ...           8 years ago
  • 0.12.7                                ...           8 years ago
  • 0.12.6                                ...           8 years ago
  • 0.12.5                                ...           8 years ago
  • 0.12.4 [deprecated]           ...           8 years ago
  • 0.12.3                                ...           8 years ago
  • 0.12.2                                ...           8 years ago
  • 0.12.1                                ...           8 years ago
  • 0.12.0                                ...           8 years ago
  • 0.11.10                                ...           8 years ago
  • 0.11.9                                ...           8 years ago
  • 0.11.8                                ...           8 years ago
  • 0.11.7                                ...           8 years ago
  • 0.11.6                                ...           8 years ago
  • 0.11.5                                ...           8 years ago
  • 0.11.4                                ...           8 years ago
  • 0.11.3                                ...           9 years ago
  • 0.11.2                                ...           9 years ago
  • 0.11.1                                ...           9 years ago
  • 0.11.0                                ...           9 years ago
  • 0.10.0                                ...           9 years ago
  • 0.10.0-rc1                                ...           9 years ago
  • 0.9.0                                ...           9 years ago
  • 0.8.6                                ...           9 years ago
  • 0.8.5                                ...           10 years ago
  • 0.8.4                                ...           10 years ago
  • 0.8.3                                ...           10 years ago
  • 0.8.2                                ...           10 years ago
  • 0.8.1                                ...           10 years ago
  • 0.8.0                                ...           10 years ago
  • 0.7.6                                ...           10 years ago
  • 0.6.23                                ...           10 years ago
  • 0.7.5                                ...           10 years ago
  • 0.7.4                                ...           10 years ago
  • 0.7.3                                ...           10 years ago
  • 0.7.2                                ...           10 years ago
  • 0.7.1                                ...           10 years ago
  • 0.7.0                                ...           10 years ago
  • 0.5.16                                ...           10 years ago
  • 0.6.22                                ...           10 years ago
  • 0.6.21                                ...           10 years ago
  • 0.6.20                                ...           10 years ago
  • 0.6.19                                ...           10 years ago
  • 0.6.18                                ...           10 years ago
  • 0.6.17                                ...           10 years ago
  • 0.6.16                                ...           10 years ago
  • 0.6.15                                ...           10 years ago
  • 0.6.14                                ...           10 years ago
  • 0.6.13                                ...           10 years ago
  • 0.6.12                                ...           10 years ago
  • 0.6.11                                ...           10 years ago
  • 0.6.10                                ...           10 years ago
  • 0.6.9                                ...           10 years ago
  • 0.6.8                                ...           10 years ago
  • 0.6.7                                ...           10 years ago
  • 0.6.6                                ...           10 years ago
  • 0.6.5                                ...           10 years ago
  • 0.6.4                                ...           10 years ago
  • 0.6.3                                ...           10 years ago
  • 0.6.2                                ...           10 years ago
  • 0.6.1                                ...           10 years ago
  • 0.6.0                                ...           10 years ago
  • 0.5.15                                ...           10 years ago
  • 0.5.14                                ...           11 years ago
  • 0.5.13                                ...           11 years ago
  • 0.5.12                                ...           11 years ago
  • 0.5.11                                ...           11 years ago
  • 0.5.10                                ...           11 years ago
  • 0.5.9                                ...           11 years ago
  • 0.5.8                                ...           11 years ago
  • 0.5.7                                ...           11 years ago
  • 0.5.6                                ...           11 years ago
  • 0.5.5                                ...           11 years ago
  • 0.5.4                                ...           11 years ago
  • 0.5.3                                ...           11 years ago
  • 0.5.2                                ...           11 years ago
  • 0.5.1                                ...           11 years ago
  • 0.5.0                                ...           11 years ago
  • 0.4.13                                ...           11 years ago
  • 0.4.12                                ...           11 years ago
  • 0.4.11                                ...           11 years ago
  • 0.4.10                                ...           11 years ago
  • 0.4.9                                ...           11 years ago
  • 0.4.8                                ...           11 years ago
  • 0.4.7                                ...           11 years ago
  • 0.4.6                                ...           11 years ago
  • 0.4.5                                ...           11 years ago
  • 0.4.4                                ...           11 years ago
  • 0.4.3                                ...           11 years ago
  • 0.2.7-alpha                                ...           11 years ago
  • 0.4.2                                ...           11 years ago
  • 0.4.1                                ...           11 years ago
  • 0.4.0                                ...           11 years ago
  • 0.2.6                                ...           11 years ago
  • 0.2.5                                ...           11 years ago
  • 0.2.4                                ...           11 years ago
  • 0.2.3                                ...           11 years ago
  • 0.2.2                                ...           11 years ago
  • 0.2.1                                ...           11 years ago
  • 0.2.0                                ...           11 years ago
  • 0.1.8                                ...           11 years ago
  • 0.1.7                                ...           11 years ago
  • 0.1.6                                ...           11 years ago
  • 0.1.5                                ...           11 years ago
  • 0.1.4                                ...           11 years ago
  • 0.1.3                                ...           11 years ago
  • 0.1.2                                ...           11 years ago
  • 0.1.1                                ...           12 years ago
  • 0.1.0                                ...           12 years ago
  • 0.0.5                                ...           12 years ago
  • 0.0.3                                ...           12 years ago
  • 0.0.2                                ...           12 years ago
  • 0.0.1                                ...           12 years ago
  • 0.0.0                                ...           12 years ago
Maintainers (1)
Downloads
Total 2
Today 1
This Week 1
This Month 1
Last Day 0
Last Week 0
Last Month 0
Dependencies (8)
Dev Dependencies (20)

© 2010 - cnpmjs.org x YWFE | Home | YWFE