This Gradle plugin connects to a database and generates Exposed table definitions for all of its tables.
Add plugin declaration into your build script:
You will need to set up a personal access token for github packages. You can do so following the guide here.
pluginManagement {
repositories {
maven {
name = "GithubPackagesExposedCodeGenerator"
url = uri("https://maven.pkg.github.com/EpicSquid/exposed-code-generator")
credentials {
username = System.getenv("GITHUB_USERNAME")
password = System.getenv("GITHUB_TOKEN")
}
}
}
plugins {
id("dev.epicsquid.exposed.gradle.plugin")
}
}
Use gradle task as
gradle generateExposedCode
Using a task configuration in a build.gradle
file you can specify how you want all the tables to be generated.
The following parameters are available:
exposedCodeGeneratorConfig {
// Set up the database connection parameters.
// Either all of the following can be filled out or a single connectionUrl can be provided
database {
user = "root"
password = "password"
databaseName = "postgres"
databaseDriver = "postgreql"
host = "localhost"
port = "5432"
}
outputDirectory.set(file("build/generated/exposed")) // Specify the output directory for the generated files
generateSingleFile = false // Should all the database tables be in a single file
packageName = "dev.epicsquid.model.generated" // Package name to use for the generated files
// Specify what date-time library to use for timestamp and date columns. Can be any of the following:
// "java-time"
// "jodatime"
// "kotlin-datetime"
// defaults to "java-time" if not present
dateTimeProvider = "kotlin-datetime"
useFullNames = false // Use schema.table name for Table class names. Defaults to true
useDao = true // Use the DAO for generated tables to define references. Defaults to false
// Custom mappings for classes in your project for columns not supported by exposed.
customMappings {
// Use the name of the column you are creating a custom mapping for
create("json_column") {
// The type of the column class (in the case of json, this could be the serializable data class)
columnPropertyClassName = "dev.epicsquid.model.columns.JsonColumnType"
// The column function to call
columnFunctionName = "dev.epicsquid.model.columns.json"
// Force generating a generic type definition for the column function call
isColumnTyped = true
}
}
// Custom mappings for enums using the customEnumeration function
enums {
create("custom_enum") {
// The enum class name in your project
enumClassName = "dev.epicsquid.model.enums.CustomEnum"
// The enum declaration in the database. In postgres this must be the name of the type created in the database.
// For H2 and mysql, this can be the Enum() declaration in sql
databaseDeclaration = "custom_enum"
// If using postgres, provide your implementation of the PGEnum object for conversion
pgEnumClassName = "dev.epicsquid.model.PGEnum"
}
}
// Tables to ignore when generating code. Must be fully qualified with the schema name
ignoreTables = listOf("public.flyway_schema_history")
}
-
connectionURL
-- connection URL as used with JDBC (e.g.jdbc:postgresql://localhost:12346/user=postgres&password=
) -
Specifying each connection parameter:
databaseDriver
, as used with JDBC (e.g.postgresql
,h2
)databaseName
user
password
host
(IPv4)port
ipv6Host
connectionProperties
- map of properties that will be added toconnectionURL
string
All of those parameters are optional; however, the expected behavior is that the user does not mix a connectionURL
with other parameters.