Giter Site home page Giter Site logo

Comments (9)

tgriesser avatar tgriesser commented on April 28, 2024

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 query the way described above you can add them wherever you need... and by not doing query() but instead query('method', args*) you stay on the context of the current model/collection you're accessing rather than dropping down into the Knex builder.

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 BaseModel is exactly what I'm thinking would be a good way to go... so you can build out the API you'd find most intuitive... it's a little more verbose the first time but it makes the library easier to wrap your head around.

Also, a get raw json as the result when what I would like is a Collection of Models.

Sorry... I'm not sure I follow... you do or do not want that?

from bookshelf.

vjpr avatar vjpr commented on April 28, 2024

Sorry... I'm not sure I follow... you do or do not want that?

query().where() produces the json from the db. When I wanted a Model or a Collection of Models.

But you have covered this above:

you stay on the context of the current model/collection you're accessing rather than dropping down into the Knex builder.

from bookshelf.

tgriesser avatar tgriesser commented on April 28, 2024

Yeah, query('where', ... will do it.

from bookshelf.

vjpr avatar vjpr commented on April 28, 2024

And yeah, a BaseModel is exactly what I'm thinking would be a good way to go... so you can build out the API you'd find most intuitive... it's a little more verbose the first time but it makes the library easier to wrap your head around.

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.

from bookshelf.

vjpr avatar vjpr commented on April 28, 2024

Yeah, query('where', ... will do it.

Would be nice if you could pass in a hash.

from bookshelf.

tgriesser avatar tgriesser commented on April 28, 2024

Would be nice if you could pass in a hash.

Interesting, so like query({where: {id: 1}, orWhere: {id: 2})

from bookshelf.

vjpr avatar vjpr commented on April 28, 2024

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 Bookshelf.Knex.

from bookshelf.

tgriesser avatar tgriesser commented on April 28, 2024

Yeah... that's the idea, unless you call query() with no arguments which returns the underlying Knex builder, which I think I'll keep in there.

Take a look at the last commit for the object support.

from bookshelf.

tgriesser avatar tgriesser commented on April 28, 2024

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 query chain be the solution for this for now.

from bookshelf.

Related Issues (20)

Recommend Projects

  • React photo React

    A declarative, efficient, and flexible JavaScript library for building user interfaces.

  • Vue.js photo Vue.js

    🖖 Vue.js is a progressive, incrementally-adoptable JavaScript framework for building UI on the web.

  • Typescript photo Typescript

    TypeScript is a superset of JavaScript that compiles to clean JavaScript output.

  • TensorFlow photo TensorFlow

    An Open Source Machine Learning Framework for Everyone

  • Django photo Django

    The Web framework for perfectionists with deadlines.

  • D3 photo D3

    Bring data to life with SVG, Canvas and HTML. 📊📈🎉

Recommend Topics

  • javascript

    JavaScript (JS) is a lightweight interpreted programming language with first-class functions.

  • web

    Some thing interesting about web. New door for the world.

  • server

    A server is a program made to process requests and deliver data to clients.

  • Machine learning

    Machine learning is a way of modeling and interpreting data that allows a piece of software to respond intelligently.

  • Game

    Some thing interesting about game, make everyone happy.

Recommend Org

  • Facebook photo Facebook

    We are working to build community through open source technology. NB: members must have two-factor auth.

  • Microsoft photo Microsoft

    Open source projects and samples from Microsoft.

  • Google photo Google

    Google ❤️ Open Source for everyone.

  • D3 photo D3

    Data-Driven Documents codes.