|
1 | 1 | # scala-db-codegen
|
2 | 2 |
|
3 |
| -Generate Scala code from your database. |
| 3 | +Generate Scala code from your database to use with the incredble library [quill](https://github.com/getquill/quill). |
4 | 4 | Only tested with postgresql, but could in theory work with any jdbc compliant database.
|
5 | 5 |
|
| 6 | +## What does it do? |
| 7 | + |
| 8 | +Say you have some database with this schema |
| 9 | + |
| 10 | +```sql |
| 11 | +create table test_users_main( |
| 12 | + id integer not null, |
| 13 | + name varchar(255), |
| 14 | + primary key (id) |
| 15 | +); |
| 16 | + |
| 17 | +create table articles( |
| 18 | + id integer not null, |
| 19 | + author_id integer not null, |
| 20 | + is_published boolean |
| 21 | +); |
| 22 | + |
| 23 | +ALTER TABLE articles |
| 24 | + ADD CONSTRAINT author_id_fk |
| 25 | + FOREIGN KEY (author_id) |
| 26 | + REFERENCES test_users_main (id); |
| 27 | +``` |
| 28 | + |
| 29 | +scala-db-codegen will then generate "type all the things!" code like this |
| 30 | + |
| 31 | +``` |
| 32 | +package tables |
| 33 | +import java.util.Date |
| 34 | +import io.getquill.WrappedValue |
| 35 | +
|
| 36 | +object Tables { |
| 37 | + ///////////////////////////////////////////////////// |
| 38 | + // Articles |
| 39 | + ///////////////////////////////////////////////////// |
| 40 | + case class Articles(id: Articles.Id, authorId: TestUsersMain.Id, isPublished: Articles.IsPublished) |
| 41 | + object Articles { |
| 42 | + def create(id: Int, authorId: Int, isPublished: Boolean): Articles = { |
| 43 | + Articles(Id(id), TestUsersMain.Id(authorId), IsPublished(isPublished)) |
| 44 | + } |
| 45 | + case class Id(value: Int) extends AnyVal with WrappedValue[Int] |
| 46 | + case class IsPublished(value: Boolean) extends AnyVal with WrappedValue[Boolean] |
| 47 | + } |
| 48 | +
|
| 49 | + ///////////////////////////////////////////////////// |
| 50 | + // TestUsersMain |
| 51 | + ///////////////////////////////////////////////////// |
| 52 | + case class TestUsersMain(id: TestUsersMain.Id, name: TestUsersMain.Name) |
| 53 | + object TestUsersMain { |
| 54 | + def create(id: Int, name: String): TestUsersMain = { |
| 55 | + TestUsersMain(Id(id), Name(name)) |
| 56 | + } |
| 57 | + case class Id(value: Int) extends AnyVal with WrappedValue[Int] |
| 58 | + case class Name(value: String) extends AnyVal with WrappedValue[String] |
| 59 | + } |
| 60 | +} |
| 61 | +``` |
| 62 | + |
| 63 | +It could in theory also generate the code differently. |
| 64 | + |
6 | 65 | ## CLI
|
7 | 66 |
|
8 | 67 | ```shell
|
@@ -37,6 +96,11 @@ Usage: db-codegen [options]
|
37 | 96 |
|
38 | 97 | ## Standalone library
|
39 | 98 |
|
40 |
| -TODO, look at source for now. |
| 99 | +```scala |
| 100 | +// 2.11 only |
| 101 | +libraryDependencies += "com.geirsson" %% "codegen" % "0.1.0" |
| 102 | +``` |
| 103 | +
|
| 104 | +Consult the source usage, at least for now ;) |
41 | 105 |
|
42 | 106 |
|
0 commit comments