2016-09-16 23:23:56 +02:00
|
|
|
# [knex.js](http://knexjs.org)
|
2014-05-29 01:57:59 -04:00
|
|
|
|
2016-09-16 23:23:56 +02:00
|
|
|
[](https://npmjs.org/package/knex)
|
2019-06-18 01:42:43 +02:00
|
|
|
[](https://npmjs.org/package/knex)
|
2019-10-15 08:16:35 -03:00
|
|
|
[](https://travis-ci.org/knex/knex)
|
2016-09-16 23:23:56 +02:00
|
|
|
[](https://coveralls.io/r/tgriesser/knex?branch=master)
|
|
|
|
|
[](https://david-dm.org/tgriesser/knex)
|
|
|
|
|
[](https://gitter.im/tgriesser/knex)
|
2019-10-15 08:16:35 -03:00
|
|
|
[](https://lgtm.com/projects/g/knex/knex/context:javascript)
|
2015-05-08 14:41:24 -04:00
|
|
|
|
2016-09-16 23:23:56 +02:00
|
|
|
> **A SQL query builder that is _flexible_, _portable_, and _fun_ to use!**
|
2014-05-29 01:57:59 -04:00
|
|
|
|
2018-10-11 18:32:05 +02:00
|
|
|
A batteries-included, multi-dialect (MSSQL, MySQL, PostgreSQL, SQLite3, Oracle (including Oracle Wallet Authentication)) query builder for
|
|
|
|
|
Node.js, featuring:
|
2014-05-29 13:40:34 -04:00
|
|
|
|
|
|
|
|
- [transactions](http://knexjs.org/#Transactions)
|
2014-06-04 12:24:33 -04:00
|
|
|
- [connection pooling](http://knexjs.org/#Installation-pooling)
|
|
|
|
|
- [streaming queries](http://knexjs.org/#Interfaces-Streams)
|
|
|
|
|
- both a [promise](http://knexjs.org/#Interfaces-Promises) and [callback](http://knexjs.org/#Interfaces-Callbacks) API
|
2019-12-10 22:52:36 +01:00
|
|
|
- a [thorough test suite](https://travis-ci.org/knex/knex)
|
2014-06-04 12:24:33 -04:00
|
|
|
- the ability to [run in the Browser](http://knexjs.org/#Installation-browser)
|
2013-05-04 12:23:55 -04:00
|
|
|
|
2019-06-04 00:37:17 +02:00
|
|
|
Node.js versions 8+ are supported.
|
2018-05-03 18:44:51 +03:00
|
|
|
|
2018-11-16 13:44:02 +01:00
|
|
|
[Read the full documentation to get started!](http://knexjs.org)
|
2019-12-10 22:52:36 +01:00
|
|
|
[Or check out our Recipes wiki to search for solutions to some specific problems](https://github.com/knex/knex/wiki/Recipes)
|
|
|
|
|
If upgrading from older version, see [Upgrading instructions](https://github.com/knex/knex/blob/master/UPGRADING.md)
|
2013-05-13 10:38:28 -04:00
|
|
|
|
2016-09-16 23:23:56 +02:00
|
|
|
For support and questions, join the `#bookshelf` channel on freenode IRC
|
2014-06-03 14:21:31 -04:00
|
|
|
|
2018-06-18 21:36:02 +02:00
|
|
|
For an Object Relational Mapper, see:
|
2019-05-13 12:21:36 +03:00
|
|
|
|
2017-11-08 02:00:10 +01:00
|
|
|
- http://bookshelfjs.org
|
|
|
|
|
- https://github.com/Vincit/objection.js
|
2013-10-02 02:11:12 -04:00
|
|
|
|
2015-08-05 18:38:37 -07:00
|
|
|
To see the SQL that Knex will generate for a given query, see: [Knex Query Lab](http://michaelavila.com/knex-querylab/)
|
|
|
|
|
|
2014-06-03 09:27:40 -04:00
|
|
|
## Examples
|
2014-05-29 01:57:59 -04:00
|
|
|
|
2014-06-03 09:27:40 -04:00
|
|
|
We have several examples [on the website](http://knexjs.org). Here is the first one to get you started:
|
2014-05-29 01:57:59 -04:00
|
|
|
|
|
|
|
|
```js
|
2018-10-11 18:32:05 +02:00
|
|
|
const knex = require('knex')({
|
2020-01-04 16:57:51 -05:00
|
|
|
client: 'sqlite3',
|
2014-09-16 09:14:35 -04:00
|
|
|
connection: {
|
2019-05-13 12:21:36 +03:00
|
|
|
filename: './data.db',
|
|
|
|
|
},
|
2014-06-03 01:37:19 -04:00
|
|
|
});
|
2014-05-29 01:57:59 -04:00
|
|
|
|
|
|
|
|
// Create a table
|
2019-05-13 12:21:36 +03:00
|
|
|
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);
|
|
|
|
|
});
|
2014-06-04 12:12:43 -04:00
|
|
|
```
|