Git Product home page Git Product logo

Comments (2)

lfades avatar lfades commented on July 28, 2024

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.

palpinter avatar palpinter commented on July 28, 2024

Thank you very much!
You're a great man!

P.

from cottz-publish-relations.

Related Issues (16)

Recommend Projects

  • React photo React

    A declarative, efficient, and flexible JavaScript library for building user interfaces.

  • Vue.js photo Vue.js

    🖖 Vue.js is a progressive, incrementally-adoptable JavaScript framework for building UI on the web.

  • Typescript photo Typescript

    TypeScript is a superset of JavaScript that compiles to clean JavaScript output.

  • TensorFlow photo TensorFlow

    An Open Source Machine Learning Framework for Everyone

  • Django photo Django

    The Web framework for perfectionists with deadlines.

  • D3 photo 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.

  • Game

    Some thing interesting about game, make everyone happy.

Recommend Org

  • Facebook photo Facebook

    We are working to build community through open source technology. NB: members must have two-factor auth.

  • Microsoft photo Microsoft

    Open source projects and samples from Microsoft.

  • Google photo Google

    Google ❤️ Open Source for everyone.

  • D3 photo D3

    Data-Driven Documents codes.