Coder Social home page Coder Social logo

knex-tutorial's Introduction

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.

Recommend Projects

  • React photo React

    A declarative, efficient, and flexible JavaScript library for building user interfaces.

  • Vue.js photo Vue.js

    ๐Ÿ–– Vue.js is a progressive, incrementally-adoptable JavaScript framework for building UI on the web.

  • Typescript photo Typescript

    TypeScript is a superset of JavaScript that compiles to clean JavaScript output.

  • TensorFlow photo TensorFlow

    An Open Source Machine Learning Framework for Everyone

  • Django photo Django

    The Web framework for perfectionists with deadlines.

  • D3 photo D3

    Bring data to life with SVG, Canvas and HTML. ๐Ÿ“Š๐Ÿ“ˆ๐ŸŽ‰

Recommend Topics

  • javascript

    JavaScript (JS) is a lightweight interpreted programming language with first-class functions.

  • web

    Some thing interesting about web. New door for the world.

  • server

    A server is a program made to process requests and deliver data to clients.

  • Machine learning

    Machine learning is a way of modeling and interpreting data that allows a piece of software to respond intelligently.

  • Game

    Some thing interesting about game, make everyone happy.

Recommend Org

  • Facebook photo Facebook

    We are working to build community through open source technology. NB: members must have two-factor auth.

  • Microsoft photo Microsoft

    Open source projects and samples from Microsoft.

  • Google photo Google

    Google โค๏ธ Open Source for everyone.

  • D3 photo D3

    Data-Driven Documents codes.