Skip to content

LukasForst/exposed-upsert

Repository files navigation

Exposed PostgreSQL Upsert

Unfortunately, current Exposed framework does not contain upsert (update or insert) functionality out of the box. This simple extension library provides implementation for PostgreSQL. Implementation is based on the comment Maxr1998 made here.

Usage can be seen for example in Wire Poll Bot here

Following code tries to insert new value of optionId to Votes table where pollId and userId is composite key. If the combination pollId and userId already exist, optionId is updated.

Votes.insertOrUpdate(Votes.pollId, Votes.userId) {
    it[pollId] = pollAction.pollId
    it[pollOption] = pollAction.optionId
    it[userId] = pollAction.userId
}

Library is hosted on Maven Central.

<dependency>
	<groupId>dev.forst</groupId>
	<artifactId>exposed-upsert</artifactId>
	<version>1.3.4</version>
</dependency>

Gradle:

implementation("dev.forst", "exposed-upsert", "1.3.4")