在Sails.js中,通过生成模型和控制器为您完成了大量工作.控制器允许通过API访问数据.它允许通过传递开始/跳过/偏移和取/限制来轻松分页.但是为了让我确定一个集合的最大页面,我需要知道集合中有多少项.
我试图在生成器中扩展蓝图以公开新的API方法计数并创建计数操作.根据Waterline documentation,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],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 }