Skip to content

send projection information to the readSnapshot middleware #1

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

Open
wants to merge 4 commits into
base: master
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from 1 commit
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
30 changes: 15 additions & 15 deletions lib/backend.js
Original file line number Diff line number Diff line change
Expand Up @@ -82,6 +82,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 @@ -166,6 +169,7 @@ Backend.prototype.addProjection = function(name, collection, fields) {
}

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

Backend.prototype._sanitizeSnapshots = function(agent, projection, collection, snapshots, callback) {
if (projection) {
Backend.prototype._sanitizeSnapshots = function(agent, projectSnapshots, projection, collection, snapshots, callback) {
if (projectSnapshots && projection) {
try {
projections.projectSnapshots(projection.fields, snapshots);
} catch (err) {
return callback(err);
}
}
var request = {collection: collection, snapshots: snapshots};
var request = {collection: collection, snapshots: snapshots, projection: projection};
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 @@ -345,9 +345,9 @@ Backend.prototype.fetch = function(agent, index, id, callback) {
};
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, function(err) {
backend._sanitizeSnapshots(agent, projectsSnapshots, projection, collection, snapshots, function(err) {
if (err) return callback(err);
backend.emit('timing', 'fetch', Date.now() - start, request);
callback(null, snapshot);
Expand All @@ -369,9 +369,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, function(err) {
backend._sanitizeSnapshots(agent, projectsSnapshots, projection, collection, snapshots, function(err) {
if (err) return callback(err);
backend.emit('timing', 'fetchBulk', Date.now() - start, request);
callback(null, snapshotMap);
Expand Down Expand Up @@ -541,7 +541,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 @@ -550,7 +550,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 = backend.db.projectsSnapshots;
callback(null, request);
});
};
Expand All @@ -559,7 +559,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, function(err) {
backend._sanitizeSnapshots(agent, request.projectsSnapshots, request.projection, request.collection, snapshots, function(err) {
callback(err, snapshots, extra);
});
});
Expand Down Expand Up @@ -591,4 +591,4 @@ function pluckIds(snapshots) {
ids.push(snapshots[i].id);
}
return ids;
}
}
7 changes: 4 additions & 3 deletions lib/query-emitter.js
Original file line number Diff line number Diff line change
Expand Up @@ -12,7 +12,8 @@ function QueryEmitter(request, stream, ids, extra) {
this.collection = request.collection;
this.fields = request.fields;
this.options = request.options;
this.snapshotProjection = request.snapshotProjection;
this.projectSnapshots = request.projectSnapshots;
this.projection = request.projection;
this.stream = stream;
this.ids = ids;
this.extra = extra;
Expand Down Expand Up @@ -186,7 +187,7 @@ QueryEmitter.prototype.queryPoll = function(callback) {
emitter.db.getSnapshotBulk(emitter.collection, inserted, emitter.fields, null, function(err, snapshotMap) {
if (err) return emitter._finishPoll(err, callback, pending);
var snapshots = emitter.backend._getSnapshotsFromMap(inserted, snapshotMap);
emitter.backend._sanitizeSnapshots(emitter.agent, emitter.snapshotProjection, emitter.collection, snapshots, function(err) {
emitter.backend._sanitizeSnapshots(emitter.agent, emitter.projectSnapshots, emitter.projection, emitter.collection, snapshots, function(err) {
if (err) return emitter._finishPoll(err, callback, pending);
emitter._emitTiming('queryEmitter.pollGetSnapshotBulk', start);
var diff = mapDiff(idsDiff, snapshotMap);
Expand Down Expand Up @@ -234,7 +235,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];
emitter.backend._sanitizeSnapshots(emitter.agent, emitter.snapshotProjection, emitter.collection, snapshots, function(err) {
emitter.backend._sanitizeSnapshots(emitter.agent, emitter.projectSnapshots, emitter.projection, emitter.collection, snapshots, function(err) {
if (err) return callback(err);
emitter.onDiff([new arraydiff.InsertDiff(index, snapshots)]);
emitter._emitTiming('queryEmitter.pollDocGetSnapshot', start);
Expand Down