最新消息:雨落星辰是一个专注网站SEO优化、网站SEO诊断、搜索引擎研究、网络营销推广、网站策划运营及站长类的自媒体原创博客

javascript - Sails.JS - Get the count of the number of objectsrows in the database - Stack Overflow

programmeradmin2浏览0评论

In Sails.js, a lot of work is done for you by generating the models and controllers. The controllers allow for access to the data through API's. It allows for easy pagination by passing the start/skip/offset and take/limit. But in order for me to determine the maximum page of a collection I need to know how many items are in a collection.

I have tried to extend the blueprint in the generator to expose a new API method count and create the count action. According to the Waterline documentation, count is an action that can be called on the model. When I call User.count(), I get this:

{ _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 }

Note: I am using a custom adapter, but I have created a count method for the adapter.

In Sails.js, a lot of work is done for you by generating the models and controllers. The controllers allow for access to the data through API's. It allows for easy pagination by passing the start/skip/offset and take/limit. But in order for me to determine the maximum page of a collection I need to know how many items are in a collection.

I have tried to extend the blueprint in the generator to expose a new API method count and create the count action. According to the Waterline documentation, count is an action that can be called on the model. When I call User.count(), I get this:

{ _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 }

Note: I am using a custom adapter, but I have created a count method for the adapter.

Share Improve this question asked Dec 30, 2013 at 15:56 TheSharpieOneTheSharpieOne 25.7k9 gold badges70 silver badges79 bronze badges 6
  • 2 User.count(yourCallbackFunction); – damphat Commented Dec 30, 2013 at 16:02
  • 1 Of course its callback based.. I don't know what I was thinking. That also explains what it was returning... the chain-able User object. – TheSharpieOne Commented Dec 30, 2013 at 16:14
  • I have not use sails, I think it should be User.count(function (err, num) { console.log(num); }); And if you defined a count method, it should be: count = function(callback) { User.count(callback) }; – damphat Commented Dec 30, 2013 at 16:30
  • I got it now with your help. If you put the information in an answer, I will mark it as the correct one. – TheSharpieOne Commented Dec 30, 2013 at 16:35
  • I want to see how and where do you expose a count method to sails, can you write the answer? – damphat Commented Dec 30, 2013 at 16:41
 |  Show 1 more comment

2 Answers 2

Reset to default 19

To get the count of the number of objects in user collection:

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

To wrap User.count in another method:

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

Now you can use sails installable hook to achieve count functionality through blueprint api methods.

I explained it in this answer: Get total count in Sails JS blueprint API

You can also pass query to the count function to get the count for a subset of items in a collection that satisfy a certain condition.

发布评论

评论列表(0)

  1. 暂无评论