diff --git a/lib/util/cmd.helper.js b/lib/util/cmd.helper.js index d128be3424..e1f9de846a 100644 --- a/lib/util/cmd.helper.js +++ b/lib/util/cmd.helper.js @@ -11,7 +11,7 @@ program.on('--help', () => { }) program - .version('0.3.2') + .version('0.3.3') .option('-h, --host ', 'hostname / localhost by default') .option('-u, --user ', 'username of database / root by default') .option('-p, --password ', 'password of database / empty by default') diff --git a/lib/xapi.js b/lib/xapi.js index 74c4f40b40..912c262ee3 100644 --- a/lib/xapi.js +++ b/lib/xapi.js @@ -189,6 +189,11 @@ class Xapi { .get(this.asyncMiddleware(this.count.bind(this))); break; + case 'distinct': + this.app.route(routes[i]['routeUrl']) + .get(this.asyncMiddleware(this.distinct.bind(this))); + break; + case 'describe': this.app.route(routes[i]['routeUrl']) .get(this.asyncMiddleware(this.tableDescribe.bind(this))); @@ -271,7 +276,7 @@ class Xapi { * @param req * @param res * @param queryParamsObj : {query, params} - * @param listType : 0:list, 1:nested, 2:findOne, 3:bulkRead + * @param listType : 0:list, 1:nested, 2:findOne, 3:bulkRead, 4:distinct * * Updates query, params for query of type listType */ @@ -280,6 +285,10 @@ class Xapi { queryParamsObj.query = 'select '; queryParamsObj.params = []; + if (listType === 4) { //list type distinct + queryParamsObj.query += ' distinct ' + } + /**************** select columns ****************/ if (req.query._groupby) { this.mysql.getColumnsForSelectStmtWithGrpBy(req.query, req.app.locals._tableName, queryParamsObj); @@ -377,6 +386,19 @@ class Xapi { } + async distinct(req, res) { + + let queryParamsObj = {} + queryParamsObj.query = '' + queryParamsObj.params = [] + + this.prepareListQuery(req, res, queryParamsObj, 4); + + let results = await this.mysql.exec(queryParamsObj.query, queryParamsObj.params); + res.status(200).json(results); + + } + async nestedList(req, res) { let queryParamsObj = {} diff --git a/lib/xsql.js b/lib/xsql.js index 02e4277d5b..472c6e294b 100644 --- a/lib/xsql.js +++ b/lib/xsql.js @@ -527,6 +527,7 @@ class Xsql { routes.push(this.prepareRoute(internal, 'get', apiPrefix, tableName + '/describe', 'describe')) routes.push(this.prepareRoute(internal, 'get', apiPrefix, tableName + '/count', 'count')) routes.push(this.prepareRoute(internal, 'get', apiPrefix, tableName + '/groupby', 'groupby')) + routes.push(this.prepareRoute(internal, 'get', apiPrefix, tableName + '/distinct', 'distinct')) routes.push(this.prepareRoute(internal, 'get', apiPrefix, tableName + '/ugroupby', 'ugroupby')) routes.push(this.prepareRoute(internal, 'get', apiPrefix, tableName + '/chart', 'chart')) routes.push(this.prepareRoute(internal, 'get', apiPrefix, tableName + '/aggregate', 'aggregate')) diff --git a/package.json b/package.json index ee0083712d..1b26d947eb 100644 --- a/package.json +++ b/package.json @@ -1,6 +1,6 @@ { "name": "xmysql", - "version": "0.3.2", + "version": "0.3.3", "description": "One command to generate REST APIs for any MySql database", "main": "index.js", "scripts": { diff --git a/tests/tests.js b/tests/tests.js index f0af8a7294..f21cc81c48 100644 --- a/tests/tests.js +++ b/tests/tests.js @@ -109,6 +109,25 @@ describe('xmysql : tests', function () { }); + it('GET /api/offices/distinct?_fields=country should PASS', function (done) { + + //http get an url + agent.get('/api/offices/distinct?_fields=country') // api url + .expect(200) // 2xx for success and 4xx for failure + .end(function (err, res) { + // Handle /api/tables error + if (err) { + return done(err); + } + + //validate response + res.body.length.should.be.equal(5); + + return done(); + + }); + + }); it('GET /api/customers/describe should PASS', function (done) {