Giter Site home page Giter Site logo

Comments (7)

tgriesser avatar tgriesser commented on April 28, 2024

Not really yet - but it should be easy to put a method like that together do yourself:

Projects = Bookshelf.Collection.extend({
  findAndCountAll: function(modifiers) {
     var knex = Bookshelf.Knex(_.result(this, 'tableName'));
     return when.all([
        this.query(modifiers).fetch(),
        knex.where.apply(knex, modifiers.where).count('*')
     ]).then(function(resp) {
        return  {
           rows: resp[0],
           count: resp[1][0].aggregate
        };
     });
  }
});

projects.findAndCountAll({where: ['title', 'like', 'foo%'], offset: 10, limit: 2}).then(...

pretty sure that or something like that should work, didn't test it or anything...

Also, you may want to look at increment.

user.query().increment('versionNumber').then(...

from bookshelf.

amitava82 avatar amitava82 commented on April 28, 2024

findAndCountAll works perfect, thanks! I've added a second argument to be passed to fetch() to load relations.
increment works fine as a standalone query, but I'm looking for a way to do it with update statement; as in when I update a row with where clause without fetching the model first.

from bookshelf.

tgriesser avatar tgriesser commented on April 28, 2024

I'm not sure I follow, would this work?

var User = Bookshelf.Model.extend({
   increment: function(where, count) {
     count = count || 1;
     return this.query().where(where).increment('field', count)
   } 
});

then:

user.increment(['id', '>', 10]).then(...

from bookshelf.

amitava82 avatar amitava82 commented on April 28, 2024

Sorry I was not very clear. What I meant was this:

UPDATE users
   SET logins = logins + 1, emailId='[email protected]', phone='1234567'
   WHERE id = 12

from bookshelf.

tgriesser avatar tgriesser commented on April 28, 2024

My general inclination is that this is something best accomplished with custom method:

incrementLogin: function() {
   var model = this;
   return this.query()
      .where(id, this.id)
      .update(_.extend(this.omit('logins'), {logins: Knex.Raw('logins + 1')})
      .then(function() {
         return model;
   });
} 

But I could consider adding something like this in the future... I just don't want to clutter the bookshelf api with too many things that could be accomplished fairly easily by dropping down to the Knex layer.

user.incrementLogin().then(...

from bookshelf.

demisx avatar demisx commented on April 28, 2024

@tgriesser Is Bookshelf.knex the current way to get a hold of the knex instance from the bookshelf instance? Bookshelf.Knex shown in the example above returns undefined.

from bookshelf.

bendrucker avatar bendrucker commented on April 28, 2024

Yes, lowercase

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.