Browse Source

Feature : Distinct API, npm v0.3.3

pull/13/head
oof1lab 7 years ago
parent
commit
5d2030a089
  1. 2
      lib/util/cmd.helper.js
  2. 24
      lib/xapi.js
  3. 1
      lib/xsql.js
  4. 2
      package.json
  5. 19
      tests/tests.js

2
lib/util/cmd.helper.js

@ -11,7 +11,7 @@ program.on('--help', () => {
})
program
.version('0.3.2')
.version('0.3.3')
.option('-h, --host <n>', 'hostname / localhost by default')
.option('-u, --user <n>', 'username of database / root by default')
.option('-p, --password <n>', 'password of database / empty by default')

24
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 = {}

1
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'))

2
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": {

19
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) {

Loading…
Cancel
Save