Skip to content

Commit 526e639

Browse files
authored
Merge pull request #44 from share/db-meta-option
implement {metadata: true} for methods that get snapshots or ops
2 parents d71d8c7 + de8f12f commit 526e639

File tree

1 file changed

+15
-12
lines changed

1 file changed

+15
-12
lines changed

index.js

Lines changed: 15 additions & 12 deletions
Original file line numberDiff line numberDiff line change
@@ -245,7 +245,7 @@ ShareDbMongo.prototype.getSnapshot = function(collectionName, id, fields, option
245245
this.getCollection(collectionName, function(err, collection) {
246246
if (err) return callback(err);
247247
var query = {_id: id};
248-
var projection = getProjection(fields);
248+
var projection = getProjection(fields, options);
249249
collection.find(query).limit(1).project(projection).next(function(err, doc) {
250250
if (err) return callback(err);
251251
var snapshot = (doc) ? castToSnapshot(doc) : new MongoSnapshot(id, 0, null, undefined);
@@ -258,7 +258,7 @@ ShareDbMongo.prototype.getSnapshotBulk = function(collectionName, ids, fields, o
258258
this.getCollection(collectionName, function(err, collection) {
259259
if (err) return callback(err);
260260
var query = {_id: {$in: ids}};
261-
var projection = getProjection(fields);
261+
var projection = getProjection(fields, options);
262262
collection.find(query).project(projection).toArray(function(err, docs) {
263263
if (err) return callback(err);
264264
var snapshotMap = {};
@@ -335,7 +335,7 @@ ShareDbMongo.prototype.getOpsToSnapshot = function(collectionName, id, from, sna
335335
var err = getSnapshotOpLinkErorr(collectionName, id);
336336
return callback(err);
337337
}
338-
this._getOps(collectionName, id, from, function(err, ops) {
338+
this._getOps(collectionName, id, from, options, function(err, ops) {
339339
if (err) return callback(err);
340340
var filtered = getLinkedOps(ops, null, snapshot._opLink);
341341
var err = checkOpsFrom(collectionName, id, filtered, from);
@@ -355,7 +355,7 @@ ShareDbMongo.prototype.getOps = function(collectionName, id, from, to, options,
355355
var err = doc && checkDocHasOp(collectionName, id, doc);
356356
if (err) return callback(err);
357357
}
358-
self._getOps(collectionName, id, from, function(err, ops) {
358+
self._getOps(collectionName, id, from, options, function(err, ops) {
359359
if (err) return callback(err);
360360
var filtered = filterOps(ops, doc, to);
361361
var err = checkOpsFrom(collectionName, id, filtered, from);
@@ -394,7 +394,7 @@ ShareDbMongo.prototype.getOpsBulk = function(collectionName, fromMap, toMap, opt
394394
// requested versions
395395
if (!conditions.length) return callback(null, opsMap);
396396
// Otherwise, get all of the ops that are newer
397-
self._getOpsBulk(collectionName, conditions, function(err, opsBulk) {
397+
self._getOpsBulk(collectionName, conditions, options, function(err, opsBulk) {
398398
if (err) return callback(err);
399399
for (var i = 0; i < conditions.length; i++) {
400400
var id = conditions[i].d;
@@ -546,26 +546,26 @@ function getOpsQuery(id, from) {
546546
{d: id, v: {$gte: from}};
547547
}
548548

549-
ShareDbMongo.prototype._getOps = function(collectionName, id, from, callback) {
549+
ShareDbMongo.prototype._getOps = function(collectionName, id, from, options, callback) {
550550
this.getOpCollection(collectionName, function(err, opCollection) {
551551
if (err) return callback(err);
552552
var query = getOpsQuery(id, from);
553553
// Exclude the `d` field, which is only for use internal to livedb-mongo.
554554
// Also exclude the `m` field, which can be used to store metadata on ops
555555
// for tracking purposes
556-
var projection = {d: 0, m: 0};
556+
var projection = (options && options.metadata) ? {d: 0} : {d: 0, m: 0};
557557
var sort = {v: 1};
558558
opCollection.find(query).project(projection).sort(sort).toArray(callback);
559559
});
560560
};
561561

562-
ShareDbMongo.prototype._getOpsBulk = function(collectionName, conditions, callback) {
562+
ShareDbMongo.prototype._getOpsBulk = function(collectionName, conditions, options, callback) {
563563
this.getOpCollection(collectionName, function(err, opCollection) {
564564
if (err) return callback(err);
565565
var query = {$or: conditions};
566566
// Exclude the `m` field, which can be used to store metadata on ops for
567567
// tracking purposes
568-
var projection = {m: 0};
568+
var projection = (options && options.metadata) ? null : {m: 0};
569569
var stream = opCollection.find(query).project(projection).stream();
570570
readOpsBulk(stream, callback);
571571
});
@@ -681,7 +681,7 @@ ShareDbMongo.prototype.query = function(collectionName, inputQuery, fields, opti
681681
var self = this;
682682
this.getCollection(collectionName, function(err, collection) {
683683
if (err) return callback(err);
684-
var projection = getProjection(fields);
684+
var projection = getProjection(fields, options);
685685
self._query(collection, inputQuery, projection, function(err, results, extra) {
686686
if (err) return callback(err);
687687
var snapshots = [];
@@ -1231,10 +1231,12 @@ function isPlainObject(value) {
12311231
// depends on the data being stored at the top level of the document. It will
12321232
// only work properly for json documents--which are the only types for which
12331233
// we really want projections.
1234-
function getProjection(fields) {
1234+
function getProjection(fields, options) {
12351235
// When there is no projection specified, still exclude returning the
12361236
// metadata that is added to a doc for querying or auditing
1237-
if (!fields) return {_m: 0, _o: 0};
1237+
if (!fields) {
1238+
return (options && options.metadata) ? {_o: 0} : {_m: 0, _o: 0};
1239+
}
12381240
// Do not project when called by ShareDB submit
12391241
if (fields.$submit) return;
12401242

@@ -1244,6 +1246,7 @@ function getProjection(fields) {
12441246
}
12451247
projection._type = 1;
12461248
projection._v = 1;
1249+
if (options && options.metadata) projection._m = 1;
12471250
return projection;
12481251
}
12491252

0 commit comments

Comments
 (0)