Skip to content

Commit

Permalink
add concreteRepo
Browse files Browse the repository at this point in the history
  • Loading branch information
oyvindberg committed Aug 22, 2024
1 parent 4e925ee commit e538a89
Show file tree
Hide file tree
Showing 8 changed files with 71 additions and 28 deletions.
3 changes: 2 additions & 1 deletion site-in/customization/overview.md
Original file line number Diff line number Diff line change
Expand Up @@ -28,7 +28,7 @@ val options = Options(
| `naming` | Configures naming conventions for generated code. See section below |
| `typeOverride` | Defines type overrides for specific database types See section below. |
| `nullabilityOverride` | Defines nullability overrides for specific columns See section below. |
| `generateMockRepos` | Specifies which repositories to generate mock versions for (default is all). |
| `generateMockRepos` | Specifies which repositories to generate mock versions for (default is all). This only works if `concreteRepo=false` (which it is by default) |
| `enableFieldValue` | Controls whether to enable `FieldValue` code generation for specific repositories (default is disabled). |
| `enableStreamingInserts` | Controls whether to enable [streaming inserts](../other-features/streaming-inserts.md) |
| `enableTestInserts` | Controls whether to enable [test inserts](../other-features/testing-with-random-values.md) for specific repositories (default is none). |
Expand All @@ -38,6 +38,7 @@ val options = Options(
| `keepDependencies` | Specifies whether to generate [table dependencies](../type-safety/type-flow.md) in generated code even if you didn't select them (default is `false`). |
| `rewriteDatabase` | Let's you perform arbitrary rewrites of database schema snapshot. you can add/remove rows, foreign keys and so on. |
| `openEnums` | Controls if you want to tag tables ids as [open string enums](../type-safety/open-string-enums.md) |
| `concreteRepo` | Controls if you want repositories be generated as just a class (if true) or an interface and class (if false) |

## Development options

Expand Down
3 changes: 2 additions & 1 deletion typo/src/scala/typo/Options.scala
Original file line number Diff line number Diff line change
Expand Up @@ -26,7 +26,8 @@ case class Options(
rewriteDatabase: MetaDb => MetaDb = identity,
executionContext: ExecutionContext = ExecutionContext.global,
schemaMode: SchemaMode = SchemaMode.MultiSchema,
openEnums: Selector = Selector.None
openEnums: Selector = Selector.None,
concreteRepo: Boolean = false
)

object Options {
Expand Down
3 changes: 2 additions & 1 deletion typo/src/scala/typo/internal/InternalOptions.scala
Original file line number Diff line number Diff line change
Expand Up @@ -19,5 +19,6 @@ case class InternalOptions(
naming: Naming,
pkg: sc.QIdent,
readonlyRepo: Selector,
typeOverride: TypeOverride
typeOverride: TypeOverride,
concreteRepo: Boolean
)
17 changes: 17 additions & 0 deletions typo/src/scala/typo/internal/codegen/FilesRelation.scala
Original file line number Diff line number Diff line change
Expand Up @@ -329,6 +329,23 @@ case class FilesRelation(
sc.File(names.RepoImplName, str, secondaryTypes = Nil, scope = Scope.Main)
}

def ConcreteRepoImplFile(dbLib: DbLib, repoMethods: NonEmptyList[RepoMethod]): sc.File = {
val renderedMethods: List[sc.Code] = repoMethods.toList.flatMap { repoMethod =>
dbLib.repoSig(repoMethod).toOption.map { sig =>
code"""|${repoMethod.comment.fold("")(c => c + "\n")}$sig = {
| ${dbLib.repoImpl(repoMethod)}
|}""".stripMargin
}
}
val str =
code"""|class ${names.RepoName} {
| ${renderedMethods.mkCode("\n")}
|}
|""".stripMargin

sc.File(names.RepoName, str, secondaryTypes = Nil, scope = Scope.Main)
}

def RepoMockFile(dbLib: DbLib, idComputed: IdComputed, repoMethods: NonEmptyList[RepoMethod]): sc.File = {
val maybeToRowParam: Option[sc.Param] =
repoMethods.toList.collectFirst { case RepoMethod.InsertUnsaved(_, _, unsaved, _, _, _) =>
Expand Down
21 changes: 14 additions & 7 deletions typo/src/scala/typo/internal/codegen/FilesSqlFile.scala
Original file line number Diff line number Diff line change
Expand Up @@ -6,14 +6,21 @@ import typo.internal.codegen.DbLib.RowType

case class FilesSqlFile(script: ComputedSqlFile, naming: Naming, options: InternalOptions) {
val relation = FilesRelation(naming, script.names, script.maybeCols.toOption, None, options, fks = Nil)
val all: List[sc.File] = List(
val all: List[sc.File] = List[Iterable[sc.File]](
relation.RowFile(RowType.Readable, comment = None, maybeUnsavedRow = None),
relation.FieldsFile,
for {
dbLib <- options.dbLib
} yield relation.RepoTraitFile(dbLib, script.repoMethods),
for {
dbLib <- options.dbLib
} yield relation.RepoImplFile(dbLib, script.repoMethods)
if (options.concreteRepo)
for {
dbLib <- options.dbLib
} yield relation.ConcreteRepoImplFile(dbLib, script.repoMethods)
else
List(
for {
dbLib <- options.dbLib
} yield relation.RepoTraitFile(dbLib, script.repoMethods),
for {
dbLib <- options.dbLib
} yield relation.RepoImplFile(dbLib, script.repoMethods)
).flatten
).flatten
}
28 changes: 18 additions & 10 deletions typo/src/scala/typo/internal/codegen/FilesTable.scala
Original file line number Diff line number Diff line change
Expand Up @@ -275,20 +275,28 @@ case class FilesTable(table: ComputedTable, fkAnalysis: FkAnalysis, options: Int
} yield relation.RepoMockFile(dbLib, id, repoMethods)
else None

val all: List[sc.File] = List(
val all: List[sc.File] = List[Iterable[sc.File]](
RowFile,
relation.FieldsFile,
UnsavedRowFile,
for {
repoMethods <- table.repoMethods
dbLib <- options.dbLib
} yield relation.RepoTraitFile(dbLib, repoMethods),
for {
repoMethods <- table.repoMethods
dbLib <- options.dbLib
} yield relation.RepoImplFile(dbLib, repoMethods),
if (options.concreteRepo)
for {
repoMethods <- table.repoMethods
dbLib <- options.dbLib
} yield relation.ConcreteRepoImplFile(dbLib, repoMethods)
else
List(
for {
repoMethods <- table.repoMethods
dbLib <- options.dbLib
} yield relation.RepoTraitFile(dbLib, repoMethods),
for {
repoMethods <- table.repoMethods
dbLib <- options.dbLib
} yield relation.RepoImplFile(dbLib, repoMethods),
maybeMockRepo
).flatten,
relation.FieldValueFile,
maybeMockRepo,
IdFile
).flatten
}
21 changes: 14 additions & 7 deletions typo/src/scala/typo/internal/codegen/FilesView.scala
Original file line number Diff line number Diff line change
Expand Up @@ -6,15 +6,22 @@ import typo.internal.codegen.DbLib.RowType

case class FilesView(view: ComputedView, options: InternalOptions) {
val relation = FilesRelation(view.naming, view.names, Some(view.cols), None, options, fks = Nil)
val all: List[sc.File] = List(
val all: List[sc.File] = List[Iterable[sc.File]](
relation.RowFile(RowType.Readable, view.view.comment, maybeUnsavedRow = None),
relation.FieldsFile,
for {
dbLib <- options.dbLib
} yield relation.RepoTraitFile(dbLib, view.repoMethods),
for {
dbLib <- options.dbLib
} yield relation.RepoImplFile(dbLib, view.repoMethods),
if (options.concreteRepo)
for {
dbLib <- options.dbLib
} yield relation.ConcreteRepoImplFile(dbLib, view.repoMethods)
else
List(
for {
dbLib <- options.dbLib
} yield relation.RepoTraitFile(dbLib, view.repoMethods),
for {
dbLib <- options.dbLib
} yield relation.RepoImplFile(dbLib, view.repoMethods)
).flatten,
relation.FieldValueFile
).flatten
}
3 changes: 2 additions & 1 deletion typo/src/scala/typo/internal/generate.scala
Original file line number Diff line number Diff line change
Expand Up @@ -51,7 +51,8 @@ object generate {
naming = naming,
pkg = pkg,
readonlyRepo = publicOptions.readonlyRepo,
typeOverride = publicOptions.typeOverride
typeOverride = publicOptions.typeOverride,
concreteRepo = publicOptions.concreteRepo
)
val customTypes = new CustomTypes(customTypesPackage)
val genOrdering = new GenOrdering(customTypes, options.pkg)
Expand Down

0 comments on commit e538a89

Please sign in to comment.