Litote / kmongo

[deprecated] KMongo - a Kotlin toolkit for Mongo
https://litote.org/kmongo/
Apache License 2.0
781 stars 74 forks source link

How to not persist a property without excluding it from Ktor serialization? #342

Closed a-vezzoli-02 closed 2 years ago

a-vezzoli-02 commented 2 years ago

Is there a way, like an annotation, that prevents a property from being written in the database without also preventing it from being serialized as a Ktor JSON response?

data class Bank(
    @BsonId @JsonProperty("_id") val id: Id<Bank> = newId(),
    var name: String,
    var initialAmount: Double = 0.0,
    @JsonIgnore var currentAmount: Double?
)

In the example above, by adding @JsonIgnore, I'm disabling the currentAmount property serialization for the whole project, not only for KMongo.

I also know that I could disable null values from being persisted, but that's not something that I'm willing to do. I'm wondering if there is a way to exclude a property, regardless of the state, from the "mongo schema" without changing the whole serialization.

In this case, I'm using Jackson as the serialization provider.

zigzago commented 2 years ago

You can provide custom Jackson modules for KMongo using https://litote.org/kmongo/dokka/kmongo/org.litote.kmongo.util/-k-mongo-configuration/register-bson-module.html

HTH