jamesfer / cypher-query-builder

An flexible and intuitive query builder for Neo4j and Cypher.
http://jamesfer.me/cypher-query-builder/index.html
MIT License
106 stars 22 forks source link

How to add conditions on one property with AND between #157

Open tgroutars opened 4 years ago

tgroutars commented 4 years ago

Seems like a stupid question, but how do I generate a query like this one:

MATCH (someNode:SomeLabel) 
WHERE someNode.someProperty > 1 AND someNode.someProperty < 100

I want to be able to add any type of conditions, not just < and >, so please don't tell me to use between 😛

Since you need to pass an object to and, it seems like there's no way to combine multiple conditions on a single property

tgroutars commented 4 years ago

@jamesfer

tgroutars commented 4 years ago

My current workaround it to use a double negative assertion, so instead of trying to build

condition1 AND condition2 AND condition3

I do:

NOT (NOT condition1 OR NOT condition2 OR NOT condition3)

But let's admit it's not very pretty 😅

jamesfer commented 4 years ago

Hey Thomas,

That's a good pickup. Definitely a bug that I will try to look at.

In the meantime, the only other workaround I can recommend is to add a space after the property name for each condition you want to check on. Very hacky, but it may or may not be a cleaner workaround than the one you came up with:

new cypher.Query().where({ 
  someNode: { 
    someProperty: cypher.greaterThan(1), 
    ['someProperty ']: cypher.lessThan(100),
  },
}).toString();
// WHERE someNode.someProperty > $someProperty AND someNode.someProperty  < $someProperty2;
jamesfer commented 4 years ago

I believe #99 and #14 would fix this issue, allowing you to write:

new cypher.Query().where({
  someNode: {
    someProperty: cypher.and([cypher.greaterThan(1), cypher.lessThan(100)]),
  },
}).toString();
// WHERE someNode.someProperty > $someProperty AND someNode.someProperty  < $someProperty2;
tgroutars commented 4 years ago

Hey Thomas,

That's a good pickup. Definitely a bug that I will try to look at.

In the meantime, the only other workaround I can recommend is to add a space after the property name for each condition you want to check on. Very hacky, but it may or may not be a cleaner workaround than the one you came up with:

new cypher.Query().where({ 
  someNode: { 
    someProperty: cypher.greaterThan(1), 
    ['someProperty ']: cypher.lessThan(100),
  },
}).toString();
// WHERE someNode.someProperty > $someProperty AND someNode.someProperty  < $someProperty2;

Oh I didn't think of that, that's definitely a better workaround than what I did 😄

I can try to open a PR for https://github.com/jamesfer/cypher-query-builder/issues/14 next week

Thanks a lot for this lib btw, it has already helped a lot 🙏