All Projects → olafurpg → Scala Db Codegen

olafurpg / Scala Db Codegen

Licence: apache-2.0
Scala code/boilerplate generator from a db schema

Programming Languages

scala
5932 projects

Projects that are alternatives of or similar to Scala Db Codegen

Jet
Type safe SQL builder with code generation and automatic query result data mapping
Stars: ✭ 373 (+661.22%)
Mutual labels:  sql, postgresql, postgres, code-generator
Jooq
jOOQ is the best way to write SQL in Java
Stars: ✭ 4,695 (+9481.63%)
Mutual labels:  sql, jdbc, postgresql, code-generator
Zombodb
Making Postgres and Elasticsearch work together like it's 2021
Stars: ✭ 3,781 (+7616.33%)
Mutual labels:  sql, postgresql, postgres
Gnorm
A database-first code generator for any language
Stars: ✭ 415 (+746.94%)
Mutual labels:  sql, postgres, code-generation
Goqu
SQL builder and query library for golang
Stars: ✭ 984 (+1908.16%)
Mutual labels:  sql, postgresql, postgres
Requery
requery - modern SQL based query & persistence for Java / Kotlin / Android
Stars: ✭ 3,071 (+6167.35%)
Mutual labels:  sql, jdbc, postgres
Loukoum
A simple SQL Query Builder
Stars: ✭ 305 (+522.45%)
Mutual labels:  sql, postgresql, postgres
Sqlx
🧰 The Rust SQL Toolkit. An async, pure Rust SQL crate featuring compile-time checked queries without a DSL. Supports PostgreSQL, MySQL, SQLite, and MSSQL.
Stars: ✭ 5,039 (+10183.67%)
Mutual labels:  sql, postgresql, postgres
Citus
Distributed PostgreSQL as an extension
Stars: ✭ 5,580 (+11287.76%)
Mutual labels:  sql, postgresql, postgres
Hibernate Springboot
Collection of best practices for Java persistence performance in Spring Boot applications
Stars: ✭ 589 (+1102.04%)
Mutual labels:  sql, jdbc, postgresql
Sqlc
Generate type-safe code from SQL
Stars: ✭ 4,564 (+9214.29%)
Mutual labels:  sql, postgresql, code-generator
Efcore.pg
Entity Framework Core provider for PostgreSQL
Stars: ✭ 838 (+1610.2%)
Mutual labels:  sql, postgresql, postgres
Xo
Command line tool to generate idiomatic Go code for SQL databases supporting PostgreSQL, MySQL, SQLite, Oracle, and Microsoft SQL Server
Stars: ✭ 2,974 (+5969.39%)
Mutual labels:  sql, postgresql, code-generator
Ansible Role Postgresql
Ansible Role - PostgreSQL
Stars: ✭ 310 (+532.65%)
Mutual labels:  sql, postgresql, postgres
Sql Lint
An SQL linter
Stars: ✭ 243 (+395.92%)
Mutual labels:  sql, postgresql, postgres
Massive Js
A data mapper for Node.js and PostgreSQL.
Stars: ✭ 2,521 (+5044.9%)
Mutual labels:  sql, postgresql, postgres
Npgsql
Npgsql is the .NET data provider for PostgreSQL.
Stars: ✭ 2,415 (+4828.57%)
Mutual labels:  sql, postgresql, postgres
Sqliterally
Lightweight SQL query builder
Stars: ✭ 231 (+371.43%)
Mutual labels:  sql, postgresql, postgres
Beam
A type-safe, non-TH Haskell SQL library and ORM
Stars: ✭ 454 (+826.53%)
Mutual labels:  sql, postgresql, postgres
Vscode Sqltools
Database management for VSCode
Stars: ✭ 741 (+1412.24%)
Mutual labels:  sql, postgresql, postgres

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
Note that the project description data, including the texts, logos, images, and/or trademarks, for each open source project belongs to its rightful owner. If you wish to add or remove any projects, please contact us at [email protected].