2022-03-13 15:54:31 +02:00
# [knex.js](https://knex.github.io/documentation/)
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)
2020-12-25 21:02:20 +02:00

2020-12-25 20:56:09 +02:00
[](https://coveralls.io/r/knex/knex?branch=master)
2022-04-11 23:32:37 +02:00
[](https://libraries.io/npm/knex)
2016-09-16 23:23:56 +02:00
[](https://gitter.im/tgriesser/knex)
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
2021-10-15 18:14:32 +03:00
A batteries-included, multi-dialect (PostgreSQL, MySQL, CockroachDB, MSSQL, SQLite3, Oracle (including Oracle Wallet Authentication)) query builder for
2018-10-11 18:32:05 +02:00
Node.js, featuring:
2014-05-29 13:40:34 -04:00
2022-03-13 15:54:31 +02:00
- [transactions ](https://knex.github.io/documentation/#Transactions )
- [connection pooling ](https://knex.github.io/documentation/#Installation-pooling )
- [streaming queries ](https://knex.github.io/documentation/#Interfaces-Streams )
- both a [promise ](https://knex.github.io/documentation/#Interfaces-Promises ) and [callback ](https://knex.github.io/documentation/#Interfaces-Callbacks ) API
2020-12-27 16:30:40 +02:00
- a [thorough test suite ](https://github.com/knex/knex/actions )
2013-05-04 12:23:55 -04:00
2022-01-16 02:47:09 +02:00
Node.js versions 12+ are supported.
2018-05-03 18:44:51 +03:00
2023-07-12 21:42:57 +01:00
- Take a look at the [full documentation ](https://knex.github.io/documentation ) to get started!
- Browse the [list of plugins and tools ](https://github.com/knex/knex/blob/master/ECOSYSTEM.md ) built for knex
- Check out our [recipes wiki ](https://github.com/knex/knex/wiki/Recipes ) to search for solutions to some specific problems
- In case of upgrading from an older version, see [migration guide ](https://github.com/knex/knex/blob/master/UPGRADING.md )
2020-12-27 16:30:40 +02:00
You can report bugs and discuss features on the [GitHub issues page ](https://github.com/knex/knex/issues ) or send tweets to [@kibertoad ](http://twitter.com/kibertoad ).
2021-01-07 01:37:53 +02:00
For support and questions, join our [Gitter channel ](https://gitter.im/tgriesser/knex ).
2014-06-03 14:21:31 -04:00
2020-12-27 16:30:40 +02:00
For knex-based Object Relational Mapper, see:
2019-05-13 12:21:36 +03:00
2017-11-08 02:00:10 +01:00
- https://github.com/Vincit/objection.js
2020-12-27 16:30:40 +02:00
- https://github.com/mikro-orm/mikro-orm
- https://bookshelfjs.org
2013-10-02 02:11:12 -04:00
2021-01-07 01:37:53 +02:00
To see the SQL that Knex will generate for a given query, you can use [Knex Query Lab ](https://michaelavila.com/knex-querylab/ )
2015-08-05 18:38:37 -07:00
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
2020-12-27 16:30:40 +02:00
try {
// Create a table
await knex.schema
2023-07-12 21:42:57 +01:00
.createTable('users', (table) => {
2020-12-27 16:30:40 +02:00
table.increments('id');
table.string('user_name');
})
// ...and another
2023-07-12 21:42:57 +01:00
.createTable('accounts', (table) => {
2020-12-27 16:30:40 +02:00
table.increments('id');
table.string('account_name');
2023-07-12 21:42:57 +01:00
table.integer('user_id').unsigned().references('users.id');
});
2019-05-13 12:21:36 +03:00
// Then query the table...
2023-07-12 21:42:57 +01:00
const insertedRows = await knex('users').insert({ user_name: 'Tim' });
2019-05-13 12:21:36 +03:00
// ...and using the insert id, insert into the other table.
2023-07-12 21:42:57 +01:00
await knex('accounts').insert({
account_name: 'knex',
user_id: insertedRows[0],
});
2019-05-13 12:21:36 +03:00
// Query both of the rows.
2020-12-27 16:30:40 +02:00
const selectedRows = await knex('users')
.join('accounts', 'users.id', 'accounts.user_id')
2023-07-12 21:42:57 +01:00
.select('users.user_name as user', 'accounts.account_name as account');
2019-05-13 12:21:36 +03:00
2020-05-07 11:39:52 -04:00
// map over the results
2023-07-12 21:42:57 +01:00
const enrichedRows = selectedRows.map((row) => ({ ...row, active: true }));
2019-05-13 12:21:36 +03:00
2020-12-27 16:30:40 +02:00
// Finally, add a catch statement
2023-07-12 21:42:57 +01:00
} catch (e) {
2020-12-27 16:30:40 +02:00
console.error(e);
2023-07-12 21:42:57 +01:00
}
2014-06-04 12:12:43 -04:00
```
2020-12-30 22:26:08 +02:00
## TypeScript example
2023-07-12 21:42:57 +01:00
2021-01-16 19:23:29 +04:00
```ts
2023-07-12 21:42:57 +01:00
import { Knex, knex } from 'knex';
2020-12-30 22:26:08 +02:00
interface User {
id: number;
age: number;
name: string;
active: boolean;
departmentId: number;
}
const config: Knex.Config = {
client: 'sqlite3',
connection: {
filename: './data.db',
},
2021-05-12 04:23:45 +08:00
};
2020-12-30 22:26:08 +02:00
const knexInstance = knex(config);
try {
const users = await knex< User > ('users').select('id', 'age');
} catch (err) {
// error handling
}
```
2021-03-11 18:49:41 +02:00
## 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:
2023-07-12 21:42:57 +01:00
2021-03-11 18:49:41 +02:00
```js
2023-07-12 21:42:57 +01:00
import { knex } from 'knex/knex.mjs';
2021-03-11 18:49:41 +02:00
```
You can also just do the default import:
2023-07-12 21:42:57 +01:00
2021-03-11 18:49:41 +02:00
```js
2023-07-12 21:42:57 +01:00
import knex from 'knex';
2021-03-11 18:49:41 +02:00
```
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:
2023-07-12 21:42:57 +01:00
2021-03-11 18:49:41 +02:00
```js
/**
* @type {Knex}
*/
const database = knex({
2023-07-12 21:42:57 +01:00
client: 'mysql',
connection: {
host: '127.0.0.1',
user: 'your_database_user',
password: 'your_database_password',
database: 'myapp_test',
},
});
2021-03-11 18:49:41 +02:00
database.migrate.latest();
```