Skip to content

typedlabs/anorm-postgres

Folders and files

NameName
Last commit message
Last commit date

Latest commit

 

History

4 Commits
 
 
 
 
 
 
 
 
 
 
 
 

Repository files navigation

anorm-postgres

Anorm Toolkit and Extensions to work with PostgreSQL database

  1. SBT
  2. Connect
  3. Migrations
  4. Supported Extensions
  5. Util

Quick start

SBT

Download

In plugins.sbt, add the following bintray resolver

Resolver.bintrayIvyRepo("typedlabs", "releases")

Connect

To connect to a postgresql database you can add the following settings on an application.conf file

# application.conf
anorm.db.test.driverClassName = "org.postgresql.Driver"
anorm.db.test.jdbcUrl = "jdbc:postgresql://localhost:5432/test"
anorm.db.test.username=dbadmin
anorm.db.test.password=dbadmin
anorm.db.test.maximumPoolSize=5
anorm.db.test.logSql=false

Then you create an anorm.Database object.

  val config = ConfigFactory.load("application.dev.conf")
  val database = Database("test", config)

with anorm.Database you can query and get the results:

  database.withConnection{ implicit connection =>
    SQL"select 1 as total"
      .as(SqlParser.scalar[Long].singleOpt)
  }

  database.withTransaction{ implicit connection =>
    SQL"select 1 as total"
      .as(SqlParser.scalar[Long].singleOpt)
  }  

Migrations

anorm-postgres also has a migration API based on Flyway, migration files will be found on the resources folder under s"db/migration/${db.name}".

To run migration you just pass the database object.

val migration = new Migration(database)

// Careful, it clears all database, useful for testing
migration.clean

migration.migrate

Supported Extensions

  • HStore
  • LTree
  • PostGIS
  • Json(play-json)

Util

anorm-postgres also has an util class DAO[A] which has some useful boilerplate when working with anorm.

About

Anorm Toolkit and Extensions to work with PostgreSQL database

Resources

License

Stars

Watchers

Forks

Releases

No releases published

Packages

No packages published

Languages