knex

A query builder for PostgreSQL, MySQL, SQL Server, SQLite3 and Oracle, designed to be flexible, portable, and fun to use.

Showing:

Popularity

Downloads/wk

831K

GitHub Stars

14.7K

Maintenance

Last Commit

7d ago

Contributors

472

Reviews

Average Rating

4.3/523
Read All Reviews
s-r-aman
oldCoder29
Charles8211
nikhil2882
sawan-hardcoder
katharinbenson
bvego

Top Feedback

18Great Documentation
12Easy to Use
11Performant
2Highly Customizable

Readme

knex.js

npm version npm downloads 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.

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

Rate & Review

Great Documentation18
Easy to Use12
Performant11
Highly Customizable2
Bleeding Edge0
Responsive Maintainers0
Poor Documentation0
Hard to Use0
Slow0
Buggy0
Abandoned0
Unwelcoming Community0
100
SR AmanNew Delhi, India88 Ratings93 Reviews
Trying new things.
2 months ago
Great Documentation
Easy to Use

I feel like the best award for no complexity and shenanigans awards for any orm should go to knex. It has such a huge support for so many databases that are SQL. And you can provide an underlying connection, this is the tool that I reach out for whenever I am doing anything with SQL, that is easy and not in typescript. Its easy enough, the docs are great, learning curve is like non existent. You will be really happy choosing this, has no bugs at all. And has support for raw queries too. Great great package, and such a joy to work with. Cannot compliment more. But this is only for intermediate and there is very less typescript support, I would like to see some robust typecript support from the knex team, otherwise a great package.


1
monsieurpigeon
oldCoder2978 Ratings81 Reviews
8 months ago
Easy to Use
Great Documentation
Performant

Knex makes query building easy without worry about the syntax of different SQL, if you need to fast development of your application and wan to deal with database part and related orm setup to a minimum I think knex would be a perfect fit there. Making SQL queries to a minimum and very beginner-friendly. However, there are few downsides to this, as if you require complex queries you need to dig down deeper in the library.


1
monsieurpigeon
Charles821153 Ratings47 Reviews
5 days ago
Great Documentation
Easy to Use
Performant

I prefer SQL DBs over No-SQL DBs and that is why i use knexjs a lot in my projects. The API is well crafted and abstracted which makes it easy to use. The community around this project is wide as well. And the best in class documentation covers many advanced use cases that I typically run into on a daily basis. Highly recommended.


1
ashikmeerankutty
nikhil288248 Ratings56 Reviews
8 months ago

When dealing with different databases and every time you have to recall different querying methods for a different type of db, but knex made it very easy, when I constantly keep on switching DBS in different projects, knex keep me good on the DB part and I have less number of confusions while dealing with the data part.


1
monsieurpigeon
sawan-hardcoder41 Ratings66 Reviews
7 months ago
Easy to Use
Great Documentation

I mostly switch between databases and it sucks, I mean it first I have a great ORM with MongoDB and then I switch back to SQL and I am writing raw queries, knex is great if you in a similar situation it is great to query builder and similar to sequilize. you need to spend some time tough but it will be worth it


1
monsieurpigeon