micronaut-projects / micronaut-projects/micronaut-data

Partial updates using DTOs

Open
#2,020 0 comments 1 reaction 0 assignees View on GitHub

Nobody has claimed this yet.

status: example attached type: improvement
Dominant language
Java
Stars
482
Forks
229
Avg merge
1d 7h
Merged PRs (30d)
32

Description

### Feature description

I would like to request a new feature: the ability to pass a DTO to an **update** function in the repository, considering that the DTO has the same properties as the original entity.

To avoid confusion with the actual semantics of the **update** functions, it may be necessary to create a new annotation to instruct Micronaut Data that the argument is a DTO.

Please see the following code for a better example. Suppose that the `@Dto` is the new annotation.

```kotlin
import io.micronaut.data.annotation.Id
import io.micronaut.data.annotation.Dto // New annotation
import io.micronaut.data.jdbc.annotation.JdbcRepository
import io.micronaut.data.model.query.builder.sql.Dialect
import io.micronaut.data.repository.CrudRepository
import io.micronaut.serde.annotation.Serdeable
import jakarta.persistence.Entity
import jakarta.persistence.GeneratedValue
import java.math.BigDecimal

@Entity
@Serdeable
open class Item {

@jakarta.persistence.Id
@GeneratedValue
var id: Long? = null

lateinit var name: String

lateinit var price: BigDecimal
}

@Serdeable
class ItemDTO {
lateinit var name: String
}

@JdbcRepository(dialect = Dialect.POSTGRES)
interface ItemRepository : CrudRepository {

// NEW FEATURE: Update using DTOs.
fun update(@Id id: Long, @Dto item: ItemDTO)

// NEW FEATURE: Update using DTOs.
fun update(@Dto item: ItemDTO)

}
```

I understand that the actual **update** functions allow doing partial updates, but that requires writing a lot of code. Using DTOs is a better way to reuse code, the same object instance of the DTO can be passed from the Controller to the service/repository layers.

Contributor guide

Open the contributing guide

First steps

  1. Read the whole issue, then the project's contributing guide.
  2. Comment on the issue to say you are picking it up — it saves two people doing the same work.
  3. Fork the repository and make your change on a branch.
  4. Open a pull request that references the issue number.

Research direction

Use the ItemRepository example as the entry point, and first read how existing update functions distinguish partial updates from entity updates. Done means the requested DTO-based update forms work without changing the semantics of current update methods, including the variants with and without an id.

Written by the indexing model from the issue text.

Assessment

Tech stack
java, kotlin, sql
Domain
backend, database
Issue type
Feature
Difficulty
5/5
Estimated time
Over a week
Activity status
Stale
Clarity
Mostly clear
Newbie friendliness
30/100

Get new issues in your inbox

A short digest of beginner-friendly GitHub issues.