Skip to content

Commit 7cc4964

Browse files
Merge branch 'main' of https://github.com/terminusdb/terminusdb-client-js into fixLinting
Signed-off-by: NeelParihar <[email protected]>
2 parents 0fe4704 + 4698c5b commit 7cc4964

File tree

5 files changed

+189
-163
lines changed

5 files changed

+189
-163
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: 140 additions & 138 deletions
Original file line numberDiff line numberDiff line change
@@ -391,146 +391,148 @@ WOQLQuery.prototype.put = function (asvars, query, query_resource) {
391391
* @param {...(array|string)} varList variable number of arguments
392392
* @returns WOQLQuery
393393
*/
394-
WOQLQuery.prototype.as = function (...varList) {
395-
// if (varList && varList[0] === 'args')
396-
// return [['indexed_as_var', 'named_as_var']]
397-
if (!Array.isArray(this.query)) this.query = [];
398-
if (Array.isArray(varList[0])) {
399-
if (!varList[1]) {
400-
// indexed as vars
401-
for (var i = 0; i < varList[0].length; i++) {
402-
const iasv = this.asv(i, varList[0][i]);
403-
this.query.push(iasv);
404-
}
405-
} else {
406-
// eslint-disable-next-line no-redeclare
407-
for (var i = 0; i < varList.length; i++) {
408-
const onemap = varList[i];
409-
if (Array.isArray(onemap) && onemap.length >= 2) {
410-
const type = onemap && onemap.length > 2 ? onemap[2] : false;
411-
const oasv = this.asv(onemap[0], onemap[1], type);
412-
this.query.push(oasv);
394+
WOQLQuery.prototype.as = function(...varList) {
395+
//if (varList && varList[0] == 'args')
396+
//return [['indexed_as_var', 'named_as_var']]
397+
if (!Array.isArray(this.query)) this.query = []
398+
if (Array.isArray(varList[0])) {
399+
if (!varList[1]) {
400+
//indexed as vars
401+
for (var i = 0; i < varList[0].length; i++) {
402+
let iasv = this.asv(i, varList[0][i])
403+
this.query.push(iasv)
404+
}
405+
} else {
406+
for (var i = 0; i < varList.length; i++) {
407+
let onemap = varList[i]
408+
if (Array.isArray(onemap) && onemap.length >= 2) {
409+
let type = onemap && onemap.length > 2 ? onemap[2] : false
410+
let oasv = this.asv(onemap[0], onemap[1], type)
411+
this.query.push(oasv)
412+
}
413+
}
413414
}
414-
}
415-
}
416-
} else if (typeof varList[0] === 'number' || typeof varList[0] === 'string') {
417-
if (varList[2] && typeof varList[2] === 'string') {
418-
var oasv = this.asv(varList[0], varList[1], varList[2]);
419-
} else if (varList[1] && typeof varList[1] === 'string') {
420-
if (varList[1].substring(0, 4) === 'xsd:' || varList[1].substring(0, 4) === 'xdd:') {
421-
var oasv = this.asv(this.query.length, varList[0], varList[1]);
422-
} else {
423-
var oasv = this.asv(varList[0], varList[1]);
424-
}
425-
} else {
426-
var oasv = this.asv(this.query.length, varList[0]);
415+
} else if (typeof varList[0] == 'number' || typeof varList[0] == 'string') {
416+
if (varList[2] && typeof varList[2] == 'string') {
417+
var oasv = this.asv(varList[0], varList[1], varList[2])
418+
} else if (varList[1] && typeof varList[1] == 'string') {
419+
if (varList[1].substring(0, 4) == 'xsd:' || varList[1].substring(0, 4) == 'xdd:') {
420+
var oasv = this.asv(this.query.length, varList[0], varList[1])
421+
} else {
422+
var oasv = this.asv(varList[0], varList[1])
423+
}
424+
} else {
425+
var oasv = this.asv(this.query.length, varList[0])
426+
}
427+
this.query.push(oasv)
428+
} else if (typeof varList[0] == 'object') {
429+
//check if it is an class object with an json method
430+
this.query.push(varList[0].json ? varList[0].json() : varList[0])
427431
}
428-
this.query.push(oasv);
429-
} else if (typeof varList[0] === 'object') {
430-
// check if it is an class object with an json method
431-
this.query.push(varList[0].json ? varList[0].json() : varList[0]);
432-
}
433-
return this;
434-
};
435-
436-
WOQLQuery.prototype.file = function (fpath, opts) {
437-
// if (fpath && fpath === 'args') return ['file', 'format']
438-
if (this.cursor['@type']) this.wrapCursorWithAnd();
439-
this.cursor['@type'] = 'QueryResource';
440-
this.cursor.source = { '@type': 'Source', file: fpath };
441-
this.cursor.format = 'csv'; // hard coded for now
442-
if (typeof opts !== 'undefined') this.cursor.options = opts;
443-
return this;
444-
};
445-
446-
WOQLQuery.prototype.remote = function (uri, opts) {
447-
// if (uri && uri === 'args') return ['remote_uri', 'format']
448-
if (this.cursor['@type']) this.wrapCursorWithAnd();
449-
this.cursor['@type'] = 'QueryResource';
450-
this.cursor.source = { '@type': 'Source', url: uri };
451-
this.cursor.format = 'csv'; // hard coded for now
452-
if (typeof opts !== 'undefined') this.cursor.options = opts;
453-
return this;
454-
};
455-
456-
WOQLQuery.prototype.post = function (fpath, opts) {
457-
// if (fpath && fpath === 'args') return ['file', 'format']
458-
if (this.cursor['@type']) this.wrapCursorWithAnd();
459-
this.cursor['@type'] = 'PostResource';
460-
this.cursor.source = { '@type': 'Source', file: fpath };
461-
this.cursor.format = 'csv'; // hard coded for now
462-
this.cursor.options = opts;
463-
if (typeof opts !== 'undefined') this.cursor.options = opts;
464-
return this;
465-
};
466-
467-
WOQLQuery.prototype.delete_triple = function (Subject, Predicate, Object_or_Literal) {
468-
if (this.cursor['@type']) this.wrapCursorWithAnd();
469-
const args = this.triple(Subject, Predicate, Object_or_Literal);
470-
// if (Subject && Subject === 'args') return args
471-
this.cursor['@type'] = 'DeleteTriple';
472-
return this.updated();
473-
};
474-
475-
WOQLQuery.prototype.add_triple = function (Subject, Predicate, Object_or_Literal) {
476-
if (this.cursor['@type']) this.wrapCursorWithAnd();
477-
const args = this.triple(Subject, Predicate, Object_or_Literal);
478-
// if (Subject && Subject === 'args') return args
479-
this.cursor['@type'] = 'AddTriple';
480-
return this.updated();
481-
};
482-
483-
WOQLQuery.prototype.delete_quad = function (a, b, c, g) {
484-
if (this.cursor['@type']) this.wrapCursorWithAnd();
485-
const args = this.triple(a, b, c);
486-
// if (a && a === 'args') return args.concat(['graph'])
487-
if (!g) {
488-
return this.parameterError(
489-
'Delete Quad takes four parameters, the last should be a graph id',
490-
);
491-
}
492-
this.cursor['@type'] = 'DeleteTriple';
493-
this.cursor.graph = this.cleanGraph(g);
494-
return this.updated();
495-
};
496-
497-
WOQLQuery.prototype.add_quad = function (a, b, c, g) {
498-
if (this.cursor['@type']) this.wrapCursorWithAnd();
499-
const args = this.triple(a, b, c);
500-
// if (a && a === 'args') return args.concat(['graph'])
501-
if (!g) return this.parameterError('Add Quad takes four parameters, the last should be a graph id');
502-
this.cursor['@type'] = 'AddTriple';
503-
this.cursor.graph = this.cleanGraph(g);
504-
return this.updated();
505-
};
506-
507-
WOQLQuery.prototype.trim = function (untrimmed, trimmed) {
508-
// if (untrimmed && untrimmed === 'args')
509-
// return ['untrimmed', 'trimmed']
510-
if (this.cursor['@type']) this.wrapCursorWithAnd();
511-
this.cursor['@type'] = 'Trim';
512-
this.cursor.untrimmed = this.cleanDataValue(untrimmed);
513-
this.cursor.trimmed = this.cleanDataValue(trimmed);
514-
return this;
515-
};
516-
517-
WOQLQuery.prototype.eval = function (arith, res) {
518-
// if (arith && arith === 'args')
519-
// return ['expression', 'result']
520-
if (this.cursor['@type']) this.wrapCursorWithAnd();
521-
this.cursor['@type'] = 'Eval';
522-
this.cursor.expression = arith.json ? arith.json() : arith;
523-
this.cursor.result = this.cleanArithmeticValue(res);
524-
return this;
525-
};
526-
527-
WOQLQuery.prototype.plus = function (...args) {
528-
// if (args && args[0] === 'args') return ['first', 'second']
529-
if (this.cursor['@type']) this.wrapCursorWithAnd();
530-
this.cursor['@type'] = 'Plus';
531-
this.cursor.left = this.arop(args.shift());
532-
if (args.length > 1) {
533-
this.cursor.right = this.jobj(new WOQLQuery().plus(...args));
432+
return this
433+
}
434+
435+
WOQLQuery.prototype.file = function(fpath, opts) {
436+
//if (fpath && fpath == 'args') return ['file', 'format']
437+
if (this.cursor['@type']) this.wrapCursorWithAnd()
438+
this.cursor['@type'] = 'QueryResource'
439+
this.cursor['source'] = {'@type': 'Source', 'file': fpath}
440+
this.cursor['format'] = "csv" // hard coded for now
441+
if (typeof opts != 'undefined')
442+
this.cursor['options'] = opts
443+
return this
444+
}
445+
446+
WOQLQuery.prototype.remote = function(uri, opts) {
447+
//if (uri && uri == 'args') return ['remote_uri', 'format']
448+
if (this.cursor['@type']) this.wrapCursorWithAnd()
449+
this.cursor['@type'] = 'QueryResource'
450+
this.cursor['source'] = {'@type': 'Source', 'url': uri}
451+
this.cursor['format'] = "csv" // hard coded for now
452+
if (typeof opts != 'undefined')
453+
this.cursor['options'] = opts
454+
return this
455+
}
456+
457+
WOQLQuery.prototype.post = function(fpath, opts, source = 'post') {
458+
//if (fpath && fpath == 'args') return ['file', 'format']
459+
if (this.cursor['@type']) this.wrapCursorWithAnd()
460+
this.cursor['@type'] = 'QueryResource'
461+
this.cursor['source'] = {'@type': 'Source', [source]: fpath}
462+
this.cursor['format'] = "csv" // hard coded for now
463+
this.cursor['options'] = opts
464+
if (typeof opts != 'undefined')
465+
this.cursor['options'] = opts
466+
return this
467+
}
468+
469+
WOQLQuery.prototype.delete_triple = function(Subject, Predicate, Object_or_Literal) {
470+
if (this.cursor['@type']) this.wrapCursorWithAnd()
471+
let args = this.triple(Subject, Predicate, Object_or_Literal)
472+
//if (Subject && Subject == 'args') return args
473+
this.cursor['@type'] = 'DeleteTriple'
474+
return this.updated()
475+
}
476+
477+
WOQLQuery.prototype.add_triple = function(Subject, Predicate, Object_or_Literal) {
478+
if (this.cursor['@type']) this.wrapCursorWithAnd()
479+
let args = this.triple(Subject, Predicate, Object_or_Literal)
480+
//if (Subject && Subject == 'args') return args
481+
this.cursor['@type'] = 'AddTriple'
482+
return this.updated()
483+
}
484+
485+
WOQLQuery.prototype.delete_quad = function(a, b, c, g) {
486+
if (this.cursor['@type']) this.wrapCursorWithAnd()
487+
let args = this.triple(a, b, c)
488+
//if (a && a == 'args') return args.concat(['graph'])
489+
if (!g)
490+
return this.parameterError(
491+
'Delete Quad takes four parameters, the last should be a graph id',
492+
)
493+
this.cursor['@type'] = 'DeleteTriple'
494+
this.cursor['graph'] = this.cleanGraph(g)
495+
return this.updated()
496+
}
497+
498+
WOQLQuery.prototype.add_quad = function(a, b, c, g) {
499+
if (this.cursor['@type']) this.wrapCursorWithAnd()
500+
let args = this.triple(a, b, c)
501+
//if (a && a == 'args') return args.concat(['graph'])
502+
if (!g)
503+
return this.parameterError('Add Quad takes four parameters, the last should be a graph id')
504+
this.cursor['@type'] = 'AddTriple'
505+
this.cursor['graph'] = this.cleanGraph(g)
506+
return this.updated()
507+
}
508+
509+
WOQLQuery.prototype.trim = function(untrimmed, trimmed) {
510+
//if (untrimmed && untrimmed == 'args')
511+
//return ['untrimmed', 'trimmed']
512+
if (this.cursor['@type']) this.wrapCursorWithAnd()
513+
this.cursor['@type'] = 'Trim'
514+
this.cursor['untrimmed'] = this.cleanDataValue(untrimmed)
515+
this.cursor['trimmed'] = this.cleanDataValue(trimmed)
516+
return this
517+
}
518+
519+
WOQLQuery.prototype.eval = function(arith, res) {
520+
//if (arith && arith == 'args')
521+
//return ['expression', 'result']
522+
if (this.cursor['@type']) this.wrapCursorWithAnd()
523+
this.cursor['@type'] = 'Eval'
524+
this.cursor['expression'] = arith.json ? arith.json() : arith
525+
this.cursor['result'] = this.cleanArithmeticValue(res)
526+
return this
527+
}
528+
529+
WOQLQuery.prototype.plus = function(...args) {
530+
//if (args && args[0] == 'args') return ['first', 'second']
531+
if (this.cursor['@type']) this.wrapCursorWithAnd()
532+
this.cursor['@type'] = 'Plus'
533+
this.cursor['left'] = this.arop(args.shift())
534+
if (args.length > 1) {
535+
this.cursor['right'] = this.jobj(new WOQLQuery().plus(...args))
534536
} else {
535537
this.cursor.right = this.arop(args[0]);
536538
}

lib/woql.js

Lines changed: 4 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -375,13 +375,14 @@ WOQL.file = function (url, formatObj) {
375375
* HTTP POST request, with the format defined through the options
376376
* @param {string} url - The Path on the server at which the file resource can be accessed
377377
* @param {typedef.DataFormatObj} [formatObj] - imput options, optional
378+
* @param {string} [source] - It defines the source of the file, it can be 'url','post'
378379
* @returns {WOQLQuery} A WOQLQuery which contains the Post resource identifier
379380
* @example
380381
* post("/.../.../", {type:'csv'})
381382
*/
382-
WOQL.post = function (url, formatObj) {
383-
return new WOQLQuery().post(url, formatObj);
384-
};
383+
WOQL.post = function(url, formatObj, source) {
384+
return new WOQLQuery().post(url, formatObj, source)
385+
}
385386

386387
/**
387388
* Deletes a single triple from the default graph of the database

lib/woqlClient.js

Lines changed: 40 additions & 20 deletions
Original file line numberDiff line numberDiff line change
@@ -1,12 +1,14 @@
1-
/* eslint-disable camelcase */
2-
/* eslint-disable no-param-reassign */
3-
/* eslint-disable consistent-return */
4-
/* eslint-disable no-underscore-dangle */
5-
/// /@ts-check
6-
const CONST = require('./const');
7-
const DispatchRequest = require('./dispatchRequest');
8-
const ErrorMessage = require('./errorMessage');
9-
const ConnectionConfig = require('./connectionConfig');
1+
////@ts-check
2+
const typedef = require('./typedef')
3+
const CONST = require('./const')
4+
const DispatchRequest = require('./dispatchRequest')
5+
const ErrorMessage = require('./errorMessage')
6+
const ConnectionConfig = require('./connectionConfig')
7+
const WOQL = require('./woql')
8+
const WOQLQuery = require('./query/woqlBuilder')
9+
const { default: axios } = require('axios')
10+
const FormData = require('form-data');
11+
const fs = require('fs');
1012

1113
/**
1214
* @license Apache Version 2
@@ -557,20 +559,38 @@ WOQLClient.prototype.info = function () {
557559
* @example
558560
* const result = await client.query(WOQL.star())
559561
*/
560-
WOQLClient.prototype.query = function (woql, commitMsg, allWitnesses, lastDataVersion = '', getDataVersion = false) {
561-
allWitnesses = allWitnesses || false;
562-
commitMsg = commitMsg || 'Commit generated with javascript client without message';
563-
if (woql && woql.json && (!woql.containsUpdate() || commitMsg)) {
564-
const doql = woql.containsUpdate() ? this.generateCommitInfo(commitMsg) : {};
565-
doql.query = woql.json();
566-
if (allWitnesses) doql.all_witnesses = true;
562+
WOQLClient.prototype.query = function(woql, commitMsg, allWitnesses, lastDataVersion='', getDataVersion=false) {
563+
allWitnesses = allWitnesses || false
564+
commitMsg = commitMsg || 'Commit generated with javascript client without message'
565+
if (woql && woql.json && (!woql.containsUpdate() || commitMsg)) {
566+
let doql = woql.containsUpdate() ? this.generateCommitInfo(commitMsg) : {}
567+
doql.query = woql.json()
567568

568-
if (typeof lastDataVersion === 'string' && lastDataVersion !== '') {
569-
this.customHeaders({ 'TerminusDB-Data-Version': lastDataVersion });
569+
let postBody;
570+
571+
if(doql.query.resource && doql.query.resource['@type'] === 'QueryResource' && doql.query.resource.source.post) {
572+
const fileName = doql.query.resource.source.post.split('/').pop();
573+
574+
const formData = new FormData();
575+
formData.append('file', fs.createReadStream(doql.query.resource.source.post));
576+
doql.query.resource.source.post = fileName;
577+
formData.append('payload', Buffer.from(JSON.stringify(doql)), { filename: 'body.json', contentType: 'application/json' });
578+
this.customHeaders(formData.getHeaders());
579+
580+
postBody = formData;
581+
} else {
582+
postBody = doql;
583+
}
584+
585+
if (allWitnesses) doql.all_witnesses = true
586+
587+
if(typeof lastDataVersion === 'string' && lastDataVersion !== '') {
588+
this.customHeaders({'TerminusDB-Data-Version': lastDataVersion})
589+
}
590+
591+
return this.dispatch(CONST.WOQL_QUERY, this.connectionConfig.queryURL(), postBody, getDataVersion)
570592
}
571593

572-
return this.dispatch(CONST.WOQL_QUERY, this.connectionConfig.queryURL(), doql, getDataVersion);
573-
}
574594
let errmsg = 'WOQL query parameter error';
575595
if (woql && woql.json && woql.containsUpdate() && !commitMsg) {
576596
errmsg += ' - you must include a textual commit message to perform this update';

0 commit comments

Comments
 (0)