Meteor Collection转换:是在服务器上完成还是在客户端上完成?或者取决于情况

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

本文关键字:或者 取决于 情况 客户端 服务器 Meteor 转换 Collection      更新时间:2023-09-26

我想使用transform从集合中生成一个"虚拟字段"。然而,我正在添加的新字段(在transform函数中)向返回的文档添加了相当多的数据。

如果转换是在客户端内部进行的,这是可以的。如果它是在服务器端完成的,那么就会有带宽问题。

所以我想知道转换是在服务器上还是在客户端上完成的,还是取决于我如何查找/获取文档?

UPDATE:可以在服务器上进行转换。

你可以在客户端上进行这样的转换:

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

Meteor在发布的查询(从Meteor.publish内)上忽略transform。客户端看到文档时,就好像转换不存在一样。

如果你想在服务器上使用转换,你可以这样做:

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();
});

您可以添加并使用以下函数:

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();
    });
}

用法(服务器端):

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

缺点:如果保存已发布的文档,也将保存添加的更改。在这里,客户端不知道在发布过程中添加了属性"aField"。

当您为集合或findOne、fetch等指定转换选项时,您可以在两侧使用转换

transform函数
可选的转换函数。在从fetch或findOne返回文档之前,以及在传递给observe、allow和deny的回调之前,文档将通过此函数传递。

如果您需要从具有转换选项的集合中获取原始文档。

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

您也可以直接在集合定义中添加转换。注意:如上所述,这会在更新时将额外数据存储到集合中。对于只读收藏来说,这并不是一个真正的问题。

我也希望它能存在于共享的lib文件夹或类似的文件夹中,客户端和服务器都可以看到这个定义。

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;
})();

还有一个流星包,它允许在服务器端添加用于发布的转换https://atmospherejs.com/maximum/server-transform