• Stars
    star
    246
  • Rank 164,726 (Top 4 %)
  • Language
    Scala
  • License
    BSD 2-Clause "Sim...
  • Created over 11 years ago
  • Updated about 1 year ago

Reviews

There are no reviews yet. Be the first to send feedback to the community and the maintainers!

Repository Details

Slick with JodaTime

slick-joda-mapper

CI

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.6.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.6.0

  • Update dependencies
  • Add Scala 3 to cross build

2.5.0

  • Update dependencies
  • Migrate from travis-ci to GitHub Actions
  • Add explicit type annotations to implicit val
  • etc

2.4.2

  • Update joda-time to 2.10.3

2.4.1

  • Support Scala 2.13.0
  • Support Slick 3.3.1

2.4.0

  • Support Slick 3.3.0

2.3.0

  • Support 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.

More Repositories

1

scala-csv

CSV Reader/Writer for Scala
Scala
683
star
2

play-flyway

Flyway plugin for Play >= 2.1
Scala
87
star
3

sbt-slick-codegen

slick-codegen compile hook for sbt
Shell
69
star
4

sbt-musical

NO MUSIC, NO BUILD. Enjoy your compile time.
Scala
63
star
5

play-json4s

Scala
52
star
6

play-json-naming

Custom naming convention for play-json to map snake_case json to camelCase case classes
Scala
50
star
7

staticmock

A mockery-like DSL to replace static method in test.
PHP
42
star
8

sbt-build-files-watcher

show message when sbt build files changed
Scala
27
star
9

play-ascii-art-plugin

play-ascii-art-plugin
Scala
23
star
10

scala-http-client

A thin wrapper of Apache HttpClient
Scala
20
star
11

play-joda-routes-binder

QueryString / Path Binder for Play 2.x
Scala
18
star
12

scala-js-slide

slide show written in Scala.js
Scala
17
star
13

play-json-generic

Scala
14
star
14

scala-fixture

Simple fixture library for Scala
Scala
14
star
15

sbt-slick-codegen-example

Scala
14
star
16

alpaca

A tiny scripting language for web browser automation
Scala
12
star
17

play-reactjs-example

Simple Twitter App with Play2 and React.js
Scala
12
star
18

scala-base64

BASE64ใ‚จใƒณใ‚ณใƒผใƒ€
Scala
10
star
19

scala-base62

Base62 encode/decoder for Scala
Scala
10
star
20

dbcache

Cache library with RDB backend
Scala
9
star
21

lift-json-play-module

lift-json module for Play20
Scala
9
star
22

sbt-install

Install script for sbt
Shell
9
star
23

gfm-editor

Github Flavored Markdown Editor
CoffeeScript
9
star
24

play-dependency-graph

9
star
25

gitter-twitter-bot

Gitter twitter bot
Scala
7
star
26

mvnsearch

Scala
7
star
27

moomin-el

Edit MoinMoin with Emacs
Emacs Lisp
6
star
28

dotemacs

Emacs Lisp
6
star
29

gistub-el

Gistub client for Emacs
Emacs Lisp
6
star
30

nyanda

Database Accessor for cats
Scala
6
star
31

selector

jQuery-like selector
Scala
6
star
32

play-auth-social

An experimental implementation of social login with play-auth
Scala
6
star
33

configpath

typosafe typesafe config
Scala
5
star
34

feedly-tweet

feedly โ†’ twitter
Python
5
star
35

sbt-automkcol

