多维表格
You can not select more than 25 topics Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.

509 lines
14 KiB

6 years ago
"use strict";
7 years ago
6 years ago
var Xsql = require("./xsql.js");
var Xctrl = require("./xctrl.js");
var multer = require("multer");
const path = require("path");
7 years ago
6 years ago
const v8 = require("v8"),
os = require("os");
7 years ago
//define class
class Xapi {
constructor(args, mysqlPool, app) {
this.config = args;
6 years ago
this.mysql = new Xsql(args, mysqlPool);
7 years ago
this.app = app;
this.ctrls = [];
7 years ago
/**************** START : multer ****************/
this.storage = multer.diskStorage({
6 years ago
destination: function(req, file, cb) {
cb(null, process.cwd());
},
6 years ago
filename: function(req, file, cb) {
console.log(file);
6 years ago
cb(null, Date.now() + "-" + file.originalname);
}
6 years ago
});
6 years ago
this.upload = multer({ storage: this.storage });
/**************** END : multer ****************/
7 years ago
}
init(cbk) {
this.mysql.init((err, results) => {
6 years ago
this.app.use(this.urlMiddleware.bind(this));
let stat = this.setupRoutes();
this.app.use(this.errorMiddleware.bind(this));
cbk(err, stat);
});
7 years ago
}
urlMiddleware(req, res, next) {
// get only request url from originalUrl
6 years ago
let justUrl = req.originalUrl.split("?")[0];
let pathSplit = [];
// split by apiPrefix
6 years ago
let apiSuffix = justUrl.split(this.config.apiPrefix);
if (apiSuffix.length === 2) {
// split by /
6 years ago
pathSplit = apiSuffix[1].split("/");
if (pathSplit.length) {
if (pathSplit.length >= 3) {
// handle for relational routes
6 years ago
req.app.locals._parentTable = pathSplit[0];
req.app.locals._childTable = pathSplit[2];
} else {
// handles rest of routes
6 years ago
req.app.locals._tableName = pathSplit[0];
}
}
}
next();
}
7 years ago
errorMiddleware(err, req, res, next) {
6 years ago
if (err && err.code) res.status(400).json({ error: err });
else if (err && err.message)
6 years ago
res.status(500).json({ error: "Internal server error : " + err.message });
else res.status(500).json({ error: "Internal server error : " + err });
7 years ago
next(err);
}
asyncMiddleware(fn) {
return (req, res, next) => {
6 years ago
Promise.resolve(fn(req, res, next)).catch(err => {
next(err);
});
};
7 years ago
}
root(req, res) {
let routes = [];
6 years ago
routes = this.mysql.getSchemaRoutes(
false,
req.protocol + "://" + req.get("host") + this.config.apiPrefix
);
routes = routes.concat(
this.mysql.globalRoutesPrint(
req.protocol + "://" + req.get("host") + this.config.apiPrefix
)
);
res.json(routes);
7 years ago
}
setupRoutes() {
let stat = {}
stat.tables = 0
stat.apis = 0
stat.routines = 0
// console.log('this.config while setting up routes', this.config);
// show routes for database schema
6 years ago
this.app.get("/", this.asyncMiddleware(this.root.bind(this)));
// show all resouces
6 years ago
this.app
.route(this.config.apiPrefix + "tables")
7 years ago
.get(this.asyncMiddleware(this.tables.bind(this)));
6 years ago
this.app
.route(this.config.apiPrefix + "xjoin")
.get(this.asyncMiddleware(this.xjoin.bind(this)));
stat.apis += 3;
/**************** START : setup routes for each table ****************/
let resources = [];
resources = this.mysql.getSchemaRoutes(true, this.config.apiPrefix);
6 years ago
stat.tables += resources.length;
// iterate over each resource
for (var j = 0; j < resources.length; ++j) {
6 years ago
let resourceCtrl = new Xctrl(this.app, this.mysql);
this.ctrls.push(resourceCtrl);
6 years ago
let routes = resources[j]["routes"];
7 years ago
6 years ago
stat.apis += resources[j]["routes"].length;
// iterate over each routes in resource and map function
for (var i = 0; i < routes.length; ++i) {
6 years ago
switch (routes[i]["routeType"]) {
case "list":
this.app
.route(routes[i]["routeUrl"])
.get(this.asyncMiddleware(resourceCtrl.list.bind(resourceCtrl)));
break;
6 years ago
case "findOne":
this.app
.route(routes[i]["routeUrl"])
.get(
this.asyncMiddleware(resourceCtrl.findOne.bind(resourceCtrl))
);
break;
6 years ago
case "create":
if (!this.config.readOnly)
6 years ago
this.app
.route(routes[i]["routeUrl"])
.post(
this.asyncMiddleware(resourceCtrl.create.bind(resourceCtrl))
);
break;
6 years ago
case "read":
this.app
.route(routes[i]["routeUrl"])
.get(this.asyncMiddleware(resourceCtrl.read.bind(resourceCtrl)));
break;
6 years ago
case "bulkInsert":
if (!this.config.readOnly) {
6 years ago
this.app
.route(routes[i]["routeUrl"])
.post(
this.asyncMiddleware(
resourceCtrl.bulkInsert.bind(resourceCtrl)
)
);
}
break;
6 years ago
case "bulkRead":
if (!this.config.readOnly) {
this.app
.route(routes[i]["routeUrl"])
.get(
this.asyncMiddleware(resourceCtrl.bulkRead.bind(resourceCtrl))
);
} else {
stat.apis--;
}
break;
6 years ago
case "bulkDelete":
if (!this.config.readOnly) {
this.app
.route(routes[i]["routeUrl"])
.delete(
this.asyncMiddleware(
resourceCtrl.bulkDelete.bind(resourceCtrl)
)
);
} else {
stat.apis--;
}
break;
6 years ago
case "patch":
if (!this.config.readOnly) {
this.app
.route(routes[i]["routeUrl"])
.patch(
this.asyncMiddleware(resourceCtrl.patch.bind(resourceCtrl))
);
} else {
stat.apis--;
}
break;
6 years ago
case "update":
if (!this.config.readOnly) {
this.app
.route(routes[i]["routeUrl"])
.put(
this.asyncMiddleware(resourceCtrl.update.bind(resourceCtrl))
);
} else {
stat.apis--;
}
break;
6 years ago
case "delete":
if (!this.config.readOnly) {
this.app
.route(routes[i]["routeUrl"])
.delete(
this.asyncMiddleware(resourceCtrl.delete.bind(resourceCtrl))
);
} else {
stat.apis--;
}
break;
6 years ago
case "exists":
this.app
.route(routes[i]["routeUrl"])
.get(
this.asyncMiddleware(resourceCtrl.exists.bind(resourceCtrl))
);
break;
6 years ago
case "count":
this.app
.route(routes[i]["routeUrl"])
.get(this.asyncMiddleware(resourceCtrl.count.bind(resourceCtrl)));
break;
6 years ago
case "distinct":
this.app
.route(routes[i]["routeUrl"])
.get(
this.asyncMiddleware(resourceCtrl.distinct.bind(resourceCtrl))
);
break;
6 years ago
case "describe":
this.app
.route(routes[i]["routeUrl"])
.get(this.asyncMiddleware(this.tableDescribe.bind(this)));
break;
6 years ago
case "relational":
this.app
.route(routes[i]["routeUrl"])
.get(
this.asyncMiddleware(resourceCtrl.nestedList.bind(resourceCtrl))
);
break;
6 years ago
case "groupby":
this.app
.route(routes[i]["routeUrl"])
.get(
this.asyncMiddleware(resourceCtrl.groupBy.bind(resourceCtrl))
);
break;
6 years ago
case "ugroupby":
this.app
.route(routes[i]["routeUrl"])
.get(
this.asyncMiddleware(resourceCtrl.ugroupby.bind(resourceCtrl))
);
break;
6 years ago
case "chart":
this.app
.route(routes[i]["routeUrl"])
.get(this.asyncMiddleware(resourceCtrl.chart.bind(resourceCtrl)));
break;
6 years ago
case "autoChart":
this.app
.route(routes[i]["routeUrl"])
.get(
this.asyncMiddleware(resourceCtrl.autoChart.bind(resourceCtrl))
);
break;
6 years ago
case "aggregate":
this.app
.route(routes[i]["routeUrl"])
.get(
this.asyncMiddleware(resourceCtrl.aggregate.bind(resourceCtrl))
);
break;
}
}
}
/**************** END : setup routes for each table ****************/
7 years ago
if (this.config.dynamic === 1 && !this.config.readOnly) {
6 years ago
this.app
.route("/dynamic*")
7 years ago
.post(this.asyncMiddleware(this.runQuery.bind(this)));
/**************** START : multer routes ****************/
6 years ago
this.app.post(
"/upload",
this.upload.single("file"),
this.uploadFile.bind(this)
);
this.app.post(
"/uploads",
this.upload.array("files", 10),
this.uploadFiles.bind(this)
);
this.app.get("/download", this.downloadFile.bind(this));
/**************** END : multer routes ****************/
stat.apis += 4;
}
/**************** START : health and version ****************/
6 years ago
this.app.get("/_health", this.asyncMiddleware(this.health.bind(this)));
this.app.get("/_version", this.asyncMiddleware(this.version.bind(this)));
stat.apis += 2;
/**************** END : health and version ****************/
6 years ago
/**************** START : call stored procedures ****************/
this.app.get('/_proc', this.asyncMiddleware(this.proc.bind(this)))
stat.apis += 1
const procResources = this.mysql.getProcList(true, this.config.apiPrefix)
this.app.post('/_proc/:proc', this.asyncMiddleware(this.callProc.bind(this)))
stat.routines += procResources.length
stat.apis += procResources.length
6 years ago
/**************** END : call stored procedures ****************/
6 years ago
let statStr = ' Generated: ' + stat.apis + ' REST APIs for ' + stat.tables + ' tables '
console.log(' - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - ');
console.log(' ');
console.log(' Database : %s', this.config.database);
console.log(' Number of Tables : %s', stat.tables);
console.log(' Number of Routines : %s', stat.routines);
console.log(' ');
console.log(' REST APIs Generated : %s'.green.bold, stat.apis);
console.log(' ');
return stat
6 years ago
}
async xjoin(req, res) {
6 years ago
let obj = {};
6 years ago
obj.query = "";
obj.params = [];
6 years ago
this.mysql.prepareJoinQuery(req, res, obj);
//console.log(obj);
if (obj.query.length) {
6 years ago
let results = await this.mysql.exec(obj.query, obj.params);
res.status(200).json(results);
} else {
6 years ago
res.status(400).json({ err: "Invalid Xjoin request" });
}
7 years ago
}
async tableDescribe(req, res) {
6 years ago
let query = "describe ??";
let params = [req.app.locals._tableName];
7 years ago
let results = await this.mysql.exec(query, params);
res.status(200).json(results);
}
async tables(req, res) {
6 years ago
let query =
"SELECT table_name AS resource FROM information_schema.tables WHERE table_schema = ? ";
let params = [this.config.database];
if (Object.keys(this.config.ignoreTables).length > 0) {
6 years ago
query += "and table_name not in (?)";
params.push(Object.keys(this.config.ignoreTables));
}
7 years ago
6 years ago
let results = await this.mysql.exec(query, params);
7 years ago
6 years ago
res.status(200).json(results);
7 years ago
}
async runQuery(req, res) {
let query = req.body.query;
let params = req.body.params;
let results = await this.mysql.exec(query, params);
res.status(200).json(results);
}
/**************** START : files related ****************/
downloadFile(req, res) {
let file = path.join(process.cwd(), req.query.name);
res.download(file);
}
uploadFile(req, res) {
if (req.file) {
console.log(req.file.path);
res.end(req.file.path);
} else {
6 years ago
res.end("upload failed");
}
}
uploadFiles(req, res) {
if (!req.files || req.files.length === 0) {
6 years ago
res.end("upload failed");
} else {
let files = [];
for (let i = 0; i < req.files.length; ++i) {
files.push(req.files[i].path);
}
res.end(files.toString());
}
}
/**************** END : files related ****************/
/**************** START : health and version ****************/
async getMysqlUptime() {
6 years ago
let v = await this.mysql.exec("SHOW GLOBAL STATUS LIKE 'Uptime';", []);
return v[0]["Value"];
}
async getMysqlHealth() {
6 years ago
let v = await this.mysql.exec("select version() as version", []);
return v[0]["version"];
}
async health(req, res) {
let status = {};
6 years ago
status["process_uptime"] = process.uptime();
status["mysql_uptime"] = await this.getMysqlUptime();
if (Object.keys(req.query).length) {
6 years ago
status["process_memory_usage"] = process.memoryUsage();
status["os_total_memory"] = os.totalmem();
status["os_free_memory"] = os.freemem();
status["os_load_average"] = os.loadavg();
status["v8_heap_statistics"] = v8.getHeapStatistics();
}
res.json(status);
}
async version(req, res) {
let version = {};
6 years ago
version["Xmysql"] = this.app.get("version");
version["mysql"] = await this.getMysqlHealth();
version["node"] = process.versions.node;
res.json(version);
}
/**************** END : health and version ****************/
6 years ago
async proc(req, res) {
6 years ago
let query = 'SELECT routine_name AS resource FROM information_schema.routines WHERE routine_schema = ? ';
6 years ago
let params = [this.config.database];
let results = await this.mysql.exec(query, params)
res.status(200).json(results)
}
async callProc(req, res) {
let query = 'CALL ??(?)'
let params = [req.params.proc, Object.values(req.body)]
let results = await this.mysql.exec(query, params)
res.status(200).json(results)
}
7 years ago
}
//expose class
module.exports = Xapi;