https://github.com/alenkacz/postgres-scala
Asynchronous library to access PostgreSQL using Scala
https://github.com/alenkacz/postgres-scala
postgresql scala
Last synced: 9 months ago
JSON representation
Asynchronous library to access PostgreSQL using Scala
- Host: GitHub
- URL: https://github.com/alenkacz/postgres-scala
- Owner: alenkacz
- License: mit
- Created: 2017-01-11T21:05:30.000Z (over 9 years ago)
- Default Branch: master
- Last Pushed: 2017-08-19T06:27:07.000Z (almost 9 years ago)
- Last Synced: 2025-10-10T03:32:11.168Z (9 months ago)
- Topics: postgresql, scala
- Language: Scala
- Homepage:
- Size: 141 KB
- Stars: 5
- Watchers: 4
- Forks: 1
- Open Issues: 5
-
Metadata Files:
- Readme: README.md
- License: LICENSE
Awesome Lists containing this project
README
# postgres-scala
---------------------
[](https://travis-ci.org/alenkacz/postgres-scala) [  ](https://bintray.com/alenkacz/maven/postgres-scala_2.12/_latestVersion)
Asynchronous postgres client for Scala 2.12. It does not reimplement the wheel because currently it uses [postgresql-async](https://github.com/mauricio/postgresql-async) under the hood. So it is just a nicer API for this library.
## Motivation
There are several very good postgresql clients for Scala but in my opinion there is still room for another one for these reasons:
- [scalike](https://github.com/scalikejdbc/scalikejdbc) (asynchronous implementation is not in production-ready state)
- [postgresql-async](https://github.com/mauricio/postgresql-async) (the API is too low-level)
- [slick](https://github.com/slick/slick) (too heavy for plain SQL usage)
Sometimes your just want to write plain SQL queries (e.g. for performance reasons) and map them to domain objects by hand. This library enable that with a nice scala API.
## Getting started
Current version of the library can be found on [bintray](https://bintray.com/alenkacz/maven/postgres-scala_2.12/_latestVersion)
SBT
```scala
resolvers += Resolver.jcenterRepo
```
```scala
"cz.alenkacz.db" %% "postgres-scala_2.12" % ""
```
Gradle
```groovy
repositories {
jcenter()
}
```
```groovy
compile 'cz.alenkacz.db:postgres-scala_2.12:'
```
### Example usage
```scala
package cz.alenkacz.db.postgresscala
import com.typesafe.config.ConfigFactory
import cz.alenkacz.db.postgresscala._
import scala.concurrent.ExecutionContext.Implicits.global
object Example {
def main(agrs: Array[String]): Unit = {
implicit val connection: Connection = PostgresConnection.fromConfig(ConfigFactory.load())
val testListValue = List("a", "b")
sql"SELECT * FROM table WHERE a IN ($testListValue)".query(row => DomainObject(row(0).string(), row(1).int()))
sql"SELECT id FROM table WHERE a IN ($testListValue)".queryValue[Int]()
val testValue = 1
sql"SELECT * FROM table WHERE b=$testValue".query(row => DomainObject(row("a").string(), row("b").int()))
connection.inTransaction(c => {
for {
val1 <- sql"SELECT COUNT(*) FROM abc".count()(c)
val2 <- sql"SELECT COUNT(*) FROM withuniquekey".count()(c)
} yield val1.getOrElse(0L) + val2.getOrElse(0L)
})
}
}
case class DomainObject(testString: String, testInt: Int)
```
## Convenient builder
Since [typesafe config](https://github.com/typesafehub/config) is de facto standard configuration library for scala, there is an easy way how to create postgres client directly from config. To do that, you need to have a config similar to this one:
```
database {
connectionString = "jdbc:postgresql://localhost:5432/test_db?user=your_username&password=your_password"
maxMessageSize = 16777216 // optional
connectTimeout = 5 seconds // optional
disconnectTimeout = 5 seconds // optional
testTimeout = 5 seconds // optional
queryTimeout = 6 seconds // optional
// optional, do not use pool section if you do not want pooled connection
pool {
maxConnections = 3
idleTime = 5 seconds
maxQueueSize = 10
}
}
```
To create the connection in the code, all you have to do is call
```scala
import cz.alenkacz.db.postgresscala
import import com.typesafe.config.ConfigFactory
val connection = PostgresConnection.fromConfig(ConfigFactory.load().getConfig("database"))
```