Last active
January 23, 2017 13:11
-
-
Save richardsimko/fee44b444df3b3e768cdd530311dbd3d to your computer and use it in GitHub Desktop.
Bookshelf broken idAttribute
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'; | |
const _ = require('lodash'); | |
const Promise = require('bluebird'); | |
const knex = require('knex')({ | |
client: 'sqlite3', | |
connection: ':memory:', | |
useNullAsDefault: true, | |
debug: true | |
}); | |
const bookshelf = require('bookshelf')(knex); | |
bookshelf.plugin('registry'); | |
bookshelf.plugin('visibility'); | |
const table1Promise = knex.schema.createTable('table1', (table) => { | |
table.text('client_key').primary(); | |
table.text('other_value'); | |
}); | |
const table2Promise = knex.schema.createTable('table2', (table) => { | |
table.increments(); | |
table.text('client_key').references('client_key').inTable('table1').onDelete('CASCADE'); | |
}); | |
const table1SeedPromise = knex('table1').insert([{ | |
client_key: 'test', | |
other_value: 'whatever' | |
}, { | |
client_key: 'other', | |
other_value: 'whatever' | |
}]); | |
const table2SeedPromise = knex('table2').insert([ | |
{ | |
client_key: 'test' | |
} | |
]); | |
const Table2Model = bookshelf.Model.extend({ | |
tableName: 'table2', | |
parse: function(attrs) { | |
return _.reduce(attrs, function(memo, val, key) { | |
memo[_.camelCase(key)] = val; | |
return memo; | |
}, {}); | |
}, | |
format: function(attrs) { | |
return _.reduce(attrs, function(result, val, key) { | |
result[_.snakeCase(key)] = val; | |
return result; | |
}, {}); | |
} | |
}); | |
const Table1Model = bookshelf.Model.extend({ | |
idAttribute: 'client_key', | |
tableName: 'table1', | |
constructor: function() { | |
bookshelf.Model.apply(this, arguments); | |
if (this.get('clientKey')) { | |
this.id = this.get('clientKey'); | |
} | |
}, | |
parse: function(attrs) { | |
return _.reduce(attrs, function(memo, val, key) { | |
memo[_.camelCase(key)] = val; | |
return memo; | |
}, {}); | |
}, | |
format: function(attrs) { | |
return _.reduce(attrs, function(result, val, key) { | |
result[_.snakeCase(key)] = val; | |
return result; | |
}, {}); | |
}, | |
table2s: function() { | |
return this.hasMany('table2', 'client_key'); | |
} | |
}); | |
const Table1Collection = bookshelf.Collection.extend({ | |
model: Table1Model | |
}, { | |
getWhateverClients: function() { | |
return bookshelf.transaction((trx) => { | |
return Table1Collection.query((qb) => { | |
qb.where('other_value', '=', 'whatever'); | |
}).fetch({ | |
withRelated: ['table2s'], | |
transacting: trx | |
}).then((clients) => { | |
const shouldBeFilled = clients.models[0].relations.table2s; | |
console.log(shouldBeFilled.length); | |
debugger; | |
}); | |
}); | |
} | |
}); | |
bookshelf.model('table1', Table1Model); | |
bookshelf.collection('table1Collection', Table1Collection); | |
bookshelf.model('table2', Table2Model); | |
Promise.all([table1Promise, table2Promise]).then(() => { | |
return Promise.all([table1SeedPromise, table2SeedPromise]); | |
}).then(() => { | |
return Table1Collection.getWhateverClients(); | |
}); |
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'; | |
const _ = require('lodash'); | |
const Promise = require('bluebird'); | |
const knex = require('knex')({ | |
client: 'sqlite3', | |
connection: ':memory:', | |
useNullAsDefault: true, | |
debug: true | |
}); | |
const bookshelf = require('bookshelf')(knex); | |
bookshelf.plugin('registry'); | |
bookshelf.plugin('visibility'); | |
const table1Promise = knex.schema.createTable('table1', (table) => { | |
table.text('clientKey').primary(); | |
table.text('other_value'); | |
}); | |
const table2Promise = knex.schema.createTable('table2', (table) => { | |
table.increments(); | |
table.text('clientKey').references('clientKey').inTable('table1').onDelete('CASCADE'); | |
}); | |
const table1SeedPromise = knex('table1').insert([{ | |
clientKey: 'test', | |
other_value: 'whatever' | |
}, { | |
clientKey: 'other', | |
other_value: 'whatever' | |
}]); | |
const table2SeedPromise = knex('table2').insert([ | |
{ | |
clientKey: 'test' | |
} | |
]); | |
const Table2Model = bookshelf.Model.extend({ | |
tableName: 'table2', | |
parse: function(attrs) { | |
return _.reduce(attrs, function(memo, val, key) { | |
memo[_.camelCase(key)] = val; | |
return memo; | |
}, {}); | |
}, | |
format: function(attrs) { | |
return _.reduce(attrs, function(result, val, key) { | |
result[_.snakeCase(key)] = val; | |
return result; | |
}, {}); | |
} | |
}); | |
const Table1Model = bookshelf.Model.extend({ | |
idAttribute: 'clientKey', | |
tableName: 'table1', | |
constructor: function() { | |
bookshelf.Model.apply(this, arguments); | |
if (this.get('clientKey')) { | |
this.id = this.get('clientKey'); | |
} | |
}, | |
parse: function(attrs) { | |
return _.reduce(attrs, function(memo, val, key) { | |
memo[_.camelCase(key)] = val; | |
return memo; | |
}, {}); | |
}, | |
format: function(attrs) { | |
return _.reduce(attrs, function(result, val, key) { | |
result[_.snakeCase(key)] = val; | |
return result; | |
}, {}); | |
}, | |
table2s: function() { | |
return this.hasMany('table2', 'clientKey'); | |
} | |
}); | |
const Table1Collection = bookshelf.Collection.extend({ | |
model: Table1Model | |
}, { | |
getWhateverClients: function() { | |
return bookshelf.transaction((trx) => { | |
return Table1Collection.query((qb) => { | |
qb.where('other_value', '=', 'whatever'); | |
}).fetch({ | |
withRelated: ['table2s'], | |
transacting: trx | |
}).then((clients) => { | |
const shouldBeFilled = clients.models[0].relations.table2s; | |
console.log(shouldBeFilled.length); | |
debugger; | |
}); | |
}); | |
} | |
}); | |
bookshelf.model('table1', Table1Model); | |
bookshelf.collection('table1Collection', Table1Collection); | |
bookshelf.model('table2', Table2Model); | |
Promise.all([table1Promise, table2Promise]).then(() => { | |
return Promise.all([table1SeedPromise, table2SeedPromise]); | |
}).then(() => { | |
return Table1Collection.getWhateverClients(); | |
}); |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment