Skip to content
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
19 changes: 19 additions & 0 deletions lib/query/lib/createGraph.js
Original file line number Diff line number Diff line change
Expand Up @@ -24,6 +24,8 @@ export function createNodes(root) {
return;
}

root.body = sortFieldNames(root.body);

_.each(root.body, (body, fieldName) => {
if (!body) {
return;
Expand Down Expand Up @@ -83,6 +85,23 @@ export function createNodes(root) {
}
}

function sortFieldNames(body) {
const bodyCompareFunction = function (key, value) {
// push fields first
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Not sure if sorting is the right idea here. It may fail for nested links. I think the solution is to not mark the field as scheduled for deletion, or use it above the link.

Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Yeah, with that sorting function we are doing kind of a guesswork. Why not make this process 2-pass? First, we determine what are props, fields, reducers and links. Second, we add them. Props and fields first, then links and reducers.

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Thank you for your time reviewing
Since the fields are not yet added in _shouldCleanStorage, the hasField method cannot see that the field is supposed to be there.
We could update this line and change it with

result = !this.hasField(node.linkStorageField, true) && !(node.linkStorageField in node.parent.body)

Would that be a better solution @theodorDiaconu ?

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);
Expand Down
29 changes: 29 additions & 0 deletions lib/query/testing/server.test.js
Original file line number Diff line number Diff line change
Expand Up @@ -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({
Expand Down