Created
May 10, 2014 20:04
-
-
Save wbyoung/df282829742a390846ff to your computer and use it in GitHub Desktop.
People & Cars
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
'use strict'; | |
exports.up = function(knex, Promise) { | |
var createTable = knex.schema.createTable.bind(knex.schema); | |
var sequence = [ | |
createTable.bind(null, 'people', function (table) { | |
table.increments('id').primary(); | |
table.string('name'); | |
}), | |
createTable.bind(null, 'cars', function (table) { | |
table.increments('id').primary(); | |
table.string('model'); | |
}), | |
createTable.bind(null, 'cars_drivers', function (table) { | |
table.integer('car_id').references('id').inTable('cars'); | |
table.integer('person_id').references('id').inTable('people'); | |
}) | |
]; | |
return Promise.reduce(sequence, function(aggregate, fn) { return fn(); }, null); | |
}; | |
exports.down = function(knex, Promise) { | |
var dropTable = knex.schema.dropTable.bind(knex.schema); | |
var sequence = [ | |
dropTable.bind(null, 'cars_drivers'), | |
dropTable.bind(null, 'cars'), | |
dropTable.bind(null, 'people') | |
]; | |
return Promise.reduce(sequence, function(aggregate, fn) { return fn(); }, null); | |
}; |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
'use strict'; | |
var Car; | |
var Person; | |
Car = exports.Car = DB.Model.extend({ | |
tableName: 'cars', | |
drivers: function() { | |
return this.belongsToMany(Person, 'cars_drivers'); | |
} | |
}); | |
Person = exports.Person = DB.Model.extend({ | |
tableName: 'people', | |
cars: function() { | |
return this.belongsToMany(Car, 'cars_drivers'); | |
} | |
}); | |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment