Skip to content

Commit

Permalink
Merge pull request #263 from share/readSnapshots-context
Browse files Browse the repository at this point in the history
For readSnapshots middleware, add method and parameter properties to first parameter
  • Loading branch information
ericyhwang authored Feb 14, 2019
2 parents 7938f7f + 7e12683 commit 66c7e00
Show file tree
Hide file tree
Showing 3 changed files with 79 additions and 17 deletions.
90 changes: 73 additions & 17 deletions lib/backend.js
Original file line number Diff line number Diff line change
Expand Up @@ -77,6 +77,24 @@ Backend.prototype.MIDDLEWARE_ACTIONS = {
submit: 'submit'
};

// Context provided to readSnapshots middleware
function ReadSnapshotsContext(method, parameters, snapshotType) {
this.method = method;
this.parameters = parameters;
this.snapshotType = snapshotType;
this.collection = null; // Set during _sanitizeSnapshots
this.snapshots = null; // Set during _sanitizeSnapshots
}

Backend.prototype.READ_SNAPSHOTS_METHODS = {
fetch: 'fetch',
fetchBulk: 'fetchBulk',
fetchSnapshot: 'fetchSnapshot',
fetchSnapshotByTimestamp: 'fetchSnapshotByTimestamp',
queryFetch: 'queryFetch',
querySubscribe: 'querySubscribe'
};

Backend.prototype.SNAPSHOT_TYPES = {
// The current snapshot is being fetched (eg through backend.fetch)
current: 'current',
Expand Down Expand Up @@ -274,7 +292,7 @@ Backend.prototype._sanitizeOpsBulk = function(agent, projection, collection, ops
}, callback);
};

