@synatic/noql
Version:
Convert SQL statements to mongo queries or aggregates
58 lines (53 loc) • 1.9 kB
JavaScript
const makeFilterConditionModule = require('./makeFilterCondition');
const makeProjectionExpressionPartModule = require('./makeProjectionExpressionPart');
exports.makeCaseCondition = makeCaseCondition;
/**
* Makes a $cond from a case statement
* @param {import('../types').Expression} expr - the expression object to turn into a case
* @param {import('../types').NoqlContext} context - The Noql context to use when generating the output
* @returns {any}
*/
function makeCaseCondition(expr, context) {
if (expr.type !== 'case') {
throw new Error(`Expression is not case`);
}
const elseExpr = expr.args.find((a) => a.type === 'else');
const whens = expr.args.filter((a) => a.type === 'when');
return {
$switch: {
branches: whens.map((w) => {
const caseCondition =
makeFilterConditionModule.makeFilterCondition(
w.cond,
context,
false,
false,
null,
false,
true,
undefined,
true
);
const thenCondition =
makeFilterConditionModule.makeFilterCondition(
w.result,
context,
false,
false,
null,
false,
true
);
return {
case: caseCondition,
then: thenCondition,
};
}),
default:
makeProjectionExpressionPartModule.makeProjectionExpressionPart(
elseExpr.result,
context
),
},
};
}