Skip to content

Type-safe Kotlin query extension [DATAMONGO-2138] #3007

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Closed
spring-projects-issues opened this issue Nov 22, 2018 · 5 comments
Closed

Type-safe Kotlin query extension [DATAMONGO-2138] #3007

spring-projects-issues opened this issue Nov 22, 2018 · 5 comments
Assignees
Labels
in: repository Repositories abstraction type: enhancement A general enhancement

Comments

@spring-projects-issues
Copy link

Tjeu opened DATAMONGO-2138 and commented

MongoTemplate already has many Kotlin extensions, and I'm suggesting to add extensions for "type-safe queries". This will prevent typos in field names, and improve the readability of a query.

The KMongo library uses Kotlin's Property References and infix functions for a type safe query syntax:

data class Jedi(val name: String)
val yoda = col.findOne(Jedi::name eq "Yoda")

https://github.com/Litote/kmongo/blob/master/kmongo-kdoc/docs/typed-queries.md

Spring Data MonoDB could provide a similar syntax:

val classic = Criteria("price").gt(1100)
    .and("available").isEqualTo(true)

val typed = typedCriteria {
    Book::price gt 1100
    Book::available isEqualTo true
}

assertEquals(classic, typed)

https://gist.github.com/TjeuKayim/91f52ae21bd5c33b99de028383d5ed89

I like to help coding this feature. It would be my first contribution to Spring


Reference URL: #622

Issue Links:

  • DATAMONGO-2162 Add support for extended AND/OR query criteria using type-safe Kotlin queries
    ("is depended on by")
  • DATAMONGO-2163 Type-safe Kotlin update extension
    ("is depended on by")
  • DATAREST-1433 UnsatisfiedDependencyException with custom ID mapping in Kotlin

Referenced from: pull request #622

0 votes, 5 watchers

@spring-projects-issues
Copy link
Author

Oliver Drotbohm commented

Sounds great to me. Maybe Sébastien Deleuze can advise?

@spring-projects-issues
Copy link
Author

Sébastien Deleuze commented

Big +1 for that, and FYI this is an idea I would like to apply later to SQL for Spring Data JDBC and Spring Data R2DBC, so working on the Mongo side could help

@spring-projects-issues
Copy link
Author

Tjeu commented

We can make the queries even more type safe with KProperty1.

The following function definition restricts the KProperty to members of the Entity class.

infix fun <T : Any> KProperty1<ENTITY, T>.isEqualTo(value: T)

// So the code below won't compile, because the classes don't match.
typedCriteria<Book>{ OtherClass::name isEqualTo "myBook"}

A disadvantage is that the generic type parameter "Book" has to be repeated in a call like this:

operations.find<Book>(Query(typedCriteria<Book> { Book::price gt 1100 }))

Query and CriteriaDefinition could be made generic, so that the generic parameter can be passed through to the typed query builder.

What do you think?

@spring-projects-issues
Copy link
Author

Tjeu commented

Making Query and CriteriaDefinition generic won't be backward compatible. So it is not really a good idea

@spring-projects-issues
Copy link
Author

Mark Paluch commented

I filed  DATAMONGO-2162 and DATAMONGO-2163 to continue on query operators and Update support

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
in: repository Repositories abstraction type: enhancement A general enhancement
Projects
None yet
Development

No branches or pull requests

2 participants