bookshelf.js count method

后端 未结 6 916
星月不相逢
星月不相逢 2021-02-14 01:20

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

6条回答
  •  天命终不由人
    2021-02-14 01:53

    //user.js
    var db = require('./db');
    var User = db.Model.extend({  
      tableName: 'users',
      hasTimestamps: true,
    });
    module.exports = User; 
    
    //main.js
    var User = require('./user');
    User.where('pet', 'dog').count('*').then(function(count) {
      console.log("Total Count : " , count);
    });
    

    In the above code snippet User is a model having the attributes like pet and name.

    The generated query will be as follows: select count(*) from user where pet='dog';

提交回复
热议问题