Skip to content

Commit 4698c5b

Browse files
Merge pull request #118 from terminusdb/fixWQPost
fixed WOQL.post function
2 parents e2547de + c2b51ba commit 4698c5b

File tree

5 files changed

+31
-8
lines changed

5 files changed

+31
-8
lines changed

docs/api/woql.js.md

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -394,7 +394,7 @@ file("/path/to/file", {type: 'turtle'} )
394394
```
395395

396396
### post
397-
#### WOQL.post(url, [formatObj]) ⇒ <code>WOQLQuery</code>
397+
#### WOQL.post(url, [formatObj], [source]) ⇒ <code>WOQLQuery</code>
398398
Identifies a resource as a local path on the client, to be sent to the server through a
399399
HTTP POST request, with the format defined through the options
400400

@@ -404,6 +404,7 @@ HTTP POST request, with the format defined through the options
404404
| --- | --- | --- |
405405
| url | <code>string</code> | The Path on the server at which the file resource can be accessed |
406406
| [formatObj] | <code>typedef.DataFormatObj</code> | imput options, optional |
407+
| [source] | <code>string</code> | It defines the source of the file, it can be 'url','post' |
407408

408409
**Example**
409410
```js

lib/query/woqlQuery.js

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -448,11 +448,11 @@ WOQLQuery.prototype.remote = function(uri, opts) {
448448
return this
449449
}
450450

451-
WOQLQuery.prototype.post = function(fpath, opts) {
451+
WOQLQuery.prototype.post = function(fpath, opts, source = 'post') {
452452
//if (fpath && fpath == 'args') return ['file', 'format']
453453
if (this.cursor['@type']) this.wrapCursorWithAnd()
454-
this.cursor['@type'] = 'PostResource'
455-
this.cursor['source'] = {'@type': 'Source', 'file': fpath}
454+
this.cursor['@type'] = 'QueryResource'
455+
this.cursor['source'] = {'@type': 'Source', [source]: fpath}
456456
this.cursor['format'] = "csv" // hard coded for now
457457
this.cursor['options'] = opts
458458
if (typeof opts != 'undefined')

lib/woql.js

Lines changed: 3 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -368,12 +368,13 @@ WOQL.file = function(url, formatObj) {
368368
* HTTP POST request, with the format defined through the options
369369
* @param {string} url - The Path on the server at which the file resource can be accessed
370370
* @param {typedef.DataFormatObj} [formatObj] - imput options, optional
371+
* @param {string} [source] - It defines the source of the file, it can be 'url','post'
371372
* @returns {WOQLQuery} A WOQLQuery which contains the Post resource identifier
372373
* @example
373374
* post("/.../.../", {type:'csv'})
374375
*/
375-
WOQL.post = function(url, formatObj) {
376-
return new WOQLQuery().post(url, formatObj)
376+
WOQL.post = function(url, formatObj, source) {
377+
return new WOQLQuery().post(url, formatObj, source)
377378
}
378379

379380
/**

lib/woqlClient.js

Lines changed: 20 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -7,6 +7,8 @@ const ConnectionConfig = require('./connectionConfig')
77
const WOQL = require('./woql')
88
const WOQLQuery = require('./query/woqlBuilder')
99
const { default: axios } = require('axios')
10+
const FormData = require('form-data');
11+
const fs = require('fs');
1012

1113
/**
1214
* @license Apache Version 2
@@ -555,13 +557,30 @@ WOQLClient.prototype.query = function(woql, commitMsg, allWitnesses, lastDataVer
555557
if (woql && woql.json && (!woql.containsUpdate() || commitMsg)) {
556558
let doql = woql.containsUpdate() ? this.generateCommitInfo(commitMsg) : {}
557559
doql.query = woql.json()
560+
561+
let postBody;
562+
563+
if(doql.query.resource.source.post) {
564+
const fileName = doql.query.resource.source.post.split('/').pop();
565+
566+
const formData = new FormData();
567+
formData.append('file', fs.createReadStream(doql.query.resource.source.post));
568+
doql.query.resource.source.post = fileName;
569+
formData.append('payload', Buffer.from(JSON.stringify(doql)), { filename: 'body.json', contentType: 'application/json' });
570+
this.customHeaders(formData.getHeaders());
571+
572+
postBody = formData;
573+
} else {
574+
postBody = doql;
575+
}
576+
558577
if (allWitnesses) doql.all_witnesses = true
559578

560579
if(typeof lastDataVersion === 'string' && lastDataVersion !== '') {
561580
this.customHeaders({'TerminusDB-Data-Version': lastDataVersion})
562581
}
563582

564-
return this.dispatch(CONST.WOQL_QUERY, this.connectionConfig.queryURL(), doql, getDataVersion)
583+
return this.dispatch(CONST.WOQL_QUERY, this.connectionConfig.queryURL(), postBody, getDataVersion)
565584
}
566585
let errmsg = `WOQL query parameter error`
567586
if (woql && woql.json && woql.containsUpdate() && !commitMsg) {

package.json

Lines changed: 3 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -24,6 +24,7 @@
2424
"ajv":">=6.12.3",
2525
"follow-redirects":">=1.14.8",
2626
"axios": ">=0.25.0",
27+
"form-data": "^4.0.0",
2728
"babel-polyfill": "^6.26.0",
2829
"node-forge": ">=1.0.0",
2930
"underscore": ">=1.13.2"
@@ -93,6 +94,7 @@
9394
"net": false,
9495
"path": false,
9596
"stream": false,
96-
"tls": false
97+
"tls": false,
98+
"fs": false
9799
}
98100
}

0 commit comments

Comments
 (0)