mirror of
https://github.com/vale981/grapher
synced 2025-03-06 01:51:38 -05:00
commit
824047824d
5 changed files with 108 additions and 13 deletions
0
lib/namedQuery/namedQuery.base.js
Normal file → Executable file
0
lib/namedQuery/namedQuery.base.js
Normal file → Executable file
63
lib/query/lib/prepareForDelivery.js
Normal file → Executable file
63
lib/query/lib/prepareForDelivery.js
Normal file → Executable file
|
@ -9,6 +9,7 @@ import {Minimongo} from 'meteor/minimongo';
|
||||||
|
|
||||||
export default (node, params) => {
|
export default (node, params) => {
|
||||||
snapBackCaches(node);
|
snapBackCaches(node);
|
||||||
|
storeOneResults(node, node.results);
|
||||||
applyReducers(node, params);
|
applyReducers(node, params);
|
||||||
cleanReducerLeftovers(node);
|
cleanReducerLeftovers(node);
|
||||||
|
|
||||||
|
@ -21,7 +22,6 @@ export default (node, params) => {
|
||||||
});
|
});
|
||||||
|
|
||||||
removeLinkStorages(node, node.results);
|
removeLinkStorages(node, node.results);
|
||||||
storeOneResults(node, node.results);
|
|
||||||
applyPostFilters(node);
|
applyPostFilters(node);
|
||||||
applyPostOptions(node);
|
applyPostOptions(node);
|
||||||
applyPostFilter(node, params);
|
applyPostFilter(node, params);
|
||||||
|
@ -67,11 +67,31 @@ function applyPostFilter(node, params) {
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
|
/**
|
||||||
|
*
|
||||||
|
* Helper function which transforms results into the array.
|
||||||
|
* Results are an object for 'one' links.
|
||||||
|
*
|
||||||
|
* @param results
|
||||||
|
* @return array
|
||||||
|
*/
|
||||||
|
export function getResultsArray(results) {
|
||||||
|
if (_.isArray(results)) {
|
||||||
|
return results;
|
||||||
|
}
|
||||||
|
else if (_.isUndefined(results)) {
|
||||||
|
return [];
|
||||||
|
}
|
||||||
|
return [results];
|
||||||
|
}
|
||||||
|
|
||||||
export function removeLinkStorages(node, sameLevelResults) {
|
export function removeLinkStorages(node, sameLevelResults) {
|
||||||
if (!sameLevelResults) {
|
if (!sameLevelResults) {
|
||||||
return;
|
return;
|
||||||
}
|
}
|
||||||
|
|
||||||
|
sameLevelResults = getResultsArray(sameLevelResults);
|
||||||
|
|
||||||
_.each(node.collectionNodes, collectionNode => {
|
_.each(node.collectionNodes, collectionNode => {
|
||||||
const removeStorageField = collectionNode.shouldCleanStorage;
|
const removeStorageField = collectionNode.shouldCleanStorage;
|
||||||
_.each(sameLevelResults, result => {
|
_.each(sameLevelResults, result => {
|
||||||
|
@ -114,11 +134,19 @@ function cloneMetaChildren(node, parentResults) {
|
||||||
const linkName = node.linkName;
|
const linkName = node.linkName;
|
||||||
const isMeta = node.isMeta;
|
const isMeta = node.isMeta;
|
||||||
|
|
||||||
|
// parentResults might be an object (for type==one links)
|
||||||
|
parentResults = getResultsArray(parentResults);
|
||||||
|
|
||||||
parentResults.forEach(parentResult => {
|
parentResults.forEach(parentResult => {
|
||||||
if (isMeta && parentResult[linkName]) {
|
if (isMeta && parentResult[linkName]) {
|
||||||
parentResult[linkName] = parentResult[linkName].map(object => {
|
if (node.isOneResult) {
|
||||||
return Object.assign({}, object);
|
parentResult[linkName] = Object.assign({}, parentResult[linkName]);
|
||||||
});
|
}
|
||||||
|
else {
|
||||||
|
parentResult[linkName] = parentResult[linkName].map(object => {
|
||||||
|
return Object.assign({}, object);
|
||||||
|
});
|
||||||
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
node.collectionNodes.forEach(collectionNode => {
|
node.collectionNodes.forEach(collectionNode => {
|
||||||
|
@ -128,6 +156,8 @@ function cloneMetaChildren(node, parentResults) {
|
||||||
}
|
}
|
||||||
|
|
||||||
export function assembleMetadata(node, parentResults) {
|
export function assembleMetadata(node, parentResults) {
|
||||||
|
parentResults = getResultsArray(parentResults);
|
||||||
|
|
||||||
// assembling metadata is depth first
|
// assembling metadata is depth first
|
||||||
node.collectionNodes.forEach(collectionNode => {
|
node.collectionNodes.forEach(collectionNode => {
|
||||||
_.each(parentResults, result => {
|
_.each(parentResults, result => {
|
||||||
|
@ -140,20 +170,31 @@ export function assembleMetadata(node, parentResults) {
|
||||||
_.each(parentResults, parentResult => {
|
_.each(parentResults, parentResult => {
|
||||||
const childResult = parentResult[node.linkName];
|
const childResult = parentResult[node.linkName];
|
||||||
|
|
||||||
_.each(childResult, object => {
|
if (node.isOneResult) {
|
||||||
const storage = object[node.linkStorageField];
|
const storage = childResult[node.linkStorageField];
|
||||||
|
storeMetadata(childResult, parentResult, storage, true);
|
||||||
|
} else {
|
||||||
|
_.each(childResult, object => {
|
||||||
|
const storage = object[node.linkStorageField];
|
||||||
|
|
||||||
storeMetadata(object, parentResult, storage, true);
|
storeMetadata(object, parentResult, storage, true);
|
||||||
});
|
});
|
||||||
|
}
|
||||||
})
|
})
|
||||||
} else {
|
} else {
|
||||||
_.each(parentResults, parentResult => {
|
_.each(parentResults, parentResult => {
|
||||||
const childResult = parentResult[node.linkName];
|
const childResult = parentResult[node.linkName];
|
||||||
const storage = parentResult[node.linkStorageField];
|
const storage = parentResult[node.linkStorageField];
|
||||||
|
|
||||||
_.each(childResult, object => {
|
if (node.isOneResult) {
|
||||||
storeMetadata(object, parentResult, storage, false);
|
if (childResult) {
|
||||||
});
|
storeMetadata(childResult, parentResult, storage, false);
|
||||||
|
}
|
||||||
|
} else {
|
||||||
|
_.each(childResult, object => {
|
||||||
|
storeMetadata(object, parentResult, storage, false);
|
||||||
|
});
|
||||||
|
}
|
||||||
})
|
})
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
13
lib/query/testing/bootstrap/comments/links.js
Normal file → Executable file
13
lib/query/testing/bootstrap/comments/links.js
Normal file → Executable file
|
@ -17,3 +17,16 @@ Comments.addLinks({
|
||||||
index: true
|
index: true
|
||||||
}
|
}
|
||||||
});
|
});
|
||||||
|
|
||||||
|
Comments.addReducers({
|
||||||
|
authorLinkReducer: {
|
||||||
|
body: {
|
||||||
|
author: {
|
||||||
|
name: 1,
|
||||||
|
},
|
||||||
|
},
|
||||||
|
reduce(object) {
|
||||||
|
return object.author;
|
||||||
|
}
|
||||||
|
}
|
||||||
|
});
|
||||||
|
|
18
lib/query/testing/reducers.server.test.js
Normal file → Executable file
18
lib/query/testing/reducers.server.test.js
Normal file → Executable file
|
@ -1,4 +1,6 @@
|
||||||
import { createQuery } from 'meteor/cultofcoders:grapher';
|
import { createQuery } from 'meteor/cultofcoders:grapher';
|
||||||
|
import Authors from './bootstrap/authors/collection';
|
||||||
|
import Comments from './bootstrap/comments/collection';
|
||||||
|
|
||||||
describe('Reducers', function () {
|
describe('Reducers', function () {
|
||||||
it('Should work with field only reducers', function () {
|
it('Should work with field only reducers', function () {
|
||||||
|
@ -71,6 +73,22 @@ describe('Reducers', function () {
|
||||||
})
|
})
|
||||||
});
|
});
|
||||||
|
|
||||||
|
it('Should work with One link reducers', function () {
|
||||||
|
const sampleComment = Comments.findOne();
|
||||||
|
|
||||||
|
const comment = createQuery({
|
||||||
|
comments: {
|
||||||
|
$filters: {
|
||||||
|
_id: sampleComment._id,
|
||||||
|
},
|
||||||
|
authorLinkReducer: 1,
|
||||||
|
}
|
||||||
|
}).fetchOne();
|
||||||
|
|
||||||
|
assert.isObject(comment);
|
||||||
|
assert.isObject(comment.authorLinkReducer);
|
||||||
|
});
|
||||||
|
|
||||||
it('Should work with links and nested reducers', function () {
|
it('Should work with links and nested reducers', function () {
|
||||||
const data = createQuery({
|
const data = createQuery({
|
||||||
authors: {
|
authors: {
|
||||||
|
|
23
lib/query/testing/server.test.js
Normal file → Executable file
23
lib/query/testing/server.test.js
Normal file → Executable file
|
@ -170,6 +170,29 @@ describe('Hypernova', function() {
|
||||||
});
|
});
|
||||||
});
|
});
|
||||||
|
|
||||||
|
it('Should fetch Many-Meta links correctly where parent is One link', function () {
|
||||||
|
const data = createQuery({
|
||||||
|
posts: {
|
||||||
|
$options: { limit: 5 },
|
||||||
|
author: {
|
||||||
|
groups: {
|
||||||
|
isAdmin: 1
|
||||||
|
}
|
||||||
|
}
|
||||||
|
}
|
||||||
|
}).fetch();
|
||||||
|
|
||||||
|
_.each(data, post => {
|
||||||
|
assert.isObject(post.author);
|
||||||
|
assert.isArray(post.author.groups);
|
||||||
|
|
||||||
|
_.each(post.author.groups, group => {
|
||||||
|
assert.isObject(group.$metadata);
|
||||||
|
assert.isBoolean(group.$metadata.isAdmin);
|
||||||
|
});
|
||||||
|
});
|
||||||
|
});
|
||||||
|
|
||||||
it('Should fetch Many-Meta inversed links correctly', function() {
|
it('Should fetch Many-Meta inversed links correctly', function() {
|
||||||
const data = createQuery({
|
const data = createQuery({
|
||||||
groups: {
|
groups: {
|
||||||
|
|
Loading…
Add table
Reference in a new issue