Coder Social home page Coder Social logo

tutorial-thingy's Introduction

tutorial-thingy

using knex for migrations

install knex migrations cli

npm install knex -g

if there is no knexfile.js - the file which contains our various database configurations.

knex init

create new migrations

to create a new migration:

knex migrate:make migration_name

replace migration_name with the name of the migration. something like add_details_to_users or create_comments . It will create a file that is timestamped like 20170520122711_create_ratings in './migrations'.

edit migration

go to your newly created file in './migrations' and you will find

exports.up = function(knex, Promise) {

};

exports.down = function(knex, Promise) {

};

in the exports.up function, you will write code to be run when the migration runs. While exports.down is what runs to roll-back the migration. (usually used to undo what is done in exports.up). Syntax for schema building can be found here on the knex docs page. here is a sample:

exports.up = function(knex, Promise) {

  return knex.schema.createTable('ratings', function (table) {
    table.increments('id').unsigned().primary();
    table.integer('rating').notNull();
    table.integer('user_id').unsigned().notNull().references('id').inTable('users');
    table.integer('tutorial_id').unsigned().notNull().references('id').inTable('tutorials');
    table.timestamps();
  })

};

exports.down = function(knex, Promise) {
  return knex.schema.dropTable('ratings')
};

here we're creating a table called 'ratings' with the columns id, rating, user_id, tutorial_id. Each column has some properties associated with it.

run migrations

Once you have finished writing the migrations, you can update the database matching your NODE_ENV by running:

knex migrate:latest

To rollback the last batch of migrations:

knex migrate:rollback

Migrations are run in the order they were created so try and create main tables and columns before you create things that depend on them.

Seed files using knex CLI

To create a seed file, execute:

knex seed:make seed_name

replacing seed_name with what you're seeding.. users tutorials tags ..etc

this will create a file in "./seeds/dev" docs located here

To run seed files, execute:

knex seed:run
seed files are run in ALPHABETICAL ORDER so make sure you create dependent items first.

We will be numbering our seed files as such

1_users_see.js
2_posts_seed.js

next up, creating models with bookshelf.js

tutorial-thingy's People

Contributors

a-tawfik avatar mary-chapman avatar eyegio avatar

Watchers

James Cloos avatar  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.