This is crazy but the following code seemed to work well when I ran it a couple of weeks ago - now I am returning to study it more closely but I can't find it again in the documentation! Does anyone recognise it please??
//
const person1Name = 'Alice'
const person2Name = 'David'
try {
// To learn more about the Cypher syntax, see The Neo4j Cypher Manual v5 - Cypher Manual
// The Reference Card is also a good resource for keywords Cypher Cheat Sheet
const writeQuery = MERGE (p1:Person { name: $person1Name }) MERGE (p2:Person { name: $person2Name }) MERGE (p1)-[:KNOWS]->(p2) RETURN p1, p2
// Write transactions allow the driver to handle retries and transient errors
const writeResult = await session.writeTransaction(tx =>
tx.run(writeQuery, { person1Name, person2Name })
)
writeResult.records.forEach(record => {
const person1Node = record.get('p1')
const person2Node = record.get('p2')
console.log(
Created friendship between: ${person1Node.properties.name}, ${person2Node.properties.name}
)
})
const readQuery = MATCH (p:Person) WHERE p.name = $personName RETURN p.name AS name
const readResult = await session.readTransaction(tx =>
tx.run(readQuery, { personName: person1Name })
)
readResult.records.forEach(record => {
console.log(Found person: ${record.get('name')}
)
})
} catch (error) {
console.error('Something went wrong: ', error)
} finally {
await session.close()
}
// Don't forget to close the driver connection when you're finished with it
await driver.close()
})();