Skip to content

A small Knex tutorial for setting things up, migrations, and seeds

Notifications You must be signed in to change notification settings

kauri-2016/knex-tutorial

Folders and files

NameName
Last commit message
Last commit date

Latest commit

7dc0417 · May 29, 2016

History

6 Commits
 
 
 
 
 
 
 
 
 
 
 
 
 
 

Repository files navigation

Small Knex.js tutorial

Initial steps

  • Create the app and set stuff up.

    git init
    nvm use 6
    npm init
    npm i tape --save-dev
    npm i knex sqlite3 --save
    echo "node_modules" > .gitignore
    echo "*.sqlite" >> .gitignore
    echo "6" > .nvmrc
    git add .
    git commit -m "Initial commit"
  • Add scripts in package.json to run knex.

    "scripts": {
      "init": "knex init",
      "migrate:make": "knex migrate:make",
      "migrate:latest": "knex migrate:latest",
      "migrate:rollback": "knex migrate:rollback",
      "seed:make": "knex seed:make",
      "seed:run": "knex seed:run",
      "test": "tape tests.js"
    },
  • Create knexfile.js by running npm run init.

  • Prune the knexfile.js to:

    module.exports = {
      development: {
        client: 'sqlite3',
        connection: {
          filename: './dev.sqlite'
        },
        useNullAsDefault: true
      }
    } 
  • Create Users table by running npm run migrate:make create-users

  • Edit the migration file accordingly:

    exports.up = function (knex, Promise) {
      console.log('Creating Users')
      return knex.schema.createTableIfNotExists('Users', function (table) {
        table.increments('id')
        table.string('firstName')
        table.string('lastName')
        table.string('username')
      })
    }
    
    exports.down = function (knex, Promise) {
      console.log('Dropping Users')
      return knex.schema.dropTableIfExists('Users').then(function () {
        console.log('Users table was dropped')
      })
    }
  • Apply the migration file by running npm run migrate:latest.

  • Open dev.sqlite in the SQLite Manager tool and verify schema.

  • Create a new seed by running npm run seed:make test-users.

  • Change table to Users and add some objects with firstName, lastName and username properties.

  • Apply the seed data by running npm run seed:run.

  • Refresh the Users table and verify the data was inserted.

  • Smile 😃 nice work!

Next steps

  • Drop the Users table by running npm run migrate:rollback.

  • Reapply the migration to add the table back and add the test data back.

  • Write some scripts that perform CRUD operations on the database. You will need to read the docs to do this because we haven't talked about how to do this yet.

About

A small Knex tutorial for setting things up, migrations, and seeds

Resources

Stars

Watchers

Forks

Releases

No releases published

Packages

No packages published