Skip to content

Instantly share code, notes, and snippets.

@wbyoung
Created May 10, 2014 20:04
Show Gist options
  • Save wbyoung/df282829742a390846ff to your computer and use it in GitHub Desktop.
Save wbyoung/df282829742a390846ff to your computer and use it in GitHub Desktop.
People & Cars
'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);
};
'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