-
Notifications
You must be signed in to change notification settings - Fork 568
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Helper to get single result from a M2M relation #30
Comments
So this is probably what you want: class MyModel extends Bookshelf.Model
items: (id) ->
@belongsToMany(Item)
new MyModel(id: 1).items().query('where', 'id', id).fetch().then (c) ->
c.at(0) The reason I didn't want to put finder methods in is because there's a million permutations of them, and once you start adding direct support for them the API can quickly become a bit of a mess. By using Though I agree some way to fetch/return a single model from a collection could a good addition in this case (hadn't considered relations), I'll take a look into supporting it... And yeah, a
Sorry... I'm not sure I follow... you do or do not want that? |
But you have covered this above:
|
Yeah, |
I agree. The thing that drew me to Bookshelf was the fact that it was layered really nicely. In Sequelize and most ORMs if you find something you can't do you have to drop straight down to SQL, and extensibility is difficult. |
Would be nice if you could pass in a hash. |
Interesting, so like |
Yep. That would be great. Also, if a query is called on a collection I think it should always return the models as a collection of models. If the user wants to run a raw query they should use |
Yeah... that's the idea, unless you call Take a look at the last commit for the object support. |
* master: 0.2.0 some docs cleanups minor docs tweaks Pivot column typo, fixes #31 Fix for #33, empty EagerRelation attachment, with tests. #30, allowing objects to be passed to query builder docs tweaks, blank target for some external links returning null for empty model fetch - #21 better docs for attach, fixes #28 fixing issue with exec plugin
So I think that I'm going to revisit the idea of fetching a single object from a collection at some point down the road... for now I'm going to have the object support for the |
A common query would be to get a single result from a M2M.
It becomes something like:
In Sequelize I could write `User.getCollection(where: {id: id}).done ...) which would return a single result.
Having a
single
method which returns on object or null rather than an array. Maybe more for the Knex lib?@belongsToMany(db.Models.Collection).query().where(id: id).single()
You could go even shorter too.
Also, a get raw json as the result when what I would like is a Collection of Models.
On a side note, I'm finding a lot of common queries are quite verbose in Bookshelf. I think
finder
method would help with this.At present I've got a
BaseModel
class with lots of finder methods. I love the extensibility and clean Backbone-style codebase + documentation compared with Sequelize, but simple common operations are still a little verbose. I'm very conscious though of the desire to keep the lib quite small without the kitchen-sink, like Backbone.The text was updated successfully, but these errors were encountered: