Giter Site home page Giter Site logo

scala-db-codegen's Introduction

scala-db-codegen Build Status Maven

Generate Scala code from your database to use with the incredible library quill. Only tested with postgresql, but could in theory work with any jdbc compliant database.

What does it do?

Say you have some database with this schema

create table test_user(
  id integer not null,
  name varchar(255),
  primary key (id)
);

create table article(
  id integer not null,
  author_id integer,
  is_published boolean
);

ALTER TABLE article
  ADD CONSTRAINT author_id_fk
  FOREIGN KEY (author_id)
  REFERENCES test_user (id);

scala-db-codegen will then generate "type all the things!" code like this

package com.geirsson.codegen
import java.util.Date
import io.getquill.WrappedValue

object Tables {
  /////////////////////////////////////////////////////
  // Article
  /////////////////////////////////////////////////////
  case class Article(id: Article.Id, authorId: Option[TestUser.Id], isPublished: Option[Article.IsPublished])
  object Article {
    def create(id: Int, authorId: Option[Int], isPublished: Option[Boolean]): Article = {
      Article(Id(id), authorId.map(TestUser.Id.apply), isPublished.map(IsPublished.apply))
    }
    case class Id(value: Int)              extends AnyVal with WrappedValue[Int]
    case class IsPublished(value: Boolean) extends AnyVal with WrappedValue[Boolean]
  }

  /////////////////////////////////////////////////////
  // TestUser
  /////////////////////////////////////////////////////
  case class TestUser(id: TestUser.Id, name: Option[TestUser.Name])
  object TestUser {
    def create(id: Int, name: Option[String]): TestUser = {
      TestUser(Id(id), name.map(Name.apply))
    }
    case class Id(value: Int)      extends AnyVal with WrappedValue[Int]
    case class Name(value: String) extends AnyVal with WrappedValue[String]
  }
}

Type all the things!

It could in theory also generate the code differently.

CLI

Download 13kb bootstrap script scala-db-codegen and execute it. The script will download all dependencies on first execution.

# print to stdout, works with running postgres instance on
# localhost:5432 with user "postgres", password "postgres" and database "postgres"
$ scala-db-codegen
# Override any default settings with flags.
$ scala-db-codegen --user myuser --password mypassword --url jdbc:postgresql://myhost:8888/postgres --file Tables.scala --type-map "bool,Boolean;int4,Int;int8,Long"
...

For more details:

$ scala-db-codegen --help
Usage: scala-db-codegen [options]
  --usage
        Print usage and exit
  --help | -h
        Print help message and exit
  --user  <value>
        user on database server
  --password  <value>
        password for user on database server
  --url  <value>
        jdbc url
  --schema  <value>
        schema on database
  --jdbc-driver  <value>
        only tested with postgresql
  --imports  <value>
        top level imports of generated file
  --package  <value>
        package name for generated classes
  --type-map  <value>
        Which types should write to which types? Format is: numeric,BigDecimal;int8,Long;...
  --excluded-tables  <value>
        Do not generate classes for these tables.
  --file  <value>
        Write generated code to this filename. Prints to stdout if not set.

Standalone library

Maven

// 2.11 only
libraryDependencies += "com.geirsson" %% "scala-db-codegen" % "<version>"

Consult the source code, at least for now ;)

SBT

Clone this repo into a subdirectory of your project. In your build.sbt:

import sbt.Project.projectToRef
lazy val `scala-db-codegen` = ProjectRef(file("scala-db-codegen"), "scala-db-codegen")
lazy val codegen = project.dependsOn(`scala-db-codegen`)

Run from sbt:

codegen/runMain com.geirsson.codegen.Codegen --package tables --file myfile.scala

Hack on the code to further customize to your needs.

Why not slick-codgen?

The Slick code generator is excellent and please use that if you are using Slick. Really, the slick codegenerator is extremely customizable and can probably even do stuff that this library does.

I created this library because I struggled to get the slick code generator to do exactly what I wanted. Instead of learning more about slick models and which methods to override on the slick code generator, I decided to roll my own code generator and hopefully learn more about jdbc along the way :)

Changelog

0.2.1

  • No longer abort on missing key in --type-map, see #3. Thanks @nightscape!

0.2.0

  • Map nullable columns to Option types.
  • Rename maven artifact name to scala-db-codegen for consistency.

0.1.0

  • Basic code generation
  • Command line interface

scala-db-codegen's People

Contributors

olafurpg avatar morpheby avatar felixmulder avatar nightscape avatar

Watchers

James Cloos avatar  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.