GithubHelp home page GithubHelp logo

knexbasics's Introduction

Knex Basics walkthrough

Setting up and using knex

  • Must have node, the express generator, and postgres installed
  • To get node and postgres:
  • Install brew:
$ /usr/bin/ruby -e "$(curl -fsSL https://raw.githubusercontent.com/Homebrew/install/master/install)"
  • Install node and postgres
$ brew update
$ brew install node
$ brew install postgres
  • Install the express generator
$ npm install express-generator -g
  • Install nodemon to run your server(using the command $ nodemon) from the root directory of your application
$ npm install -g nodemon
  • Note: new versions of knex have a couple of bugs, so it's advised to specifically download an older version(0.9.0) until those issues get worked out
$ take someApp
$ express --hbs --git .
$ npm install
$ npm install --save [email protected]
$ npm install --save pg
$ knex init
  • Open your directory in your text editor and go to your newly created knexfile.js (created by $ knex init) and replace the code there with this
module.exports = {

  development: {
    client: 'postgresql',
    connection: 'postgresql://localhost/knex-humans',
    pool: {
      min: 2,
      max: 10
    }
  }

};
  • To keep it simple we'll just work with one database environment(development) and create a pointer to a knex-humans database, which we need to manually create:
$ createdb knex-humans
  • Now we'll use knex to create a migrations folder and make a create_humans migrations file
$ knex migrate:make create_humans
  • You should now have a migrations folder with a file in it, open it up and replace the code in there with this for the time being:
exports.up = function(knex, Promise) {
  return knex.schema.createTable('humans', function (table) {
    table.increments();
    table.string('name');
    table.integer('age');
    table.timestamps();
  })
};

exports.down = function(knex, Promise) {

};
  • Now if you run this command, your schema should be correctly established in your local knex-humans postgresql database:
$ knex migrate:latest
  • To properly hook knex up to your routes file you need to add this line to your routes files - in this case routes/index.js file:
var knex = require('knex')(require('../knexfile')['development']);

CRUD examples

  • You can find examples of some basic CRUD in the routes/index.js file of this repository

knexbasics's People

Contributors

akyunaakish avatar

Watchers

James Cloos avatar Anthony Simpson avatar

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.