Ecosyste.ms: Awesome
An open API service indexing awesome lists of open source software.
https://github.com/klahap/pgen
Generate Kotlin Exposed tables from a PostgreSQL database schema
https://github.com/klahap/pgen
database-integration generate-code gradle-plugin kotlin-exposed
Last synced: 18 days ago
JSON representation
Generate Kotlin Exposed tables from a PostgreSQL database schema
- Host: GitHub
- URL: https://github.com/klahap/pgen
- Owner: klahap
- License: mit
- Created: 2024-11-25T09:24:46.000Z (about 1 month ago)
- Default Branch: main
- Last Pushed: 2024-11-25T09:31:19.000Z (about 1 month ago)
- Last Synced: 2024-11-25T10:38:11.034Z (about 1 month ago)
- Topics: database-integration, generate-code, gradle-plugin, kotlin-exposed
- Language: Kotlin
- Homepage:
- Size: 77.1 KB
- Stars: 0
- Watchers: 1
- Forks: 0
- Open Issues: 0
-
Metadata Files:
- Readme: README.md
- License: LICENSE
Awesome Lists containing this project
README
# Kotlin Exposed Table Generator Gradle Plugin
## Overview
The **Kotlin Exposed Table Generator** is a Gradle plugin designed to generate
Kotlin [Exposed](https://github.com/JetBrains/Exposed) table definitions directly from a PostgreSQL database schema.
Simplify your workflow by automating the creation of table mappings and keeping them in sync with your database.## Features
- Generate Kotlin Exposed DSL table definitions.
- Filter tables by schema for precise control.
- Keep your code synchronized with database schema changes effortlessly.## Installation
Add the plugin to your `build.gradle.kts`:
```kotlin
plugins {
id("io.github.klahap.pgen") version "$VERSION"
}
```## Configuration
To configure the plugin, add the `pgen` block to your `build.gradle.kts`:
```kotlin
pgen {
dbConnectionConfig(
url = System.getenv("DB_URL"), // Database URL
user = System.getenv("DB_USER"), // Database username
password = System.getenv("DB_PASSWORD") // Database password
)
packageName("io.example.db") // Target package for generated tables
tableFilter {
addSchemas("public") // Include only specific schemas (e.g., "public")
}
outputPath("./output") // Output directory for generated files
}
```### Environment Variables
Make sure to set the following environment variables:
- `DB_URL`: The connection URL for your PostgreSQL database.
- `DB_USER`: Your database username.
- `DB_PASSWORD`: Your database password.## Running the Plugin
Once configured, generate your Kotlin Exposed table definitions by running:
```bash
./gradlew pgen
```This will create Kotlin files in the specified `outputPath`.
## Example
### Input: Database Schema
Assume a PostgreSQL schema with the following table:
```sql
CREATE TYPE status AS ENUM ('ACTIVE', 'INACTIVE');CREATE TABLE users
(
id SERIAL PRIMARY KEY,
name TEXT NOT NULL,
status status
);
```### Output: Generated Kotlin File
The plugin will generate a Kotlin Exposed DSL table:
```kotlin
package io.example.dbimport org.jetbrains.exposed.sql.Table
enum class Status(
override val pgEnumLabel: String,
) : PgEnum {
ACTIVE(pgEnumLabel = "ACTIVE"),
INACTIVE(pgEnumLabel = "INACTIVE");override val pgEnumTypeName: String = "public.status"
}object Users : Table("users") {
val id: Column = integer(name = "id")
val name: Column = text(name = "name")
val status: Column = customEnumeration(
name = "status",
sql = "status",
fromDb = { getPgEnumByLabel(it as String) },
toDb = { it.toPgObject() },
)override val primaryKey: Table.PrimaryKey = PrimaryKey(id, name = "users_pkey")
}
```## Contributing
Contributions are welcome! Feel free to open issues or submit pull requests to improve the plugin.
## License
This project is licensed under the [MIT License](LICENSE).