Created
February 6, 2016 18:48
-
-
Save manonthemat/cfb8272ef6695daf7624 to your computer and use it in GitHub Desktop.
copying nodes with labels and properties - hacky
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
// requires cypher-rest | |
// npm i cypher-rest | |
// gist for http://stackoverflow.com/questions/35191765/how-can-we-copy-labels-from-one-node-to-another-in-one-cypher | |
'use strict'; | |
const util = require('util'); | |
const c = require('cypher-rest'); | |
const neoUrl = 'http://127.0.0.1:7474/db/data/transaction/commit'; | |
const copyNode = propertyObjMatch => { | |
return new Promise((resolve, reject) => { | |
// find node(s) via property matching and return it(/them) | |
const cypher = `MATCH (x ${util.inspect(propertyObjMatch)}) RETURN DISTINCT x, LABELS(x) AS labels`; | |
return c.run(cypher, neoUrl, true) // third parameter set to true to always return a list of results | |
.then(results => { | |
// iterate over results and create a copy one by one | |
results.forEach(result => { | |
const copy = `CREATE (copy:${[...result.labels].join(':')}) SET copy = ${util.inspect(result.x)} RETURN copy`; | |
c.run(copy, neoUrl); | |
}); | |
}) | |
}); | |
}; | |
// create a node | |
c.run('CREATE (x:LABEL1:LABEL2 {withProp: "and value", anotherProp: "test"}) RETURN x', neoUrl).then(() => { | |
copyNode({withProp: 'and value', anotherProp: 'test'}) | |
.then(console.log) | |
}); |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment