Code Monkey home page Code Monkey logo

neo4s's Introduction

neo4s

Neo4s - scala wrapper for Neo4j. The core ideas were taken from doobie.

Install

libraryDependencies ++= Seq(
  "com.nryanov.neo4s" %% "neo4s-core" % "[version]"
)

Usage example

import neo4s._
import neo4s.implicits._
import cats.effect.{ExitCode, IO, IOApp}
import cats.syntax.flatMap._

object BasicSample extends IOApp {
  private val NEO4J_URI = "bolt://localhost:7687/db/neo4j"

  case class Address(street: String)
  case class Person(name: String, age: Int, address: Address)

  override def run(args: List[String]): IO[ExitCode] = Neo4jTransactor.create[IO](NEO4J_URI).use { transactor =>
    val name = "Sherlock Holmes"
    val age = 60
    val street = "221B Baker Street"

    val query = for {
      _ <- cypher"create (n:Person {name: $name, age: $age, address: $street})".update.run
      person <- cypher"match (n:Person {name: $name}) return n.name, n.age, n.address".query[Person].unique
    } yield person

    query
      .transact(transactor)
      .flatTap(returnedName => IO.delay(println(s"Returned person: $returnedName")))
      // Returned person: Person(Sherlock Holmes,60,Address(221B Baker Street))
      .map(_ => ExitCode.Success)
      .handleErrorWith(error => IO.delay(println(s"Error: ${error.getLocalizedMessage}")).map(_ => ExitCode.Error))
  }
}

Notes

As this library is just a wrapper for java driver, all threading management is done by underlying driver.

Custom mappings

By default, neo4s provides Get[A] and Put[A] (Meta[A]) for:

  • JVM numeric types Byte, Short, Int, Long, Float, and Double;
  • BigDecimal and BigInteger;
  • Boolean, Char, String, and Array[Byte];
  • Date, Time, and Timestamp from the java.sql package;
  • LocalDate, LocalTime, LocalDateTime, OffsetTime, OffsetDateTime and ZonedDateTime from the java.time package; and
  • List[A], Array[A], Map[String, A]
  • single-element case classes wrapping one of the above types.

Deriving Get and Put for custom type

To derive Get and Put instances for custom type you can use existing Meta[_]:

sealed abstract class Digit(val value: Int)
case object One extends Digit(1)
case object Two extends Digit(2)
case class Another(value: Int) extends Digit(value)

implicit val digitMeta: Meta[Digit] = Meta[Int].imap {
  case 1 => One
  case 2 => Two
  case x => Another(x)
} {
  digit => digit.value
}

Also, you can do the same but using existing Put/Get:

implicit val digitGet: Get[Digit] = Get[Int].map {
  case 1 => One
  case 2 => Two
  case x => Another(x)
}

implicit val digitPut: Put[Digit] = Put[Int].contramap(digit => digit.value)

Recommend Projects

  • React photo React

    A declarative, efficient, and flexible JavaScript library for building user interfaces.

  • Vue.js photo Vue.js

    ๐Ÿ–– Vue.js is a progressive, incrementally-adoptable JavaScript framework for building UI on the web.

  • Typescript photo Typescript

    TypeScript is a superset of JavaScript that compiles to clean JavaScript output.

  • TensorFlow photo TensorFlow

    An Open Source Machine Learning Framework for Everyone

  • Django photo Django

    The Web framework for perfectionists with deadlines.

  • D3 photo D3

    Bring data to life with SVG, Canvas and HTML. ๐Ÿ“Š๐Ÿ“ˆ๐ŸŽ‰

Recommend Topics

  • javascript

    JavaScript (JS) is a lightweight interpreted programming language with first-class functions.

  • web

    Some thing interesting about web. New door for the world.

  • server

    A server is a program made to process requests and deliver data to clients.

  • Machine learning

    Machine learning is a way of modeling and interpreting data that allows a piece of software to respond intelligently.

  • Game

    Some thing interesting about game, make everyone happy.

Recommend Org

  • Facebook photo Facebook

    We are working to build community through open source technology. NB: members must have two-factor auth.

  • Microsoft photo Microsoft

    Open source projects and samples from Microsoft.

  • Google photo Google

    Google โค๏ธ Open Source for everyone.

  • D3 photo D3

    Data-Driven Documents codes.