mdmoss / doobie-codegen

Generate Doobie database code from sql statements.
MIT License
10 stars 7 forks source link

doobie-codegen

Generates Doobie database code from sql schema files.

Here's some sample output. Please excuse the style :)

Features

Output

All tables
Tables with a primary key

find takes the type of the primary key column as a parameter.

multiget and friends are stable! If you ask for B, C, and A, you'll get matching rows back in that order.

getByField and multigetByField are generated for columns containing foreign keys.

Caveats

Currently only targets PostgreSQL. Attempts to output straightforward code and tests.

This probably won't work unless your sql looks like my sql. Could be made more robust.

Definitely a work in progress! But has CI, now.

Usage

I tend to add a project to my build.sbt like the following.

lazy val gen = (project in file("doobie-codegen"))
  .settings(
    scalaVersion := "2.11.7",
    resolvers += "Jitpack" at "https://jitpack.io",
    libraryDependencies += "com.github.mdmoss" %% "doobie-codegen" % "v0.3.1"
  )

Then in the doobie-codegen directory, I add an object with a main something like the following:

package gen

import mdmoss.doobiegen.Runner.{Target, TestDatabase}

object Generator {

  def main(args: Array[String]) {

    val target = Target(
      "schema/",
      TestDatabase(
        "org.postgresql.Driver",
        "jdbc:postgresql:db",
        "user",
        "password"
      ),
      "src",
      "com.project.db"
    )

    mdmoss.doobiegen.Runner.run(target)
  }
}

Then run the project with sbt gen/run

Development

Tests require a local postgres instance on 5432 with a user called test, password test. See the sql/make-test.sql helper.