https://github.com/derekmorr/refined-anorm
Refined support for Anorm
https://github.com/derekmorr/refined-anorm
anorm database refined scala
Last synced: 6 months ago
JSON representation
Refined support for Anorm
- Host: GitHub
- URL: https://github.com/derekmorr/refined-anorm
- Owner: derekmorr
- License: mit
- Created: 2017-03-08T20:30:42.000Z (over 9 years ago)
- Default Branch: master
- Last Pushed: 2017-11-04T13:43:56.000Z (over 8 years ago)
- Last Synced: 2025-10-12T07:43:51.937Z (9 months ago)
- Topics: anorm, database, refined, scala
- Language: Scala
- Size: 20.5 KB
- Stars: 2
- Watchers: 1
- Forks: 2
- Open Issues: 1
-
Metadata Files:
- Readme: README.md
- License: LICENSE
Awesome Lists containing this project
README
# Refined Anorm
Adds support for [Refinement types](https://github.com/fthomas/refined) to the
[Anorm](https://github.com/playframework/anorm) database access layer for Scala.
[](https://travis-ci.org/derekmorr/refined-anorm)
# Installation #
Add the following to your `build.sbt`:
``` scala
libraryDependencies += "com.github.derekmorr" %% "refined-anorm" % "0.1"
```
`refined-anorm` is available for Scala 2.11 and 2.12 for Anorm 2.5.
# Usage
Add the following to Anorm models:
```scala
import refined.anorm._
```
Then you'll be able to serialize and deserialize refined types with JDBC.
For example:
```scala
/** 802.1Q Vlan ID */
type VlanId = Int Refined Interval.Closed[W.`0`.T, W.`4095`.T]
type NonBlankString = String Refined And[NonEmpty, Exists[Not[Whitespace]]]
case class Vlan(id: VlanId, name: NonBlankString)
object Vlan {
import refined.anorm._
val parser: RowParser[Vlan] = Macro.namedParser[Vlan]
def getById(id: VlanId)(implicit connection: Connection): Option[Vlan] = {
SQL"""SELECT id, name FROM vlans WHERE id = $id""".as(Vlan.parser.singleOpt)
}
def create(vlan: Vlan)(implicit connection: Connection): Boolean = {
SQL"""INSERT INTO vlans (id, name) VALUES (${vlan.id}, ${vlan.name})""".executeUpdate() == 1
}
}
```
There is a complete, working example in the integration tests (in `src/it`).
# Testing the app
To run unit tests, run
sbt test
To run the integration tests:
1. Install a database (PostgreSQL is the default).
2. Create a database named `refinement`. Grant a user read/write and `create table` access to it.
3. Edit `src/it/resources/application.conf`:
1. Edit the `username` and `password` fields
2. Adjust the database URL if necessary.
4. Run `sbt it:test`
# Code coverage report
To generate a code coverage report run,
sbt clean coverage test coverageReport
The HTML report will be written to `target/scala-2.12/scoverage-report/index.html`.
# Code quality analysis
The project uses the [scapegoat tool](https://github.com/sksamuel/scapegoat) for code quality analysis.
Run run a scapegoat report, run
sbt scapegoat
The HTML report will be written to `target/scala-2.12/scapegoat-report/scapegoat.html`