Comments (7)
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.
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.
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.
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.
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.
@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.
Yes, lowercase
from bookshelf.
Related Issues (20)
- Change primary key value
- withRelated TypeScript error HOT 1
- Bookshelf.js update query not returning expected data
- Bookshelf.js update query not returning expected data
- Enhancement: ignore extra keys in withRelated option?
- ErrorCtor [CustomError]: EmptyResponse HOT 1
- How to save a object with hasMany/hasOne relations ?
- Make has_one relation with another model with composite id, Is it possible?
- [email protected]" has incorrect peer dependency "knex@>=0.15.0 <0.22.0" HOT 23
- Unable to use method named as attributes( ) in a model.
- Doc: MSSQL support
- How to know if a transaction has been committed? HOT 1
- Typescript models do not have relationship functions HOT 1
- Loop of transactions or transactions + promise pool
- Remove freenode references HOT 1
- Bookshelf using vulnerable version of lodash
- Property 'fetchPage' does not exist on type.
- Is Bookshelf actively maintained? HOT 1
- Limited SQL Injection Vulnerability in Bookshelf.js
- Specify sub-ordinate database name to connect to through Bookshelf model
Recommend Projects
-
React
A declarative, efficient, and flexible JavaScript library for building user interfaces.
-
Vue.js
🖖 Vue.js is a progressive, incrementally-adoptable JavaScript framework for building UI on the web.
-
Typescript
TypeScript is a superset of JavaScript that compiles to clean JavaScript output.
-
TensorFlow
An Open Source Machine Learning Framework for Everyone
-
Django
The Web framework for perfectionists with deadlines.
-
Laravel
A PHP framework for web artisans
-
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.
-
Visualization
Some thing interesting about visualization, use data art
-
Game
Some thing interesting about game, make everyone happy.
Recommend Org
-
Facebook
We are working to build community through open source technology. NB: members must have two-factor auth.
-
Microsoft
Open source projects and samples from Microsoft.
-
Google
Google ❤️ Open Source for everyone.
-
Alibaba
Alibaba Open Source for everyone
-
D3
Data-Driven Documents codes.
-
Tencent
China tencent open source team.
from bookshelf.