mirror of
https://github.com/vale981/grapher
synced 2025-03-04 17:11:38 -05:00
Handling empty inversedBy unique links, fix for #315
This commit is contained in:
parent
7635337c37
commit
3344f6d5f7
2 changed files with 116 additions and 4 deletions
|
@ -96,9 +96,18 @@ export function removeLinkStorages(node, sameLevelResults) {
|
||||||
|
|
||||||
_.each(node.collectionNodes, collectionNode => {
|
_.each(node.collectionNodes, collectionNode => {
|
||||||
const removeStorageField = collectionNode.shouldCleanStorage;
|
const removeStorageField = collectionNode.shouldCleanStorage;
|
||||||
|
|
||||||
_.each(sameLevelResults, result => {
|
_.each(sameLevelResults, result => {
|
||||||
if (removeStorageField) {
|
if (removeStorageField) {
|
||||||
delete result[collectionNode.linkStorageField];
|
if (collectionNode.isVirtual) {
|
||||||
|
const childResults = getResultsArray(result[collectionNode.linkName]);
|
||||||
|
_.each(childResults, childResult => {
|
||||||
|
delete childResult[collectionNode.linkStorageField];
|
||||||
|
});
|
||||||
|
}
|
||||||
|
else {
|
||||||
|
delete result[collectionNode.linkStorageField];
|
||||||
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
removeLinkStorages(collectionNode, result[collectionNode.linkName]);
|
removeLinkStorages(collectionNode, result[collectionNode.linkName]);
|
||||||
|
@ -173,12 +182,13 @@ export function assembleMetadata(node, parentResults) {
|
||||||
const childResult = parentResult[node.linkName];
|
const childResult = parentResult[node.linkName];
|
||||||
|
|
||||||
if (node.isOneResult) {
|
if (node.isOneResult) {
|
||||||
const storage = childResult[node.linkStorageField];
|
if (_.isObject(childResult)) {
|
||||||
storeMetadata(childResult, parentResult, storage, true);
|
const storage = childResult[node.linkStorageField];
|
||||||
|
storeMetadata(childResult, parentResult, storage, true);
|
||||||
|
}
|
||||||
} else {
|
} else {
|
||||||
_.each(childResult, object => {
|
_.each(childResult, object => {
|
||||||
const storage = object[node.linkStorageField];
|
const storage = object[node.linkStorageField];
|
||||||
|
|
||||||
storeMetadata(object, parentResult, storage, true);
|
storeMetadata(object, parentResult, storage, true);
|
||||||
});
|
});
|
||||||
}
|
}
|
||||||
|
|
|
@ -14,6 +14,35 @@ const ShoppingCart = new Mongo.Collection('__projection_operators_cart');
|
||||||
const Clients = new Mongo.Collection('__text_search_clients');
|
const Clients = new Mongo.Collection('__text_search_clients');
|
||||||
Clients._ensureIndex({name: 'text'});
|
Clients._ensureIndex({name: 'text'});
|
||||||
|
|
||||||
|
Clients.addLinks({
|
||||||
|
shoppingCart: {
|
||||||
|
type: 'one',
|
||||||
|
collection: ShoppingCart,
|
||||||
|
metadata: true,
|
||||||
|
field: 'shoppingCartData',
|
||||||
|
unique: true
|
||||||
|
},
|
||||||
|
|
||||||
|
shoppingCarts: {
|
||||||
|
collection: ShoppingCart,
|
||||||
|
type: 'many',
|
||||||
|
metadata: true,
|
||||||
|
field: 'shoppingCartsData',
|
||||||
|
}
|
||||||
|
});
|
||||||
|
|
||||||
|
ShoppingCart.addLinks({
|
||||||
|
user: {
|
||||||
|
collection: Clients,
|
||||||
|
inversedBy: 'shoppingCart'
|
||||||
|
},
|
||||||
|
|
||||||
|
users: {
|
||||||
|
collection: Clients,
|
||||||
|
inversedBy: 'shoppingCarts'
|
||||||
|
}
|
||||||
|
});
|
||||||
|
|
||||||
describe('Hypernova', function() {
|
describe('Hypernova', function() {
|
||||||
it('Should support projection operators', () => {
|
it('Should support projection operators', () => {
|
||||||
ShoppingCart.remove({});
|
ShoppingCart.remove({});
|
||||||
|
@ -310,6 +339,8 @@ describe('Hypernova', function() {
|
||||||
},
|
},
|
||||||
}).fetch();
|
}).fetch();
|
||||||
|
|
||||||
|
// console.log(JSON.stringify(data, null, 2));
|
||||||
|
|
||||||
_.each(data, post => {
|
_.each(data, post => {
|
||||||
assert.isObject(post.author);
|
assert.isObject(post.author);
|
||||||
assert.isArray(post.author.groups);
|
assert.isArray(post.author.groups);
|
||||||
|
@ -719,4 +750,75 @@ describe('Hypernova', function() {
|
||||||
});
|
});
|
||||||
});
|
});
|
||||||
});
|
});
|
||||||
|
|
||||||
|
it('Should handle empty inversedBy meta unique fields', () => {
|
||||||
|
ShoppingCart.remove({});
|
||||||
|
ShoppingCart.insert({date: new Date(), items: [{title: 'Something'}]});
|
||||||
|
|
||||||
|
const data = ShoppingCart.createQuery({
|
||||||
|
user: {
|
||||||
|
name: 1
|
||||||
|
}
|
||||||
|
}).fetch();
|
||||||
|
|
||||||
|
assert.equal(data.length, 1);
|
||||||
|
const [cart] = data;
|
||||||
|
assert.isUndefined(cart.user);
|
||||||
|
});
|
||||||
|
|
||||||
|
it('Should remove link storage inversedBy meta unique fields', () => {
|
||||||
|
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 = ShoppingCart.createQuery({
|
||||||
|
user: {
|
||||||
|
name: 1
|
||||||
|
}
|
||||||
|
}).fetch();
|
||||||
|
|
||||||
|
assert.equal(data.length, 1);
|
||||||
|
const [cart] = data;
|
||||||
|
assert.isObject(cart.user);
|
||||||
|
assert.isString(cart.user.name);
|
||||||
|
// no link storage
|
||||||
|
assert.isUndefined(cart.user.shoppingCartData);
|
||||||
|
});
|
||||||
|
|
||||||
|
it('Should remove link storage inversedBy meta many fields', () => {
|
||||||
|
ShoppingCart.remove({});
|
||||||
|
const cartId = ShoppingCart.insert({date: new Date(), items: [{title: 'Something'}]});
|
||||||
|
|
||||||
|
Clients.remove({});
|
||||||
|
Clients.insert({
|
||||||
|
name: 'John',
|
||||||
|
shoppingCartsData: [{
|
||||||
|
prime: 1,
|
||||||
|
_id: cartId
|
||||||
|
}]
|
||||||
|
})
|
||||||
|
|
||||||
|
const data = ShoppingCart.createQuery({
|
||||||
|
users: {
|
||||||
|
name: 1
|
||||||
|
}
|
||||||
|
}).fetch();
|
||||||
|
|
||||||
|
assert.equal(data.length, 1);
|
||||||
|
const [cart] = data;
|
||||||
|
assert.isArray(cart.users);
|
||||||
|
assert.equal(cart.users.length, 1);
|
||||||
|
const [user] = cart.users;
|
||||||
|
assert.isString(user.name);
|
||||||
|
// no link storage
|
||||||
|
assert.isUndefined(user.shoppingCartsData);
|
||||||
|
});
|
||||||
});
|
});
|
||||||
|
|
Loading…
Add table
Reference in a new issue