cheatsheets/knex.md

519 lines
7.2 KiB
Markdown
Raw Permalink Normal View History

2017-09-23 01:41:37 +00:00
---
title: Knex
2020-07-04 13:33:09 +00:00
updated: 2020-06-03
2018-12-06 22:15:40 +00:00
category: Databases
2017-09-23 02:17:34 +00:00
intro: |
[Knex](http://knexjs.org/) is an SQL query builder for Node.js.
This guide targets v0.13.0.
2017-09-23 01:41:37 +00:00
---
## Getting started
{: .-three-column}
2017-09-23 02:17:34 +00:00
### Connect
```js
require('knex')({
client: 'pg',
connection: 'postgres://user:pass@localhost:5432/dbname'
})
```
See: [Connect](#connect-1)
2017-09-23 01:41:37 +00:00
### Create table
```js
2017-09-23 02:17:34 +00:00
knex.schema.createTable('user', (table) => {
2017-09-23 01:41:37 +00:00
table.increments('id')
2017-09-23 02:17:34 +00:00
table.string('name')
table.integer('age')
2017-09-23 01:41:37 +00:00
})
2017-09-23 02:17:34 +00:00
.then(() => ···)
2017-09-23 01:41:37 +00:00
```
2017-09-23 02:17:34 +00:00
See: [Schema](#schema)
2017-09-23 01:41:37 +00:00
### Select
```js
knex('users')
2017-09-23 02:17:34 +00:00
.where({ email: 'hi@example.com' })
.then(rows => ···)
```
{: data-line="2"}
2018-11-07 20:29:10 +00:00
See: [Select](#select-1)
2017-09-23 02:17:34 +00:00
### Insert
```js
knex('users')
.insert({ email: 'hi@example.com' })
```
{: data-line="2"}
See: [Insert](#insert-1)
### Update
```js
knex('users')
.where({ id: 135 })
.update({ email: 'hi@example.com' })
```
{: data-line="2,3"}
See: [Update](#update-1)
### Migrations
2020-01-13 21:29:07 +00:00
```bash
2017-09-23 02:17:34 +00:00
knex init
knex migrate:make migration_name
2020-01-13 21:29:07 +00:00
knex migrate:make migration_name -x ts # Generates a TypeScript migration file
2017-09-23 02:17:34 +00:00
knex migrate:latest
knex migrate:rollback
2017-09-23 01:41:37 +00:00
```
2017-09-23 02:17:34 +00:00
See: [Migrations](#migrations-1)
2020-01-13 21:29:07 +00:00
### Seeds
```bash
knex seed:make seed_name
knex seed:make seed_name -x ts # Generates a TypeScript seed file
knex seed:run # Runs all seed files
knex seed:run --specific=seed-filename.js # Runs a specific seed file
```
See: [Seeds](http://knexjs.org/#Seeds)
2017-09-23 01:41:37 +00:00
## Connect
{: .-three-column}
### Libraries
| `pg` | PostgreSQL |
| `mysql` | MySQL or MariaDB |
| `sqlite3` | Sqlite3 |
| `mssql` | MSSQL |
Install any of these packages along with `knex`.
See: [Node.js installation](http://knexjs.org/#Installation-node)
### Connect via host
```js
var knex = require('knex')({
client: 'mysql',
connection: {
host: '127.0.0.1',
user: 'your_database_user',
password: 'your_database_password',
database: 'myapp_test'
},
pool: { min: 0, max: 7 }
})
```
{: data-line="2,3"}
See: [Initializing the library](http://knexjs.org/#Installation-client)
### Connect via URL
```js
var pg = require('knex')({
client: 'pg',
connection: process.env.DATABASE_URL,
searchPath: 'knex,public',
pool: { min: 0, max: 7 }
})
```
{: data-line="2,3"}
### Connect via Sqlite
```js
var knex = require('knex')({
client: 'sqlite3',
connection: { filename: './mydb.sqlite' }
})
```
{: data-line="2,3"}
## Select
### Where
```js
knex
.from('books')
.select('title', 'author', 'year')
```
#### Where
```js
.where('title', 'Hello')
.where({ title: 'Hello' })
.whereIn('id', [1, 2, 3])
.whereNot(···)
2020-06-03 13:51:11 +00:00
.whereNotIn('id', [1, 2, 3])
2017-09-23 01:41:37 +00:00
```
#### Where conditions
```js
.whereNull('updated_at')
.whereNotNull(···)
```
```js
.whereExists('updated_at')
.whereNotExists(···)
```
```js
.whereBetween('votes', [1, 100])
.whereNotBetween(···)
```
```js
.whereRaw('id = ?', [1])
```
#### Where grouping
```js
.where(function () {
this
.where('id', 1)
.orWhere('id', '>', 10)
})
```
2017-09-23 02:17:34 +00:00
See: [Where clauses](http://knexjs.org/#Builder-wheres)
2017-09-23 01:41:37 +00:00
### Join
```js
knex('users')
```
#### Basic join
```js
.join('contacts', 'users.id', '=', 'contacts.id')
.join('contacts', {'users.id': 'contacts.id'})
```
#### Strings
```js
.join('accounts', 'accounts.type', '=', knex.raw('?', ['admin']))
```
#### Directions
```js
.leftJoin(···)
.leftOuterJoin(···)
.rightJoin(···)
.rightOuterJoin(···)
.outerJoin(···)
.fullOuterJoin(···)
.crossJoin(···)
```
#### Raw
```js
.joinRaw('natural full join table1')
```
#### Grouping
```js
.join('accounts', function () {
this
.on('accounts.id', '=', 'users.account_id')
.orOn('accounts.owner_id', '=', 'users.id')
.onIn('accounts.id', [1, 2, 3, 5, 8])
.onNotIn(···)
.onNull('accounts.email')
.onNotNull(···)
.onExists(function () {
this.select(···)
})
.onNotExists(···)
})
```
2017-09-23 02:17:34 +00:00
See: [Join methods](http://knexjs.org/#Builder-join)
2017-09-23 01:41:37 +00:00
### Others
```js
knex('users')
.distinct()
```
#### Group
```js
.groupBy('count')
.groupByRaw('year WITH ROLLUP')
```
#### Order
```js
.orderBy('name', 'desc')
.orderByRaw('name DESC')
```
#### Offset/limit
```js
.offset(10)
.limit(20)
```
#### Having
```js
.having('count', '>', 100)
.havingIn('count', [1, 100])
```
#### Union
```js
.union(function() {
this.select(···)
})
.unionAll(···)
```
2017-09-23 02:17:34 +00:00
See: [Query builder](http://knexjs.org/#Builder)
2017-09-23 01:41:37 +00:00
### Etc
```js
knex('users')
.pluck('id')
.then(ids => { ··· })
```
```js
knex('users')
.first()
.then(user => { ··· })
```
#### Booleans
```js
.count('active')
.count('active as is_active')
```
#### Numbers
```js
.min('age')
.max('age')
.sum('age')
.sumDistinct('age')
.avg('age')
```
2017-09-23 02:17:34 +00:00
See: [Query builder](http://knexjs.org/#Builder)
2017-09-23 01:41:37 +00:00
## Schema
### Create table
```js
2017-09-23 02:17:34 +00:00
knex.schema.createTable('accounts', table => {
```
#### Columns
```js
2017-09-23 01:41:37 +00:00
table.increments('id')
table.string('account_name')
2017-09-23 02:17:34 +00:00
table.integer('age')
table.float('age')
table.decimal('balance', 8, 2)
table.boolean('is_admin')
table.date('birthday')
table.time('created_at')
table.timestamp('created_at').defaultTo(knex.fn.now())
table.json('profile')
table.jsonb('profile')
table.uuid('id').primary()
```
#### Constraints
```js
table.unique('email')
table.unique(['email', 'company_id'])
table.dropUnique(···)
```
#### Indices
```js
table.foreign('company_id')
.references('companies.id')
table.dropForeign(···)
```
#### Variations
```js
table.integer('user_id')
.unsigned()
.references('users.id')
```
```js
2017-09-23 01:41:37 +00:00
})
2017-09-23 02:17:34 +00:00
.then(() => ···)
```
{: .-setup}
See: [Schema builder](http://knexjs.org/#Schema)
### Alter table
```js
knex.schema.table('accounts', table => {
```
#### Create
```js
table.string('first_name')
```
#### Alter
```js
table.string('first_name').alter()
table.renameColumn('admin', 'is_admin')
```
#### Drop
```js
table.dropColumn('admin')
table.dropTimestamps('created_at')
```
```js
2017-09-23 01:41:37 +00:00
})
```
2017-09-23 02:17:34 +00:00
{: .-setup}
See: [Schema builder](http://knexjs.org/#Schema)
### Other methods
```js
knex.schema
.renameTable('persons', 'people')
.dropTable('persons')
```
```js
.hasTable('users').then(exists => ···)
.hasColumn('users', 'id').then(exists => ···)
```
See: [Schema builder](http://knexjs.org/#Schema)
## Modifying
{: .-three-column}
### Insert
```js
knex('users')
```
#### Insert one
```js
.insert({ name: 'John' })
```
#### Insert many
```js
.insert([
{ name: 'Starsky' },
{ name: 'Hutch' }
])
```
See: [Insert](http://knexjs.org/#Builder-insert)
### Update
```js
knex('users')
.where({ id: 2 })
.update({ name: 'Homer' })
```
See: [Update](http://knexjs.org/#Builder-update)
### Delete
```js
knex('users')
.where({ id: 2 })
.del()
```
See: [Delete](http://knexjs.org/#Builder-del)
2017-09-23 01:41:37 +00:00
## Migrations
### Setting up
#### Create knexfile.js
2017-09-23 01:41:37 +00:00
```
./node_modules/.bin/knex init
```
#### Create a migration
```
knex migrate:make migration_name
knex migrate:make migration_name --env production
2017-09-23 01:41:37 +00:00
```
#### Run migrations
```
knex migrate:latest
knex migrate:latest --env production
```
#### Rollback
```
knex migrate:rollback
knex migrate:rollback --env production
2017-09-23 01:41:37 +00:00
```
2017-09-23 02:17:34 +00:00
See: [Migrations](http://knexjs.org/#Migrations)