From 7a2c5b3f627c88c63c89e8b7ff14ac8621ff283b Mon Sep 17 00:00:00 2001 From: oof1lab Date: Tue, 14 Nov 2017 14:06:05 +0530 Subject: [PATCH] Feature : Support VIEWS npm v0.2.7 --- README.md | 5 +++-- lib/util/cmd.helper.js | 2 +- lib/util/data.helper.js | 10 +++++++--- lib/xsql.js | 27 +++++++++++++++++++-------- package.json | 2 +- tests/tests.js | 10 +++++----- 6 files changed, 36 insertions(+), 20 deletions(-) diff --git a/README.md b/README.md index dbafa54023..042095ca3f 100644 --- a/README.md +++ b/README.md @@ -73,7 +73,8 @@ Powered by popular node packages : ([express](https://github.com/expressjs/expre * Group By, Having (as query params) :fire::fire: * Group By, Having (as a separate API) :fire::fire: * Multiple group by in one API :fire::fire: -* Chart API for numeric column :fire::fire: +* Chart API for numeric column :fire::fire: +* Supports views * Prototyping (features available when using local MySql server only) * Run dynamic queries :fire::fire::fire: * Upload single file @@ -629,7 +630,7 @@ http://localhost:3000/download?name=fileName ## When NOT to use ? [:arrow_heading_up:](#api-overview) -* If you are in need of a full blown MVC framework, ACL, Authorisation etc - its early days please watch/star this repo. Thank you. +* If you are in need of a full blown MVC framework, ACL, Validations, Authorisation etc - its early days please watch/star this repo to keep a tab on progress. ### Command line options diff --git a/lib/util/cmd.helper.js b/lib/util/cmd.helper.js index 2f08263489..c13b8cf5c5 100644 --- a/lib/util/cmd.helper.js +++ b/lib/util/cmd.helper.js @@ -11,7 +11,7 @@ program.on('--help', () => { }) program - .version('0.2.6') + .version('0.2.7') .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/util/data.helper.js b/lib/util/data.helper.js index de21f6a5c4..2525bf2faa 100644 --- a/lib/util/data.helper.js +++ b/lib/util/data.helper.js @@ -20,7 +20,6 @@ exports.findOrInsertObjectArrayByKey = (obj, key, array) => { } return array[i]; - }; @@ -33,7 +32,6 @@ exports.findObjectInArrayByKey = (key, value, objArray) => { } return null; - }; @@ -134,7 +132,8 @@ exports.getSchemaQuery = function () { return 'select c.table_name, c.column_name, c.ordinal_position,c.column_key,c.is_nullable, c.data_type, c.column_type,c.extra,c.privileges, ' + 'c.column_comment,c.column_default,c.data_type,c.character_maximum_length, ' + 'k.constraint_name, k.referenced_table_name, k.referenced_column_name, ' + - 's.index_name,s.seq_in_index ' + + 's.index_name,s.seq_in_index, ' + + 'v.table_name as isView ' + 'from ' + 'information_schema.columns as c ' + 'left join ' + @@ -149,6 +148,11 @@ exports.getSchemaQuery = function () { 'c.column_name = s.column_name and ' + 'c.table_schema = s.index_schema and ' + 'c.table_name = s.table_name ' + + 'LEFT JOIN ' + + 'information_schema.VIEWS as v ' + + 'ON ' + + 'c.table_schema = v.table_schema and ' + + 'c.table_name = v.table_name ' + 'where ' + 'c.table_schema=? ' + 'order by ' + diff --git a/lib/xsql.js b/lib/xsql.js index ea5caad6e7..2aff769ee4 100644 --- a/lib/xsql.js +++ b/lib/xsql.js @@ -70,6 +70,7 @@ class Xsql { this.metaDb.tables[tableName]['foreignKeys'] = [] this.metaDb.tables[tableName]['columns'] = [] this.metaDb.tables[tableName]['indicies'] = [] + this.metaDb.tables[tableName]['isView'] = schemaRow['table_name']['isView'] } } } @@ -520,9 +521,9 @@ class Xsql { let routes = [] let tableObj = {} let table = this.metaDb.tables[tableName]; + let isView = this.metaDb.tables[tableName]['isView']; tableObj['resource'] = tableName; - 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')) @@ -530,21 +531,31 @@ class Xsql { routes.push(this.prepareRoute(internal, 'get', apiPrefix, tableName + '/chart', 'chart')) routes.push(this.prepareRoute(internal, 'get', apiPrefix, tableName + '/aggregate', 'aggregate')) routes.push(this.prepareRoute(internal, 'get', apiPrefix, tableName + '/findOne', 'findOne')) - routes.push(this.prepareRoute(internal, 'post', apiPrefix, tableName, 'create')) + + if (!isView) { + routes.push(this.prepareRoute(internal, 'post', apiPrefix, tableName, 'create')) + } routes.push(this.prepareRoute(internal, 'get', apiPrefix, tableName, 'list')) - routes.push(this.prepareRoute(internal, 'post', apiPrefix, tableName + '/bulk', 'bulkInsert')) + + if (!isView) { + routes.push(this.prepareRoute(internal, 'post', apiPrefix, tableName + '/bulk', 'bulkInsert')) + routes.push(this.prepareRoute(internal, 'delete', apiPrefix, tableName + '/bulk', 'bulkDelete')) + } routes.push(this.prepareRoute(internal, 'get', apiPrefix, tableName + '/bulk', 'bulkRead')) - routes.push(this.prepareRoute(internal, 'delete', apiPrefix, tableName + '/bulk', 'bulkDelete')) - routes.push(this.prepareRoute(internal, 'put', apiPrefix, tableName, 'update')) + + if (!isView) { + routes.push(this.prepareRoute(internal, 'put', apiPrefix, tableName, 'update')) + routes.push(this.prepareRoute(internal, 'patch', apiPrefix, tableName + '/:id', 'patch')) + routes.push(this.prepareRoute(internal, 'delete', apiPrefix, tableName + '/:id', 'delete')) + } + routes.push(this.prepareRoute(internal, 'get', apiPrefix, tableName + '/:id', 'read')) - routes.push(this.prepareRoute(internal, 'patch', apiPrefix, tableName + '/:id', 'patch')) - routes.push(this.prepareRoute(internal, 'delete', apiPrefix, tableName + '/:id', 'delete')) routes.push(this.prepareRoute(internal, 'get', apiPrefix, tableName + '/:id/exists', 'exists')) for (var j = 0; j < table['foreignKeys'].length; ++j) { let fk = table['foreignKeys'][j] - if(fk['referenced_table_name'] in this.sqlConfig.ignoreTables){ + if (fk['referenced_table_name'] in this.sqlConfig.ignoreTables) { //console.log('ignore table',fk['referenced_table_name']); } else { routes.push(this.prepareRoute(internal, 'get', apiPrefix, fk['referenced_table_name'] + '/:id/' + fk['table_name'], 'relational')) diff --git a/package.json b/package.json index 656caf11b2..bd518fef5a 100644 --- a/package.json +++ b/package.json @@ -1,6 +1,6 @@ { "name": "xmysql", - "version": "0.2.6", + "version": "0.2.7", "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 02bcfcdf23..0a351c6130 100644 --- a/tests/tests.js +++ b/tests/tests.js @@ -17,17 +17,17 @@ var api = {} var mysqlPool = {} -args['host'] = 'localhost' -args['user'] = 'root' -args['password'] = '' -args['database'] = 'classicmodels' - //desribe group of tests done describe('xmysql : tests', function () { before(function (done) { + args['host'] = 'localhost' + args['user'] = 'root' + args['password'] = '' + args['database'] = 'classicmodels' + cmdargs.handle(args) mysqlPool = mysql.createPool(args)