Sails.JS - 获取数据库中对象/行数的计数

时间:2013-12-30 15:56:45

标签: javascript node.js sails.js waterline

在Sails.js中,通过生成模型和控制器为您完成了大量工作。控制器允许通过API访问数据。它允许通过传递开始/跳过/偏移和取/限制来轻松分页。但是为了让我确定一个集合的最大页面,我需要知道集合中有多少项目。

我尝试在生成器中扩展蓝图以公开新的API方法count并创建计数操作。根据{{​​3}},count是可以在模型上调用的操作。当我致电User.count()时,我明白了:

{ _context: 
   { identity: 'user',
     adapter: 
      { syncable: false,
        defaults: [Object],
        registerCollection: [Function],
        create: [Function],
        find: [Function],
        update: [Function],
        destroy: [Function],
        count: [Function],
        identity: 'odata',
        globalId: 'odata',
        config: [Object] },
     attributes: {},
     _cast: { _types: [Object] },
     _schema: { context: [Circular], schema: [Object], hasSchema: true },
     _validator: { validations: {} },
     _callbacks: 
      { beforeValidation: [Object],
        afterValidation: [Object],
        beforeUpdate: [Object],
        afterUpdate: [Object],
        beforeCreate: [Object],
        afterCreate: [Object],
        beforeDestroy: [Object],
        afterDestroy: [Object] },
     _instanceMethods: {},
     autoPK: true,
     autoCreatedAt: true,
     autoUpdatedAt: true,
     hasSchema: true,
     migrate: 'alter',
     _model: { [Function] extend: [Function], inject: [Function], __super__: {} },
     _transformer: { _transformations: {} },
     _tableName: 'user',
     _adapterDefs: [ [Object] ],
     _adapter: 
      { adapter: [Object],
        adapterDefs: [Object],
        query: [Circular],
        collection: 'user' },
     syncable: [Function],
     defaults: [Function],
     registerCollection: [Function],
     config: [Function],
     findOneById: [Function: dynamicMethod],
     findOneByIdIn: [Function: dynamicMethod],
     findOneByIdLike: [Function: dynamicMethod],
     findById: [Function: dynamicMethod],
     findByIdIn: [Function: dynamicMethod],
     findByIdLike: [Function: dynamicMethod],
     countById: [Function: dynamicMethod],
     countByIdIn: [Function: dynamicMethod],
     countByIdLike: [Function: dynamicMethod],
     idStartsWith: [Function: dynamicMethod],
     idContains: [Function: dynamicMethod],
     idEndsWith: [Function: dynamicMethod],
     findOneByCreatedAt: [Function: dynamicMethod],
     findOneByCreatedAtIn: [Function: dynamicMethod],
     findOneByCreatedAtLike: [Function: dynamicMethod],
     findByCreatedAt: [Function: dynamicMethod],
     findByCreatedAtIn: [Function: dynamicMethod],
     findByCreatedAtLike: [Function: dynamicMethod],
     countByCreatedAt: [Function: dynamicMethod],
     countByCreatedAtIn: [Function: dynamicMethod],
     countByCreatedAtLike: [Function: dynamicMethod],
     createdAtStartsWith: [Function: dynamicMethod],
     createdAtContains: [Function: dynamicMethod],
     createdAtEndsWith: [Function: dynamicMethod],
     findOneByUpdatedAt: [Function: dynamicMethod],
     findOneByUpdatedAtIn: [Function: dynamicMethod],
     findOneByUpdatedAtLike: [Function: dynamicMethod],
     findByUpdatedAt: [Function: dynamicMethod],
     findByUpdatedAtIn: [Function: dynamicMethod],
     findByUpdatedAtLike: [Function: dynamicMethod],
     countByUpdatedAt: [Function: dynamicMethod],
     countByUpdatedAtIn: [Function: dynamicMethod],
     countByUpdatedAtLike: [Function: dynamicMethod],
     updatedAtStartsWith: [Function: dynamicMethod],
     updatedAtContains: [Function: dynamicMethod],
     updatedAtEndsWith: [Function: dynamicMethod] },
  _method: [Function],
  _criteria: {},
  _values: null }

注意:我使用的是自定义适配器,但我为适配器创建了count方法。

2 个答案:

答案 0 :(得分:19)

获取用户集合中对象数量的计数:

User.count(function (err, num) {
    if(err) {
       return console.log(err);
    }
    console.log(num);
});

用另一种方法包装User.count:

yourMethod = function (callback) {
    User.count(callback);
}

答案 1 :(得分:1)

现在你可以使用sails可安装钩子通过blueprint api方法实现计数功能。

我在这个答案中解释道: Get total count in Sails JS blueprint API

您还可以将查询传递给count函数,以获取集合中满足特定条件的项目子集的计数。