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.Model.find = function(id, options) { | |
var model = new this(); | |
model.set(this.idAttribute, id); | |
return model.fetch(options); | |
}; |
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
var Customer = bookshelf.Model.extend({ | |
account: function() { | |
return this.belongsTo(Account); | |
}, | |
}); | |
Customer.forge({id: 1}).fetch().then(function(customer) { | |
console.log(customer.toJSON()); |
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
// In Usage: | |
// ---- | |
var User = Bookshelf.Model.extend({ | |
posts: function() { | |
return this.hasMany(Post); | |
} | |
}); |
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 code, to compliment the knex code above | |
exports.PG = Bookshelf.PG = Bookshelf.initialize | |
client: 'pg' | |
connection: config.get('/postgres') | |
# property photo model | |
exports.PropertyPhoto = PropertyPhoto = Bookshelf.PG.Model.extend | |
tableName: 'property_photos' |
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
var Bookshelf = require('bookshelf'); | |
var DB = Bookshelf.initialize({ | |
client: 'sqlite3', | |
connection: { | |
filename: ':memory:' | |
} | |
}); | |
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
var Parent = Bookshelf.Model.extend({ | |
tableName: 'parent', | |
child: function() { | |
return this.hasOne(Child); | |
} | |
}); | |
var Child = Bookshelf.Model.extend({ | |
tableName: 'child', | |
//this has an attribute parent_id |
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
_.each(['hasMany', 'hasOne', 'belongsToMany', 'morphOne', 'morphMany', 'belongsTo', 'through'], function(method) { | |
var original = Model.prototype[method] | |
Model.prototype[method] = function() { | |
// Stuff here | |
return original.apply(this, ...) | |
}; | |
}); |
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
// Note: you don't need to use when.js, any good promise library will do (Q.js also would work) | |
var when = require('when'); | |
// Deleting all models in a collection at once: | |
when.all(collection.invoke('destroy'))).then(function() { | |
collection.reset(); | |
// ... | |
}); | |
// Saving all models in collection at once: |
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
test("add with parse and merge", function() { | |
var Model = Backbone.Model.extend({ | |
parse: function (data) { | |
return data.model; | |
} | |
}); | |
var collection = new Backbone.Collection(); | |
collection.model = Model; | |
collection.add({id: 1}); |
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
})(function(KNEX) { | |
// Dependencies, ordered most to least likely to be manually injected... | |
// this only applies for CommonJS style require, since AMD config can account for this otherwise. | |
var deps = ['underscore', 'when', './clients/base', './clients/mysql', './clients/postgres', './clients/sqlite3']; | |
var root = this; | |
var slice = [].slice; | |
var Initialize = function() { |