@synatic/sql-to-mongo
Advanced tools
Comparing version 0.0.3 to 0.0.4
const $check = require('check-types'); | ||
const $convert = require('@synatic/type-magic'); | ||
const ObjectID = require('bson-objectid'); | ||
@@ -530,3 +532,3 @@ // https://docs.mongodb.com/manual/meta/aggregation-quick-reference/#arithmetic-expression-operators | ||
type: 'function', | ||
parse: (parameters) => { | ||
parse: (parameters, depth, forceLiteralParse) => { | ||
const toSQLType = parameters[1] ? parameters[1].$literal || parameters[1] : null; | ||
@@ -543,4 +545,13 @@ if (!$check.string(toSQLType)) throw new Error('Type not specified for convert'); | ||
allowQuery: true, | ||
parse: (parameters) => { | ||
return {$toDate: AllowableFunctions._getSingleParameter(parameters)}; | ||
parse: (parameters, depth, forceLiteralParse) => { | ||
const paramVal = AllowableFunctions._getSingleParameter(parameters); | ||
if (forceLiteralParse && AllowableFunctions._isLiteral(paramVal)) { | ||
try { | ||
return $convert.convert(AllowableFunctions._getLiteral(paramVal), 'date'); | ||
} catch (exp) { | ||
throw new Error(`Error converting ${AllowableFunctions._getLiteral(paramVal)} to date`); | ||
} | ||
} else { | ||
return {$toDate: AllowableFunctions._getSingleParameter(parameters)}; | ||
} | ||
}, | ||
@@ -551,4 +562,13 @@ }, | ||
allowQuery: true, | ||
parse: (parameters) => { | ||
return {$toString: AllowableFunctions._getSingleParameter(parameters)}; | ||
parse: (parameters, depth, forceLiteralParse) => { | ||
const paramVal = AllowableFunctions._getSingleParameter(parameters); | ||
if (forceLiteralParse && AllowableFunctions._isLiteral(paramVal)) { | ||
try { | ||
return $convert.convert(AllowableFunctions._getLiteral(paramVal), 'string'); | ||
} catch (exp) { | ||
throw new Error(`Error converting ${AllowableFunctions._getLiteral(paramVal)} to string`); | ||
} | ||
} else { | ||
return {$toString: paramVal}; | ||
} | ||
}, | ||
@@ -559,4 +579,13 @@ }, | ||
allowQuery: true, | ||
parse: (parameters) => { | ||
return {$toDecimal: AllowableFunctions._getSingleParameter(parameters)}; | ||
parse: (parameters, depth, forceLiteralParse) => { | ||
const paramVal = AllowableFunctions._getSingleParameter(parameters); | ||
if (forceLiteralParse && AllowableFunctions._isLiteral(paramVal)) { | ||
try { | ||
return $convert.convert(AllowableFunctions._getLiteral(paramVal), 'number'); | ||
} catch (exp) { | ||
throw new Error(`Error converting ${AllowableFunctions._getLiteral(paramVal)} to number`); | ||
} | ||
} else { | ||
return {$toDecimal: paramVal}; | ||
} | ||
}, | ||
@@ -567,4 +596,13 @@ }, | ||
allowQuery: true, | ||
parse: (parameters) => { | ||
return {$toDouble: AllowableFunctions._getSingleParameter(parameters)}; | ||
parse: (parameters, depth, forceLiteralParse) => { | ||
const paramVal = AllowableFunctions._getSingleParameter(parameters); | ||
if (forceLiteralParse && AllowableFunctions._isLiteral(paramVal)) { | ||
try { | ||
return $convert.convert(AllowableFunctions._getLiteral(paramVal), 'number'); | ||
} catch (exp) { | ||
throw new Error(`Error converting ${AllowableFunctions._getLiteral(paramVal)} to number`); | ||
} | ||
} else { | ||
return {$toDouble: paramVal}; | ||
} | ||
}, | ||
@@ -575,4 +613,13 @@ }, | ||
allowQuery: true, | ||
parse: (parameters) => { | ||
return {$toInt: AllowableFunctions._getSingleParameter(parameters)}; | ||
parse: (parameters, depth, forceLiteralParse) => { | ||
const paramVal = AllowableFunctions._getSingleParameter(parameters); | ||
if (forceLiteralParse && AllowableFunctions._isLiteral(paramVal)) { | ||
try { | ||
return $convert.convert(AllowableFunctions._getLiteral(paramVal), 'integer'); | ||
} catch (exp) { | ||
throw new Error(`Error converting ${AllowableFunctions._getLiteral(paramVal)} to integer`); | ||
} | ||
} else { | ||
return {$toInt: paramVal}; | ||
} | ||
}, | ||
@@ -583,4 +630,13 @@ }, | ||
allowQuery: true, | ||
parse: (parameters) => { | ||
return {$toLong: AllowableFunctions._getSingleParameter(parameters)}; | ||
parse: (parameters, depth, forceLiteralParse) => { | ||
const paramVal = AllowableFunctions._getSingleParameter(parameters); | ||
if (forceLiteralParse && AllowableFunctions._isLiteral(paramVal)) { | ||
try { | ||
return $convert.convert(AllowableFunctions._getLiteral(paramVal), 'integer'); | ||
} catch (exp) { | ||
throw new Error(`Error converting ${AllowableFunctions._getLiteral(paramVal)} to integer`); | ||
} | ||
} else { | ||
return {$toLong: paramVal}; | ||
} | ||
}, | ||
@@ -591,4 +647,13 @@ }, | ||
allowQuery: true, | ||
parse: (parameters) => { | ||
return {$toBool: AllowableFunctions._getSingleParameter(parameters)}; | ||
parse: (parameters, depth, forceLiteralParse) => { | ||
const paramVal = AllowableFunctions._getSingleParameter(parameters); | ||
if (forceLiteralParse && AllowableFunctions._isLiteral(paramVal)) { | ||
try { | ||
return $convert.convert(AllowableFunctions._getLiteral(paramVal), 'boolean'); | ||
} catch (exp) { | ||
throw new Error(`Error converting ${AllowableFunctions._getLiteral(paramVal)} to boolean`); | ||
} | ||
} else { | ||
return {$toBool: paramVal}; | ||
} | ||
}, | ||
@@ -599,4 +664,13 @@ }, | ||
allowQuery: true, | ||
parse: (parameters) => { | ||
return {$toObjectId: AllowableFunctions._getSingleParameter(parameters)}; | ||
parse: (parameters, depth, forceLiteralParse) => { | ||
const paramVal = AllowableFunctions._getSingleParameter(parameters); | ||
if (forceLiteralParse && AllowableFunctions._isLiteral(paramVal)) { | ||
try { | ||
return new ObjectID(AllowableFunctions._getLiteral(paramVal)); | ||
} catch (exp) { | ||
throw new Error(`Error converting ${AllowableFunctions._getLiteral(paramVal)} to ObjectId`); | ||
} | ||
} else { | ||
return {$toObjectId: paramVal}; | ||
} | ||
}, | ||
@@ -1337,2 +1411,31 @@ }, | ||
/** Checks whether the value is a literal | ||
* | ||
* @param {*} val the value to check | ||
* @return {*} | ||
* @private | ||
*/ | ||
static _isLiteral(val) { | ||
return ( | ||
($check.primitive(val) && $check.string(val) ? !val.startsWith('$') : true) || | ||
($check.object(val) && !$check.undefined(val.$literal)) | ||
); | ||
} | ||
/** Retrieves the literal value | ||
* | ||
* @param {*} val the value to check | ||
* @return {*} | ||
* @private | ||
*/ | ||
static _getLiteral(val) { | ||
if ($check.primitive(val)) { | ||
return val; | ||
} else if ($check.object(val) && !$check.undefined(val.$literal)) { | ||
return val.$literal; | ||
} else { | ||
return val; | ||
} | ||
} | ||
/** Checks the type of an element | ||
@@ -1339,0 +1442,0 @@ * |
@@ -269,3 +269,4 @@ const {Parser} = require('node-sql-parser'); | ||
if (queryPart.type === 'function' || queryPart.type === 'select') return SQLParser._makeProjectionExpressionPart(queryPart); | ||
if (queryPart.type === 'function' || queryPart.type === 'select') | ||
return SQLParser._makeProjectionExpressionPart(queryPart, 0, true); | ||
if (queryPart.type === 'expr_list') return queryPart.value.map((v) => SQLParser._makeQueryPart(v)); | ||
@@ -280,6 +281,7 @@ | ||
* @param {int} [depth] - the current recursive depth | ||
* @param {boolean} [forceLiteralParse] - Forces parsing of literal expressions | ||
* @return {undefined|*} | ||
* @private | ||
*/ | ||
static _makeProjectionExpressionPart(expr, depth = 0) { | ||
static _makeProjectionExpressionPart(expr, depth = 0, forceLiteralParse = false) { | ||
const makeArg = (expr) => { | ||
@@ -329,3 +331,4 @@ // todo check all these types | ||
args.map((a) => makeArg(a)), | ||
depth | ||
depth, | ||
forceLiteralParse | ||
); | ||
@@ -335,3 +338,3 @@ } else if (expr.left && expr.right) { | ||
} else if (expr.args && expr.args.expr) { | ||
return fn.parse(makeArg(expr.args.expr), depth); | ||
return fn.parse(makeArg(expr.args.expr), depth, forceLiteralParse); | ||
} else { | ||
@@ -338,0 +341,0 @@ return makeArg(expr); |
{ | ||
"name": "@synatic/sql-to-mongo", | ||
"version": "0.0.3", | ||
"version": "0.0.4", | ||
"description": "Convert SQL to mongo queries or aggregates", | ||
@@ -50,2 +50,4 @@ "main": "index.js", | ||
"@synatic/json-magic": "^1.0.0", | ||
"@synatic/type-magic": "^1.0.0", | ||
"bson-objectid": "2.0.1", | ||
"check-types": "11.1.2", | ||
@@ -52,0 +54,0 @@ "deepmerge": "4.2.2", |
@@ -559,2 +559,14 @@ # sql-to-mongo | ||
Literal types in where statements using conversion functions are converted automatically e.g.: | ||
```sql | ||
select to_date(date) as d from customers where to_date(date) > to_date('2012-01-01') | ||
``` | ||
Will automatically convert the date value and result in the following: | ||
``` | ||
{"$expr":{"$gt":[{"$toDate":"$date"},new Date("2012-01-01T00:00:00.000Z")]}} | ||
``` | ||
#### Cast | ||
@@ -561,0 +573,0 @@ |
License Policy Violation
LicenseThis package is not allowed per your license policy. Review the package's license to ensure compliance.
Found 1 instance in 1 package
License Policy Violation
LicenseThis package is not allowed per your license policy. Review the package's license to ensure compliance.
Found 1 instance in 1 package
161900
2472
641
6
+ Added@synatic/type-magic@^1.0.0
+ Addedbson-objectid@2.0.1
+ Added@synatic/type-magic@1.0.3(transitive)
+ Addedbson-objectid@2.0.1(transitive)
+ Addedmoment@2.30.1(transitive)
+ Addednumeral@2.0.6(transitive)