Git Product home page Git Product logo

any-dao-spring's Introduction

Maven central

any-dao-spring

Any-dao-spring provides an any-dao Kotlin extension for spring JPA repositories.

Why? to have typesafe queries which may be composed (similar to JpaSepcificationExecutor) but in pure Kotlin

Define repository:

@Repository
interface UserRepository : KotlinAnyDaoRepository<UserODB, Long>

It extends both JpaSpecificationExecutor<ENTITY> and JpaRepository<ENTITY, KEY>.

Use it like this:

val todos = userRepository.findAllBy(TodoODB::class) {
    it.join(UserODB::currentTodo, JoinType.LEFT)
    select(it[UserODB::currentTodo])
}
val users = userRepository.findAllBy {
    it[UserODB::email] isIn subqueryFrom(TodoODB::class) {
        it[TodoODB::importance] greaterThan 10
        select(it[TodoODB::name])
    }
}

The good practice is to compose complicated query with named specifications that can be copmosed one into another:

// specify extension functions with predicates
fun KRoot<UserODB>.hasAdminOrNormalRole() {
    this[UserODB::userRoles].isMember(setOf(UserRole.ADMIN, UserRole.NORMAL))
}
fun KRoot<UserODB>.hasActiveStatus() {
    this[UserODB::status] eq UserStatus.ACTIVE
}
fun KRoot<UserODB>.isUserAllowedToCreateTask() {
    hasAdminOrNormalRole()
    or
    hasActiveStatus()
}

...

// later in code:    
val users = userRepository.findAllBy {
    isUserAllowedToCreateTask()
}

any-dao-spring's People

Contributors

vinga avatar

Watchers

 avatar  avatar

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.