Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- const graph = {
- nodes: [{
- id: 0,
- labels: ['Person'],
- properties: {}
- }, {
- id: 1,
- labels: ['City'],
- properties: {
- name: 'Vienna'
- }
- }],
- relationships: [{
- from: 0,
- to: 1,
- type: 'lives in'
- }]
- }
- const nodesMatch = []
- const relationsMatch = []
- function propsStringify(props) {
- if (typeof props === 'undefined') return ''
- let cProps = []
- Object.keys(props).forEach(function(propName) {
- cProps.push(
- '`' + propName + '`:' + JSON.stringify(props[propName])
- )
- })
- if (cProps.length === 0) return ''
- return ' {' + cProps.join(', ') + '}'
- }
- graph.nodes.forEach(function(n) {
- const nodeVar = 'N' + n.id
- const nodeLabels = '`' + n.labels.join(':') + '`'
- const nodeProps = propsStringify(n.properties)
- nodesMatch.push(
- 'MATCH (' + nodeVar + ':' + nodeLabels + nodeProps + ')'
- )
- })
- graph.relationships.forEach(function(r, i) {
- const relVar = 'R' + i
- const rel = '-[' + relVar + ':`' + r.type + '`' + propsStringify(r.properties) + ']->'
- const from = '(N' + r.from + ')'
- const to = '(N' + r.to + ')'
- relationsMatch.push(
- 'MATCH ' + from + rel + to
- )
- })
- const cypherQuery = [
- nodesMatch.join('\r\n'),
- relationsMatch.join('\r\n'),
- 'RETURN *'
- ].join('\r\n')
- console.log(cypherQuery)
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement