diff --git a/lib/query/lib/createGraph.js b/lib/query/lib/createGraph.js index 2611c238..80dd0090 100755 --- a/lib/query/lib/createGraph.js +++ b/lib/query/lib/createGraph.js @@ -24,6 +24,8 @@ export function createNodes(root) { return; } + root.body = sortFieldNames(root.body); + _.each(root.body, (body, fieldName) => { if (!body) { return; @@ -83,6 +85,23 @@ export function createNodes(root) { } } +function sortFieldNames(body) { + const bodyCompareFunction = function (key, value) { + // push fields first + if (!_.isObject(value)) { + return 1; + } + return 2; + }; + const keys = _.keys(body); + const sortedKeys = _.sortBy(keys, function (key) { + return bodyCompareFunction(key, body[key]) + }); + return _.object(sortedKeys, _.map(sortedKeys, function (key) { + return body[key]; + })); +} + function isProjectionOperatorExpression(body) { if (_.isObject(body)) { const keys = _.keys(body); diff --git a/lib/query/testing/server.test.js b/lib/query/testing/server.test.js index 5a6c3846..e595ed39 100755 --- a/lib/query/testing/server.test.js +++ b/lib/query/testing/server.test.js @@ -946,7 +946,36 @@ describe("Hypernova", function() { const [cart] = data; assert.isUndefined(cart.user); }); + it("Should not remove link storage fields if they are asked in the query", () => { + ShoppingCart.remove({}); + const cartId = ShoppingCart.insert({ + date: new Date(), + items: [{title: "Something"}] + }); + + Clients.remove({}); + Clients.insert({ + name: "John", + shoppingCartData: { + prime: 1, + _id: cartId + } + }); + const data = Clients.createQuery({ + shoppingCart: { + date: 1 + }, + shoppingCartData: 1 + }).fetch(); + assert.equal(data.length, 1); + const [cart] = data; + assert.isObject(cart.shoppingCart); + // shoppingCartData should be here + assert.isObject(cart.shoppingCartData); + assert.equal(cart.shoppingCartData.prime, 1); + assert.equal(cart.shoppingCartData._id, cartId); + }); it("Should remove link storage inversedBy meta unique fields", () => { ShoppingCart.remove({}); const cartId = ShoppingCart.insert({