-
Notifications
You must be signed in to change notification settings - Fork 450
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
Changes from 1 commit
5dd070b
58e29e6
57c8598
15a438f
7e12683
File filter
Filter by extension
Conversations
Jump to
Diff view
Diff view
There are no files selected for viewing
Original file line number | Diff line number | Diff line change |
---|---|---|
|
@@ -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); | ||
|
@@ -281,13 +281,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) { | ||
|
@@ -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; | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Let's leave this as |
||
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', | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. let's use |
||
requestParams: { | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. let's use |
||
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); | ||
|
@@ -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); | ||
|
@@ -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? | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe 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. There was a problem hiding this comment. Choose a reason for hiding this commentThe 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 |
||
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); | ||
}); | ||
}); | ||
|
@@ -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); | ||
|
There was a problem hiding this comment.
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 aclass
for clarity about what exact information we're expecting in the middleware.