Meteor Collection Transform: is it done on the server or on the client? or it depends

孤街浪徒 提交于 2019-12-18 03:19:20

问题


I want to use transform to make a "virtual field" out of a collection. However, the new field I'm adding (within the transform function) is adding quite a bit of data to the returned document.

This is fine if the transform is taking place inside the client. If it is done on server-side, then there will be bandwidth concerns.

So I'm wondering if the transform is done on the server, or on the client, or it depends on how I find/fetch the document?


回答1:


UPDATE: It's possible to do a transform on the server.

You can have a transform on the client like this:

return YourCollection.find({}, {transform: function (doc) {
   doc.test = true;
   return true;
}});

Meteor ignores transform on queries that are published (from within Meteor.publish). The client sees the document as if the transform didn't exist.

If you would like to use transforms on the server you can do this:

YourCollection = new Mongo.Collection("collection_name"); 

Meteor.publish("yourRecordSet", function() {

  //Transform function
  var transform = function(doc) {
    doc.date = new Date();
    return doc;
  }

  var self = this;

  var observer = YourCollection.find().observe({
      added: function (document) {
      self.added('collection_name', document._id, transform(document));
    },
    changed: function (newDocument, oldDocument) {
      self.changed('collection_name', newDocument._id, transform(newDocument));
    },
    removed: function (oldDocument) {
      self.removed('collection_name', oldDocument._id);
    }
  });

  self.onStop(function () {
    observer.stop();
  });

  self.ready();

});



回答2:


You can add and use the following function:

Meteor.publishWithTransform = function(publicationName, cursorGetter , transform)
{
    transform = transform || function(o){return o;};
    Meteor.publish(publicationName, function ()
    {
        var cursor = cursorGetter.apply(this, arguments);
        var collectionName = cursor._cursorDescription.collectionName;

        var self = this;

        var observer = cursor.observe({
            added: function (document) {
                self.added(collectionName, document._id, transform(document));
            },
            changed: function (newDocument, oldDocument) {
                self.changed(collectionName, newDocument._id, transform(newDocument));
            },
            removed: function (oldDocument) {
                self.removed(collectionName, oldDocument._id);
            }
        });

        self.onStop(function () {
            observer.stop();
        });

        self.ready();
    });
}

Usage (server side):

Meteor.publishWithTransform("publication-name", function () {
    aCollection.find({})
}, function (item)
{
    item.aField = "something";
    return item;
});

Drawback: if you save the published document you will save the added changes as well. Here the client has no clue the property "aField" has been added during the publication.




回答3:


You can use transform on both sides, when you specify a transform option to the collection or findOne,fetch,etc

transform Function
An optional transformation function. Documents will be passed through this function before being returned from fetch or findOne, and before being passed to callbacks of observe, allow, and deny.

If you need to get raw document from a collection with transform option.

myCollection.findOne({},{transform:null})



回答4:


You can also add transform directly in the collection definition as well. Note: As mentioned above this runs the risk of storing extra data to your collection when updating. For read only collections this isn't really an issue.

I am also expecting this to live in a shared lib folder or similar, both client and server see this definition.

var Assignment;

// Every record in this collection will now be an 
// instance of the Assignment class.
this.AssignmentsReport = new Mongo.Collection("assignments", {
  transform: function(doc) {
    return new Assignment(doc._id, doc.assignId, doc.masterId);
  }
});

// Creating a class instance to actually map the new object.
Assignment = (function() {
  function Assignment(_id, assignId, masterId) {
    var assign, ref;
    this._id = _id;
    this.assignId = assignId;
    this.masterId = masterId;
    this.assign = (ref = Assignments.find({
      _id: this.assignId
    }).fetch()) != null ? ref[0] : void 0;
  }

  return Assignment;

})();



回答5:


There is also meteor Package that allows to add transforms on server side for publish https://atmospherejs.com/maximum/server-transform



来源:https://stackoverflow.com/questions/18093560/meteor-collection-transform-is-it-done-on-the-server-or-on-the-client-or-it-de

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!