Skip to content

vendelieu/jooq-extension

Repository files navigation


jooq plugin extension logo

Gradle Plugin

Introduction

This repository contains a Gradle plugin that integrates a custom jOOQ generator for usage in Spring environment.

The custom generator extends the functionality of jOOQ generated DAOs (Data Access Objects) with helpful functions.

Usage

Applying the Plugin

To apply the plugin to your project, add the following to your build.gradle.kts file:

plugins {
    id("eu.vendeli.jooq.extension") version "0.1.2"
}

Examples

Below are basic examples of extended dao functions:

fun getUserDocument(id: Long, userId: Long) = getOne {
    DOCUMENT.ID.eq(id).and(DOCUMENT.USER_ID.eq(userId))
}

fun getAllUserDocuments(userId: Long) = getAll {
    DOCUMENT.USER_ID.eq(userId)
}

fun create(uuid: String): User = create { this.uuid = uuid }

fun get(uuid: String): User = getOne { USER.UUID.eq(uuid) } ?: create(uuid)

fun update(uuid: String, block: User.() -> Unit): User? = update(USER.UUID.eq(uuid), block)

fun countUsers() = count {
    USER.CREATED_AT.gt(Instant.now().minus(1.days))
}

The generator adds basic CRUD operations tied to table identifier or to passed conditions, also in dao there is access to dslContext for further extension through inheritance.

Configuring the Plugin

The plugin can be configured using the jooq extension.

You need to set as provided custom generator and some generation flags to make it work well.

Here is an example configuration:

generate.apply {
    name = "eu.vendeli.jooq.generator.ExtendedJavaJooqGenerator"
    isDeprecated = false
    isRecords = true
    isPojos = true
    isImmutablePojos = false
    isFluentSetters = true
    isInterfaces = true
    isDaos = true
    isSpringAnnotations = true
}

After this, the generated DAO will be expanded with an interface that covers most use cases, you can also inherit from generated DAO and be able to supplement it using convenient functions. Note also that the interface itself has built-in dslContext.

Running the Plugin

To generate the jOOQ classes, you can use usual generate command:

./gradlew generateJooq

Custom Generator

The custom generator extends the functionality of jOOQ generated DAOs with custom functions. For example, every table and view in your database will generate a org.jooq.DAO implementation that includes custom fetch methods with convenient Kotlin DSL, you can see interface that will be extended there.

License

This project is licensed under the Apache License, Version 2.0.

Contact

If you have any questions about this project, please open an issue on GitHub.

Acknowledgements

This project was inspired by the jOOQ documentation.