1 | # [knex.js](http://knexjs.org)
|
2 |
|
3 | [![npm version](http://img.shields.io/npm/v/knex.svg)](https://npmjs.org/package/knex)
|
4 | [![npm downloads](https://img.shields.io/npm/dm/knex.svg)](https://npmjs.org/package/knex)
|
5 | [![Build Status](https://travis-ci.org/knex/knex.svg?branch=master)](https://travis-ci.org/knex/knex)
|
6 | [![Coverage Status](https://coveralls.io/repos/tgriesser/knex/badge.svg?branch=master)](https://coveralls.io/r/tgriesser/knex?branch=master)
|
7 | [![Dependencies Status](https://david-dm.org/knex/knex.svg)](https://david-dm.org/knex/knex)
|
8 | [![Gitter chat](https://badges.gitter.im/tgriesser/knex.svg)](https://gitter.im/tgriesser/knex)
|
9 | [![Language Grade: JavaScript](https://img.shields.io/lgtm/grade/javascript/g/knex/knex.svg?logo=lgtm&logoWidth=18)](https://lgtm.com/projects/g/knex/knex/context:javascript)
|
10 |
|
11 | > **A SQL query builder that is _flexible_, _portable_, and _fun_ to use!**
|
12 |
|
13 | A batteries-included, multi-dialect (MSSQL, MySQL, PostgreSQL, SQLite3, Oracle (including Oracle Wallet Authentication)) query builder for
|
14 | Node.js, featuring:
|
15 |
|
16 | - [transactions](http://knexjs.org/#Transactions)
|
17 | - [connection pooling](http://knexjs.org/#Installation-pooling)
|
18 | - [streaming queries](http://knexjs.org/#Interfaces-Streams)
|
19 | - both a [promise](http://knexjs.org/#Interfaces-Promises) and [callback](http://knexjs.org/#Interfaces-Callbacks) API
|
20 | - a [thorough test suite](https://travis-ci.org/knex/knex)
|
21 | - the ability to [run in the Browser](http://knexjs.org/#Installation-browser)
|
22 |
|
23 | Node.js versions 10+ are supported.
|
24 |
|
25 | [Read the full documentation to get started!](http://knexjs.org)
|
26 | [Or check out our Recipes wiki to search for solutions to some specific problems](https://github.com/knex/knex/wiki/Recipes)
|
27 | If upgrading from older version, see [Upgrading instructions](https://github.com/knex/knex/blob/master/UPGRADING.md)
|
28 |
|
29 | For support and questions, join the `#bookshelf` channel on freenode IRC
|
30 |
|
31 | For an Object Relational Mapper, see:
|
32 |
|
33 | - http://bookshelfjs.org
|
34 | - https://github.com/Vincit/objection.js
|
35 |
|
36 | To see the SQL that Knex will generate for a given query, see: [Knex Query Lab](https://michaelavila.com/knex-querylab/)
|
37 |
|
38 | ## Examples
|
39 |
|
40 | We have several examples [on the website](http://knexjs.org). Here is the first one to get you started:
|
41 |
|
42 | ```js
|
43 | const knex = require('knex')({
|
44 | client: 'sqlite3',
|
45 | connection: {
|
46 | filename: './data.db',
|
47 | },
|
48 | });
|
49 |
|
50 | // Create a table
|
51 | knex.schema
|
52 | .createTable('users', table => {
|
53 | table.increments('id');
|
54 | table.string('user_name');
|
55 | })
|
56 |
|
57 | // ...and another
|
58 | .createTable('accounts', table => {
|
59 | table.increments('id');
|
60 | table.string('account_name');
|
61 | table
|
62 | .integer('user_id')
|
63 | .unsigned()
|
64 | .references('users.id');
|
65 | })
|
66 |
|
67 | // Then query the table...
|
68 | .then(() =>
|
69 | knex('users').insert({ user_name: 'Tim' })
|
70 | )
|
71 |
|
72 | // ...and using the insert id, insert into the other table.
|
73 | .then(rows =>
|
74 | knex('accounts').insert({ account_name: 'knex', user_id: rows[0] })
|
75 | )
|
76 |
|
77 | // Query both of the rows.
|
78 | .then(() =>
|
79 | knex('users')
|
80 | .join('accounts', 'users.id', 'accounts.user_id')
|
81 | .select('users.user_name as user', 'accounts.account_name as account')
|
82 | )
|
83 |
|
84 | // map over the results
|
85 | .then(rows =>
|
86 | rows.map(row => {
|
87 | console.log(row)
|
88 | })
|
89 | )
|
90 |
|
91 | // Finally, add a .catch handler for the promise chain
|
92 | .catch(e => {
|
93 | console.error(e);
|
94 | });
|
95 | ```
|