GithubHelp home page GithubHelp logo

refined-anorm's Introduction

Refined Anorm

Adds support for Refinement types to the Anorm database access layer for Scala.

Build Status

Installation

Add the following to your build.sbt:

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:

import refined.anorm._

Then you'll be able to serialize and deserialize refined types with JDBC.

For example:

/** 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:
  4. Edit the username and password fields
  5. Adjust the database URL if necessary.
  6. 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 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

refined-anorm's People

Contributors

derekmorr avatar fthomas avatar

Stargazers

 avatar  avatar

Watchers

 avatar  avatar

Forkers

fthomas ajantiss

refined-anorm's Issues

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.