Knex is a "batteries included" data access library (DAL)/Query Builder for relational databases.

DALs are typically used as a way of abstracting the differences between different dialects of SQL provided by DB vendors. Knex provides standardized responses between different query clients and dialects.

Postgres

CREATE TABLE items(
    id SERIAL
);
1
2
3

MySQL

CREATE TABLE items(
    id INT NOT NULL AUTO_INCREMENT
);
1
2
3

Knex

knex.schema.createTable('items', function (tbl) {
    tbl.increments();
});
1
2
3

# Features

Knex currently supports Postgres, MSSQL, SQLite3, MySQL/MariaDB, Oracle and Amazon Redshift.

# Installation

npm i knex
1

Also install needed db drivers:

npm install pg mssql sqlite3
1

# Knex CLI

knex init -> creates db config (knexfile.js) knex migrate knex seed

# Selecting Data

The order of chained methods is not significant.

from('book').select('title') = select('title').from('book')
knex('book') = knex.from('book') = knex.table('book')
1
2

# select

Specify column names as strings or array. Defaults to selecting all columns

# first

returns firs record of query

# Schema Building

Install knex-cli by running npm install -g knex.

run knex to list commands. knex init creates a file knexfile.js holding your db config.

knex migrate:make name

Run migration: knex migrate:latest

To rollback run knex migrate:rollback

Migrations and rollbacks are per batch and not necessarily per individual migration.

# Seeding Data

Seed files are run in alphabetical order knex seed:make name Run seed: knex seed:run

Updated: 12/30/2018, 7:38:37 PM