Knex

install

A batteries-included, multi-dialect (PostgreSQL, MySQL, CockroachDB, MSSQL, SQLite3, Oracle (including Oracle Wallet Authentication)) query builder for Node.js.

npm install knex --save

npm install -g knex

if npm install -g knex fails, use sudo npm install -g knex instead.

Usage

Knex init // create a config file named knexfile.js
Knex migrate:make users // creat a migration file with timestamp
Knex migrate:latest // run the latest migration file
knexfile.js
/**
@type { Object.<string, import("knex").Knex.Config> } 
*/ 

module.exports = {
    development: { 
        client: 'mysql', 
        connection: { 
            database: 'myDataBase', 
            user: 'root', 
            password: '' 
        }, 
        pool: { 
            min: 2, 
            max: 10 
        }, 
        migrations: { tableName: 'knex_migrations' } },
    staging: { 
        client: 'mysql', 
        connection: { 
            database: 'myDataBase', 
            user: 'root', 
            password: '' 
        }, 
        pool: { 
            min: 2, 
            max: 10 
        }, 
        migrations: { tableName: 'knex_migrations' } },
    production: { 
        client: 'mysql', 
        connection: { 
            database: 'myDataBase', 
            user: 'root', 
            password: '' 
        }, 
        pool: { 
            min: 2, 
            max: 10 
        }, 
        migrations: { tableName: 'knex_migrations' } },
};

More: http://knexjs.org/guide

Last updated