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

For readSnapshots middleware, add method and parameter properties to first parameter #263

Merged
merged 5 commits into from
Feb 14, 2019
Merged
Show file tree
Hide file tree
Changes from 2 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
60 changes: 45 additions & 15 deletions lib/backend.js
Original file line number Diff line number Diff line change
Expand Up @@ -272,7 +272,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 @@ -281,13 +281,10 @@ Backend.prototype._sanitizeSnapshots = function(agent, projection, collection, s
}
}

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

Choose a reason for hiding this comment

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

It might be a good optimisation to initialise these properties whenever requestContext is initialised. It may be beneficial to declare a class for clarity about what exact information we're expecting in the middleware.

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 @@ -366,7 +363,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 = {
method: 'fetch',
Copy link
Collaborator

Choose a reason for hiding this comment

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

It would be nice to move these magic strings into an object, similar to the middleware actions. Just to clarify which methods are available, and to make refactoring easy, etc.

parameters: {
index: index,
id: id
},
snapshotType: 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 @@ -390,7 +395,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 = {
method: 'fetchBulk',
parameters: {
index: index,
ids: ids
},
snapshotType: 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 @@ -499,7 +512,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, '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 @@ -533,7 +546,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, 'querySubscribe', request, function(err, snapshots, extra) {
if (err) {
stream.destroy();
return callback(err);
Expand Down Expand Up @@ -574,11 +587,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 = {
method: method,
parameters: {
index: request.index,
query: request.query,
options: request.options,
},
snapshotType: backend.SNAPSHOT_TYPES.current
};
backend._sanitizeSnapshots(agent, request.snapshotProjection, request.collection, snapshots, requestContext, function(err) {
callback(err, snapshots, extra);
});
});
Expand Down Expand Up @@ -616,8 +638,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 = {
method: 'fetchSnapshot',
parameters: {
index: index,
id: id,
version: version,
},
snapshotType: 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
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