Comments (2)
Your solution has a performance problem, if you have 10 messages you're doing 21 queries to the database (one to get the messages and two for every message added).
with this you can do the same with only 3 queries, one for each collection.
PublishRelations('MessagesList', function () {
var participantsJoin, messageItemsJoin;
participantsJoin = this.join(Meteor.users);
messageItemsJoin = this.join(Collections.MessageItems);
// participantsJoin will have the default selector: {_id: {$in: ids}}
messageItemsJoin.selector = function (ids) {
// ids is {$in: _ids}
return {messageId: _ids};
};
this.cursor(Collections.Messages.find({
'participants.id': this.userId
}, {
sort: {
createdAt: -1
}
}), function (id, doc) {
var participants;
participants = doc.participants;
// this "if" is important, check the documentation
if (participants) {
var ids;
ids = participants.map(function (participant) {
// don't use participantsJoin.push() here, add them all at the same time
return participant.id;
});
// with ecmascript 6: participantsJoin.push(...ids);
participantsJoin.push.apply(participantsJoin, ids);
}
messageItemsJoin.push(id);
});
participantsJoin.send();
messageItemsJoin.send();
return this.ready();
});
the previous publication should be much faster, if you want even more performance try using this.joinNonReactive
in the participants
or messageItems
or both to remove the reactivity in places where it is not needed. You can also try using fields
in queries to observe
only for the required fields
from cottz-publish-relations.
Thank you very much!
You're a great man!
P.
from cottz-publish-relations.
Related Issues (16)
- Package description identical to cottz-publish HOT 2
- Changing filters reactively HOT 3
- this.join unexpected behaviour HOT 7
- What are the advantages over reywood:publish-composite package HOT 5
- Add support for server minimongo HOT 3
- Is 'this.ready()' obligatory? HOT 4
- the parent doc is removed, the related children doc is not removed. HOT 5
- seems broken HOT 1
- Help Plz: Filter/remove some documents from a this.cursor() - call? HOT 4
- one to many relationship HOT 10
- Need help getting started HOT 6
- Crossbar problem! HOT 4
- How to get doc for relations fields? HOT 4
- Please example how to "return [cursor1, cursor2, cursor3];" HOT 2
- Trying this.listen example HOT 1
Recommend Projects
-
React
A declarative, efficient, and flexible JavaScript library for building user interfaces.
-
Vue.js
🖖 Vue.js is a progressive, incrementally-adoptable JavaScript framework for building UI on the web.
-
Typescript
TypeScript is a superset of JavaScript that compiles to clean JavaScript output.
-
TensorFlow
An Open Source Machine Learning Framework for Everyone
-
Django
The Web framework for perfectionists with deadlines.
-
Laravel
A PHP framework for web artisans
-
D3
Bring data to life with SVG, Canvas and HTML. 📊📈🎉
-
Recommend Topics
-
javascript
JavaScript (JS) is a lightweight interpreted programming language with first-class functions.
-
web
Some thing interesting about web. New door for the world.
-
server
A server is a program made to process requests and deliver data to clients.
-
Machine learning
Machine learning is a way of modeling and interpreting data that allows a piece of software to respond intelligently.
-
Visualization
Some thing interesting about visualization, use data art
-
Game
Some thing interesting about game, make everyone happy.
Recommend Org
-
Facebook
We are working to build community through open source technology. NB: members must have two-factor auth.
-
Microsoft
Open source projects and samples from Microsoft.
-
Google
Google ❤️ Open Source for everyone.
-
Alibaba
Alibaba Open Source for everyone
-
D3
Data-Driven Documents codes.
-
Tencent
China tencent open source team.
from cottz-publish-relations.