7 [Knex](http://knexjs.org/) is an SQL query builder for Node.js.
8 This guide targets v0.13.0.
19 connection: 'postgres://user:pass@localhost:5432/dbname'
23 See: [Connect](#connect-1)
28 knex.schema.createTable('user', (table) => {
29 table.increments('id')
36 See: [Schema](#schema)
42 .where({ email: 'hi@example.com' })
47 See: [Select](#select-1)
53 .insert({ email: 'hi@example.com' })
57 See: [Insert](#insert-1)
64 .update({ email: 'hi@example.com' })
68 See: [Update](#update-1)
74 knex migrate:make migration_name
75 knex migrate:make migration_name -x ts # Generates a TypeScript migration file
80 See: [Migrations](#migrations-1)
85 knex seed:make seed_name
86 knex seed:make seed_name -x ts # Generates a TypeScript seed file
87 knex seed:run # Runs all seed files
88 knex seed:run --specific=seed-filename.js # Runs a specific seed file
91 See: [Seeds](http://knexjs.org/#Seeds)
99 | `mysql` | MySQL or MariaDB |
100 | `sqlite3` | Sqlite3 |
103 Install any of these packages along with `knex`.
105 See: [Node.js installation](http://knexjs.org/#Installation-node)
110 var knex = require('knex')({
114 user: 'your_database_user',
115 password: 'your_database_password',
116 database: 'myapp_test'
118 pool: { min: 0, max: 7 }
123 See: [Initializing the library](http://knexjs.org/#Installation-client)
128 var pg = require('knex')({
130 connection: process.env.DATABASE_URL,
131 searchPath: 'knex,public',
132 pool: { min: 0, max: 7 }
137 ### Connect via Sqlite
140 var knex = require('knex')({
142 connection: { filename: './mydb.sqlite' }
154 .select('title', 'author', 'year')
160 .where('title', 'Hello')
161 .where({ title: 'Hello' })
162 .whereIn('id', [1, 2, 3])
164 .whereNotIn('id', [1, 2, 3])
167 #### Where conditions
170 .whereNull('updated_at')
175 .whereExists('updated_at')
180 .whereBetween('votes', [1, 100])
181 .whereNotBetween(···)
185 .whereRaw('id = ?', [1])
194 .orWhere('id', '>', 10)
198 See: [Where clauses](http://knexjs.org/#Builder-wheres)
209 .join('contacts', 'users.id', '=', 'contacts.id')
210 .join('contacts', {'users.id': 'contacts.id'})
216 .join('accounts', 'accounts.type', '=', knex.raw('?', ['admin']))
234 .joinRaw('natural full join table1')
240 .join('accounts', function () {
242 .on('accounts.id', '=', 'users.account_id')
243 .orOn('accounts.owner_id', '=', 'users.id')
245 .onIn('accounts.id', [1, 2, 3, 5, 8])
248 .onNull('accounts.email')
251 .onExists(function () {
258 See: [Join methods](http://knexjs.org/#Builder-join)
271 .groupByRaw('year WITH ROLLUP')
276 .orderBy('name', 'desc')
277 .orderByRaw('name DESC')
290 .having('count', '>', 100)
291 .havingIn('count', [1, 100])
303 See: [Query builder](http://knexjs.org/#Builder)
310 .then(ids => { ··· })
315 .then(user => { ··· })
322 .count('active as is_active')
335 See: [Query builder](http://knexjs.org/#Builder)
342 knex.schema.createTable('accounts', table => {
348 table.increments('id')
349 table.string('account_name')
352 table.decimal('balance', 8, 2)
353 table.boolean('is_admin')
354 table.date('birthday')
355 table.time('created_at')
356 table.timestamp('created_at').defaultTo(knex.fn.now())
357 table.json('profile')
358 table.jsonb('profile')
359 table.uuid('id').primary()
365 table.unique('email')
366 table.unique(['email', 'company_id'])
367 table.dropUnique(···)
373 table.foreign('company_id')
374 .references('companies.id')
375 table.dropForeign(···)
381 table.integer('user_id')
383 .references('users.id')
392 See: [Schema builder](http://knexjs.org/#Schema)
397 knex.schema.table('accounts', table => {
403 table.string('first_name')
409 table.string('first_name').alter()
410 table.renameColumn('admin', 'is_admin')
416 table.dropColumn('admin')
417 table.dropTimestamps('created_at')
425 See: [Schema builder](http://knexjs.org/#Schema)
431 .renameTable('persons', 'people')
432 .dropTable('persons')
436 .hasTable('users').then(exists => ···)
437 .hasColumn('users', 'id').then(exists => ···)
440 See: [Schema builder](http://knexjs.org/#Schema)
454 .insert({ name: 'John' })
466 See: [Insert](http://knexjs.org/#Builder-insert)
473 .update({ name: 'Homer' })
476 See: [Update](http://knexjs.org/#Builder-update)
486 See: [Delete](http://knexjs.org/#Builder-del)
492 #### Create knexfile.js
495 ./node_modules/.bin/knex init
498 #### Create a migration
501 knex migrate:make migration_name
502 knex migrate:make migration_name --env production
509 knex migrate:latest --env production
515 knex migrate:rollback
516 knex migrate:rollback --env production
519 See: [Migrations](http://knexjs.org/#Migrations)