Last active
February 3, 2026 20:20
-
-
Save dacr/19c3b1018789a5bf62a1f4eaa9bc4c90 to your computer and use it in GitHub Desktop.
neo4j cypher queries - transactions / published by https://github.com/dacr/code-examples-manager #fc858c0e-310b-45af-8dac-38160b94f3a1/dab7bbfaf78b09480c1fd81597fbcae183856af3
This file contains hidden or 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
| // summary : neo4j cypher queries - transactions | |
| // keywords : scala, neo4j, cypher, @testable | |
| // publish : gist | |
| // authors : David Crosson | |
| // license : Apache License Version 2.0 (https://www.apache.org/licenses/LICENSE-2.0.txt) | |
| // id : fc858c0e-310b-45af-8dac-38160b94f3a1 | |
| // created-on : 2024-06-19T16:25:01+02:00 | |
| // managed-by : https://github.com/dacr/code-examples-manager | |
| // run-with : scala-cli $file | |
| // --------------------- | |
| //> using scala "3.4.2" | |
| //> using dep "org.neo4j.test:neo4j-harness:5.20.0" | |
| //> using dep "org.neo4j.driver:neo4j-java-driver:5.21.0" | |
| // --------------------- | |
| import org.neo4j.driver.{GraphDatabase, AuthTokens} | |
| import scala.util.Using | |
| import scala.util.chaining.* | |
| import scala.jdk.CollectionConverters.* | |
| val fixture = | |
| """// ---- NODES | |
| |CREATE (jane:Person {name: 'JANE', age: 32}) | |
| |CREATE (joe:Person {name: 'JOE', age: 42}) | |
| |CREATE (james:Person {name: 'JAMES', age: 64}) | |
| |CREATE (albert:Person {name: 'ALBERT', age: 24}) | |
| | | |
| |// ---- EDGES | |
| |CREATE (jane)-[:KNOWS {since:5}]->(joe) | |
| |CREATE (joe)-[:LIKES {level:32}]->(jane) | |
| |""".stripMargin | |
| val builder = | |
| org.neo4j.harness.Neo4jBuilders | |
| .newInProcessBuilder() | |
| .withFixture(fixture) | |
| Using(builder.build()) { embedded => | |
| Using(GraphDatabase.driver(embedded.boltURI(), AuthTokens.none())) { driver => | |
| Using(driver.session()) { session => | |
| // ---------------- THE UPDATE & DELETE TRANSACTION | |
| Using(session.beginTransaction()) { transaction => | |
| val updateQuery = | |
| """MATCH (n:Person) | |
| |WHERE n.name =~ '.*[A-Z]{1}.*' | |
| |SET n.name = toLower(n.name) | |
| |""".stripMargin | |
| val updateResponse = transaction.run(updateQuery) | |
| val deleteQuery = | |
| """MATCH (n:Person {name:'james'}) | |
| |DETACH DELETE n | |
| |""".stripMargin // DETACH DELETE TO AUTOMATICALLY REMOVE RELATIONSHIPS | |
| val deleteResponse = transaction.run(deleteQuery) | |
| transaction.commit() | |
| }.tap(r => println(r)) | |
| // ------------------------------------------------ | |
| val readQuery = "MATCH (n)-[r]->() RETURN n.name as name" | |
| val readResponse = session.run(readQuery) | |
| val names = readResponse.list().asScala.map(r => r.get("name").asString()).toSet | |
| names.foreach(name => println(s"Found name $name")) | |
| assert(names == Set("jane", "joe")) | |
| // ------------------------------------------------ | |
| }.tap(r => println(r)) | |
| }.tap(r => println(r)) | |
| }.tap(r => println(r)) |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment