Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Provide a way to move documents around in Minimongo #57

Open
mitar opened this issue Dec 29, 2014 · 0 comments
Open

Provide a way to move documents around in Minimongo #57

mitar opened this issue Dec 29, 2014 · 0 comments

Comments

@mitar
Copy link

mitar commented Dec 29, 2014

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.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

No branches or pull requests

1 participant