@synatic/sql-to-mongo
Advanced tools
Comparing version
@@ -64,11 +64,3 @@ const $check = require('check-types'); | ||
if (from.table) { | ||
let table = from.table; | ||
if (table && table.endsWith('|first')) { | ||
table = table.substring(0, table.length - 6); | ||
} else if (table && table.endsWith('|last')) { | ||
table = table.substring(0, table.length - 5); | ||
} else if (table && table.endsWith('|unwind')) { | ||
table = table.substring(0, table.length - 7); | ||
} | ||
tables.push(table); | ||
tables.push(from.table.split('|')[0]); | ||
} else if (from.expr && from.expr.ast) { | ||
@@ -75,0 +67,0 @@ tables = tables.concat(getTables(from.expr.ast)); |
@@ -36,25 +36,10 @@ const makeFilterConditionModule = require('./makeFilterCondition'); | ||
let joinHint = null; | ||
if (toTable.toLowerCase().endsWith('|first')) { | ||
joinHint = 'first'; | ||
toTable = toTable.substring(0, toTable.length - 6); | ||
} else if (toTable.toLowerCase().endsWith('|last')) { | ||
joinHint = 'last'; | ||
toTable = toTable.substring(0, toTable.length - 5); | ||
} else if (toTable.toLowerCase().endsWith('|unwind')) { | ||
joinHint = 'unwind'; | ||
toTable = toTable.substring(0, toTable.length - 7); | ||
} | ||
const joinHints = toTable | ||
.toLowerCase() | ||
.split('|') | ||
.slice(1) | ||
.concat(toAs.toLowerCase().split('|').slice(1)); | ||
toTable = toTable.split('|')[0]; | ||
toAs = toAs.split('|')[0]; | ||
if (toAs.toLowerCase().endsWith('|first')) { | ||
joinHint = 'first'; | ||
toAs = toAs.substring(0, toAs.length - 6); | ||
} else if (toAs.toLowerCase().endsWith('|last')) { | ||
joinHint = 'last'; | ||
toAs = toAs.substring(0, toAs.length - 5); | ||
} else if (toTable.toLowerCase().endsWith('|unwind')) { | ||
joinHint = 'unwind'; | ||
toAs = toAs.substring(0, toAs.length - 7); | ||
} | ||
if ( | ||
@@ -66,8 +51,15 @@ join.table && | ||
) { | ||
return tableJoin(join, previousJoin, pipeline, toTable, toAs, joinHint); | ||
return tableJoin( | ||
join, | ||
previousJoin, | ||
pipeline, | ||
toTable, | ||
toAs, | ||
joinHints | ||
); | ||
} | ||
const prefixLeft = | ||
join.as && join.on.left.table ? join.on.left.table !== join.as : false; | ||
join.as && join.on.left.table ? join.on.left.table !== toAs : false; | ||
const prefixRight = | ||
join.as && join.on.right.table ? join.on.right.table !== join.as : true; | ||
join.as && join.on.right.table ? join.on.right.table !== toAs : true; | ||
const joinQuery = makeFilterConditionModule.makeFilterCondition( | ||
@@ -105,3 +97,7 @@ join.on, | ||
} | ||
lookupPipeline.push({$match: {$expr: joinQuery}}); | ||
if (joinHints.includes('optimize')) { | ||
lookupPipeline.unshift({$match: {$expr: joinQuery}}); | ||
} else { | ||
lookupPipeline.push({$match: {$expr: joinQuery}}); | ||
} | ||
@@ -116,4 +112,4 @@ pipeline.push({ | ||
}); | ||
if (joinHint) { | ||
if (joinHint === 'first') { | ||
if (joinHints && joinHints.length > 0) { | ||
if (joinHints.includes('first')) { | ||
pipeline.push({ | ||
@@ -124,3 +120,3 @@ $set: { | ||
}); | ||
} else if (joinHint === 'last') { | ||
} else if (joinHints.includes('last')) { | ||
pipeline.push({ | ||
@@ -131,3 +127,3 @@ $set: { | ||
}); | ||
} else if (joinHint === 'unwind') { | ||
} else if (joinHints.includes('unwind')) { | ||
pipeline.push({ | ||
@@ -142,3 +138,9 @@ $unwind: { | ||
if (join.join === 'INNER JOIN') { | ||
if (joinHint) { | ||
if ( | ||
joinHints && | ||
joinHints.length > 0 && | ||
(joinHints.includes('first') || | ||
joinHints.includes('last') || | ||
joinHints.includes('unwind')) | ||
) { | ||
pipeline.push({$match: {[toAs || toTable]: {$ne: null}}}); | ||
@@ -166,6 +168,6 @@ } else { | ||
* @param {string} toAs | ||
* @param {string} joinHint | ||
* @param {string[]} joinHints | ||
* @returns {void} | ||
*/ | ||
function tableJoin(join, previousJoin, pipeline, toTable, toAs, joinHint) { | ||
function tableJoin(join, previousJoin, pipeline, toTable, toAs, joinHints) { | ||
let localPart; | ||
@@ -199,4 +201,4 @@ let fromPart; | ||
}); | ||
if (joinHint) { | ||
if (joinHint === 'first') { | ||
if (joinHints && joinHints.length > 0) { | ||
if (joinHints.includes('first')) { | ||
pipeline.push({ | ||
@@ -207,3 +209,3 @@ $set: { | ||
}); | ||
} else if (joinHint === 'last') { | ||
} else if (joinHints.includes('last')) { | ||
pipeline.push({ | ||
@@ -214,3 +216,3 @@ $set: { | ||
}); | ||
} else if (joinHint === 'unwind') { | ||
} else if (joinHints.includes('unwind')) { | ||
pipeline.push({ | ||
@@ -225,3 +227,9 @@ $unwind: { | ||
if (join.join === 'INNER JOIN') { | ||
if (joinHint) { | ||
if ( | ||
joinHints && | ||
joinHints.length > 0 && | ||
(joinHints.includes('first') || | ||
joinHints.includes('last') || | ||
joinHints.includes('unwind')) | ||
) { | ||
pipeline.push({$match: {[toAs || toTable]: {$ne: null}}}); | ||
@@ -228,0 +236,0 @@ } else { |
{ | ||
"name": "@synatic/sql-to-mongo", | ||
"version": "1.1.9", | ||
"version": "1.1.10", | ||
"description": "Convert SQL to mongo queries or aggregates", | ||
@@ -5,0 +5,0 @@ "main": "index.js", |
4500
0.02%211860
-0.16%