Skip to content

Instantly share code, notes, and snippets.

@davidfurlong
Forked from ricardograca/package.json
Last active January 15, 2018 15:59
Show Gist options
  • Save davidfurlong/a52a743299e8131c966180ef76df497d to your computer and use it in GitHub Desktop.
Save davidfurlong/a52a743299e8131c966180ef76df497d to your computer and use it in GitHub Desktop.
Bookshelf bug - belongsToMany and fetchAll
{
"name": "bookshelf-bug",
"version": "0.1.0",
"dependencies": {
"bookshelf": "*",
"bluebird": "*",
"knex": "*",
"sqlite3": "*"
}
}
// This file is used for fast testing of bookshelf / knex queries.
// import Bookshelf from '../database/database';
// import { EventApplication } from '../database/index';
// const knex = Bookshelf.knex;
// var Promise = require('bluebird')
var knex = require('knex')({
client: 'sqlite3',
connection: {filename: ':memory:'},
debug: true
})
var bookshelf = require('bookshelf')(knex);
bookshelf.plugin('registry'); // Resolve circular dependencies with relations
bookshelf.plugin('visibility');
bookshelf.plugin('virtuals');
bookshelf.plugin(require('bookshelf-cascade-delete'));
bookshelf.plugin(require('bookshelf-paranoia'));
bookshelf.plugin(require('bookshelf-committed-plugin'));
var o = bookshelf.Model.extend({
tableName: 'organization',
jobs: function(){ return this.hasMany('Job', 'organization_id')}
});
var Organization = bookshelf.model('Organization', o);
var j = bookshelf.Model.extend({
tableName: 'job',
organization: function(){
return this.belongsTo('Organization', 'organization_id')
},
applications: function() {
return this.hasMany('JobApplication', 'job_id');
}
});
var Job = bookshelf.model('Job', j);
var ja = bookshelf.Model.extend({
tableName: 'job_application',
job: function () { return this.belongsTo('Job', 'job_id') }
});
var JobApplication = bookshelf.model('JobApplication', ja);
knex.schema.createTable('organization', function (table) {
table.increments()
table.string('name');
}).then(() =>
knex.schema.createTable('job', function(table) {
table.increments()
table.string('name');
table.integer('organization_id').notNullable().references('organization.id')
})
).then(() =>
knex.schema.createTable('job_application', function(table) {
table.increments()
table.string('name');
table.integer('job_id').notNullable().references('job.id')
})
).then(function() {
return knex('organization').insert({name: 'Org 1'})
.then((org) => {
return knex('job').insert({ name: 'Job 1', id: 1, organization_id: org[0] })
.then((job) =>
knex('job_application').insert({ name: 'JobApplication 1 ', job_id: job[0]})
)
})
})
.then(function () {
return JobApplication.where({ id: 1 }).fetch();
//return JobApplication.where({ id: 1 }).fetch({ withRelated: ['job.organization']})
})
.then(function(application){
return application.load(['job.organization', 'job'])
})
.then(function(application){
console.log(application.related('job').related('organization').get('name'));
})
.then(function() {
console.log('all done')
}).catch(function (err) {
console.log(err)
console.log(err.stack)
}).finally(function() {
process.exit()
})
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment