Skip to content
Open
Show file tree
Hide file tree
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
32 changes: 17 additions & 15 deletions lib/backend.js
Original file line number Diff line number Diff line change
Expand Up @@ -94,6 +94,9 @@ Backend.prototype._shimDocAction = function() {
this.use(this.MIDDLEWARE_ACTIONS.readSnapshots, function(request, callback) {
async.each(request.snapshots, function(snapshot, eachCb) {
var docRequest = {collection: request.collection, id: snapshot.id, snapshot: snapshot};
if(request.projection) {
Copy link
Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

sharedb-access expect an index property and use the deprecated doc middleware

docRequest.index = request.projection.index;
}
backend.trigger(backend.MIDDLEWARE_ACTIONS.doc, request.agent, docRequest, eachCb);
}, callback);
});
Expand Down Expand Up @@ -179,6 +182,7 @@ Backend.prototype.addProjection = function(name, collection, fields) {
}

this.projections[name] = {
index: name,
target: collection,
fields: fields
};
Expand Down Expand Up @@ -272,10 +276,10 @@ Backend.prototype._sanitizeOpsBulk = function(agent, projection, collection, ops
}, callback);
};

Backend.prototype._sanitizeSnapshots = function(agent, projection, collection, snapshots, snapshotType, callback) {
if (projection) {
Backend.prototype._sanitizeSnapshots = function(agent, projectsSnapshots, projection, collection, snapshots, snapshotType, callback) {
if (projectsSnapshots && projection) {
try {
projections.projectSnapshots(projection.fields, snapshots);
projections.projectsSnapshots(projection.fields, snapshots);
} catch (err) {
return callback(err);
}
Expand All @@ -284,16 +288,13 @@ Backend.prototype._sanitizeSnapshots = function(agent, projection, collection, s
var request = {
collection: collection,
snapshots: snapshots,
projection: projection,
snapshotType: snapshotType
};

this.trigger(this.MIDDLEWARE_ACTIONS.readSnapshots, agent, request, callback);
};

Backend.prototype._getSnapshotProjection = function(db, projection) {
return (db.projectsSnapshots) ? null : projection;
};

Backend.prototype._getSnapshotsFromMap = function(ids, snapshotMap) {
var snapshots = new Array(ids.length);
for (var i = 0; i < ids.length; i++) {
Expand Down Expand Up @@ -357,16 +358,17 @@ Backend.prototype.fetch = function(agent, index, id, callback) {
var fields = projection && projection.fields;
var backend = this;
var request = {
projection: projection,
agent: agent,
index: index,
collection: collection,
id: id
};
backend.db.getSnapshot(collection, id, fields, null, function(err, snapshot) {
if (err) return callback(err);
var snapshotProjection = backend._getSnapshotProjection(backend.db, projection);
var projectsSnapshots = !backend.db.projectsSnapshots;
var snapshots = [snapshot];
backend._sanitizeSnapshots(agent, snapshotProjection, collection, snapshots, backend.SNAPSHOT_TYPES.current, function(err) {
backend._sanitizeSnapshots(agent, snapshotProjection, projection, collection, snapshots, backend.SNAPSHOT_TYPES.current, function(err) {
if (err) return callback(err);
backend.emit('timing', 'fetch', Date.now() - start, request);
callback(null, snapshot);
Expand All @@ -388,9 +390,9 @@ Backend.prototype.fetchBulk = function(agent, index, ids, callback) {
};
backend.db.getSnapshotBulk(collection, ids, fields, null, function(err, snapshotMap) {
if (err) return callback(err);
var snapshotProjection = backend._getSnapshotProjection(backend.db, projection);
var projectsSnapshots = !backend.db.projectsSnapshots;
var snapshots = backend._getSnapshotsFromMap(ids, snapshotMap);
backend._sanitizeSnapshots(agent, snapshotProjection, collection, snapshots, backend.SNAPSHOT_TYPES.current, function(err) {
backend._sanitizeSnapshots(agent, projectsSnapshots, projection, collection, snapshots, backend.SNAPSHOT_TYPES.current, function(err) {
if (err) return callback(err);
backend.emit('timing', 'fetchBulk', Date.now() - start, request);
callback(null, snapshotMap);
Expand Down Expand Up @@ -560,7 +562,7 @@ Backend.prototype._triggerQuery = function(agent, index, query, options, callbac
query: query,
options: options,
db: null,
snapshotProjection: null,
projectsSnapshots: false,
};
var backend = this;
backend.trigger(backend.MIDDLEWARE_ACTIONS.query, agent, request, function(err) {
Expand All @@ -569,7 +571,7 @@ Backend.prototype._triggerQuery = function(agent, index, query, options, callbac
// that the db option can be changed in middleware
request.db = (options.db) ? backend.extraDbs[options.db] : backend.db;
if (!request.db) return callback({code: 4003, message: 'DB not found'});
request.snapshotProjection = backend._getSnapshotProjection(request.db, projection);
request.projectsSnapshots = !request.db.projectsSnapshots;
callback(null, request);
});
};
Expand All @@ -578,7 +580,7 @@ Backend.prototype._query = function(agent, request, callback) {
var backend = this;
request.db.query(request.collection, request.query, request.fields, request.options, function(err, snapshots, extra) {
if (err) return callback(err);
backend._sanitizeSnapshots(agent, request.snapshotProjection, request.collection, snapshots, backend.SNAPSHOT_TYPES.current, function(err) {
backend._sanitizeSnapshots(agent, request.projectsSnapshots, request.projection, request.collection, snapshots, backend.SNAPSHOT_TYPES.current, function(err) {
callback(err, snapshots, extra);
});
});
Expand Down Expand Up @@ -687,4 +689,4 @@ function pluckIds(snapshots) {
ids.push(snapshots[i].id);
}
return ids;
}
}
4 changes: 2 additions & 2 deletions lib/projections.js
Original file line number Diff line number Diff line change
@@ -1,7 +1,7 @@
var json0 = require('ot-json0').type;

exports.projectSnapshot = projectSnapshot;
exports.projectSnapshots = projectSnapshots;
exports.projectsSnapshots = projectsSnapshots;
Copy link

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Why rename this?

exports.projectOp = projectOp;
exports.isSnapshotAllowed = isSnapshotAllowed;
exports.isOpAllowed = isOpAllowed;
Expand All @@ -16,7 +16,7 @@ function projectSnapshot(fields, snapshot) {
snapshot.data = projectData(fields, snapshot.data);
}

function projectSnapshots(fields, snapshots) {
function projectsSnapshots(fields, snapshots) {
for (var i = 0; i < snapshots.length; i++) {
var snapshot = snapshots[i];
projectSnapshot(fields, snapshot);
Expand Down
10 changes: 5 additions & 5 deletions lib/query-emitter.js
Original file line number Diff line number Diff line change
Expand Up @@ -12,11 +12,12 @@ function QueryEmitter(request, stream, ids, extra) {
this.collection = request.collection;
this.fields = request.fields;
this.options = request.options;
this.snapshotProjection = request.snapshotProjection;
this.projectsSnapshots = request.projectsSnapshots;
this.projection = request.projection;
this.stream = stream;
this.ids = ids;
this.extra = extra;

this.skipPoll = this.options.skipPoll || util.doNothing;
this.canPollDoc = this.db.canPollDoc(this.collection, this.query);
this.pollDebounce =
Expand Down Expand Up @@ -187,7 +188,7 @@ QueryEmitter.prototype.queryPoll = function(callback) {
if (err) return emitter._finishPoll(err, callback, pending);
var snapshots = emitter.backend._getSnapshotsFromMap(inserted, snapshotMap);
var snapshotType = emitter.backend.SNAPSHOT_TYPES.current;
emitter.backend._sanitizeSnapshots(emitter.agent, emitter.snapshotProjection, emitter.collection, snapshots, snapshotType, function(err) {
emitter.backend._sanitizeSnapshots(emitter.agent, emitter.projectsSnapshots, emitter.projection, emitter.collection, snapshots, snapshotType, function(err) {
if (err) return emitter._finishPoll(err, callback, pending);
emitter._emitTiming('queryEmitter.pollGetSnapshotBulk', start);
var diff = mapDiff(idsDiff, snapshotMap);
Expand Down Expand Up @@ -235,8 +236,7 @@ QueryEmitter.prototype.queryPollDoc = function(id, callback) {
emitter.db.getSnapshot(emitter.collection, id, emitter.fields, null, function(err, snapshot) {
if (err) return callback(err);
var snapshots = [snapshot];
var snapshotType = emitter.backend.SNAPSHOT_TYPES.current;
emitter.backend._sanitizeSnapshots(emitter.agent, emitter.snapshotProjection, emitter.collection, snapshots, snapshotType, function(err) {
emitter.backend._sanitizeSnapshots(emitter.agent, emitter.projectsSnapshots, emitter.projection, emitter.collection, snapshots, snapshotType, function(err) {
if (err) return callback(err);
emitter.onDiff([new arraydiff.InsertDiff(index, snapshots)]);
emitter._emitTiming('queryEmitter.pollDocGetSnapshot', start);
Expand Down
Loading