From bd41cb8bf73d50d09ddfee4c0070508249ada984 Mon Sep 17 00:00:00 2001 From: Eugene Butusov Date: Wed, 18 Jan 2017 20:09:10 +0300 Subject: [PATCH 1/7] Implement distinct field option for select --- lib/controller.js | 12 ++++++++++++ lib/data-service.js | 36 ++++++++++++++++++++++++++++++++++++ 2 files changed, 48 insertions(+) diff --git a/lib/controller.js b/lib/controller.js index b1caf7b..a390ab8 100644 --- a/lib/controller.js +++ b/lib/controller.js @@ -45,6 +45,18 @@ class Controller extends DataService { this.actions.select ); + this.actions.distinct = _.merge( + {}, + this.actions.default, + { + method: ['get', 'head'], + handler: 'distinct', + name: 'distinct', + path: '' + }, + this.actions.distinct + ); + this.actions.count = _.merge( {}, this.actions.select, diff --git a/lib/data-service.js b/lib/data-service.js index da145cd..4a496bc 100644 --- a/lib/data-service.js +++ b/lib/data-service.js @@ -102,6 +102,42 @@ class DataService { }); } + distinct(scope) { + return Bb + .try(this._handlePre.bind(this, scope)) + .then(this.getFilter.bind(this, scope)) + .then((filter) => { + // field list + scope.fieldList = this.extractFieldList(scope); + // q + const q = scope.getQ(); + // orderBy + const orderBy = this.getOrderBy(scope); + const pagination = scope.pagination = this.getPagination(scope); + const distinctField = scope.req.params.distinctField; + + return this.dataSource.find({ + distinctField: distinctField, + filter: filter, + fields: scope.fieldList, + q: q, + qFields: this.qFields, + sort: orderBy, + limit: pagination.limit, + skip: (pagination.page - 1) * pagination.limit, + queryPipe: this.queryPipe ? (query) => { + this.queryPipe(query, scope); + } : undefined + }); + }) + .then((collection) => { + return this._handleCollectionPost(collection, scope); + }) + .then((collection) => { + return this._handlePostForCollection(collection, scope); + }); + } + selectOne(scope) { return this .locateModel(scope, true, true) From dce75f045967b823f51790e8fdbfd943c2ffca8d Mon Sep 17 00:00:00 2001 From: Eugene Butusov Date: Wed, 18 Jan 2017 20:45:37 +0300 Subject: [PATCH 2/7] Implement distinct field option for select add correct path --- lib/controller.js | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/lib/controller.js b/lib/controller.js index a390ab8..a7710c6 100644 --- a/lib/controller.js +++ b/lib/controller.js @@ -52,7 +52,7 @@ class Controller extends DataService { method: ['get', 'head'], handler: 'distinct', name: 'distinct', - path: '' + path: 'distinct/:distinctField' }, this.actions.distinct ); From 515d919a6332ec070f79b8689f7c1859bc0e8571 Mon Sep 17 00:00:00 2001 From: Eugene Butusov Date: Thu, 19 Jan 2017 12:04:53 +0300 Subject: [PATCH 3/7] Implement distinct field option for select pick distinctField value from params to avoid filter affecting --- lib/data-service.js | 7 ++++++- 1 file changed, 6 insertions(+), 1 deletion(-) diff --git a/lib/data-service.js b/lib/data-service.js index 4a496bc..960bc58 100644 --- a/lib/data-service.js +++ b/lib/data-service.js @@ -105,6 +105,11 @@ class DataService { distinct(scope) { return Bb .try(this._handlePre.bind(this, scope)) + .then(() => { + // pick distinctField value to avoid filter affecting + scope.context.distinctField = scope.req.params.distinctField; + delete scope.req.params.distinctField; + }) .then(this.getFilter.bind(this, scope)) .then((filter) => { // field list @@ -114,7 +119,7 @@ class DataService { // orderBy const orderBy = this.getOrderBy(scope); const pagination = scope.pagination = this.getPagination(scope); - const distinctField = scope.req.params.distinctField; + const distinctField = scope.context.distinctField; return this.dataSource.find({ distinctField: distinctField, From e768a067ff46545d47272e25edc1056e1a7091d0 Mon Sep 17 00:00:00 2001 From: Eugene Butusov Date: Thu, 19 Jan 2017 12:11:40 +0300 Subject: [PATCH 4/7] Implement distinct field option for select add distinct action checking --- lib/scope.js | 5 +++++ 1 file changed, 5 insertions(+) diff --git a/lib/scope.js b/lib/scope.js index e57629c..e219b0d 100644 --- a/lib/scope.js +++ b/lib/scope.js @@ -7,6 +7,7 @@ const _ = require('lodash'); const ACTIONS = { SELECT: 'select', + DISTINCT: 'distinct', SELECT_ONE: 'selectOne', INSERT: 'insert', REPLACE: 'replace', @@ -44,6 +45,10 @@ class RestifizerScope { return this.checkActionName(ACTIONS.SELECT, ACTIONS.SELECT_ONE, ACTIONS.COUNT); } + isSelectDistinct() { + return this.checkActionName(ACTIONS.DISTINCT); + } + isChanging() { return this.isInsert() || this.isUpdate() || this.isDelete(); } From 0c119bf93bd90dae67521164d78951bb65a67d8e Mon Sep 17 00:00:00 2001 From: Eugene Butusov Date: Sun, 29 Oct 2017 15:08:52 +0300 Subject: [PATCH 5/7] Ignore ordering for distinct --- lib/controller.js | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/lib/controller.js b/lib/controller.js index a7710c6..9d239a8 100644 --- a/lib/controller.js +++ b/lib/controller.js @@ -47,7 +47,7 @@ class Controller extends DataService { this.actions.distinct = _.merge( {}, - this.actions.default, + _.omit(this.actions.default, 'orderBy'), { method: ['get', 'head'], handler: 'distinct', From 9cc11cf8031a73e8154d0660b2924de27ca75ad3 Mon Sep 17 00:00:00 2001 From: Eugene Butusov Date: Sun, 29 Oct 2017 15:12:28 +0300 Subject: [PATCH 6/7] add missed changes --- lib/scope.js | 2 +- package.json | 2 +- 2 files changed, 2 insertions(+), 2 deletions(-) diff --git a/lib/scope.js b/lib/scope.js index e219b0d..a714a66 100644 --- a/lib/scope.js +++ b/lib/scope.js @@ -42,7 +42,7 @@ class RestifizerScope { } isSelect() { - return this.checkActionName(ACTIONS.SELECT, ACTIONS.SELECT_ONE, ACTIONS.COUNT); + return this.checkActionName(ACTIONS.SELECT, ACTIONS.SELECT_ONE, ACTIONS.COUNT, ACTIONS.DISTINCT); } isSelectDistinct() { diff --git a/package.json b/package.json index eb8f98a..4e8a4c1 100644 --- a/package.json +++ b/package.json @@ -28,7 +28,7 @@ "bluebird": "^3.4.1", "http-statuses": "^0.1.1", "lodash": "^3.10.1", - "restifizer-mongoose-ds": "^0.1.6", + "restifizer-mongoose-ds": "git://github.com/vedi/restifizer-mongoose-ds.git#f8d075fa19080ff4cfa41c222437ce22e0159c94", "restifizer-sequelize-ds": "^0.2.2" }, "author": { From ae986b6d2b383146846ac4668f4b5d719f62b9ac Mon Sep 17 00:00:00 2001 From: Eugene Butusov Date: Sun, 29 Oct 2017 15:27:09 +0300 Subject: [PATCH 7/7] remove OrderBy from distinct --- lib/data-service.js | 1 - 1 file changed, 1 deletion(-) diff --git a/lib/data-service.js b/lib/data-service.js index 960bc58..c38b889 100644 --- a/lib/data-service.js +++ b/lib/data-service.js @@ -127,7 +127,6 @@ class DataService { fields: scope.fieldList, q: q, qFields: this.qFields, - sort: orderBy, limit: pagination.limit, skip: (pagination.page - 1) * pagination.limit, queryPipe: this.queryPipe ? (query) => {