diff --git a/sof-js/src/index.js b/sof-js/src/index.js index 0265dac..4269ede 100644 --- a/sof-js/src/index.js +++ b/sof-js/src/index.js @@ -15,13 +15,17 @@ export function merge(a,b) { } export function row_product(parts) { - if(parts.length == 1) {return parts[0]} + if (parts.length == 1) { + return parts[0]; + } + let rows = [{}]; let new_rows = null; - parts.forEach((partial_rows) => { + + parts.forEach(partial_rows => { new_rows = []; partial_rows.forEach((partial_row)=> { - rows.forEach((row)=> { + rows.forEach(row => { new_rows.push(merge(partial_row,row)) }) }) @@ -129,10 +133,6 @@ function select(select_expr, node, def) { ) } -function compile(def) { - throw new Error('not impl'); -} - // * foreach / column / [select(..)] -> foreach select[column, ..] // * foreach / union / [select(..)] -> foreach select[union, ..] // * foreach / select(..) -> foreach select[..] @@ -244,18 +244,15 @@ function collect_columns(acc, def){ return def.select.reduce((acc, s)=> { return collect_columns(acc, s); }, acc) - break; case 'unionAll': return def.unionAll.reduce((acc, s)=> { return collect_columns(acc, s); }, acc) - break; case 'column': return def.column.reduce((acc, c)=> { acc.push(c.name || c.path) return acc }, acc) - break; default: return acc; } diff --git a/sof-js/tests/combination.test.js b/sof-js/tests/combination.test.js index 98c57a2..91630b7 100644 --- a/sof-js/tests/combination.test.js +++ b/sof-js/tests/combination.test.js @@ -13,31 +13,112 @@ start_case('Combinations', 'TBD', resources) describe("combinations", () => { add_test({ - title: 'select & column', + title: 'select', view: { resource: 'Patient', - status: 'active', - select: [{column: [{path: 'id', name: 'id'}]}] + select: [{ + select: [{column: [{path: 'id', name: 'id'}]}] + }] }, expect: [{id: 'pt1'}, {id: 'pt2'}, {id: 'pt3'}] }); add_test({ - title: 'top level column', + title: 'column + select', view: { resource: 'Patient', - status: 'active', - select: [{column: [{path: 'id', name: 'id'}]}] + select: [{ + column: [{path: 'id', name: 'column_id'}], + select: [ + { + column: [{path: 'id', name: 'select_id'}] + } + ] + }] }, - expect: [{id: 'pt1'}, {id: 'pt2'}, {id: 'pt3'}] + expect: [ + { column_id: "pt1", select_id: "pt1" }, + { column_id: "pt1", select_id: "pt2" }, + { column_id: "pt1", select_id: "pt3" }, + { column_id: "pt2", select_id: "pt1" }, + { column_id: "pt2", select_id: "pt2" }, + { column_id: "pt2", select_id: "pt3" }, + { column_id: "pt3", select_id: "pt1" }, + { column_id: "pt3", select_id: "pt2" }, + { column_id: "pt3", select_id: "pt3" }, + ] + }); + + add_test({ + title: 'sibling select', + view: { + resource: 'Patient', + select: [ + { column: [{path: 'id', name: 'id_1'}] }, + { column: [{path: 'id', name: 'id_2'}] } + ] + }, + expect: [ + { id_1: "pt1", id_2: "pt1" }, + { id_1: "pt1", id_2: "pt2" }, + { id_1: "pt1", id_2: "pt3" }, + { id_1: "pt2", id_2: "pt1" }, + { id_1: "pt2", id_2: "pt2" }, + { id_1: "pt2", id_2: "pt3" }, + { id_1: "pt3", id_2: "pt1" }, + { id_1: "pt3", id_2: "pt2" }, + { id_1: "pt3", id_2: "pt3" }, + ] + }); + + add_test({ + title: 'sibling select inside a select', + view: { + resource: 'Patient', + select: [{ + select: [ + { column: [{path: 'id', name: 'id_1'}] }, + { column: [{path: 'id', name: 'id_2'}] } + ] + }] + }, + expect: [ + { id_1: "pt1", id_2: "pt1" }, + { id_1: "pt1", id_2: "pt2" }, + { id_1: "pt1", id_2: "pt3" }, + { id_1: "pt2", id_2: "pt1" }, + { id_1: "pt2", id_2: "pt2" }, + { id_1: "pt2", id_2: "pt3" }, + { id_1: "pt3", id_2: "pt1" }, + { id_1: "pt3", id_2: "pt2" }, + { id_1: "pt3", id_2: "pt3" }, + ] }); + add_test({ + title: 'column + select, with where', + view: { + resource: 'Patient', + select: [{ + column: [{path: 'id', name: 'column_id'}], + select: [ + { column: [{path: 'id', name: 'select_id'}] } + ] + }], + where: [{ path: "id = 'pt1'" }] + }, + expect: [ + { column_id: "pt1", select_id: "pt1" } + ] + }); add_test({ - title: 'select & select & column', + title: 'unionAll + forEach + column + select', view: { resource: 'Patient', - select: [{select: [{column: [{path: 'id', name: 'id'}]}]}] + select: [{ + select: [{column: [{path: 'id', name: 'id'}]}] + }] }, expect: [{id: 'pt1'}, {id: 'pt2'}, {id: 'pt3'}] });