Skip to content

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

Merged
merged 11 commits into from
Feb 27, 2018

Conversation

zmillman
Copy link
Contributor

@zmillman zmillman commented Feb 20, 2018

These changes bring sharedb-mingo-memory closer to a drop-in replacement for sharedb-mongo

Changes

  • Add support for sharedb projections (already has test coverage)
  • Transform mingo query conditions to mimic execution of how they'd perform on Mongo docs persisted by sharedb-mongo
  • Update mingo dependency to fix <property>: null queries
  • Add tests for ^

@zmillman zmillman changed the title Handle Handle queries on Mongo doc properties (like _id) and fix comparisons to null Feb 20, 2018
.travis.yml Outdated
- 5
- 4
- 0.10
Copy link
Contributor Author

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-

@zmillman zmillman changed the title Handle queries on Mongo doc properties (like _id) and fix comparisons to null Handle projections, queries on Mongo doc properties (like _id) and fix comparisons to null Feb 20, 2018
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));
Copy link
Contributor

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);
Copy link
Contributor

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.

Copy link
Contributor

@nateps nateps left a 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.

  1. 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?

  2. 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
Copy link
Contributor

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;
Copy link
Contributor

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?

Copy link
Contributor Author

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) {
Copy link
Contributor

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.

@nateps
Copy link
Contributor

nateps commented Feb 23, 2018

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 projectsSnapshots property for future developers?
https://github.com/share/sharedb/blob/master/lib/db/index.js#L10

@zmillman
Copy link
Contributor Author

@nateps share/sharedb#196

I also gave 2. above a try, but had trouble getting it to play nicely with how MemoryDb was written (it doesn't really snapshots and docs particularly separate). Happy to collaborate on getting it working, but casting queries was the much easier (but hackier) approach.

Copy link
Contributor

@ericyhwang ericyhwang left a 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!

@nateps
Copy link
Contributor

nateps commented Feb 27, 2018

🎉

@zmillman zmillman deleted the fix/snapshot-queries branch August 15, 2018 17:04
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

Successfully merging this pull request may close these issues.

3 participants