Hi, we can't use exposed DAO for a lot of parts be...
# exposed
h
Hi, we can't use exposed DAO for a lot of parts because we have many composite keys and I kind of don't want to mix DAO and DSL implementations. So everything is just the DSL. I have a lot of these snippets in
ye olden code
is there no better way to do this in exposed?
Copy code
fun upsertTelegramFilter(filter: IbisTelegramFilterDTO) = transaction {
        val exists = TelegramFilterTable
            .select { TelegramFilterTable.id eq filter.id }
            .singleOrNull() != null
        
        if(exists)
            TelegramFilterTable.update { 
                it[this.companyId] = filter.companyId
                it[this.name] = filter.name
                it[this.type] = filter.type
                it[this.text] = filter.text
            }
        else
            TelegramFilterTable.insert {
                it[this.companyId] = filter.companyId
                it[this.name] = filter.name
                it[this.type] = filter.type
                it[this.text] = filter.text
            }
    }
Ah sorry, I cut away too much trying to construct an example. It seems our exposed version is just too old and I currently cannot update on the old server part. The new server luckily has everything up to date. A quick google search for exposed upsert only yielded me with questions about implementing upsert so I assumed it wasn't (yet) implemented. Thank you!
The insert doesn't have an ID because it's
auto_increment
. Which makes me wonder, what is the behavior of your
upsert
on non-existing ids? Does it just omit the given ID and use the one according to the counter? Or does the ID for "inserts with upsert" must be 0 or something?
Hey, thanks for your answers. I will definitely check it out in a playground project. > If it always defaults to a "new record" sentinel value. Yeah this is essentially how I currently do it without the upsert (because exposed is too old). So I do have to handle that myself with a marker value in any case, didn't knew the word sentinel value before (ESL) very cool. Makes sense though, how would exposed otherwise know if I want to insert or update.
Yeah that is what I meant. I have to distinguish the cases by providing an id or not.
151 Views