Skip to content

Commit 89dd825

Browse files
committed
Updated links for deeply nested documents
1 parent 291a470 commit 89dd825

File tree

3 files changed

+36
-9
lines changed

3 files changed

+36
-9
lines changed

lib/query/hypernova/aggregateSearchFilters.js

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -9,10 +9,10 @@ function getIdsFromObject(object, field) {
99

1010
const rootValue = object[parts[0]];
1111
if (_.isArray(rootValue)) {
12-
return rootValue.map(item => dot.pick(parts.slice(1).join(','), item));
12+
return rootValue.map(item => dot.pick(parts.slice(1).join('.'), item));
1313
}
1414
else if (_.isObject(rootValue)) {
15-
return [dot.pick(parts.slice(1).join(','), rootValue)];
15+
return [dot.pick(parts.slice(1).join('.'), rootValue)];
1616
}
1717
return [];
1818
}

lib/query/hypernova/assembler.js

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -180,7 +180,8 @@ export function assembleOne(parentResult, {
180180
resultsByKeyId[value],
181181
{ limit, skip }
182182
);
183-
result[path.slice(1).join('.')] = data;
183+
dot.set(path.slice(1).join('.'), data, result);
184+
// result[path.slice(1).join('.')] = data;
184185
});
185186
return;
186187
}
@@ -206,7 +207,6 @@ export default (childCollectionNode, { limit, skip, metaFilters }) => {
206207
const linker = childCollectionNode.linker;
207208

208209
const strategy = linker.strategy;
209-
const isSingle = linker.isSingle();
210210
const isMeta = linker.isMeta();
211211
const fieldStorage = linker.linkStorageField;
212212

lib/query/lib/createGraph.js

Lines changed: 32 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -1,3 +1,4 @@
1+
import dot from 'dot-object';
12
import CollectionNode from '../nodes/collectionNode.js';
23
import FieldNode from '../nodes/fieldNode.js';
34
import ReducerNode from '../nodes/reducerNode.js';
@@ -90,6 +91,32 @@ function isProjectionOperatorExpression(body) {
9091
return false;
9192
}
9293

94+
function tryFindLink(root, dottizedPath) {
95+
// This would be the link in form of {nestedDocument: {linkedCollection: {...fields...}}}
96+
const parts = dottizedPath.split('.');
97+
const firstPart = parts.slice(0, 2);
98+
// Here we have a situation where we have link inside a nested document of a nested document
99+
// {nestedDocument: {subnestedDocument: {linkedCollection: {...fields...}}}
100+
const nestedParts = parts.slice(2);
101+
102+
const potentialLinks = nestedParts.reduce((acc, part) => {
103+
return [
104+
...acc,
105+
`${_.last(acc)}.${part}`,
106+
];
107+
}, [firstPart.join('.')]);
108+
109+
// Trying to find topmost link
110+
while (potentialLinks[0]) {
111+
const linkerKey = potentialLinks.splice(0, 1);
112+
const linker = root.collection.getLinker(linkerKey);
113+
if (linker) {
114+
return linker;
115+
}
116+
}
117+
}
118+
119+
93120
/**
94121
* @param body
95122
* @param fieldName
@@ -102,13 +129,13 @@ export function addFieldNode(body, fieldName, root) {
102129
let dotted = dotize.convert({[fieldName]: body});
103130
_.each(dotted, (value, key) => {
104131
// check for link
105-
const parts = key.split('.');
106-
const linkerKey = parts.slice(0, 2).join('.');
132+
const linker = tryFindLink(root, key);
107133

108-
const linker = root.collection.getLinker(linkerKey);
134+
if (linker && !root.hasCollectionNode(linker.linkName)) {
135+
const path = linker.linkName.split('.').slice(1).join('.');
136+
const subrootBody = dot.pick(path, body);
109137

110-
if (linker && !root.hasCollectionNode(linkerKey)) {
111-
const subroot = new CollectionNode(linker.getLinkedCollection(), body[parts[1]], linkerKey);
138+
const subroot = new CollectionNode(linker.getLinkedCollection(), subrootBody, linker.linkName);
112139
// must be before adding linker because _shouldCleanStorage method
113140
createNodes(subroot);
114141
root.add(subroot, linker);

0 commit comments

Comments
 (0)