Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

fix issue with removeLinkStorages when specificaly querying the link field #388

Open
wants to merge 7 commits into
base: master
Choose a base branch
from
19 changes: 19 additions & 0 deletions lib/query/lib/createGraph.js
Original file line number Diff line number Diff line change
Expand Up @@ -23,6 +23,8 @@ export function createNodes(root) {
return;
}

root.body = sortFieldNames(root.body);

_.each(root.body, (body, fieldName) => {
if (!body) {
return;
Expand Down Expand Up @@ -82,6 +84,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 @@ -777,7 +777,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