Skip to content

Commit

Permalink
added denormalize, updated tests
Browse files Browse the repository at this point in the history
  • Loading branch information
Bero committed Aug 10, 2024
1 parent c79edca commit 04a497e
Show file tree
Hide file tree
Showing 10 changed files with 130 additions and 141 deletions.
2 changes: 1 addition & 1 deletion lib/namedQuery/testing/client.test.js
Original file line number Diff line number Diff line change
Expand Up @@ -11,7 +11,7 @@ import Users from '../../query/testing/bootstrap/users/collection';
import { _ } from 'meteor/underscore';

describe('Named Query', function () {
it('Should return proper values', function (done) {
it.only('Should return proper values', function (done) {
const query = createQuery({
postListExposure: {
title: 'User Post - 3',
Expand Down
25 changes: 16 additions & 9 deletions lib/query/query.server.js
Original file line number Diff line number Diff line change
Expand Up @@ -16,12 +16,9 @@ export default class Query extends Base {
* @returns {*}
*/
fetch(context = {}) {
const node = createGraph(
this.collection,
prepareForProcess(this.body, this.params),
throw new Error(
'You must call fetchAsync instead of fetch from server-side',
);

return hypernova(node, context.userId, { params: this.params });
}

/**
Expand All @@ -42,9 +39,9 @@ export default class Query extends Base {
* @returns {*}
*/
fetchOne(context = {}) {
context.$options = context.$options || {};
context.$options.limit = 1;
return _.first(this.fetch(context));
throw new Error(
'You must call fetchOneAsync instead of fetchOne from server-side',
);
}

/**
Expand All @@ -63,6 +60,16 @@ export default class Query extends Base {
* @returns {number}
*/
getCount() {
return this.collection.find(this.body.$filters || {}, {}).count();
throw new Error(
'You must call countAsync instead of count from server-side',
);
}

/**
* Gets the count of matching elements.
* @returns {Promise<number>}
*/
getCountAsync() {
return this.collection.find(this.body.$filters || {}, {}).countAsync();
}
}
34 changes: 16 additions & 18 deletions lib/query/testing/bootstrap/posts/links.js
Original file line number Diff line number Diff line change
Expand Up @@ -31,29 +31,27 @@ Posts.addLinks({
type: 'one',
collection: Authors,
field: 'ownerId',
// TODO(v3): denormalize
// denormalize: {
// field: 'authorCache',
// body: {
// name: 1,
// profile: {
// firstName: 1,
// lastName: 1,
// }
// }
// }
denormalize: {
field: 'authorCache',
body: {
name: 1,
profile: {
firstName: 1,
lastName: 1,
},
},
},
},
tagsCached: {
collection: Tags,
type: 'many',
field: 'tagIds',
// TODO(v3): denormalize
// denormalize: {
// field: 'tagsCache',
// body: {
// name: 1
// }
// }
denormalize: {
field: 'tagsCache',
body: {
name: 1,
},
},
},
});

Expand Down
23 changes: 11 additions & 12 deletions lib/query/testing/bootstrap/users/links.js
Original file line number Diff line number Diff line change
Expand Up @@ -11,16 +11,15 @@ Users.addLinks({
field: 'subordinateIds',
type: 'many',
},
// TODO(v3): denormalize not working
// friendsCached: {
// collection: Users,
// field: 'friendIds',
// type: 'many',
// denormalize: {
// field: 'friendsCache',
// body: {
// name: 1,
// },
// },
// },
friendsCached: {
collection: Users,
field: 'friendIds',
type: 'many',
denormalize: {
field: 'friendsCache',
body: {
name: 1,
},
},
},
});
3 changes: 1 addition & 2 deletions lib/query/testing/client.test.js
Original file line number Diff line number Diff line change
Expand Up @@ -270,8 +270,7 @@ describe('Query Client Tests', function () {
assert.isFunction(clone.setParams({}).fetchOne);
});

// TODO(v3): denormalize
it.skip('Should work with denormalized fields in the many links', async () => {
it('Should work with denormalized fields in the many links', async () => {
const query = createQuery({
users: {
$filters: {
Expand Down
106 changes: 49 additions & 57 deletions lib/query/testing/link-cache/collections.js
Original file line number Diff line number Diff line change
Expand Up @@ -15,67 +15,62 @@ Posts.addLinks({
type: 'one',
collection: Authors,
field: 'authorId',
// TODO(v3): denormalize
// denormalize: {
// field: 'authorCache',
// body: {
// name: 1,
// address: 1,
// },
// },
denormalize: {
field: 'authorCache',
body: {
name: 1,
address: 1,
},
},
},
categories: {
type: 'many',
metadata: true,
collection: Categories,
field: 'categoryIds',
// TODO(v3): denormalize
// denormalize: {
// field: 'categoriesCache',
// body: {
// name: 1,
// },
// },
denormalize: {
field: 'categoriesCache',
body: {
name: 1,
},
},
},
});

Authors.addLinks({
posts: {
collection: Posts,
inversedBy: 'author',
// TODO(v3): denormalize
// denormalize: {
// field: 'postCache',
// body: {
// title: 1,
// },
// },
denormalize: {
field: 'postCache',
body: {
title: 1,
},
},
},
groups: {
type: 'many',
collection: Groups,
field: 'groupIds',
// TODO(v3): denormalize
// denormalize: {
// field: 'groupsCache',
// body: {
// name: 1,
// },
// },
denormalize: {
field: 'groupsCache',
body: {
name: 1,
},
},
},
profile: {
type: 'one',
metadata: true,
collection: AuthorProfiles,
field: 'profileId',
unique: true,
// TODO(v3): denormalize
// denormalize: {
// field: 'profileCache',
// body: {
// name: 1,
// },
// },
denormalize: {
field: 'profileCache',
body: {
name: 1,
},
},
},
});

Expand All @@ -84,40 +79,37 @@ AuthorProfiles.addLinks({
collection: Authors,
inversedBy: 'profile',
unique: true,
// TODO(v3): denormalize
// denormalize: {
// field: 'authorCache',
// body: {
// name: 1,
// },
// },
denormalize: {
field: 'authorCache',
body: {
name: 1,
},
},
},
});

Groups.addLinks({
authors: {
collection: Authors,
inversedBy: 'groups',
// TODO(v3): denormalize
// denormalize: {
// field: 'authorsCache',
// body: {
// name: 1,
// },
// },
denormalize: {
field: 'authorsCache',
body: {
name: 1,
},
},
},
});

Categories.addLinks({
posts: {
collection: Posts,
inversedBy: 'categories',
// TODO(v3): denormalize
// denormalize: {
// field: 'postsCache',
// body: {
// title: 1,
// },
// },
denormalize: {
field: 'postsCache',
body: {
title: 1,
},
},
},
});
Loading

0 comments on commit 04a497e

Please sign in to comment.