Skip to content

Commit 35b761f

Browse files
linted merged changes
Signed-off-by: NeelParihar <[email protected]>
1 parent 7cc4964 commit 35b761f

File tree

3 files changed

+178
-176
lines changed

3 files changed

+178
-176
lines changed

lib/query/woqlQuery.js

Lines changed: 137 additions & 140 deletions
Original file line numberDiff line numberDiff line change
@@ -391,148 +391,145 @@ 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-
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-
}
414-
}
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])
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+
for (var i = 0; i < varList.length; i++) {
407+
const onemap = varList[i];
408+
if (Array.isArray(onemap) && onemap.length >= 2) {
409+
const type = onemap && onemap.length > 2 ? onemap[2] : false;
410+
const oasv = this.asv(onemap[0], onemap[1], type);
411+
this.query.push(oasv);
426412
}
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])
413+
}
431414
}
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))
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]);
431+
}
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') this.cursor.options = opts;
442+
return this;
443+
};
444+
445+
WOQLQuery.prototype.remote = function (uri, opts) {
446+
// if (uri && uri == 'args') return ['remote_uri', 'format']
447+
if (this.cursor['@type']) this.wrapCursorWithAnd();
448+
this.cursor['@type'] = 'QueryResource';
449+
this.cursor.source = { '@type': 'Source', url: uri };
450+
this.cursor.format = 'csv'; // hard coded for now
451+
if (typeof opts !== 'undefined') this.cursor.options = opts;
452+
return this;
453+
};
454+
455+
WOQLQuery.prototype.post = function (fpath, opts, source = 'post') {
456+
// if (fpath && fpath == 'args') return ['file', 'format']
457+
if (this.cursor['@type']) this.wrapCursorWithAnd();
458+
this.cursor['@type'] = 'QueryResource';
459+
this.cursor.source = { '@type': 'Source', [source]: fpath };
460+
this.cursor.format = 'csv'; // hard coded for now
461+
this.cursor.options = opts;
462+
if (typeof opts !== 'undefined') this.cursor.options = opts;
463+
return this;
464+
};
465+
466+
WOQLQuery.prototype.delete_triple = function (Subject, Predicate, Object_or_Literal) {
467+
if (this.cursor['@type']) this.wrapCursorWithAnd();
468+
const args = this.triple(Subject, Predicate, Object_or_Literal);
469+
// if (Subject && Subject == 'args') return args
470+
this.cursor['@type'] = 'DeleteTriple';
471+
return this.updated();
472+
};
473+
474+
WOQLQuery.prototype.add_triple = function (Subject, Predicate, Object_or_Literal) {
475+
if (this.cursor['@type']) this.wrapCursorWithAnd();
476+
const args = this.triple(Subject, Predicate, Object_or_Literal);
477+
// if (Subject && Subject == 'args') return args
478+
this.cursor['@type'] = 'AddTriple';
479+
return this.updated();
480+
};
481+
482+
WOQLQuery.prototype.delete_quad = function (a, b, c, g) {
483+
if (this.cursor['@type']) this.wrapCursorWithAnd();
484+
const args = this.triple(a, b, c);
485+
// if (a && a == 'args') return args.concat(['graph'])
486+
if (!g) {
487+
return this.parameterError(
488+
'Delete Quad takes four parameters, the last should be a graph id',
489+
);
490+
}
491+
this.cursor['@type'] = 'DeleteTriple';
492+
this.cursor.graph = this.cleanGraph(g);
493+
return this.updated();
494+
};
495+
496+
WOQLQuery.prototype.add_quad = function (a, b, c, g) {
497+
if (this.cursor['@type']) this.wrapCursorWithAnd();
498+
const args = this.triple(a, b, c);
499+
// if (a && a == 'args') return args.concat(['graph'])
500+
if (!g) return this.parameterError('Add Quad takes four parameters, the last should be a graph id');
501+
this.cursor['@type'] = 'AddTriple';
502+
this.cursor.graph = this.cleanGraph(g);
503+
return this.updated();
504+
};
505+
506+
WOQLQuery.prototype.trim = function (untrimmed, trimmed) {
507+
// if (untrimmed && untrimmed == 'args')
508+
// return ['untrimmed', 'trimmed']
509+
if (this.cursor['@type']) this.wrapCursorWithAnd();
510+
this.cursor['@type'] = 'Trim';
511+
this.cursor.untrimmed = this.cleanDataValue(untrimmed);
512+
this.cursor.trimmed = this.cleanDataValue(trimmed);
513+
return this;
514+
};
515+
516+
WOQLQuery.prototype.eval = function (arith, res) {
517+
// if (arith && arith == 'args')
518+
// return ['expression', 'result']
519+
if (this.cursor['@type']) this.wrapCursorWithAnd();
520+
this.cursor['@type'] = 'Eval';
521+
this.cursor.expression = arith.json ? arith.json() : arith;
522+
this.cursor.result = this.cleanArithmeticValue(res);
523+
return this;
524+
};
525+
526+
WOQLQuery.prototype.plus = function (...args) {
527+
// if (args && args[0] == 'args') return ['first', 'second']
528+
if (this.cursor['@type']) this.wrapCursorWithAnd();
529+
this.cursor['@type'] = 'Plus';
530+
this.cursor.left = this.arop(args.shift());
531+
if (args.length > 1) {
532+
this.cursor.right = this.jobj(new WOQLQuery().plus(...args));
536533
} else {
537534
this.cursor.right = this.arop(args[0]);
538535
}

lib/woql.js

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -380,9 +380,9 @@ WOQL.file = function (url, formatObj) {
380380
* @example
381381
* post("/.../.../", {type:'csv'})
382382
*/
383-
WOQL.post = function(url, formatObj, source) {
384-
return new WOQLQuery().post(url, formatObj, source)
385-
}
383+
WOQL.post = function (url, formatObj, source) {
384+
return new WOQLQuery().post(url, formatObj, source);
385+
};
386386

387387
/**
388388
* Deletes a single triple from the default graph of the database

lib/woqlClient.js

Lines changed: 38 additions & 33 deletions
Original file line numberDiff line numberDiff line change
@@ -1,14 +1,17 @@
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')
1+
/* eslint-disable camelcase */
2+
/* eslint-disable no-param-reassign */
3+
/* eslint-disable no-underscore-dangle */
4+
/* eslint-disable no-unused-vars */
5+
/// /@ts-check
106
const FormData = require('form-data');
117
const fs = require('fs');
8+
const typedef = require('./typedef');
9+
const CONST = require('./const');
10+
const DispatchRequest = require('./dispatchRequest');
11+
const ErrorMessage = require('./errorMessage');
12+
const ConnectionConfig = require('./connectionConfig');
13+
const WOQL = require('./woql');
14+
const WOQLQuery = require('./query/woqlBuilder');
1215

1316
/**
1417
* @license Apache Version 2
@@ -356,6 +359,7 @@ WOQLClient.prototype.set = function (params) {
356359
* @example
357360
* const branch_resource = client.resource("branch")
358361
*/
362+
// eslint-disable-next-line consistent-return
359363
WOQLClient.prototype.resource = function (resourceType, resourceId) {
360364
let base = `${this.organization()}/${this.db()}/`;
361365
if (resourceType === 'db') return base;
@@ -559,38 +563,39 @@ WOQLClient.prototype.info = function () {
559563
* @example
560564
* const result = await client.query(WOQL.star())
561565
*/
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()
566+
WOQLClient.prototype.query = function (woql, commitMsg, allWitnesses, lastDataVersion = '', getDataVersion = false) {
567+
allWitnesses = allWitnesses || false;
568+
commitMsg = commitMsg || 'Commit generated with javascript client without message';
569+
if (woql && woql.json && (!woql.containsUpdate() || commitMsg)) {
570+
const doql = woql.containsUpdate() ? this.generateCommitInfo(commitMsg) : {};
571+
doql.query = woql.json();
568572

569-
let postBody;
573+
let postBody;
570574

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();
575+
if (doql.query.resource && doql.query.resource['@type'] === 'QueryResource' && doql.query.resource.source.post) {
576+
const fileName = doql.query.resource.source.post.split('/').pop();
573577

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());
578+
const formData = new FormData();
579+
formData.append('file', fs.createReadStream(doql.query.resource.source.post));
580+
doql.query.resource.source.post = fileName;
581+
formData.append('payload', Buffer.from(JSON.stringify(doql)), { filename: 'body.json', contentType: 'application/json' });
582+
this.customHeaders(formData.getHeaders());
579583

580-
postBody = formData;
581-
} else {
582-
postBody = doql;
583-
}
584-
585-
if (allWitnesses) doql.all_witnesses = true
584+
postBody = formData;
585+
} else {
586+
postBody = doql;
587+
}
586588

587-
if(typeof lastDataVersion === 'string' && lastDataVersion !== '') {
588-
this.customHeaders({'TerminusDB-Data-Version': lastDataVersion})
589-
}
589+
if (allWitnesses) doql.all_witnesses = true;
590590

591-
return this.dispatch(CONST.WOQL_QUERY, this.connectionConfig.queryURL(), postBody, getDataVersion)
591+
if (typeof lastDataVersion === 'string' && lastDataVersion !== '') {
592+
this.customHeaders({ 'TerminusDB-Data-Version': lastDataVersion });
592593
}
593594

595+
// eslint-disable-next-line max-len
596+
return this.dispatch(CONST.WOQL_QUERY, this.connectionConfig.queryURL(), postBody, getDataVersion);
597+
}
598+
594599
let errmsg = 'WOQL query parameter error';
595600
if (woql && woql.json && woql.containsUpdate() && !commitMsg) {
596601
errmsg += ' - you must include a textual commit message to perform this update';

0 commit comments

Comments
 (0)