MCPcopy Create free account
hub / github.com/chartbrew/chartbrew / joinData

Function joinData

server/controllers/DatasetController.js:23–103  ·  view source on GitHub ↗
(joins, index, requests, data)

Source from the content-addressed store, hash-verified

21const runtimeCache = require("../modules/runtimeCache");
22
23function joinData(joins, index, requests, data) {
24 const dr = requests.find((r) => r?.dataRequest?.id === joins[index].dr_id);
25 const joinDr = requests.find((r) => r?.dataRequest?.id === joins[index].join_id);
26
27 if (!dr || !joinDr) return data;
28
29 let drData = data;
30 const joinDrData = joinDr.responseData.data;
31
32 if (!drData || !joinDrData) return data;
33
34 const drField = joins[index].dr_field;
35 const joinField = joins[index].join_field;
36
37 if (!drField || !joinField) return data;
38
39 const newData = [];
40
41 // extract the selected data from the object
42 let drSelector = drField;
43 // if (index === 0) {
44 if (drField.indexOf("root[]") > -1) {
45 drSelector = drSelector.replace("root[].", "");
46 // and data stays the same
47 drData = data;
48 } else {
49 const arrayFinder = drSelector.substring(0, drSelector.indexOf("]") - 1).replace("root.", "");
50 drSelector = drSelector.substring(drSelector.indexOf("]") + 2);
51
52 drData = _.get(data, arrayFinder);
53 }
54 // }
55
56 // extract the selected data from the second request
57 let joinSelectedData = joinDrData;
58 let joinSelector = joinField;
59 if (joinField.indexOf("root[]") > -1) {
60 joinSelector = joinSelector.replace("root[].", "");
61 joinSelectedData = joinDrData;
62 } else {
63 const arrayFinder = joinSelector.substring(0, joinSelector.indexOf("]") - 1).replace("root.", "");
64 joinSelector = joinSelector.substring(joinSelector.indexOf("]") + 2);
65
66 joinSelectedData = _.get(joinDrData, arrayFinder);
67 }
68
69 drData.forEach((drItem) => {
70 // check if the dr was a join previously
71 const existingIndex = joins.findIndex((j) => {
72 return (j.join_id === dr?.dataRequest?.id && j.dr_id !== dr?.dataRequest?.id);
73 });
74 const newObjectFields = {};
75
76 joinSelectedData.forEach((joinItem) => {
77 if (existingIndex > -1
78 && (_.get(drItem, `${joins[index - 1].alias}.${drSelector}`, drItem[joins[index - 1].alias][drSelector]) === _.get(joinItem, joinSelector, joinItem[joinSelector]))
79 ) {
80 Object.keys(joinItem).forEach((key) => {

Callers 1

runRequestMethod · 0.85

Calls 1

getMethod · 0.45

Tested by

no test coverage detected