{"action":"created","issue":{"url":"https://api.github.com/repos/keystonejs/keystone/issues/2095","labels_url":"https://api.github.com/repos/keystonejs/keystone/issues/2095/labels{/name}","comments_url":"https://api.github.com/repos/keystonejs/keystone/issues/2095/comments","events_url":"https://api.github.com/repos/keystonejs/keystone/issues/2095/events","html_url":"https://github.com/keystonejs/keystone/pull/2095","id":126575759,"number":2095,"title":"Refactor Pagination to allow optional expressions","user":{"login":"alsoicode","id":86738,"avatar_url":"https://avatars.githubusercontent.com/u/86738?v=3","gravatar_id":"","url":"https://api.github.com/users/alsoicode","html_url":"https://github.com/alsoicode","followers_url":"https://api.github.com/users/alsoicode/followers","following_url":"https://api.github.com/users/alsoicode/following{/other_user}","gists_url":"https://api.github.com/users/alsoicode/gists{/gist_id}","starred_url":"https://api.github.com/users/alsoicode/starred{/owner}{/repo}","subscriptions_url":"https://api.github.com/users/alsoicode/subscriptions","organizations_url":"https://api.github.com/users/alsoicode/orgs","repos_url":"https://api.github.com/users/alsoicode/repos","events_url":"https://api.github.com/users/alsoicode/events{/privacy}","received_events_url":"https://api.github.com/users/alsoicode/received_events","type":"User","site_admin":false},"labels":[],"state":"open","locked":false,"assignee":null,"milestone":null,"comments":1,"created_at":"2016-01-14T04:40:06Z","updated_at":"2016-01-14T05:31:37Z","closed_at":null,"pull_request":{"url":"https://api.github.com/repos/keystonejs/keystone/pulls/2095","html_url":"https://github.com/keystonejs/keystone/pull/2095","diff_url":"https://github.com/keystonejs/keystone/pull/2095.diff","patch_url":"https://github.com/keystonejs/keystone/pull/2095.patch"},"body":"## Rationale\r\nA user may want to paginate results that are from a full text search where an extra expression is used for sorting. Example:\r\n\r\n    Product.paginate({\r\n            page: req.query.page || 1,\r\n            perPage: 1,\r\n            filters: {\r\n                $text: { $search: req.query.searchTerm || '' }\r\n            },\r\n            optionalExpression: {\r\n                score: { $meta: 'textScore' }\r\n            }\r\n        })\r\n        .sort({\r\n            score: { $meta: 'textScore' }\r\n        })\r\n        .exec(function(error, results) {\r\n            locals.results = results;\r\n            next(error);\r\n        });\r\n\r\n## Problem\r\n- The current implementation of [lib/list/paginate.js](https://github.com/keystonejs/keystone/blob/master/lib/list/paginate.js) does not allow for extra expressions to be passed to `model.find()`\r\n- When extra metadata is added to the find statement, it interferes with the `query.count()` operation, causing it to fail with:\r\n    \r\n    ```Error: field selection and slice cannot be used with count```\r\n\r\n## Solution\r\n- Added `options.optionalExpression` value to `model.find()`. This allows an additional object to be passed to the find method. If `options.optionalExpression` is undefined, the `.find()` operation is unaffected.\r\n- Added an additional query that does not include the `optionalExpression` object to perform the count of objects. This allows the count to be performed without additional parameters. See: https://github.com/aheckmann/mquery/issues/55 for more information.\r\n\r\n## Testing / Linting\r\n- All tests are passing via: `npm test`\r\n- No jslint errors"},"comment":{"url":"https://api.github.com/repos/keystonejs/keystone/issues/comments/171535943","html_url":"https://github.com/keystonejs/keystone/pull/2095#issuecomment-171535943","issue_url":"https://api.github.com/repos/keystonejs/keystone/issues/2095","id":171535943,"user":{"login":"ericelliott","id":364727,"avatar_url":"https://avatars.githubusercontent.com/u/364727?v=3","gravatar_id":"","url":"https://api.github.com/users/ericelliott","html_url":"https://github.com/ericelliott","followers_url":"https://api.github.com/users/ericelliott/followers","following_url":"https://api.github.com/users/ericelliott/following{/other_user}","gists_url":"https://api.github.com/users/ericelliott/gists{/gist_id}","starred_url":"https://api.github.com/users/ericelliott/starred{/owner}{/repo}","subscriptions_url":"https://api.github.com/users/ericelliott/subscriptions","organizations_url":"https://api.github.com/users/ericelliott/orgs","repos_url":"https://api.github.com/users/ericelliott/repos","events_url":"https://api.github.com/users/ericelliott/events{/privacy}","received_events_url":"https://api.github.com/users/ericelliott/received_events","type":"User","site_admin":false},"created_at":"2016-01-14T05:31:37Z","updated_at":"2016-01-14T05:31:37Z","body":"Any way you could add some functional tests to ensure that this works correctly?"}}