From 8ca85a98a5161b7655d7e72e2faeda653ce64341 Mon Sep 17 00:00:00 2001 From: oof1lab Date: Tue, 31 Oct 2017 16:51:36 +0000 Subject: [PATCH] feature addition: make file upload and download - available only in local --- README.md | 18 +++++++++++------- lib/util/cmd.helper.js | 4 ++-- lib/xapi.js | 18 +++++++++++------- lib/xsql.js | 7 ++++++- 4 files changed, 30 insertions(+), 17 deletions(-) diff --git a/README.md b/README.md index abd863c1f1..e32e73c949 100644 --- a/README.md +++ b/README.md @@ -170,6 +170,14 @@ eg: SELECT country,city,count(*) FROM offices GROUP BY country,city ORDER BY cit eg: SELECT country,city,count(*) FROM offices GROUP BY country,city ORDER BY city ASC, country DESC +## Relational Tables +xmysql identifies foreign key relations automatically and provides GET api. +``` +/api/customers/103/payments +``` +eg: Customers is parent table and payments is child table. API invocation will result in all payments with customer 103. + + ## Run dynamic queries Dynamic queries on a database can be run by POST method to URL localhost:3000/dynamic @@ -188,12 +196,6 @@ POST /dynamic "params": ["customers"] } ``` -## Relational Tables -xmysql identifies foreign key relations automatically and provides GET api. -``` -/api/customers/103/payments -``` -eg: Customers is parent table and payments is child table. API invocation will result in all payments with customer 103. ## Upload single file @@ -206,6 +208,7 @@ returns uploaded file name else 'upload failed' (Note: POSTMAN has issues with file uploading hence examples with curl) + ## Upload multiple files ``` POST /uploads @@ -219,7 +222,8 @@ returns uploaded file names as string ## Download file http://localhost:3000/download?name=fileName -For upload and download of files -> you can specify storage folder using -s option +> For upload and download of files -> you can specify storage folder using -s option +> Upload and download apis are available only with local mysql server ## When to use ? * You need just REST APIs without much hassle for (ANY) MySql database. diff --git a/lib/util/cmd.helper.js b/lib/util/cmd.helper.js index 605411008d..e643c52359 100644 --- a/lib/util/cmd.helper.js +++ b/lib/util/cmd.helper.js @@ -16,8 +16,8 @@ program .option('-d, --database ', 'database schema name') .option('-u, --user ', 'username of database / root by default') .option('-p, --password ', 'password of database / empty by default') - .option('-n, --portNumber ', 'port number : 3000 by default') - .option('-s, --storageFolder ', 'storage folder / current working dir by default') + .option('-n, --portNumber ', 'port number / 3000 by default') + .option('-s, --storageFolder ', 'storage folder / current working dir by default / available only with local') .parse(process.argv) diff --git a/lib/xapi.js b/lib/xapi.js index 6ee7c94a9e..b9e3fc93e9 100644 --- a/lib/xapi.js +++ b/lib/xapi.js @@ -9,7 +9,7 @@ class Xapi { constructor(args, mysqlPool, app) { - this.sqlConfig = args; + this.config = args; this.mysql = new Xsql(args, mysqlPool) this.app = app; @@ -174,17 +174,21 @@ class Xapi { /**************** END : setup routes for each table ****************/ - if (this.sqlConfig.dynamic === 1) { + if (this.config.dynamic === 1) { + this.app.route('/dynamic*') .post(this.asyncMiddleware(this.runQuery.bind(this))); + + /**************** START : multer routes ****************/ + 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 ****************/ + + } - /**************** START : multer routes ****************/ - 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 ****************/ } diff --git a/lib/xsql.js b/lib/xsql.js index 05cf8b6d92..b99eedc8fe 100644 --- a/lib/xsql.js +++ b/lib/xsql.js @@ -419,8 +419,13 @@ class Xsql { r.push(apiPrefix + "tables") - if (this.sqlConfig.dynamic) + if (this.sqlConfig.dynamic) { r.push(apiPrefix + "dynamic") + r.push("/upload") + r.push("/uploads") + r.push("/download") + } + return r;