canjs / can-query-logic

Perform data queries and compare queries against each other. Provides logic useful for data caching and real-time behavior.

Home Page:https://canjs.com/doc/can-query-logic.html

Geek Repo:Geek Repo

Github PK Tool:Github PK Tool

Add supports for $and, $not, and $all for complex Mongo queries

matthewp opened this issue · comments

I pushed what we worked on into the and-all-not branch.

What I think needs to be done:

  1. A types/values-and.js needs to be created with a isMember method. It should work something like:
new ValuesAnd([
  new KeysAnd({tags: new is.All(['sbux']) }),
  new KeysAnd({tags: new ValuesNot( new is.All(['dfw'])  })
]).isMember({
  tags: ["sbux"]
}) //-> true
  1. You'll need to make serializers/basic-query.js able to hydrate to a structure like above. A test in serializers/basic-query-test.js should look like:
	var query = {
		filter: {
			$and: [
				{tags: {$all: ['sbux']}},
				{tags: {$not: {$all: ['dfw']}}}
		  ]
		}
    };

    var converter = makeBasicQueryConvert(EmptySchema);

    var basicQuery = converter.hydrate(query);

    assert(basicQuery.filter instanceof ValuesAnd);

this test is already started

  1. If everything prior to this is working, you should be able to see if your fixtures work. If they do, that's great. Though there's some extra work to make sure $not, $all and $and really work in anything but your exact scenario.

  2. Implement comparisons with $all. What is a union of {$all: ["a"]} and {$all: ["b"]}? I suspect that UNKNOWN will need to be the result. But that's ok.

  3. Implement comparisons and tests with $all and all the other operators. For the most part, I think we might be able to actually test that trying to do a set.union( new GreaterThan(5), new All([5,6])) actually throws an error (as it does currently). After all, comparisons like GreaterThan() operate on a single key value. All() operates on an array of items. We might even want to make an array-comparisons.js and put All in there, so $elemMatch and $size could eventually land.

  4. ValueNot should probably be added to comparisons and tested that way. Or its hydration should be able to invert most comparisons.

    For example, we should be able to do something like

    union( new ValueNot( new LessThan(5 ) ),
      new LessThan(5) )
    

    But, if someone tried to hydrate {$not: {$lt: 5}} this should really be hydrated to {$gte: 5}

  5. We need to make serializing work for $not, $and, and $all