MeteorCommunity/discussions

Provide a way to move documents around in Minimongo

mitar opened this issue · 0 comments

Sent also to the meteor-core.

I am trying to use local collections as a cache around an ordered query (the idea is that while query is read only, cache is not). But I would like that native order of the local collections matches that of ordered query.

It seems there is no way to move documents in the local collection. Any internal functions which might do the trick would be great. The important part is that somebody using observeChanges and addedBefore/movedBefore on the local collection should get notified as documents move around as well.

documents.find({}, {sort: <sort>, limit: <limit>}).observeChanges({
  addedBefore: function (id, fields, before) {
    if (before === null) {
      localCollection.insert(_.extend({_id: id}, fields}));
    }
    else {
      // What to do here?
    }
  },
  changed: function (id, fields) {
    var modifier = {
      $set: {},
      $unset: {}
    };
    var key, value;

    for (key in fields) {
      value = fields[key];
      if (_.isUndefined(value)) {
        modifier.$unset[key] = '';
      }
      else {
        modifier.$set[key] = value;
      }
    }

    localCollection.update(id, modifier);
  },
  movedBefore: function (id, before) {
    // What to do here?
  }
  removed: function (id) {
    localCollection.remove(id);
  }
})

This would allow one to create a query against a read-only database, but allow local modifications (like reordering of the data which is displayed to the user, of adding flags, like "read" or "seen"). It is useful to have this so that one can then render things directly.