GithubHelp home page GithubHelp logo

osguima3 / jooqdsl Goto Github PK

View Code? Open in Web Editor NEW
5.0 1.0 0.0 409 KB

Maven plugin that lets you configure jOOQ in a type-safe way

License: Apache License 2.0

Kotlin 90.40% Groovy 1.69% Java 7.91%
java jooq-generator kotlin

jooqdsl's Introduction

jOOQ DSL Maven Plugin

This plugin is an extension to jOOQ's code generation plugin that lets you define the user model (Forced types) from a type-safe .kts (Kotlin script) file.

Configuration

To use this plugin, just replace jOOQ's groupId, artifactId and version. You can leave all other configuration untouched.

Here is a basic example:

<plugin>
  <groupId>io.github.osguima3.jooqdsl</groupId>
  <artifactId>jooqdsl-maven-plugin</artifactId>
  <version>${jooqdsl.version}</version>
  <executions>
    <execution>
      <id>generate-jooq</id>
      <goals>
        <goal>generate-jooq</goal>
      </goals>
    </execution>
  </executions>
  <configuration>
    <jdbc>
      <driver>org.postgresql.Driver</driver>
      <user>my_user</user>
      <password>my_password</password>
    </jdbc>
    <generator>
      <generate>
        <routines>false</routines>
        <pojos>false</pojos>
        <fluentSetters>true</fluentSetters>
        <javaTimeTypes>true</javaTimeTypes>
      </generate>
      <database>
        <name>org.jooq.meta.postgres.PostgresDatabase</name>
        <inputSchema>public</inputSchema>
        <includes>public.*</includes>
        <recordVersionFields>version</recordVersionFields>
      </database>
      <target>
        <packageName>org.example.project.model</packageName>
      </target>
    </generator>
  </configuration>
</plugin>

Additionally, you will need to add a dependency to the definition model, like so:

<dependency>
    <groupId>io.github.osguima3.jooqdsl</groupId>
    <artifactId>jooqdsl-model</artifactId>
    <version>${jooqdsl.version}</version>
</dependency>

The main advantage over jOOQ's plugin is the way ForcedTypes can be defined:

ModelDefinition {
    tables {
        table("customer") {
            field("id", CustomerId::class) // customer.id will be converted to CustomerId
            field("string", CustomerName::class)
            field("registration", CustomerRegistrationTime::class)
            field("wallet_amount", CustomerWalletAmount::class)

            // Has CustomerRegistrationStatus in java/kotlin, String type in the database 
            field("registration_status") { enum(CustomerRegistrationStatus::class, databaseType = "String") }
            
            // Custom field conversion
            field("address") { custom(converter = CustomerAddressConverter::class) }
        }
    }
}

Thanks to Kotlin's type safety, it will verify that the classes you use exist and that custom converters implement the necessary interfaces.

The plugin also provides default converter implementations for types like java.time.Instant or value objects (POJOs with a single field), popularly used in Domain-Driven Design.

It also provides a simplified interface to define custom converters, using type reification to infer the fromType and toType fields required in jOOQ's Converter so you don't need to add them.

Using jOOQ with test containers

This plugin also integrates with Test containers, which starts a small docker container with the needed database implementation, which can run migration scripts before executing jOOQ's generator.

This is a sample setup for postgres:

<plugin>
  <!-- ... -->
  <dependencies>
    <dependency>
      <groupId>org.testcontainers</groupId>
      <artifactId>postgresql</artifactId>
      <version>1.10.6</version>
    </dependency>
  </dependencies>
  <configuration>
    <container>
      <provider>org.testcontainers.containers.PostgreSQLContainerProvider</provider>
      <version>10.3</version>
      <migrationPath>src/main/resources/db/migration</migrationPath>
    </container>
    <!-- ... -->
  </configuration>
</plugin>

License

This project is licensed under the Apache License

jooqdsl's People

Contributors

dependabot[bot] avatar osguima3 avatar

Stargazers

 avatar  avatar  avatar  avatar  avatar

Watchers

 avatar

jooqdsl's Issues

Upload artifact to central repository

This plugin needs to be registered and uploaded in a public maven repository so that is available as a dependency without downloading the source code.

Also, decide whether to change the groupId/artifactId before submitting

Make postgres testcontainers dependency optional

Currently, the plugin is depending directly on a postgres testcontainer.

This direct dependency should be removed, and the user should be able to choose which database should be used with testcontainers, if any.

Model/converter dependency in plugin

Model and custom converters used in the model definition need to be compiled before the plugin is executed.
This is problematic because it forces code to be split into two maven modules.

Add an option to pre-compile additional necessary classes before the jOOQ's generator executes

Add README file

A README file should be added with an explanation of how to use this plugin

Recommend Projects

  • React photo React

    A declarative, efficient, and flexible JavaScript library for building user interfaces.

  • Vue.js photo Vue.js

    ๐Ÿ–– Vue.js is a progressive, incrementally-adoptable JavaScript framework for building UI on the web.

  • Typescript photo Typescript

    TypeScript is a superset of JavaScript that compiles to clean JavaScript output.

  • TensorFlow photo TensorFlow

    An Open Source Machine Learning Framework for Everyone

  • Django photo Django

    The Web framework for perfectionists with deadlines.

  • D3 photo D3

    Bring data to life with SVG, Canvas and HTML. ๐Ÿ“Š๐Ÿ“ˆ๐ŸŽ‰

Recommend Topics

  • javascript

    JavaScript (JS) is a lightweight interpreted programming language with first-class functions.

  • web

    Some thing interesting about web. New door for the world.

  • server

    A server is a program made to process requests and deliver data to clients.

  • Machine learning

    Machine learning is a way of modeling and interpreting data that allows a piece of software to respond intelligently.

  • Game

    Some thing interesting about game, make everyone happy.

Recommend Org

  • Facebook photo Facebook

    We are working to build community through open source technology. NB: members must have two-factor auth.

  • Microsoft photo Microsoft

    Open source projects and samples from Microsoft.

  • Google photo Google

    Google โค๏ธ Open Source for everyone.

  • D3 photo D3

    Data-Driven Documents codes.