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

Column comparisons & Fix unique index bug #847

Merged
merged 6 commits into from
Jul 30, 2020

Conversation

radex
Copy link
Contributor

@radex radex commented Jun 29, 2020

Fixes #636.

This introduces a new class of filters, that do comparisons on document level, instead of on property level.

The key use case is allowing column comparisons, e.g. fetch all tasks where task.foo > task.bar. The syntax is this:

// simple column comparison
{ foo: { $$gt: 'bar' } } 

// for more flexibility, function can be passed
{ foo: { $$gt: doc => doc.bar + 1 } }

I took care not to affect hot path's performance. So I don't do any $$ checks — instead, all LokiOps now take a third argument (document) that's passed around - but only $$ops actually use it.

Nested filters - $or, $and, $not, $len, $size, $elemMatch all work.

I'm also thinking of adding object-level $filter .find({ $filter: (doc) => xxx }) and extending property-level $filter .find({ foo: { $filter: (foo, doc) => xxx } }) for object-level comparisons without having to completely resort to using .where(). But I didn't do it in this PR.

Additionally, I fix #839 here. Plain objects {} are not safe for use with arbitrary keys. Since Loki doesn't do ES6, I used prototype-less Objects to fix the issue.

Copy link
Owner

@techfort techfort left a comment

Choose a reason for hiding this comment

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

LGTM - i won't have time to test this for a few days, so i'm going on trust here :)

@techfort techfort merged commit 1c3114e into techfort:master Jul 30, 2020
@radex
Copy link
Contributor Author

radex commented Jul 30, 2020

I hope so! I'm shipping this in production for the last couple of weeks and nothing broke :)

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.

Unique Indices will fail for some user input Column comparisons
2 participants