zbrox zbrox - 1 month ago 10
Node.js Question

bookshelf.js count method

I was searching high and low to find how to do basic counting (like SELECT COUNT(something) FROM table) with Bookshelf.js, but to no avail. Is there anything I'm missing? Or is it just used with a manual select query?

Thanks!

Answer

For now it's a manual query... e.g:

bookshelf.knex(tableName).count('columnName').then(...

Long story as to why that's the case, but it's mainly because of a few complexities around counts in relations, I don't want to hack a half working one on there just for the sake of having it there for the time being... the ultimate solution is going to be to have:

model.query().count(column).then(...

But currently that mutates the knex query builder instance, so doesn't quite work. I've been working on a big refactor of knex and I'm looking to get this implemented in the near future.