-
Notifications
You must be signed in to change notification settings - Fork 8
Handle projections, queries on Mongo doc properties (like _id) and fix comparisons to null #3
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
Conversation
.travis.yml
Outdated
- 5 | ||
- 4 | ||
- 0.10 |
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.
The newest version of mingo
uses Symbol.Iterator
, which wasn't supported until Node 0.12
: http://node.green/#ES2015-built-ins-typed-arrays--TypedArray--prototype-Symbol-iterator-
index.js
Outdated
// mingo projections. | ||
ShareDBMingo.prototype.getSnapshot = function(collection, id, fields, options, callback) { | ||
MemoryDB.prototype.getSnapshot.call(this, collection, id, fields, options, function (err, snapshot) { | ||
callback(err, projectSnapshot(fields, 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.
From testing, snapshot
is undefined when err
is present, which causes an uncaught exception in projectSnapshot
.
index.js
Outdated
ShareDBMingo.prototype.query = function(collection, query, fields, options, callback) { | ||
MemoryDB.prototype.query.call(this, collection, query, fields, options, function (err, snapshots, extra) { | ||
var projectSnapshotWithFields = projectSnapshot.bind(null, fields); | ||
callback(err, snapshots.map(projectSnapshotWithFields), extra); |
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.
Same deal, snapshots
could be undefined.
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.
Thanks for taking the initiative on this! Eric and I reviewed it together, and spotted a couple of things that we might look into further.
-
ShareDB is supposed to handle the projections, so ideally we wouldn't need to add anything in this module to do that. Are there any specific things you found that we should fix in ShareDB?
-
At first glance, I think it might be better to cast the format in which we store the docs rather than cast the query. This is how sharedb-mongo works, so I think it would be easier to make it more consistent. We might also be able to share code by creating another module that this and sharedb-mongo depend on or by at least copy-pasting and cross referencing the code.
.travis.yml
Outdated
@@ -1,8 +1,8 @@ | |||
language: node_js | |||
node_js: | |||
- 8 | |||
- 5 |
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.
while we are at it, lets remove 5 and add 6 and 9. we should have all LTS versions and the latest stable node version
index.js
Outdated
@@ -8,10 +17,33 @@ function extendMemoryDB(MemoryDB) { | |||
|
|||
ShareDBMingo.prototype = Object.create(MemoryDB.prototype); | |||
|
|||
ShareDBMingo.prototype.projectsSnapshots = true; |
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.
We shouldn't need to implement projections if the code in ShareDB is working correctly. By default, ShareDB is supposed to implement projections unless the database adapter chooses to implement them natively. Since this is all being done in memory anyway, we might as well leverage the projection code in ShareDB.
Did you discover that projections were not working?
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.
Looks like you're right. Tests relying on projections still pass if all this projection code is removed.
I think I had assumed that projectsSnapshots = true
was required in order for projections to work, but didn't realize that it mean that it controlled whether the adapter or ShareDB was responsible for doing the actual field projection.
Removing! 👋
index.js
Outdated
// Build a query object that mimics how the query would be executed if it were | ||
// made against snapshots persisted with `sharedb-mongo` | ||
// FIXME: This doesn't handle nested doc properties with dots like: {'_m.mtime': 12300} | ||
function castToSnapshotQuery(query) { |
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.
Alternate option to explore would be casting the document data into the same format that sharedb-mongo uses rather than casting the query. Advantage here would be that we'd be more sure it would work like sharedb-mongo for all types of queries. Should possible to make either work, however.
Glad to hear that removing the projection code still works! Sorry that wasn't more clear up front. Might you be able to submit a PR to sharedb to add some documentation or comments indicating the purpose of the |
I also gave |
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.
I talked with Nate, and he's ok with accepting this as is it now, as it brings sharedb-mingo-memory closer to accurately replicating the behavior of sharedb-mongo. We can work on his idea of changing the in-memory doc structure later. I added a skipped test for that case, and added some more comments.
Nate says I can handle the merging and publishing of 1.0.0. Thanks for the PR and for doing the subsequent tweaks!
🎉 |
These changes bring
sharedb-mingo-memory
closer to a drop-in replacement forsharedb-mongo
Changes
mingo
query conditions to mimic execution of how they'd perform on Mongo docs persisted bysharedb-mongo
mingo
dependency to fix<property>: null
queries