tg-client-query-builder
Advanced tools
Comparing version 2.9.2 to 2.10.0
{ | ||
"name": "tg-client-query-builder", | ||
"version": "2.9.2", | ||
"version": "2.10.0", | ||
"description": "Teselagen Client Side (browser) SQL Query Builder", | ||
@@ -5,0 +5,0 @@ "main": "index.js", |
@@ -31,9 +31,9 @@ // valid filters: | ||
return (...args) => { | ||
if (args.some((arg) => { | ||
return !(_.isDate(arg) || _.isString(arg) || _.isNumber(arg)) | ||
})) { | ||
throw new Error( | ||
`QueryBuilderError: You must pass a date or number as args to ${opName}. You passed: Args ${args.join(',')}` | ||
); | ||
} | ||
if (args.some((arg) => { | ||
return !(_.isDate(arg) || _.isString(arg) || _.isNumber(arg)) | ||
})) { | ||
throw new Error( | ||
`QueryBuilderError: You must pass a date or number as args to ${opName}. You passed: Args ${args.join(',')}` | ||
); | ||
} | ||
} | ||
@@ -43,7 +43,7 @@ } | ||
return (arg) => { | ||
if (!_.isArray(arg)) { | ||
throw new Error( | ||
`QueryBuilderError: You must pass an array for ${opName} filters. You passed: ${arg}` | ||
); | ||
} | ||
if (!_.isArray(arg)) { | ||
throw new Error( | ||
`QueryBuilderError: You must pass an array for ${opName} filters. You passed: ${arg}` | ||
); | ||
} | ||
} | ||
@@ -53,7 +53,7 @@ } | ||
return (arg) => { | ||
if (!_.isString(arg)) { | ||
throw new Error( | ||
`QueryBuilderError: You must pass a string for ${opName} filters. You passed: ${arg}` | ||
); | ||
} | ||
if (!_.isString(arg)) { | ||
throw new Error( | ||
`QueryBuilderError: You must pass a string for ${opName} filters. You passed: ${arg}` | ||
); | ||
} | ||
} | ||
@@ -208,21 +208,21 @@ } | ||
{ | ||
opName: "fuzzy", | ||
sanityChecks: [ | ||
numberOfArgs("fuzzy", 1), | ||
isString("fuzzy") | ||
], | ||
transform: (arg)=> { | ||
// Build Regex String | ||
var matchTerm = '' | ||
// Split all the search terms | ||
var terms = arg.replace(/\W/g, '').replace(' ', '').split(""); | ||
for(var i = 0; i < terms.length; i++) { | ||
matchTerm += '.*' + terms[i]; | ||
} | ||
matchTerm += '.*'; | ||
return { | ||
newOpName: 'matchesRegex', | ||
newArgs: [matchTerm] | ||
} | ||
opName: "fuzzy", | ||
sanityChecks: [ | ||
numberOfArgs("fuzzy", 1), | ||
isString("fuzzy") | ||
], | ||
transform: (arg) => { | ||
// Build Regex String | ||
var matchTerm = '' | ||
// Split all the search terms | ||
var terms = arg.replace(/\W/g, '').replace(' ', '').split(""); | ||
for (var i = 0; i < terms.length; i++) { | ||
matchTerm += '.*' + terms[i]; | ||
} | ||
matchTerm += '.*'; | ||
return { | ||
newOpName: 'matchesRegex', | ||
newArgs: [matchTerm] | ||
} | ||
} | ||
} | ||
@@ -234,3 +234,3 @@ | ||
module.exports = (function() { | ||
module.exports = (function () { | ||
// to be implemented | ||
@@ -291,3 +291,3 @@ // | ||
let self = this; | ||
this.toFilter = function(filterBuilder, name) { | ||
this.toFilter = function (filterBuilder, name) { | ||
this.query.foreignKey = name; | ||
@@ -301,3 +301,3 @@ return this.toJSON(); | ||
QueryBuilder.prototype.field = function(fieldName) { | ||
QueryBuilder.prototype.field = function (fieldName) { | ||
return { | ||
@@ -309,3 +309,3 @@ __objectType: "field", | ||
QueryBuilder.prototype.related = function(relatedEntity) { | ||
QueryBuilder.prototype.related = function (relatedEntity) { | ||
var tokens = relatedEntity.split("."); | ||
@@ -321,3 +321,3 @@ var entity = tokens[0]; | ||
QueryBuilder.prototype.notRelated = function(relatedEntity) { | ||
QueryBuilder.prototype.notRelated = function (relatedEntity) { | ||
var tokens = relatedEntity.split("."); | ||
@@ -333,7 +333,18 @@ var entity = tokens[0]; | ||
QueryBuilder.prototype.toJSON = function() { | ||
return JSON.parse(JSON.stringify(this.query)); | ||
QueryBuilder.prototype.toJSON = function () { | ||
let qry = JSON.parse(JSON.stringify(this.query)); | ||
if (qry.filters.length > 1) { | ||
qry.filters = [ | ||
{ | ||
type: "group", | ||
operator: 'and', | ||
chainedWith: 'and', | ||
filters: qry.filters | ||
} | ||
]; | ||
} | ||
return qry; | ||
}; | ||
QueryBuilder.prototype.convertToFilter = function(argDef, operator) { | ||
QueryBuilder.prototype.convertToFilter = function (argDef, operator) { | ||
var whereArgs = {}; | ||
@@ -375,3 +386,3 @@ var filters = []; | ||
QueryBuilder.prototype.where = function() { | ||
QueryBuilder.prototype.where = function () { | ||
var args = [].slice.call(arguments); | ||
@@ -381,3 +392,3 @@ return this.whereAll(args); | ||
QueryBuilder.prototype.orWhere = function() { | ||
QueryBuilder.prototype.orWhere = function () { | ||
var args = [].slice.call(arguments); | ||
@@ -387,3 +398,3 @@ return this.orWhereAll(args); | ||
QueryBuilder.prototype.andWhere = function() { | ||
QueryBuilder.prototype.andWhere = function () { | ||
var args = [].slice.call(arguments); | ||
@@ -393,3 +404,3 @@ return this.andWhereAll(args); | ||
QueryBuilder.prototype.whereAny = function() { | ||
QueryBuilder.prototype.whereAny = function () { | ||
var args = [].slice.call(arguments); | ||
@@ -399,3 +410,3 @@ return whereAny(this, args); | ||
QueryBuilder.prototype.whereAll = function() { | ||
QueryBuilder.prototype.whereAll = function () { | ||
var args = [].slice.call(arguments); | ||
@@ -405,3 +416,3 @@ return whereAll(this, args); | ||
QueryBuilder.prototype.andWhereAny = function() { | ||
QueryBuilder.prototype.andWhereAny = function () { | ||
var args = [].slice.call(arguments); | ||
@@ -411,3 +422,3 @@ return whereAny(this, args, "and"); | ||
QueryBuilder.prototype.orWhereAny = function() { | ||
QueryBuilder.prototype.orWhereAny = function () { | ||
var args = [].slice.call(arguments); | ||
@@ -417,3 +428,3 @@ return whereAny(this, args, "or"); | ||
QueryBuilder.prototype.andWhereAll = function() { | ||
QueryBuilder.prototype.andWhereAll = function () { | ||
var args = [].slice.call(arguments); | ||
@@ -423,3 +434,3 @@ return whereAll(this, args, "and"); | ||
QueryBuilder.prototype.orWhereAll = function() { | ||
QueryBuilder.prototype.orWhereAll = function () { | ||
var args = [].slice.call(arguments); | ||
@@ -429,3 +440,3 @@ return whereAll(this, args, "or"); | ||
QueryBuilder.prototype.count = function() { | ||
QueryBuilder.prototype.count = function () { | ||
var args = [].slice.call(arguments); | ||
@@ -461,11 +472,11 @@ if (this.query.type === "subquery") { | ||
expressionOperators.forEach(({ opName, sanityChecks, transform }) => { | ||
const filter = function(...args) { | ||
const filter = function (...args) { | ||
let argsToUse = args | ||
let opNameToUse = opName | ||
if (transform) { | ||
let {newOpName, newArgs} = transform(...args) | ||
argsToUse = newArgs | ||
opNameToUse = newOpName | ||
let { newOpName, newArgs } = transform(...args) | ||
argsToUse = newArgs | ||
opNameToUse = newOpName | ||
} | ||
sanityChecks.forEach((sanityCheck) => {sanityCheck(...argsToUse)}) | ||
sanityChecks.forEach((sanityCheck) => { sanityCheck(...argsToUse) }) | ||
return new FilterExpression(opNameToUse, argsToUse); | ||
@@ -509,9 +520,10 @@ }; | ||
if (filterDef.filters.length === 1) { | ||
filterBuilder.query.filters.push(filterDef.filters[0]); | ||
} else { | ||
filterBuilder.query.filters.push(filterDef); | ||
} | ||
// if (filterDef.filters.length === 1) { | ||
// filterBuilder.query.filters.push(filterDef.filters[0]); | ||
// } else { | ||
// filterBuilder.query.filters.push(filterDef); | ||
// } | ||
filterBuilder.query.filters.push(filterDef); | ||
return filterBuilder; | ||
} | ||
})(); |
72767
775