Skip to content

Instantly share code, notes, and snippets.

@dacr
Created June 28, 2024 23:14
Show Gist options
  • Save dacr/5b807bc6d286304c38f6c9f3c72a4222 to your computer and use it in GitHub Desktop.
Save dacr/5b807bc6d286304c38f6c9f3c72a4222 to your computer and use it in GitHub Desktop.
neo4j through neotypes - query parameters / published by https://github.com/dacr/code-examples-manager #3a466b4c-5eab-4c2b-ae08-70e7530b129e/c342e326ec13b63bf0622849d07f582ab95911ae
// summary : neo4j through neotypes - query parameters
// keywords : scala, scalatest, neo4j, neotypes, cypher, dsl, @testable
// publish : gist
// authors : David Crosson
// license : Apache NON-AI License Version 2.0 (https://raw.githubusercontent.com/non-ai-licenses/non-ai-licenses/main/NON-AI-APACHE2)
// id : 3a466b4c-5eab-4c2b-ae08-70e7530b129e
// created-on : 2024-06-26T14:41:41+02:00
// managed-by : https://github.com/dacr/code-examples-manager
// run-with : scala-cli $file
// ---------------------
//> using scala "3.4.2"
//> using dep io.github.neotypes::neotypes-core:1.1.0
//> using dep org.neo4j.test:neo4j-harness:5.20.0
// ---------------------
import neotypes.GraphDatabase
import neotypes.mappers.ResultMapper
import neotypes.syntax.all.*
import org.neo4j.driver.AuthTokens
import scala.util.Using
import scala.concurrent.{Await, Future}
import scala.concurrent.ExecutionContext.Implicits.global
import scala.concurrent.duration._
case class Someone(firstName: String, lastName: String, age: Int)
val fixture =
"""
|CREATE (:Person {firstName: 'Jane', lastName: 'Fonda', age: 42})
|CREATE (:Person {firstName: 'John', lastName: 'Doe', age: 24})
|CREATE (:Person {firstName: 'Jerry', lastName: 'Tom', age: 84})
|""".stripMargin
val builder =
org.neo4j.harness.Neo4jBuilders
.newInProcessBuilder()
.withFixture(fixture)
Using(builder.build()) { embedded =>
val driver = GraphDatabase.asyncDriver[Future](embedded.boltURI(), AuthTokens.none())
val ageLimit = 64
val peopleFuture =
c"MATCH (p: Person) WHERE p.age < $ageLimit return p"
.query(ResultMapper.fromFunction(Someone.apply))
.list(driver)
val dump = for {
people <- peopleFuture
} yield people.foreach(person => println(s"Hello $person"))
Await.result(dump, 5.seconds)
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment