Browse Source

feature addition: make file upload and download - available only in local

pull/8/head
oof1lab 7 years ago
parent
commit
8ca85a98a5
  1. 18
      README.md
  2. 4
      lib/util/cmd.helper.js
  3. 18
      lib/xapi.js
  4. 7
      lib/xsql.js

18
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 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 ## Run dynamic queries
Dynamic queries on a database can be run by POST method to URL localhost:3000/dynamic Dynamic queries on a database can be run by POST method to URL localhost:3000/dynamic
@ -188,12 +196,6 @@ POST /dynamic
"params": ["customers"] "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 ## 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) (Note: POSTMAN has issues with file uploading hence examples with curl)
## Upload multiple files ## Upload multiple files
``` ```
POST /uploads POST /uploads
@ -219,7 +222,8 @@ returns uploaded file names as string
## Download file ## Download file
http://localhost:3000/download?name=fileName 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 ? ## When to use ?
* You need just REST APIs without much hassle for (ANY) MySql database. * You need just REST APIs without much hassle for (ANY) MySql database.

4
lib/util/cmd.helper.js

@ -16,8 +16,8 @@ program
.option('-d, --database <n>', 'database schema name') .option('-d, --database <n>', 'database schema name')
.option('-u, --user <n>', 'username of database / root by default') .option('-u, --user <n>', 'username of database / root by default')
.option('-p, --password <n>', 'password of database / empty by default') .option('-p, --password <n>', 'password of database / empty by default')
.option('-n, --portNumber <n>', 'port number : 3000 by default') .option('-n, --portNumber <n>', 'port number / 3000 by default')
.option('-s, --storageFolder <n>', 'storage folder / current working dir by default') .option('-s, --storageFolder <n>', 'storage folder / current working dir by default / available only with local')
.parse(process.argv) .parse(process.argv)

18
lib/xapi.js

@ -9,7 +9,7 @@ class Xapi {
constructor(args, mysqlPool, app) { constructor(args, mysqlPool, app) {
this.sqlConfig = args; this.config = args;
this.mysql = new Xsql(args, mysqlPool) this.mysql = new Xsql(args, mysqlPool)
this.app = app; this.app = app;
@ -174,17 +174,21 @@ class Xapi {
/**************** END : setup routes for each table ****************/ /**************** END : setup routes for each table ****************/
if (this.sqlConfig.dynamic === 1) { if (this.config.dynamic === 1) {
this.app.route('/dynamic*') this.app.route('/dynamic*')
.post(this.asyncMiddleware(this.runQuery.bind(this))); .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 ****************/
} }

7
lib/xsql.js

@ -419,8 +419,13 @@ class Xsql {
r.push(apiPrefix + "tables") r.push(apiPrefix + "tables")
if (this.sqlConfig.dynamic) if (this.sqlConfig.dynamic) {
r.push(apiPrefix + "dynamic") r.push(apiPrefix + "dynamic")
r.push("/upload")
r.push("/uploads")
r.push("/download")
}
return r; return r;

Loading…
Cancel
Save