brikteknologier / seraph-model

thin model layer for seraph/neo4j (node.js)
MIT License
111 stars 28 forks source link

Added include query option #131

Closed igor-yamshchykov closed 2 years ago

igor-yamshchykov commented 6 years ago

Adds option to write a condition for the include query so that one can make a call as follows Adds strict property that changes OPTIONAL MATCH to MATCH for relationship selection if set to true

const opts = {
  varName: 'person',
  include: {
    'Student': {
      model: Student, // assume imported at the top
      relName: 'IS_STUDENT_OF',
      many: true,
      strict: true,
      where: { university: 'LP' }
    }
  }
};
Person.findAll(opts, function(err, results) {
  // will result return only Persons that have a relation to Student with a `university` property === 'LP'   
})

supports multiple include queries Additionally this PR adds in option for the query condition, example with multiple queries and in statement below

const opts = {
  varName: 'person',
  include: {
    'Student': {
      model: Student, // assume imported at the top
      relName: 'IS_STUDENT_OF',
      many: true,
      strict: true,
      where: { university: 'LP' }
    },
    'City': {
      model: City, // assume imported at the top
      relName: 'IS_CITY',
      many: true,
      strict: true,
      where: { name: ['Lviv', 'Kyiv', 'Bergen'] }
    }
  }
};
Person.findAll(opts, function(err, results) {
  // will result return only Persons that have a relation to Student with a `university` property === 'LP' 
  // and with has relation to City with `name` value in `['Lviv', 'Kyiv', 'Bergen']`
})

if strict option is not set OPTIONAL MATCH will be performed, so that all the records for the Person will be returned, and only those that have relation will be extended. The only restriction is that all the strict include relations should be placed before the non-strict ones, as MATCH can't follow OPTIONAL MATCH

All tests are passing

igor-yamshchykov commented 6 years ago

@jonpacker Hope it's descriptive enough. Should bring any breaking changes. If you have any questions ping me.