Advertisement
Guest User

Untitled

a guest
May 24th, 2018
71
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 1.31 KB | None | 0 0
  1. const graph = {
  2. nodes: [{
  3. id: 0,
  4. labels: ['Person'],
  5. properties: {}
  6. }, {
  7. id: 1,
  8. labels: ['City'],
  9. properties: {
  10. name: 'Vienna'
  11. }
  12. }],
  13. relationships: [{
  14. from: 0,
  15. to: 1,
  16. type: 'lives in'
  17. }]
  18. }
  19.  
  20. const nodesMatch = []
  21. const relationsMatch = []
  22.  
  23. function propsStringify(props) {
  24. if (typeof props === 'undefined') return ''
  25. let cProps = []
  26. Object.keys(props).forEach(function(propName) {
  27. cProps.push(
  28. '`' + propName + '`:' + JSON.stringify(props[propName])
  29. )
  30. })
  31. if (cProps.length === 0) return ''
  32. return ' {' + cProps.join(', ') + '}'
  33. }
  34.  
  35. graph.nodes.forEach(function(n) {
  36. const nodeVar = 'N' + n.id
  37. const nodeLabels = '`' + n.labels.join(':') + '`'
  38. const nodeProps = propsStringify(n.properties)
  39. nodesMatch.push(
  40. 'MATCH (' + nodeVar + ':' + nodeLabels + nodeProps + ')'
  41. )
  42. })
  43.  
  44. graph.relationships.forEach(function(r, i) {
  45. const relVar = 'R' + i
  46. const rel = '-[' + relVar + ':`' + r.type + '`' + propsStringify(r.properties) + ']->'
  47. const from = '(N' + r.from + ')'
  48. const to = '(N' + r.to + ')'
  49. relationsMatch.push(
  50. 'MATCH ' + from + rel + to
  51. )
  52. })
  53.  
  54. const cypherQuery = [
  55. nodesMatch.join('\r\n'),
  56. relationsMatch.join('\r\n'),
  57. 'RETURN *'
  58. ].join('\r\n')
  59.  
  60. console.log(cypherQuery)
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement