Implement save and destroy on collections
Hey, first of all: great ORM! Tried many others, sticked with bookshelf!
I'm currently working on a bigger project using bookshelf and came across some problems with the collections. I'm often running into a situation where I need to save/update/destroy many models at once. You can do this by invoking the save/update/destroy methods for each model present in the collection. This comes with a big disadvantage: Every model will run an own query. If you want to insert 100 models you need to run 100 single INSERT queries. MySQL however can insert multiple rows at once. (Also supported by knex). Why not making collections smarter and implement some useful methods, that take care of all models present in the collection? (I'm sure @tgriesser is working on similar solutions for these problems, but I'd like to share some thoughts and ideas with you :))
I implemented three methods for my base collection, namely saveAll, createAll and destroyAll
createAll(data, options)
The createAll method takes the passed data, prepares the models (setting timestamps, defaults, saveConstraints) and creates a knex.insert query for inserting all the data at once. After successfully inserting the models, the models get updated with their new ids and are added to the collection. Example:
var data = [
{username: 'foo', email: '[email protected]'},
{username: 'bar', email: '[email protected]'}
];
Users.forge().createAll(data).then(function(users) {
// Here we can access the saved models, yay!
users.at(0).get('id') == 1 // true
});
The query would look like this:
insert into `users` (`username`, `email`, `created_at`) values (?, ?, ?), (?, ?, ?);I still have no ideal solution for running the query. Actually this should be done with help of the Bookshelf.sync method. But I was not able to extend the Bookshelf.sync instance. So what createAll does is, it grabs the query builder instance by calling this.query() without arguments and then running the knex.insert method.
What about related models? I don't have a really good solution for this. I adopted the model.save behavior, running Helpers.saveConstraints(model, relatedData); and this.attach(..) for each single model. (Works for now)
saveAll([data], options)
The saveAll method takes the all the present models (optional: also the passed raw data) and separates them into two sets: Those models, that are new (having an ID set) will be created with the help of createAll. The models that are not new will get updated with a single update query for each model. Yes, this is not the smartest way, maybe this should be also done in a single query for all models, but I think the implementation is a bit harder. Example:
var data = [
// Will be updated
{id: 1, username: 'foofoo'},
{id: 2, username: 'barbar'},
// Will be created
{username: 'bar', email: '[email protected]'}
];
Users.forge().saveAll(data).then(function(users) {
// Here we can access all three models!
users.at(1).get('username') // barbar
users.at(2).get('id') == 3 // true
});
The query would look like this:
insert into `users` (`username`, `email`, `created_at`) values (?, ?, ?);
update `users` set `username` = ?, `updated_at` = ? where `id` = ?
update `users` set `username` = ?, `updated_at` = ? where `id` = ? destroyAll(options)
The destroyAll method grabs all the ids of the present models and builds a delete query like:
delete from `users` where `id` in (?, ?, ?)Conclusion
So far, these methods do really work well for me. Still there are some questions about how to handle related models and implement a consistent behavior across the different databases. (I'm currently working with a MySQL database).
Would love to see a similar Collections API in upcoming releases. And I would like to hear what @tgriesser thinks about these suggestions.
Best regards! Malte
Source: bookshelf/bookshelf