Browse Source

Feature : Support VIEWS npm v0.2.7

pull/13/head
oof1lab 7 years ago
parent
commit
7a2c5b3f62
  1. 5
      README.md
  2. 2
      lib/util/cmd.helper.js
  3. 10
      lib/util/data.helper.js
  4. 27
      lib/xsql.js
  5. 2
      package.json
  6. 10
      tests/tests.js

5
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

2
lib/util/cmd.helper.js

@ -11,7 +11,7 @@ program.on('--help', () => {
})
program
.version('0.2.6')
.version('0.2.7')
.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')

10
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 ' +

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

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

10
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)

Loading…
Cancel
Save