Fork of webdav4sbt(https://bitbucket.org/diversit/webdav4sbt/src)
Scala
5
star
36

unfiltered-scalate-scalaquery-example-bookmarks

JavaScript
5
star
37

jenkins-idobata-notifier-plugin

Idobata Notifier for Jenkins
Java
5
star
38

sbt-shintyoku-doudesuka

้€ฒๆ—ใฉใ†ใงใ™ใ‹๏ผŸ
Scala
5
star
39

scala-itunes

Scala
5
star
40

php-parser-combinator

Parse combinator for PHP
PHP
5
star
41

play-scalate

This project has been migrated to http://github.com/scalate/play-scalate.
Scala
5
star
42

kushi

A very simple HTTP proxy written in Scala and Finagle.
Scala
4
star
43

OAuthSample

OAuthใฎใ‚ตใƒณใƒ—ใƒซใงใ™
Java
4
star
44

hatenacala

ใฏใฆใชใƒ€ใ‚คใ‚ขใƒชใƒผ็”จใฎใ‚ณใƒžใƒณใƒ‰ใƒฉใ‚คใƒณใƒ„ใƒผใƒซ
Scala
4
star
45

seedbed

An alternative to using database fixtures in your Scala unit tests
Scala
4
star
46

helm-find-files-in-project

Emacs Lisp
4
star
47

sbt-hot-reload-example

This project shows how to implement Hot Reload by sbt like Play framework.
Scala
4
star
48

github-timeline-tweet

Github timeline โ†’ twitter
Python
4
star
49

tostring-macro

Scala
3
star
50

play-ws-standalone-json4s

Scala
3
star
51

scalikejdbc-batch.g8

g8 template for scalikejdbc
Scala
3
star
52

stumpwmrc

3
star
53

tototoshi.github.io

JavaScript
3
star
54

json2tsv

convert json log to tsv
Scala
3
star
55

epub4s

Scala
3
star
56

scala-tsv

tsvใƒ•ใ‚กใ‚คใƒซ็”จใฎใ‚†ใƒผใฆใƒใ‚Šใฆใƒ
Scala
3
star
57

cl-misc

common lisp
Common Lisp
2
star
58

ansible-playbook-moinmoin

CSS
2
star
59

dirclean

remove unix backup files
C
2
star
60

gitlab-merge-request

Shell
2
star
61

scalatra-bootstrap.g8

JavaScript
2
star
62

play-twitter-oauth-sample

Sample application of twitter oauth with Play 2.3
Scala
2
star
63

tt-el

emacs lisp utilities especially for me
Emacs Lisp
2
star
64

play-twitterauth

Twitter Login for Play2
Scala
2
star
65

rfc-view-el

RFC viewer for emacs
Emacs Lisp
2
star
66

hcut

Cutter for text files with header
Python
2
star
67

g8.g8

Scala
2
star
68

org-exporter-for-scala

JavaScript
2
star
69

screenrc

2
star
70

firefox-addon-for-twitter

Post the current page to Twitter
JavaScript
2
star
71

my-xmonad.hs

Haskell
2
star
72

unfiltered-example-bookmarks

An example app for Unfiltered, a Scala HTTP/REST toolkit
Scala
2
star
73

queriform

SQL formatter
Scala
2
star
74

slides

JavaScript
2
star
75

anything-find-files-in-project

This project is deprecated and moved to tototoshi/helm-find-files-in-project
Emacs Lisp
2
star
76

dotfiles

dotfiles
Emacs Lisp
2
star
77

play-install

Install script for Play Framework 2.0
Shell
2
star
78

firefox-addon-for-eijiro-on-the-web

่‹ฑ่พž้ƒŽ on the web ็”จใฎFirefoxใ‚ขใƒ‰ใ‚ชใƒณ
JavaScript
2
star
79

moomin

Command line tool for MoinMoin wiki.
Python
1
star
80

php-install

My PHP installer for PHP 5.3-5.6
Shell
1
star
81

php-slack-webhook

PHP
1
star
82

play-reverse-router-extension

Scala
1
star
83

git-grep-el

git grep in emacs
Emacs Lisp
1
star
84

hello-play-26.g8

g8 template for play 2.6
Scala
1
star
85

php-slim-project-template

Template for small Slim Framework projects
HTML
1
star
86

python-install

My Python installer
Shell
1
star
87

lgtmify

LGTM stamp
PHP
1
star
88

freearg

Scala
1
star
89

date_range

Python
1
star
90

memo-el

My instant memo
Emacs Lisp
1
star
91

hello.g8

Scala
1
star
92

csv2xls

Command line utility to convert csv to horrible format
Scala
1
star
93

simple-slides

Simple presentation tool
JavaScript
1
star
94

homebrew-butter

Ruby
1
star
95

scala-ojisan

1
star
96

phpunithelper

Yet another PHPUnit skeleton generator.
PHP
1
star
97

markdown-to-pdf-example

Makefile
1
star
98

phpmethodreplacer

Replace static method at runtime
PHP
1
star
99

sawk

awk in scala?
Scala
1
star
100

elasticsearch-embedded-test-example

Scala
1
star