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

Retain metadata in snapshot if needed #138

Open
wants to merge 1 commit into
base: master
Choose a base branch
from
Open
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
13 changes: 8 additions & 5 deletions index.js
Original file line number Diff line number Diff line change
Expand Up @@ -345,7 +345,7 @@ ShareDbMongo.prototype.getSnapshot = function(collectionName, id, fields, option

collection.find(request.query, request.findOptions).limit(1).project(projection).next(function(err, doc) {
if (err) return callback(err);
var snapshot = (doc) ? castToSnapshot(doc) : new MongoSnapshot(id, 0, null, undefined);
var snapshot = (doc) ? castToSnapshot(doc, fields, options) : new MongoSnapshot(id, 0, null, undefined);
callback(null, snapshot);
});
});
Expand All @@ -367,7 +367,7 @@ ShareDbMongo.prototype.getSnapshotBulk = function(collectionName, ids, fields, o
if (err) return callback(err);
var snapshotMap = {};
for (var i = 0; i < docs.length; i++) {
var snapshot = castToSnapshot(docs[i]);
var snapshot = castToSnapshot(docs[i], fields, options);
snapshotMap[snapshot.id] = snapshot;
}
for (var i = 0; i < ids.length; i++) {
Expand Down Expand Up @@ -894,7 +894,7 @@ ShareDbMongo.prototype.query = function(collectionName, inputQuery, fields, opti
if (err) return callback(err);
var snapshots = [];
for (var i = 0; i < results.length; i++) {
var snapshot = castToSnapshot(results[i]);
var snapshot = castToSnapshot(results[i], fields, options);
snapshots.push(snapshot);
}
callback(null, snapshots, extra);
Expand Down Expand Up @@ -1406,7 +1406,7 @@ function castToDoc(id, snapshot, opLink) {
return doc;
}

function castToSnapshot(doc) {
function castToSnapshot(doc, fields, options) {
var id = doc._id;
var version = doc._v;
var type = doc._type;
Expand All @@ -1423,7 +1423,10 @@ function castToSnapshot(doc) {
delete data._id;
delete data._v;
delete data._type;
delete data._m;
var includeMetadata = (fields && fields.$submit) || (options && options.metadata);
if(!includeMetadata) {
delete data._m;
}
delete data._o;
return new MongoSnapshot(id, version, type, data, meta, opLink);
}
Expand Down