Giter Site home page Giter Site logo

Comments (10)

vjpr avatar vjpr commented on April 28, 2024

Maybe you should be able to declaratively set the where clause on the table.

class UserCollection extends Bookshelf.Collection
  tableName: 'users'
  where: {id: 1}

users = new UserCollection
users.fetch().then (models) -> ...

The where is then like the url in Backbone.

At the moment fetch gets the entire table which is not what you want most of the time.

Or perhaps just adding a where method onto the collection directly?

from bookshelf.

tgriesser avatar tgriesser commented on April 28, 2024

You can use the query method on both the Model or Collection to do something like this:

new UserCollection().query('where', {active: true}).fetch().then(function(collection) {

Where the first param will be the Knex method you want to call, and the rest of the arguments will be apply'ed on that function, and ultimately returns the model or collection you're working with...

from bookshelf.

vjpr avatar vjpr commented on April 28, 2024

Ok. I can just extend Bookshelf.Collection if need be.

from bookshelf.

tgriesser avatar tgriesser commented on April 28, 2024

I think ultimately I may add some class finder class methods, I just wanted to start off without them.

from bookshelf.

tgriesser avatar tgriesser commented on April 28, 2024

Going to close this for now, will reopen down the line if/when static finder methods are something that make sense to add (probably after the core api is nailed down a bit further).

from bookshelf.

tgriesser avatar tgriesser commented on April 28, 2024

Alright, so I'm starting a branch called active-record which will extend the base of Bookshelf to provide an almost 1-to-1 map with Rails' Active-record.

Feel free to take a look and see the direction that's being taken over there

from bookshelf.

vjpr avatar vjpr commented on April 28, 2024

Great to see this reopened :) I'll take a look.

from bookshelf.

tgriesser avatar tgriesser commented on April 28, 2024

I've got some cool ideas for how this could be done with the bluebird promise lib's error specific catch, and by creating a deferred chain for the model calls, until a then coerces it to a promise:

Err = require 'bookshelf/errors'
Model.find(1).destroy().then ->
   console.log('model destroyed');
.catch Err.ModelNotFound, (err) ->
   console.log('model not found' + err.stack);   
.catch Err.ModelNotDestroyed, (err) ->
   console.log('model could not be destroyed' + err.stack);

which would expand to:

Err = require 'bookshelf/errors'
Model.find(1).then ->
   return model.destroy()
.then ->
   console.log('model destroyed');
.catch Err.ModelNotFound, (err) ->
   console.log('model not found' + err.stack);   
.catch Err.ModelNotDestroyed, (err) ->
   console.log('model could not be destroyed' + err.stack);

In that instance it only saves a line, but I feel like this idea could make for a really cool looking chaining api here.

/cc @rstacruz

from bookshelf.

mistakia avatar mistakia commented on April 28, 2024

That looks great.

p.s. Ive setup a simple temp solution using simple class property wrappers
like these for now -
https://github.com/TryGhost/Ghost/blob/master/core/server/models/base.js

On Monday, November 25, 2013, Tim Griesser wrote:

I've got some cool ideas for how this could be done with the bluebird
promise lib's error specific catch, and by creating a deferred chain for
the model calls, until a then coerces it to a promise:

Err = require 'bookshelf/errors'Model.find(1).destroy().then ->
console.log('model destroyed');.catch Err.ModelNotFound, (err) ->
console.log('model not found' + err.stack); .catch Err.ModelNotDestroyed, (err) ->
console.log('model could not be destroyed' + err.stack);

which would expand to:

Err = require 'bookshelf/errors'Model.find(1).then ->
return model.destroy().then ->
console.log('model destroyed');.catch Err.ModelNotFound, (err) ->
console.log('model not found' + err.stack); .catch Err.ModelNotDestroyed, (err) ->
console.log('model could not be destroyed' + err.stack);

In that instance it only saves a line, but I feel like this idea could
make for a really cool looking chaining api here.

/cc @rstacruz https://github.com/rstacruz


Reply to this email directly or view it on GitHubhttps://github.com//issues/27#issuecomment-29226238
.

from bookshelf.

tgriesser avatar tgriesser commented on April 28, 2024

Added model.fetchAll for fetching a collection, as well as .where, and .query as both static and prototype methods to the model.

So with that, you can now do

User.where({id: 1}).fetch().then(function(model) {

User.where('active', true).fetchAll().then(function(collection) { ...

User.query({where: {active: true}, orWhere: {admin: true}})
  .fetchAll().then(function(collection) {...

which should provide a nice syntax for dealing with the most common use cases until the more robust lib which extends bookshelf with scopes/relations/validations etc is done.

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.