Backend.prototype._sanitizeSnapshots = function(agent, projection, collection, snapshots, snapshotType, callback) {
Backend.prototype._sanitizeSnapshots = function(agent, projection, collection, snapshots, requestContext, callback) {
if (projection) {
try {
projections.projectSnapshots(projection.fields, snapshots);
Expand All @@ -283,13 +301,10 @@ Backend.prototype._sanitizeSnapshots = function(agent, projection, collection, s
}
}

var request = {
collection: collection,
snapshots: snapshots,
snapshotType: snapshotType
};
requestContext.collection = collection;
requestContext.snapshots = snapshots;

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

Backend.prototype._getSnapshotProjection = function(db, projection) {
Expand Down Expand Up @@ -368,7 +383,15 @@ Backend.prototype.fetch = function(agent, index, id, callback) {
if (err) return callback(err);
var snapshotProjection = backend._getSnapshotProjection(backend.db, projection);
var snapshots = [snapshot];
backend._sanitizeSnapshots(agent, snapshotProjection, collection, snapshots, backend.SNAPSHOT_TYPES.current, function(err) {
var requestContext = new ReadSnapshotsContext(
backend.READ_SNAPSHOTS_METHODS.fetch,
{
index: index,
id: id
},
backend.SNAPSHOT_TYPES.current
);
backend._sanitizeSnapshots(agent, snapshotProjection, collection, snapshots, requestContext, function(err) {
if (err) return callback(err);
backend.emit('timing', 'fetch', Date.now() - start, request);
callback(null, snapshot);
Expand All @@ -392,7 +415,15 @@ Backend.prototype.fetchBulk = function(agent, index, ids, callback) {
if (err) return callback(err);
var snapshotProjection = backend._getSnapshotProjection(backend.db, projection);
var snapshots = backend._getSnapshotsFromMap(ids, snapshotMap);
backend._sanitizeSnapshots(agent, snapshotProjection, collection, snapshots, backend.SNAPSHOT_TYPES.current, function(err) {
var requestContext = new ReadSnapshotsContext(
backend.READ_SNAPSHOTS_METHODS.fetchBulk,
{
index: index,
ids: ids
},
backend.SNAPSHOT_TYPES.current
);
backend._sanitizeSnapshots(agent, snapshotProjection, collection, snapshots, requestContext, function(err) {
if (err) return callback(err);
backend.emit('timing', 'fetchBulk', Date.now() - start, request);
callback(null, snapshotMap);
Expand Down Expand Up @@ -501,7 +532,7 @@ Backend.prototype.queryFetch = function(agent, index, query, options, callback)
var backend = this;
backend._triggerQuery(agent, index, query, options, function(err, request) {
if (err) return callback(err);
backend._query(agent, request, function(err, snapshots, extra) {
backend._query(agent, backend.READ_SNAPSHOTS_METHODS.queryFetch, request, function(err, snapshots, extra) {
if (err) return callback(err);
backend.emit('timing', 'queryFetch', Date.now() - start, request);
callback(null, snapshots, extra);
Expand Down Expand Up @@ -535,7 +566,7 @@ Backend.prototype.querySubscribe = function(agent, index, query, options, callba
return;
}
// Issue query on db to get our initial results
backend._query(agent, request, function(err, snapshots, extra) {
backend._query(agent, backend.READ_SNAPSHOTS_METHODS.querySubscribe, request, function(err, snapshots, extra) {
if (err) {
stream.destroy();
return callback(err);
Expand Down Expand Up @@ -576,11 +607,20 @@ Backend.prototype._triggerQuery = function(agent, index, query, options, callbac
});
};

Backend.prototype._query = function(agent, request, callback) {
Backend.prototype._query = function(agent, method, 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) {
var requestContext = new ReadSnapshotsContext(
method,
{
index: request.index,
query: request.query,
options: request.options,
},
backend.SNAPSHOT_TYPES.current
);
backend._sanitizeSnapshots(agent, request.snapshotProjection, request.collection, snapshots, requestContext, function(err) {
callback(err, snapshots, extra);
});
});
Expand Down Expand Up @@ -618,8 +658,16 @@ Backend.prototype.fetchSnapshot = function(agent, index, id, version, callback)
if (error) return callback(error);
var snapshotProjection = backend._getSnapshotProjection(backend.db, projection);
var snapshots = [snapshot];
var snapshotType = backend.SNAPSHOT_TYPES.byVersion;
backend._sanitizeSnapshots(agent, snapshotProjection, collection, snapshots, snapshotType, function (error) {
var requestContext = new ReadSnapshotsContext(
backend.READ_SNAPSHOTS_METHODS.fetchSnapshot,
{
index: index,
id: id,
version: version,
},
backend.SNAPSHOT_TYPES.byVersion
);
backend._sanitizeSnapshots(agent, snapshotProjection, collection, snapshots, requestContext, function (error) {
if (error) return callback(error);
backend.emit('timing', 'fetchSnapshot', Date.now() - start, request);
callback(null, snapshot);
Expand Down Expand Up @@ -671,8 +719,16 @@ Backend.prototype.fetchSnapshotByTimestamp = function (agent, index, id, timesta
if (error) return callback(error);
var snapshotProjection = backend._getSnapshotProjection(backend.db, projection);
var snapshots = [snapshot];
var snapshotType = backend.SNAPSHOT_TYPES.byTimestamp;
backend._sanitizeSnapshots(agent, snapshotProjection, collection, snapshots, snapshotType, function (error) {
var requestContext = new ReadSnapshotsContext(
backend.READ_SNAPSHOTS_METHODS.fetchSnapshotByTimestamp,
{
index: index,
id: id,
timestamp: timestamp,
},
backend.SNAPSHOT_TYPES.byTimestamp
);
backend._sanitizeSnapshots(agent, snapshotProjection, collection, snapshots, requestContext, function (error) {
if (error) return callback(error);
backend.emit('timing', 'fetchSnapshot', Date.now() - start, request);
callback(null, snapshot);
Expand Down
2 changes: 2 additions & 0 deletions test/client/snapshot-timestamp-request.js
Original file line number Diff line number Diff line change
Expand Up @@ -326,6 +326,8 @@ describe('SnapshotTimestampRequest', function () {
it('triggers the middleware', function (done) {
backend.use(backend.MIDDLEWARE_ACTIONS.readSnapshots,
function (request) {
expect(request.method).to.eql(backend.READ_SNAPSHOTS_METHODS.fetchSnapshotByTimestamp);
expect(request.parameters).to.eql({index: 'books', id: 'time-machine', timestamp: day3});
expect(request.snapshots[0]).to.eql(v3);
expect(request.snapshotType).to.be(backend.SNAPSHOT_TYPES.byTimestamp);
done();
Expand Down
4 changes: 4 additions & 0 deletions test/middleware.js
Original file line number Diff line number Diff line change
Expand Up @@ -199,6 +199,8 @@ describe('middleware', function() {
var doneAfter = util.callAfter(1, done);
backend.use('readSnapshots', function(request, next) {
expect(request.snapshots).to.have.length(1);
expect(request.method).to.be.a('string');
expect(request.parameters).to.be.ok;
expectFido(request);
doneAfter();
next();
Expand All @@ -210,6 +212,8 @@ describe('middleware', function() {
var doneAfter = util.callAfter(1, done);
backend.use('readSnapshots', function(request, next) {
expect(request.snapshots).to.have.length(2);
expect(request.method).to.be.a('string');
expect(request.parameters).to.be.ok;
expectFido(request);
expectSpot(request);
doneAfter();
Expand Down

0 comments on commit 66c7e00

Please sign in to comment.