mirror of
https://github.com/meteor/meteor.git
synced 2026-05-02 03:01:46 -04:00
41 lines
1.3 KiB
JavaScript
41 lines
1.3 KiB
JavaScript
// Wrap a transform function to return objects that have the _id field
|
|
// of the untransformed document. This ensures that subsystems such as
|
|
// the observe-sequence package that call `observe` can keep track of
|
|
// the documents identities.
|
|
//
|
|
// - Require that it returns objects
|
|
// - If the return value has an _id field, verify that it matches the
|
|
// original _id field
|
|
// - If the return value doesn't have an _id field, add it back.
|
|
LocalCollection.wrapTransform = function (transform) {
|
|
if (!transform)
|
|
return null;
|
|
|
|
return function (doc) {
|
|
if (!_.has(doc, '_id')) {
|
|
// XXX do we ever have a transform on the oplog's collection? because that
|
|
// collection has no _id.
|
|
throw new Error("can only transform documents with _id");
|
|
}
|
|
|
|
var id = doc._id;
|
|
// XXX consider making tracker a weak dependency and checking Package.tracker here
|
|
var transformed = Tracker.nonreactive(function () {
|
|
return transform(doc);
|
|
});
|
|
|
|
if (!isPlainObject(transformed)) {
|
|
throw new Error("transform must return object");
|
|
}
|
|
|
|
if (_.has(transformed, '_id')) {
|
|
if (!EJSON.equals(transformed._id, id)) {
|
|
throw new Error("transformed document can't have different _id");
|
|
}
|
|
} else {
|
|
transformed._id = id;
|
|
}
|
|
return transformed;
|
|
};
|
|
};
|