4.0 KiB
4.0 KiB
title | category | layout |
---|---|---|
Knex | Hidden | 2017/sheet |
Getting started
{: .-three-column}
Create table
knex.schema.createTable('accounts', (table) => {
table.increments('id')
table.string('account_name')
table.integer('user_id').unsigned().references('users.id')
})
.then(() => {
})
Select
knex('users')
.where({ email: 'hello@example.com' })
.then(rows => {
})
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
Connect via host
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"}
Connect via URL
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
var knex = require('knex')({
client: 'sqlite3',
connection: { filename: './mydb.sqlite' }
})
{: data-line="2,3"}
Select
Where
knex
.from('books')
.select('title', 'author', 'year')
Where
.where('title', 'Hello')
.where({ title: 'Hello' })
.whereIn('id', [1, 2, 3])
.whereNot(···)
Where conditions
.whereNull('updated_at')
.whereNotNull(···)
.whereExists('updated_at')
.whereNotExists(···)
.whereBetween('votes', [1, 100])
.whereNotBetween(···)
.whereRaw('id = ?', [1])
Where grouping
.where(function () {
this
.where('id', 1)
.orWhere('id', '>', 10)
})
Join
knex('users')
Basic join
.join('contacts', 'users.id', '=', 'contacts.id')
.join('contacts', {'users.id': 'contacts.id'})
Strings
.join('accounts', 'accounts.type', '=', knex.raw('?', ['admin']))
Directions
.leftJoin(···)
.leftOuterJoin(···)
.rightJoin(···)
.rightOuterJoin(···)
.outerJoin(···)
.fullOuterJoin(···)
.crossJoin(···)
Raw
.joinRaw('natural full join table1')
Grouping
.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(···)
})
Others
knex('users')
.distinct()
Group
.groupBy('count')
.groupByRaw('year WITH ROLLUP')
Order
.orderBy('name', 'desc')
.orderByRaw('name DESC')
Offset/limit
.offset(10)
.limit(20)
Having
.having('count', '>', 100)
.havingIn('count', [1, 100])
Union
.union(function() {
this.select(···)
})
.unionAll(···)
Etc
knex('users')
.pluck('id')
.then(ids => { ··· })
knex('users')
.first()
.then(user => { ··· })
Booleans
.count('active')
.count('active as is_active')
Numbers
.min('age')
.max('age')
.sum('age')
.sumDistinct('age')
.avg('age')
Schema
Create table
knex.schema.createTable('accounts', (table) => {
table.increments('id')
table.string('account_name')
table.integer('user_id').unsigned().references('users.id')
})
.then(() => {
})
Migrations
Setting up
Creates knexfile.js
./node_modules/.bin/knex init
Create a migration
knex migrate:make migration_name
Run migrations
knex migrate:latest
knex migrate:latest --env production
Rollback
knex migrate:rollback