Last active
December 28, 2015 23:35
-
-
Save RobAWilkinson/5943e65c78ead42a1478 to your computer and use it in GitHub Desktop.
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
// given these models | |
Soldier = bookshelf.Model.extend({ | |
tableName: 'soldiers', | |
tasks: function() { | |
return this.hasMany(Activity); | |
}, | |
company: function() { | |
return this.belongsTo(Company); | |
}, | |
MOS: function() { | |
return this.belongsTo(MOS); | |
}, | |
AOC: function() { | |
return this.belongsTo(AOC); | |
}, | |
rank: function() { | |
return this.belongsTo(Rank); | |
}, | |
certifications: function(){ | |
return this.belongsToMany(Certification, 'soldier_certifications', 'soldier_id', 'certification_id'); | |
}, | |
hasTimestamps: true | |
}); | |
Company = bookshelf.Model.extend({ | |
tableName: 'companies', | |
soldiers: function(){ | |
return this.hasMany(Soldier); | |
}, | |
hasTimestamps: true | |
}); | |
// this js query | |
Soldier.fetchAll({withRelated: ['company']}) | |
//runs these queries | |
/* | |
{ method: 'select', | |
options: {}, | |
bindings: [], | |
sql: 'select "soldiers".* from "soldiers"' } | |
{ method: 'select', | |
options: {}, | |
bindings: [ 1, 39, 68 ], | |
sql: 'select "companies".* from "companies" where "companies"."id" in (?, ?, ?)' } | |
*/ |
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
bookshelf.knex | |
.select('soldiers.name', 'ranks.label as rank', 'soldiers.class', 'aoc.label as AOC', 'mos.label as MOS', 'companies.name as Company') | |
.from('soldiers') | |
.leftJoin('aoc', 'soldiers.aoc_id', '=', 'aoc.id') | |
.leftJoin('mos', 'soldiers.mos_id', '=','mos.id') | |
.leftJoin('ranks', 'soldiers.rank_id', '=', 'ranks.id') | |
.leftJoin('companies', 'soldiers.company_id', 'companies.id') |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment