Coder Social home page Coder Social logo

slick-joda-mapper's Introduction

slick-joda-mapper

Build Status

Enables you to use joda-time with Slick. You can persist DateTime, Instant, LocalDateTime, LocalDate, LocalTime, DateTimeZone with Slick.

Usage

For Slick 3.x

Slick version slick-joda-mapper version
3.3.x 2.4.0
3.2.x 2.3.0
3.1.x 2.2.0
3.0.x 2.0.0
libraryDependencies ++= Seq(
  "com.typesafe.slick" %% "slick" % slickVersion,
  "com.github.tototoshi" %% "slick-joda-mapper" % slickJodaMapperVersion,
  "joda-time" % "joda-time" % "2.7",
  "org.joda" % "joda-convert" % "1.7"
)

Import the appropriate xJodaSupport class suitable for the database you use (H2JodaSupport, PostgresJodaSupport, MySQLJodaSupport, etc.). For example, import Slick's H2Driver API and H2JodaSupport if you are using the H2 database:

import slick.driver.H2Driver.api._
import com.github.tototoshi.slick.H2JodaSupport._

As another example, if you are using the Postgres database:

import slick.driver.PostgresDriver.api._
import com.github.tototoshi.slick.PostgresJodaSupport._

Different drivers can't be mixed, You can't do the following.

import scala.slick.driver.H2Driver.api._
import com.github.tototoshi.slick.JdbcJodaSupport._

Write your own JdbcSupport when you want to write db agnostic model class.

object PortableJodaSupport extends com.github.tototoshi.slick.GenericJodaSupport(yourAbstractDriver)

import PortableJodaSupport._

For Slick 2.x

libraryDependencies ++= Seq(
    "com.typesafe.slick" %% "slick" % "2.1.0",
    "joda-time" % "joda-time" % "2.4",
    "org.joda" % "joda-convert" % "1.6",
    "com.github.tototoshi" %% "slick-joda-mapper" % "1.2.0"
)

Import xJodaSupport(H2JodaSupport, PostgresJodaSupport, MySQLJodaSupport...) class suitable for the database you use. For example, import H2JodaSupport if you are using H2Driver.

import scala.slick.driver.H2Driver.simple._
import com.github.tototoshi.slick.H2JodaSupport._

Different drivers can't be mixed, You can't do the following.

import scala.slick.driver.H2Driver.simple._
import com.github.tototoshi.slick.JdbcJodaSupport._

Write your own JdbcSupport when you want to write db agnostic model class.

object PortableJodaSupport extends com.github.tototoshi.slick.GenericJodaSupport(yourAbstractDriver)

// with play-slick
object PortableJodaSupport extends com.github.tototoshi.slick.GenericJodaSupport(play.api.db.slick.Config.driver)

import PortableJodaSupport._

Code generation

Write a custom code generator that replaces java.sql.Timestamp with whatever Joda classes you prefer.

This example maps java.sql.Timestamp to org.joda.time.DateTime using the Postgres support. When you modify it to suit your needs, make sure that the imports refer to the correct JdbcSupport class for your database and Joda classes.

import scala.slick.{model => m}
import scala.slick.codegen.SourceCodeGenerator

class CustomSourceCodeGenerator(model: m.Model) extends SourceCodeGenerator(model) {

  // add some custom imports
  // TODO: fix these imports to refer to your JdbcSupport and your Joda imports
  override def code = "import com.github.tototoshi.slick.PostgresJodaSupport._\n" + "import org.joda.time.DateTime\n" + super.code

  override def Table = new Table(_) {
    override def Column = new Column(_) {

      // munge rawType -> SQL column type HERE (scaladoc in Slick 2.1.0 is outdated or incorrect, GeneratorHelpers#mapJdbcTypeString does not exist)
      // you can filter on model.name for the column name or model.tpe for the column type
      // your IDE won't like the String here but don't worry, the return type the compiler expects here is String
      override def rawType = model.tpe match {
        case "java.sql.Timestamp"               => "DateTime" // kill j.s.Timestamp
        case _ => {
//          println(s"${model.table.table}#${model.name} tpe=${model.tpe} rawType=${super.rawType}")
          super.rawType
        }
      }
    }
  }
}

Then write a simple app harness to run code generation:

import scala.slick.driver.JdbcProfile

object CodeGen extends App {

  // http://slick.typesafe.com/doc/2.1.0/code-generation.html

  val slickDriver = "scala.slick.driver.PostgresDriver"  // TODO: replace this with your Slick driver
  val jdbcDriver = "org.postgresql.Driver"               // TODO: replace this with your JDBC driver
  val url = "jdbc:postgresql://127.0.0.1:5432/foo"       // TODO: replace this with your database's JDBC URL
  val outputFolder = "src/main/scala"                    // TODO: or whatever output folder you're in the mood for
  val pkg = "foo"                                        // TODO: your package name
  val user = "postgres"                                  // TODO: database username - optional, use forURL supports both with and without credentials
  val password = ""                                      // TODO: database password - optional, use forURL supports both with and without credentials

  val driver: JdbcProfile = scala.slick.driver.PostgresDriver  // TODO: replace this with your Slick driver

  val db = {
    // UNCOMMENT this if your database doesn't need credentials
    // driver.simple.Database.forURL(url, jdbcDriver)
    driver.simple.Database.forURL(url, driver = jdbcDriver, user = user, password = password)
  }

  db.withSession { implicit session =>
    new CustomSourceCodeGenerator(driver.createModel()).writeToFile(slickDriver, outputFolder, pkg)
  }

}

You can run and keep adjusting your source code generation using sbt run. If you get compile errors on the generated Tables.scala, just delete it and try again.

For Slick 1.x

libraryDependencies += "com.github.tototoshi" %% "slick-joda-mapper" % "0.4.1"
import com.github.tototoshi.slick.JodaSupport._

Example

https://github.com/tototoshi/slick-joda-mapper/blob/master/src/test/scala/com/github/tototoshi/slick/JodaSupportSpec.scala

Changelog

2.4.0

  • Suppoer Slick 3.3.0

2.3.0

  • Suppoer Slick 3.2.0

2.2.0

  • Use JdbcProfile since JdbcDriver is deprecated.

2.1.0

  • Removed deprecated Access support

2.0.0

  • Support Slick 3.0.0.

1.2.0

  • Support Slick 2.1.0.

1.1.0

  • Added DateTimeZone support.

1.0.1

  • Added JdbcJodaSupport.

slick-joda-mapper's People

Contributors

tototoshi avatar xuwei-k avatar guersam avatar dk8996 avatar yurique avatar rktoomey avatar mslinn avatar paul-lysak avatar onsails 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.