bookshelf.js count method

后端 未结 6 822
星月不相逢
星月不相逢 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:55

    Here's the format that I'm currently using to do the following from a model:

    var Sample = bookshelf.Model.extend({
      tableName: 'example',
    
      count: function (cb) {
        bookshelf.knex('example')
          .count('id')
          .then(function (count) {
            cb(null, count)
          })
          .catch(function (err) {
            cb(err)
          })
      }
    })
    

    Now, to count this table, simply call

    new Sample().count(function(err, result) {
        console.log(result)
    });
    

提交回复
热议问题