Code Monkey home page Code Monkey logo

jdub-async's Introduction

jdub-async

A damn simple postgres-async wrapper. Y'know. For asynchronous access to databases.

Requirements

  • Java 6 or above
  • Scala 2.11.x

How To Use

First, specify Jdub-async as a dependency:

resolvers += Resolver.jcenterRepo
libraryDepenencies += "com.kyleu" %% "jdub-async" % "1.0"

(The postgres-async driver is automatically imported)

Second, connect to a database:

val db = new Database("localhost", "mydatabase", "myaccount", "mypassword").open()

Third, run some queries:

// Query returning an optional single result.
case class GetAge(name: String) extends FlatSingleRowQuery[Int] {
  override val sql = "SELECT age FROM people WHERE name = ?"
  override val values = Seq(name)
  override def flatMap(row: Row) = {
    row.as[Int]("age")
  }

}

val age = db.query(GetAge("Old Guy")).getOrElse(-1)
// Query returning a Person object for each row.
case object GetPeople extends Query[Seq[Person]] {
  override val sql = "SELECT name, email, age FROM people"
  override val values = Nil
  override def reduce(rows: Iterator[Row]) = rows.map { row =>
    val name = row.as[String]("name").getOrElse(throw new IllegalStateException())
    val email = row.as[String]("email").getOrElse("")
    val age = row.as[Int]("age").getOrElse(0)
    Person(name, email, age)
  }.toSeq
}

val people = db.query(GetPeople)

Fourth, execute some statements:

case class UpdateEmail(name: String, newEmail: String) extends Statement {
  override val sql = trim("UPDATE people SET email = ? WHERE name = ?")
  override val values = Seq(newEmail, name)
}

val affectedRowCount = db.execute(UpdateEmail("Old Guy", "[email protected]"))

Fifth, extend BaseQueries for common classes:

case object UserQueries extends BaseQueries {
  override protected val tableName = "users"
  override protected val columns = Seq("id", "username", "full_name", "password")
  override protected val searchColumns = Seq("username", "full_name")

  val insert = Insert
  val getById = GetById
  val search = Search
}

Database.execute(UserQueries.insert(User(1, "kyle", "Kyle U", "password")))
val searchResults = Database.query(UserQueries.search("kyle"))

License

Copyright (c) 2015 Kyle Unverferth

Inspired by jdub.

Published under The MIT License, see LICENSE.md

jdub-async's People

Contributors

codahale avatar jklukas avatar ieure avatar kyleu avatar parsnips avatar swenson avatar stevensurgnier avatar jarreds avatar

Watchers

wp.yi' avatar

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.