@synatic/sql-to-mongo
Advanced tools
Comparing version 1.0.7 to 1.1.0
@@ -41,2 +41,12 @@ const $check = require('check-types'); | ||
if ( | ||
!subAst.where && | ||
!subAst.from && | ||
subAst.ast && | ||
subAst.tableList && | ||
subAst.columnList | ||
) { | ||
return getTables(subAst.ast); | ||
} | ||
if (whereContainsOtherTable(subAst.where)) { | ||
@@ -43,0 +53,0 @@ const queries = getWhereAstQueries(subAst.where); |
@@ -24,2 +24,16 @@ const groupByColumnParserModule = require('./groupByColumnParser'); | ||
function makeAggregatePipeline(ast, options = {}) { | ||
if ( | ||
!ast.from && | ||
!ast.where && | ||
!ast.groupby && | ||
!ast.columns && | ||
!ast.orderby && | ||
!ast.limit && | ||
!ast.union | ||
) { | ||
if (ast.ast) { | ||
return makeAggregatePipeline(ast.ast, options); | ||
} | ||
throw new Error(`AST is missing properties required for processing`); | ||
} | ||
/** @type {import('../types').PipelineFn[]} */ | ||
@@ -38,3 +52,9 @@ let pipeline = []; | ||
astQueryColumn.ast.forEach((actualAst) => { | ||
const subPl = makeAggregatePipeline(actualAst, options); | ||
const ast = actualAst.columns | ||
? actualAst | ||
: actualAst.ast | ||
? actualAst.ast | ||
: actualAst; | ||
const subPl = makeAggregatePipeline(ast, options); | ||
pipeline = pipeline.concat(subPl); | ||
@@ -45,3 +65,3 @@ | ||
from: localTableName, | ||
localField: actualAst.columns[0].expr.column, | ||
localField: ast.columns[0].expr.column, | ||
foreignField, | ||
@@ -48,0 +68,0 @@ as: localTableName, |
@@ -52,2 +52,24 @@ const makeProjectionExpressionPartModule = require('./makeProjectionExpressionPart'); | ||
} | ||
if (queryPart.operator === 'NOT LIKE') { | ||
const likeVal = queryPart.right.value; | ||
const regexString = sqlStringToRegex(likeVal); | ||
const input = makeFilterCondition( | ||
queryPart.left, | ||
includeThis, | ||
prefixRight, | ||
'left', | ||
prefixLeft | ||
); | ||
return { | ||
$not: [ | ||
{ | ||
$regexMatch: { | ||
input, | ||
regex: regexString, | ||
options: 'i', | ||
}, | ||
}, | ||
], | ||
}; | ||
} | ||
const operation = operatorMap[queryPart.operator]; | ||
@@ -54,0 +76,0 @@ if (!operation) { |
@@ -76,13 +76,19 @@ const getParsedValueFromBinaryExpressionModule = require('./getParsedValueFromBinaryExpression'); | ||
return makeProjectionExpressionPart(expr); | ||
} else if (expr.type === 'column_ref') { | ||
} | ||
if (expr.type === 'column_ref') { | ||
return `$${expr.table ? expr.table + '.' : ''}${expr.column}`; | ||
} else if (expr.type === 'binary_expr') { | ||
} | ||
if (expr.type === 'binary_expr') { | ||
return getParsedValueFromBinaryExpressionModule.getParsedValueFromBinaryExpression( | ||
expr | ||
); | ||
} else if (expr.type === 'select' && expr.from) { | ||
} | ||
if (expr.type === 'select' && expr.from) { | ||
return makeArraySubSelectPartModule.makeArraySubSelectPart(expr, depth); | ||
} else if (expr.type === 'select' && !expr.from) { | ||
} | ||
if (expr.type === 'select' && !expr.from) { | ||
return makeObjectFromSelectModule.makeObjectFromSelect(expr); | ||
} else if (expr.type === 'unary_expr') { | ||
} | ||
if (expr.type === 'unary_expr') { | ||
if (expr.operator === '-') { | ||
@@ -95,9 +101,13 @@ return {$multiply: [-1, makeProjectionExpressionPart(expr.expr)]}; | ||
} | ||
} else if (expr.type === 'case') { | ||
} | ||
if (expr.type === 'case') { | ||
return makeCaseConditionModule.makeCaseCondition(expr); | ||
} else if (expr.value !== undefined) { | ||
} | ||
if (expr.value !== undefined) { | ||
return {$literal: expr.value}; | ||
} else { | ||
throw new Error(`Unable to parse expression type:${expr.type}`); | ||
} | ||
if (!expr.type && expr.ast) { | ||
return makeArg(expr.ast, depth); | ||
} | ||
throw new Error(`Unable to parse expression type:${expr.type}`); | ||
} |
@@ -129,2 +129,13 @@ const makeProjectionExpressionPartModule = require('./makeProjectionExpressionPart'); | ||
} | ||
if (queryPart.operator === 'NOT LIKE') { | ||
const likeVal = queryPart.right.value; | ||
const regexString = sqlStringToRegex(likeVal); | ||
// eslint-disable-next-line security/detect-non-literal-regexp | ||
const regex = new RegExp(regexString, 'i'); | ||
return { | ||
[getColumnNameOrVal(queryPart.left)]: { | ||
$not: regex, | ||
}, | ||
}; | ||
} | ||
if (queryPart.operator === 'IS NOT') { | ||
@@ -131,0 +142,0 @@ return makeOperator('$ne'); |
@@ -111,2 +111,6 @@ const getParsedValueFromBinaryExpressionModule = require('./getParsedValueFromBinaryExpression'); | ||
if (column.expr.type === 'aggr_func') { | ||
if (column.expr.name.toLowerCase() === 'count') { | ||
result.count.push({$count: column.as}); | ||
return; | ||
} | ||
result.parsedProject.$project[column.as] = | ||
@@ -185,3 +189,11 @@ makeProjectionExpressionPartModule.makeProjectionExpressionPart( | ||
} | ||
throw new Error(`Not Supported:${column.expr.type}`); | ||
if (column.expr.type) { | ||
throw new Error(`Not Supported:${column.expr.type}`); | ||
} | ||
if (column.expr.ast) { | ||
return projectColumnParser({...column, expr: column.expr.ast}, result); | ||
} | ||
throw new Error( | ||
`Column not supported:\n${JSON.stringify(column, null, 4)}` | ||
); | ||
} |
{ | ||
"name": "@synatic/sql-to-mongo", | ||
"version": "1.0.7", | ||
"version": "1.1.0", | ||
"description": "Convert SQL to mongo queries or aggregates", | ||
@@ -18,3 +18,4 @@ "main": "index.js", | ||
"test-ci": "nyc --reporter text-summary mocha -- --reporter spec --check-leaks test/", | ||
"test-cov": "nyc --reporter lcov --reporter text mocha -- --reporter dot --check-leaks test/" | ||
"test-cov": "nyc --reporter lcov --reporter text mocha -- --reporter dot --check-leaks test/", | ||
"update": "npm-check -u" | ||
}, | ||
@@ -56,8 +57,9 @@ "repository": { | ||
"@synatic/type-magic": "^1.0.0", | ||
"bson-objectid": "2.0.3", | ||
"check-types": "11.1.2", | ||
"bson-objectid": "2.0.4", | ||
"check-types": "11.2.2", | ||
"deepmerge": "4.2.2", | ||
"node-sql-parser": "4.0.2" | ||
"node-sql-parser": "4.6.4" | ||
}, | ||
"devDependencies": { | ||
"@types/mocha": "^10.0.1", | ||
"@synatic/eslint-config-synatic-node": "^1.0.13", | ||
@@ -67,6 +69,6 @@ "async": "^3.2.0", | ||
"clone-deep": "^4.0.1", | ||
"deep-equal": "^2.0.5", | ||
"eslint": "^8.16.0", | ||
"deep-equal": "^2.2.0", | ||
"eslint": "^8.31.0", | ||
"eslint-config-google": "^0.14.0", | ||
"eslint-config-prettier": "^8.5.0", | ||
"eslint-config-prettier": "^8.6.0", | ||
"eslint-plugin-import": "^2.26.0", | ||
@@ -78,9 +80,10 @@ "eslint-plugin-jsdoc": "^39.3.2", | ||
"eslint-plugin-security": "^1.5.0", | ||
"eslint-plugin-sonarjs": "^0.13.0", | ||
"eslint-plugin-sonarjs": "^0.17.0", | ||
"mocha": "^10.0.0", | ||
"moment": "^2.29.1", | ||
"mongodb": "^4.6.0", | ||
"moment": "^2.29.4", | ||
"mongodb": "^4.13.0", | ||
"mongodb-language-model": "^2.3.0", | ||
"npm-check": "^6.0.1", | ||
"nyc": "^15.1.0", | ||
"prettier": "^2.3.1", | ||
"prettier": "^2.8.2", | ||
"underscore.string": "^3.3.5", | ||
@@ -87,0 +90,0 @@ "webpack": "^5.73.0", |
Sorry, the diff of this file is too big to display
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
201798
4204
27
+ Addedbson-objectid@2.0.4(transitive)
+ Addedcheck-types@11.2.2(transitive)
+ Addednode-sql-parser@4.6.4(transitive)
- Removedbson-objectid@2.0.3(transitive)
- Removedcheck-types@11.1.2(transitive)
- Removednode-sql-parser@4.0.2(transitive)
Updatedbson-objectid@2.0.4
Updatedcheck-types@11.2.2
Updatednode-sql-parser@4.6.4