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 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
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 @@ -353,20 +350,28 @@ Backend.prototype.getOpsBulk = function(agent, index, fromMap, toMap, callback)
Backend.prototype.fetch = function(agent, index, id, callback) {
var start = Date.now();
var projection = this.projections[index];
var collection = (projection) ? projection.target : index;
var dbCollection = (projection) ? projection.target : index;
Copy link
Contributor

Choose a reason for hiding this comment

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

Let's leave this as collection for this PR. we could consider changing it, but we'd want to be consistent everywhere

var fields = projection && projection.fields;
var backend = this;
var request = {
agent: agent,
index: index,
collection: collection,
collection: dbCollection,
id: id
};
backend.db.getSnapshot(collection, id, fields, null, function(err, snapshot) {
backend.db.getSnapshot(dbCollection, id, fields, null, function(err, snapshot) {
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 = {
requestMethod: 'fetch',
Copy link
Contributor

Choose a reason for hiding this comment

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

let's use method

requestParams: {
Copy link
Contributor

Choose a reason for hiding this comment

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

let's use parameters

index: index,
id: id
},
snapshotType: backend.SNAPSHOT_TYPES.current
};
backend._sanitizeSnapshots(agent, snapshotProjection, dbCollection, 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 = {
requestMethod: 'fetchBulk',
requestParams: {
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 @@ -578,7 +591,16 @@ 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) {
var requestContext = {
requestMethod: 'query', // TODO: Should this distinguish between queryFetch and querySubscribe?
Copy link
Contributor Author

Choose a reason for hiding this comment

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

Question here for reviewers - This currently doesn't distinguish between "queryFetch" and the initial query issued by "querySubscribe". Should it?

The initial "querySubscribe" DB query is for all intents and purposes identical to the query issued by "queryFetch". The big difference is in whether a pub/sub subscription is registered.

Copy link
Contributor

Choose a reason for hiding this comment

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

yes, let's make the method be the same name as the public method on Backend, so queryFetch or querySubscribe

requestParams: {
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 = {
requestMethod: 'fetchSnapshot',
requestParams: {
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.requestMethod).to.be.a('string');
expect(request.requestParams).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.requestMethod).to.be.a('string');
expect(request.requestParams).to.be.ok;
expectFido(request);
expectSpot(request);
doneAfter();
Expand Down