|
|
@ -149,6 +149,21 @@ class Xapi { |
|
|
|
.get(this.asyncMiddleware(this.read.bind(this))); |
|
|
|
.get(this.asyncMiddleware(this.read.bind(this))); |
|
|
|
break; |
|
|
|
break; |
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
case 'bulkInsert': |
|
|
|
|
|
|
|
this.app.route(routes[i]['routeUrl']) |
|
|
|
|
|
|
|
.post(this.asyncMiddleware(this.bulkInsert.bind(this))); |
|
|
|
|
|
|
|
break; |
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
case 'bulkRead': |
|
|
|
|
|
|
|
this.app.route(routes[i]['routeUrl']) |
|
|
|
|
|
|
|
.get(this.asyncMiddleware(this.bulkRead.bind(this))); |
|
|
|
|
|
|
|
break; |
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
case 'bulkDelete': |
|
|
|
|
|
|
|
this.app.route(routes[i]['routeUrl']) |
|
|
|
|
|
|
|
.delete(this.asyncMiddleware(this.bulkDelete.bind(this))); |
|
|
|
|
|
|
|
break; |
|
|
|
|
|
|
|
|
|
|
|
case 'patch': |
|
|
|
case 'patch': |
|
|
|
this.app.route(routes[i]['routeUrl']) |
|
|
|
this.app.route(routes[i]['routeUrl']) |
|
|
|
.patch(this.asyncMiddleware(this.patch.bind(this))); |
|
|
|
.patch(this.asyncMiddleware(this.patch.bind(this))); |
|
|
@ -230,7 +245,7 @@ class Xapi { |
|
|
|
console.log(' - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - '); |
|
|
|
console.log(' - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - '); |
|
|
|
console.log(' '); |
|
|
|
console.log(' '); |
|
|
|
console.log(' Database : %s', this.config.database); |
|
|
|
console.log(' Database : %s', this.config.database); |
|
|
|
console.log(' Number of resources : %s', stat.tables); |
|
|
|
console.log(' Number of Tables : %s', stat.tables); |
|
|
|
console.log(' '); |
|
|
|
console.log(' '); |
|
|
|
console.log(' REST APIs Generated : %s'.green.bold, stat.apis); |
|
|
|
console.log(' REST APIs Generated : %s'.green.bold, stat.apis); |
|
|
|
console.log(' '); |
|
|
|
console.log(' '); |
|
|
@ -256,7 +271,7 @@ class Xapi { |
|
|
|
* @param req |
|
|
|
* @param req |
|
|
|
* @param res |
|
|
|
* @param res |
|
|
|
* @param queryParamsObj : {query, params} |
|
|
|
* @param queryParamsObj : {query, params} |
|
|
|
* @param listType : 0:list, 1:nested, 2:findOne |
|
|
|
* @param listType : 0:list, 1:nested, 2:findOne, 3:bulkRead |
|
|
|
* |
|
|
|
* |
|
|
|
* Updates query, params for query of type listType |
|
|
|
* Updates query, params for query of type listType |
|
|
|
*/ |
|
|
|
*/ |
|
|
@ -297,6 +312,28 @@ class Xapi { |
|
|
|
|
|
|
|
|
|
|
|
this.mysql.getWhereClause(req.query._where, req.app.locals._tableName, queryParamsObj, ' and '); |
|
|
|
this.mysql.getWhereClause(req.query._where, req.app.locals._tableName, queryParamsObj, ' and '); |
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
} else if (listType === 3) { //bulkRead
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
// select * from table where pk in (ids) and whereConditions
|
|
|
|
|
|
|
|
queryParamsObj.params.push(req.app.locals._tableName); |
|
|
|
|
|
|
|
queryParamsObj.query += ' where ?? in '; |
|
|
|
|
|
|
|
queryParamsObj.params.push(this.mysql.getPrimaryKeyName(req.app.locals._tableName)); |
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
queryParamsObj.query += '(' |
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
if (req.query && req.query._ids) { |
|
|
|
|
|
|
|
let ids = req.query._ids.split(',') |
|
|
|
|
|
|
|
for (var i = 0; i < ids.length; ++i) { |
|
|
|
|
|
|
|
if (i) { |
|
|
|
|
|
|
|
queryParamsObj.query += ',' |
|
|
|
|
|
|
|
} |
|
|
|
|
|
|
|
queryParamsObj.query += '?' |
|
|
|
|
|
|
|
queryParamsObj.params.push(ids[i]) |
|
|
|
|
|
|
|
} |
|
|
|
|
|
|
|
} |
|
|
|
|
|
|
|
queryParamsObj.query += ') ' |
|
|
|
|
|
|
|
this.mysql.getWhereClause(req.query._where, req.app.locals._tableName, queryParamsObj, ' and '); |
|
|
|
|
|
|
|
|
|
|
|
} else { |
|
|
|
} else { |
|
|
|
|
|
|
|
|
|
|
|
queryParamsObj.params.push(req.app.locals._tableName); |
|
|
|
queryParamsObj.params.push(req.app.locals._tableName); |
|
|
@ -468,44 +505,6 @@ class Xapi { |
|
|
|
|
|
|
|
|
|
|
|
} |
|
|
|
} |
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
// async update(req, res) {
|
|
|
|
|
|
|
|
//
|
|
|
|
|
|
|
|
// let query = 'UPDATE ?? SET ';
|
|
|
|
|
|
|
|
// let keys = Object.keys(req.body);
|
|
|
|
|
|
|
|
//
|
|
|
|
|
|
|
|
// // SET clause
|
|
|
|
|
|
|
|
// let updateKeys = '';
|
|
|
|
|
|
|
|
// for (let i = 0; i < keys.length; ++i) {
|
|
|
|
|
|
|
|
// updateKeys += keys[i] + ' = ? '
|
|
|
|
|
|
|
|
// if (i !== keys.length - 1)
|
|
|
|
|
|
|
|
// updateKeys += ', '
|
|
|
|
|
|
|
|
// }
|
|
|
|
|
|
|
|
//
|
|
|
|
|
|
|
|
// // where clause
|
|
|
|
|
|
|
|
// query += updateKeys + ' where '
|
|
|
|
|
|
|
|
// let clause = this.mysql.getPrimaryKeyWhereClause(req.app.locals._tableName,
|
|
|
|
|
|
|
|
// req.params.id.split('___'));
|
|
|
|
|
|
|
|
//
|
|
|
|
|
|
|
|
// if (!clause) {
|
|
|
|
|
|
|
|
// return res.status(400).send({
|
|
|
|
|
|
|
|
// error: "Table is made of composite primary keys - all keys were not in input"
|
|
|
|
|
|
|
|
// })
|
|
|
|
|
|
|
|
// }
|
|
|
|
|
|
|
|
//
|
|
|
|
|
|
|
|
// query += clause;
|
|
|
|
|
|
|
|
//
|
|
|
|
|
|
|
|
// // params
|
|
|
|
|
|
|
|
// let params = [];
|
|
|
|
|
|
|
|
// params.push(req.app.locals._tableName);
|
|
|
|
|
|
|
|
// params = params.concat(Object.values(req.body));
|
|
|
|
|
|
|
|
//
|
|
|
|
|
|
|
|
// let results = await this.mysql.exec(query, params);
|
|
|
|
|
|
|
|
// res.status(200).json(results);
|
|
|
|
|
|
|
|
//
|
|
|
|
|
|
|
|
//
|
|
|
|
|
|
|
|
// }
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
async delete(req, res) { |
|
|
|
async delete(req, res) { |
|
|
|
|
|
|
|
|
|
|
|
let query = 'DELETE FROM ?? WHERE '; |
|
|
|
let query = 'DELETE FROM ?? WHERE '; |
|
|
@ -530,6 +529,68 @@ class Xapi { |
|
|
|
|
|
|
|
|
|
|
|
} |
|
|
|
} |
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
async bulkInsert(req, res) { |
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
let queryParamsObj = {} |
|
|
|
|
|
|
|
queryParamsObj.query = '' |
|
|
|
|
|
|
|
queryParamsObj.params = [] |
|
|
|
|
|
|
|
let results = [] |
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
//console.log(req.app.locals._tableName, req.body);
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
this.mysql.getBulkInsertStatement(req.app.locals._tableName, req.body, queryParamsObj) |
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
results = await this.mysql.exec(queryParamsObj.query, queryParamsObj.params); |
|
|
|
|
|
|
|
res.status(200).json(results); |
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
} |
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
async bulkDelete(req, res) { |
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
let query = 'delete from ?? where ?? in '; |
|
|
|
|
|
|
|
let params = []; |
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
params.push(req.app.locals._tableName); |
|
|
|
|
|
|
|
params.push(this.mysql.getPrimaryKeyName(req.app.locals._tableName)); |
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
query += '(' |
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
if (req.query && req.query._ids) { |
|
|
|
|
|
|
|
let ids = req.query._ids.split(',') |
|
|
|
|
|
|
|
for (var i = 0; i < ids.length; ++i) { |
|
|
|
|
|
|
|
if (i) { |
|
|
|
|
|
|
|
query += ',' |
|
|
|
|
|
|
|
} |
|
|
|
|
|
|
|
query += '?' |
|
|
|
|
|
|
|
params.push(ids[i]) |
|
|
|
|
|
|
|
} |
|
|
|
|
|
|
|
} |
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
query += ')' |
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
//console.log(query, params);
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
var results = await this.mysql.exec(query, params); |
|
|
|
|
|
|
|
res.status(200).json(results); |
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
} |
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
async bulkRead(req, res) { |
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
let queryParamsObj = {} |
|
|
|
|
|
|
|
queryParamsObj.query = '' |
|
|
|
|
|
|
|
queryParamsObj.params = [] |
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
this.prepareListQuery(req, res, queryParamsObj, 3); |
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
//console.log(queryParamsObj.query, queryParamsObj.params);
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
let results = await this.mysql.exec(queryParamsObj.query, queryParamsObj.params); |
|
|
|
|
|
|
|
res.status(200).json(results); |
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
} |
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
async count(req, res) { |
|
|
|
async count(req, res) { |
|
|
|
|
|
|
|
|
|
|
|
let query = 'select count(1) as no_of_rows from ??'; |
|
|
|
let query = 'select count(1) as no_of_rows from ??'; |
|
|
|