SQL query builder for Node.js - queries, schema, migrations, and transactions.
Install Knex.js and configure database connection
# Install knex and your database driver
npm install knex --save
# Database drivers (choose one)
npm install pg # PostgreSQL
npm install mysql2 # MySQL
npm install sqlite3 # SQLite
npm install tedious # MSSQLconst knex = require('knex')({
client: 'mysql2',
connection: {
host: '127.0.0.1',
port: 3306,
user: 'your_database_user',
password: 'your_database_password',
database: 'myapp_test'
},
pool: { min: 0, max: 7 }
});// Connection string
const knex = require('knex')({
client: 'pg',
connection: process.env.DATABASE_URL,
searchPath: ['knex', 'public']
});
// SQLite in-memory
const knex = require('knex')({
client: 'sqlite3',
connection: { filename: ':memory:' },
useNullAsDefault: true
});Select, insert, update, and delete operations
// Select all
knex('users').select('*')
// SELECT * FROM users
// Select specific columns
knex('users').select('id', 'name', 'email')
// SELECT id, name, email FROM users
// Select with alias
knex('users').select('id', 'name as full_name')
// SELECT id, name as full_name FROM users
// First result
knex('users').first('*')
// SELECT * FROM users LIMIT 1// Insert single row
knex('users').insert({ name: 'John', email: 'john@example.com' })
// Insert multiple rows
knex('users').insert([
{ name: 'John', email: 'john@example.com' },
{ name: 'Jane', email: 'jane@example.com' }
])
// Insert with returning (PostgreSQL)
knex('users')
.insert({ name: 'John', email: 'john@example.com' })
.returning('id')// Update all rows
knex('users').update({ status: 'active' })
// Update specific rows
knex('users')
.where('id', 1)
.update({ name: 'John Updated' })
// Update with returning
knex('users')
.where('id', 1)
.update({ name: 'John' })
.returning('*')// Delete all rows
knex('users').del()
// Delete specific rows
knex('users').where('id', 1).del()
// Delete with returning
knex('users')
.where('id', 1)
.del()
.returning('*')Filtering data with various conditions
// Simple where
knex('users').where('id', 1)
knex('users').where({ id: 1, status: 'active' })
// Where with operator
knex('users').where('age', '>', 18)
knex('users').where('created_at', '<=', '2024-01-01')
// Multiple conditions (AND)
knex('users')
.where('status', 'active')
.where('age', '>', 18)
// OR conditions
knex('users')
.where('status', 'active')
.orWhere('role', 'admin')// Where IN
knex('users').whereIn('id', [1, 2, 3])
// Where NOT IN
knex('users').whereNotIn('status', ['banned', 'deleted'])
// Where NULL
knex('users').whereNull('deleted_at')
// Where NOT NULL
knex('users').whereNotNull('email')
// Where BETWEEN
knex('users').whereBetween('age', [18, 65])
// Where NOT BETWEEN
knex('users').whereNotBetween('age', [0, 18])// Complex nested conditions
knex('users')
.where('status', 'active')
.where(function() {
this.where('role', 'admin')
.orWhere('role', 'moderator')
})
// WHERE status = 'active' AND (role = 'admin' OR role = 'moderator')
// Where EXISTS
knex('users')
.whereExists(function() {
this.select('*')
.from('orders')
.whereRaw('orders.user_id = users.id')
})
// Raw WHERE
knex('users').whereRaw('age > ?', [18])Combining data from multiple tables
// INNER JOIN
knex('users')
.join('orders', 'users.id', '=', 'orders.user_id')
.select('users.name', 'orders.total')
// LEFT JOIN
knex('users')
.leftJoin('orders', 'users.id', 'orders.user_id')
.select('users.name', 'orders.total')
// RIGHT JOIN
knex('users')
.rightJoin('orders', 'users.id', 'orders.user_id')
// FULL OUTER JOIN
knex('users')
.fullOuterJoin('orders', 'users.id', 'orders.user_id')// Join with multiple conditions
knex('users')
.join('orders', function() {
this.on('users.id', '=', 'orders.user_id')
.andOn('orders.status', '=', knex.raw('?', ['completed']))
})
// Join with OR condition
knex('users')
.join('orders', function() {
this.on('users.id', '=', 'orders.user_id')
.orOn('users.email', '=', 'orders.email')
})
// Cross join
knex('users').crossJoin('roles')Aggregate functions and GROUP BY operations
// Count
knex('users').count('id as total')
// Count distinct
knex('users').countDistinct('email')
// Sum
knex('orders').sum('total as revenue')
// Average
knex('orders').avg('total as average_order')
// Min and Max
knex('orders').min('total as min_order')
knex('orders').max('total as max_order')// Group by
knex('orders')
.select('user_id')
.count('id as order_count')
.groupBy('user_id')
// Group by multiple columns
knex('orders')
.select('user_id', 'status')
.sum('total as revenue')
.groupBy('user_id', 'status')
// Having clause
knex('orders')
.select('user_id')
.count('id as order_count')
.groupBy('user_id')
.having('order_count', '>', 5)
// Having with raw
knex('orders')
.select('user_id')
.sum('total as revenue')
.groupBy('user_id')
.havingRaw('SUM(total) > ?', [1000])Sort and paginate query results
// Order by ascending
knex('users').orderBy('name')
knex('users').orderBy('name', 'asc')
// Order by descending
knex('users').orderBy('created_at', 'desc')
// Multiple order columns
knex('users')
.orderBy('status', 'asc')
.orderBy('created_at', 'desc')
// Order by array
knex('users').orderBy([
{ column: 'status', order: 'asc' },
{ column: 'name', order: 'desc' }
])
// Order by raw
knex('users').orderByRaw('age DESC NULLS LAST')// Limit
knex('users').limit(10)
// Offset
knex('users').offset(20)
// Pagination
knex('users')
.limit(10)
.offset(20)
// Distinct
knex('users').distinct('email')
// Distinct on multiple columns
knex('users').distinct('email', 'status')Define and create database tables
// Create table
knex.schema.createTable('users', function(table) {
table.increments('id').primary();
table.string('name', 100).notNullable();
table.string('email', 255).unique().notNullable();
table.integer('age');
table.boolean('is_active').defaultTo(true);
table.timestamps(true, true);
});
// Create table if not exists
knex.schema.createTableIfNotExists('users', function(table) {
table.increments('id');
table.string('email').unique();
});// Common column types
table.increments('id') // Auto-incrementing ID
table.integer('votes') // Integer
table.bigInteger('big_votes') // Big integer
table.text('description') // Text
table.string('name', 255) // String with length
table.float('amount') // Float
table.decimal('price', 8, 2) // Decimal(8,2)
table.boolean('is_admin') // Boolean
table.date('birth_date') // Date
table.datetime('created_at') // Datetime
table.time('alarm_time') // Time
table.timestamp('updated_at') // Timestamp
table.json('metadata') // JSON
table.jsonb('data') // JSONB (PostgreSQL)
table.uuid('id') // UUID
table.enum('status', ['active', 'inactive']) // Enum// Column modifiers
table.string('email')
.notNullable() // NOT NULL
.unique() // UNIQUE
.defaultTo('default') // DEFAULT value
.unsigned() // UNSIGNED (for numbers)
.index() // Add index
.comment('Email address') // Add comment
// Foreign keys
table.integer('user_id')
.unsigned()
.notNullable()
.references('id')
.inTable('users')
.onDelete('CASCADE')
.onUpdate('CASCADE');
// Timestamps helpers
table.timestamps(true, true) // created_at, updated_at
table.timestamp('created_at').defaultTo(knex.fn.now())Modify existing table structures
// Add column
knex.schema.table('users', function(table) {
table.string('phone', 20);
});
// Add multiple columns
knex.schema.table('users', function(table) {
table.string('phone', 20);
table.string('address', 255);
table.integer('zip_code');
});
// Alter column
knex.schema.alterTable('users', function(table) {
table.string('email', 500).alter();
});
// Rename column
knex.schema.table('users', function(table) {
table.renameColumn('name', 'full_name');
});// Drop column
knex.schema.table('users', function(table) {
table.dropColumn('phone');
});
// Drop multiple columns
knex.schema.table('users', function(table) {
table.dropColumns('phone', 'address');
});
// Drop table
knex.schema.dropTable('users');
// Drop table if exists
knex.schema.dropTableIfExists('users');
// Rename table
knex.schema.renameTable('users', 'customers');// Check if table exists
knex.schema.hasTable('users')
.then(exists => {
if (!exists) {
return knex.schema.createTable('users', table => {
table.increments('id');
});
}
});
// Check if column exists
knex.schema.hasColumn('users', 'email')
.then(exists => {
if (!exists) {
return knex.schema.table('users', table => {
table.string('email');
});
}
});Create and manage database indexes and relationships
// Create index
knex.schema.table('users', function(table) {
table.index('email');
});
// Create named index
knex.schema.table('users', function(table) {
table.index('email', 'idx_users_email');
});
// Composite index
knex.schema.table('users', function(table) {
table.index(['status', 'created_at'], 'idx_status_created');
});
// Unique index
knex.schema.table('users', function(table) {
table.unique('email');
});
// Drop index
knex.schema.table('users', function(table) {
table.dropIndex('email');
table.dropIndex([], 'idx_users_email'); // by name
});// Add foreign key
knex.schema.table('orders', function(table) {
table.foreign('user_id')
.references('id')
.inTable('users');
});
// Foreign key with actions
knex.schema.table('orders', function(table) {
table.foreign('user_id')
.references('id')
.inTable('users')
.onDelete('CASCADE')
.onUpdate('RESTRICT');
});
// Named foreign key
knex.schema.table('orders', function(table) {
table.foreign('user_id', 'fk_orders_user')
.references('users.id');
});
// Drop foreign key
knex.schema.table('orders', function(table) {
table.dropForeign('user_id');
table.dropForeign([], 'fk_orders_user'); // by name
});Execute multiple queries atomically
// Basic transaction
knex.transaction(async (trx) => {
await trx('users').insert({ name: 'John' });
await trx('orders').insert({ user_id: 1, total: 100 });
})
.then(() => console.log('Transaction committed'))
.catch((err) => console.error('Transaction failed:', err));
// Transaction with return value
const userId = await knex.transaction(async (trx) => {
const [id] = await trx('users')
.insert({ name: 'John' })
.returning('id');
await trx('profiles')
.insert({ user_id: id, bio: 'Hello' });
return id;
});// Manual transaction control
const trx = await knex.transaction();
try {
await trx('users').insert({ name: 'John' });
await trx('orders').insert({ user_id: 1, total: 100 });
await trx.commit();
} catch (err) {
await trx.rollback();
throw err;
}
// Transaction with savepoints
knex.transaction(async (trx) => {
await trx('users').insert({ name: 'John' });
const savepoint = await trx.savepoint();
try {
await trx('orders').insert({ user_id: 1, total: 100 });
await savepoint.release();
} catch (err) {
await savepoint.rollback();
}
});Execute raw SQL when needed
// Simple raw query
knex.raw('SELECT * FROM users WHERE id = ?', [1])
// Raw query with named bindings
knex.raw('SELECT * FROM users WHERE id = :userId', {
userId: 1
})
// Raw in select
knex('users')
.select(knex.raw('COUNT(*) as total'))
.where('status', 'active')
// Raw in where
knex('users')
.whereRaw('age > ?', [18])
.orWhereRaw('status = ?', ['admin'])// Raw update
knex('users')
.update({
updated_at: knex.raw('NOW()'),
views: knex.raw('views + 1')
})
.where('id', 1)
// Raw insert
knex('users').insert({
name: 'John',
created_at: knex.raw('CURRENT_TIMESTAMP')
})
// Using knex.ref for column references
knex('users')
.where('created_at', '>', knex.ref('updated_at'))Version control for database schema
# Create migration
npx knex migrate:make create_users_table
# Run migrations
npx knex migrate:latest
# Rollback last batch
npx knex migrate:rollback
# Rollback all migrations
npx knex migrate:rollback --all
# Check migration status
npx knex migrate:status
# Run specific migration
npx knex migrate:up 001_create_users.js// Migration file structure
exports.up = function(knex) {
return knex.schema.createTable('users', function(table) {
table.increments('id').primary();
table.string('name', 100).notNullable();
table.string('email', 255).unique().notNullable();
table.timestamp('created_at').defaultTo(knex.fn.now());
});
};
exports.down = function(knex) {
return knex.schema.dropTable('users');
};// Migration with multiple operations
exports.up = async function(knex) {
// Create users table
await knex.schema.createTable('users', table => {
table.increments('id');
table.string('email').unique();
});
// Create orders table
await knex.schema.createTable('orders', table => {
table.increments('id');
table.integer('user_id').unsigned();
table.foreign('user_id').references('users.id');
});
// Seed initial data
await knex('users').insert([
{ email: 'admin@example.com' }
]);
};
exports.down = async function(knex) {
await knex.schema.dropTable('orders');
await knex.schema.dropTable('users');
};Populate database with test data
# Create seed file
npx knex seed:make 01_users
# Run all seeds
npx knex seed:run
# Run specific seed
npx knex seed:run --specific=01_users.js// Basic seed file
exports.seed = async function(knex) {
// Delete existing entries
await knex('users').del();
// Insert seed data
await knex('users').insert([
{ name: 'John Doe', email: 'john@example.com' },
{ name: 'Jane Smith', email: 'jane@example.com' },
{ name: 'Bob Johnson', email: 'bob@example.com' }
]);
};// Seed with relations
exports.seed = async function(knex) {
// Clear tables in correct order
await knex('orders').del();
await knex('users').del();
// Insert users and get IDs
const [userId1] = await knex('users')
.insert({ name: 'John', email: 'john@example.com' })
.returning('id');
const [userId2] = await knex('users')
.insert({ name: 'Jane', email: 'jane@example.com' })
.returning('id');
// Insert related orders
await knex('orders').insert([
{ user_id: userId1, total: 100.50 },
{ user_id: userId1, total: 75.25 },
{ user_id: userId2, total: 200.00 }
]);
};Type-safe queries with TypeScript
// Define table types
interface User {
id: number;
name: string;
email: string;
created_at: string;
updated_at: string;
}
interface UserInsert {
name: string;
email: string;
}
interface UserUpdate {
name?: string;
email?: string;
updated_at?: string;
}// Augment Knex types
import { Knex } from 'knex';
declare module 'knex/types/tables' {
interface Tables {
users: User;
users_composite: Knex.CompositeTableType<
User,
UserInsert,
UserUpdate
>;
}
}
// Type-safe queries
const users = await knex('users').select('*');
// users is typed as User[]
const newUser = await knex('users_composite')
.insert({ name: 'John', email: 'john@example.com' })
.returning('*');// Generic query types
const getUserById = async (id: number): Promise<User | undefined> => {
return knex<User>('users')
.where({ id })
.first();
};
const createUser = async (data: UserInsert): Promise<User> => {
const [user] = await knex<User>('users')
.insert(data)
.returning('*');
return user;
};
const updateUser = async (
id: number,
data: UserUpdate
): Promise<User | undefined> => {
const [user] = await knex<User>('users')
.where({ id })
.update(data)
.returning('*');
return user;
};Advanced Knex.js capabilities
// Upsert (insert or update)
knex('users')
.insert({ id: 1, name: 'John', email: 'john@example.com' })
.onConflict('id')
.merge()
// Upsert with specific columns
knex('users')
.insert({ id: 1, name: 'John', email: 'john@example.com' })
.onConflict('email')
.merge(['name'])
// Insert ignore (MySQL)
knex('users')
.insert({ email: 'john@example.com' })
.onConflict('email')
.ignore()// WITH (Common Table Expressions)
knex.with('active_users', (qb) => {
qb.select('*').from('users').where('status', 'active');
})
.select('*')
.from('active_users')
.where('age', '>', 18);
// Recursive CTE
knex.withRecursive('tree', (qb) => {
qb.select('*').from('categories').where('parent_id', null)
.union((qb2) => {
qb2.select('c.*')
.from('categories as c')
.join('tree as t', 'c.parent_id', 't.id');
});
})
.select('*').from('tree');// Subqueries
knex('users')
.whereIn('id', function() {
this.select('user_id').from('orders').where('total', '>', 100);
})
// Subquery in select
knex('users')
.select('name')
.select(
knex('orders')
.count('*')
.where('orders.user_id', knex.raw('??', ['users.id']))
.as('order_count')
)
// Subquery in from
knex
.select('*')
.from(
knex('users')
.where('status', 'active')
.as('active_users')
)// Window functions
knex('orders')
.select('*')
.select(
knex.raw('ROW_NUMBER() OVER (PARTITION BY user_id ORDER BY created_at DESC) as row_num')
)
// JSON operations (PostgreSQL)
knex('users')
.select('name')
.select(knex.raw("metadata->>'age' as age"))
.where(knex.raw("metadata->>'city'"), 'New York')
// Array operations (PostgreSQL)
knex('users')
.whereRaw('? = ANY(tags)', ['javascript'])Manage database connections and pool
// Configure connection pool
const knex = require('knex')({
client: 'pg',
connection: {
host: '127.0.0.1',
user: 'user',
password: 'password',
database: 'myapp'
},
pool: {
min: 2,
max: 10,
acquireTimeoutMillis: 30000,
idleTimeoutMillis: 30000
}
});
// Destroy connection pool
await knex.destroy();
// Re-initialize
knex.initialize();// Connection afterCreate hook
const knex = require('knex')({
client: 'pg',
connection: { /* ... */ },
pool: {
afterCreate: function(conn, done) {
conn.query('SET timezone="UTC";', function(err) {
done(err, conn);
});
}
}
});
// Dynamic connection
const knex = require('knex')({
client: 'mysql',
connection: async () => {
const token = await getAuthToken();
return {
host: 'localhost',
user: 'user',
password: token,
database: 'myapp'
};
}
});// Debugging queries
const knex = require('knex')({
client: 'pg',
connection: { /* ... */ },
debug: true // Log all queries
});
// Debug specific query
knex('users')
.select('*')
.debug()
.then(rows => console.log(rows));
// Get SQL without executing
const sql = knex('users')
.where('id', 1)
.toSQL();
console.log(sql.sql); // SQL string
console.log(sql.bindings); // Parameters