Skip to content

Commit

Permalink
add insertAndRefreshRecord (#2)
Browse files Browse the repository at this point in the history
  • Loading branch information
Lukas-Kaufmann authored Mar 4, 2024
1 parent caf2ecb commit a38ded2
Show file tree
Hide file tree
Showing 3 changed files with 46 additions and 0 deletions.
6 changes: 6 additions & 0 deletions CHANGES.md
Original file line number Diff line number Diff line change
@@ -1,5 +1,11 @@
# Changes

## unreleased

### Feature

- added `insertAndRefreshRecord` function

## 2024-01-18 / 0.1.0

### Feature
Expand Down
14 changes: 14 additions & 0 deletions src/main/kotlin/ls/jooq/execute/RecordMutators.kt
Original file line number Diff line number Diff line change
@@ -1,5 +1,6 @@
package ls.jooq.execute

import kotlinx.coroutines.reactive.awaitFirst
import kotlinx.coroutines.reactive.awaitSingle
import ls.jooq.prepare.insert
import ls.jooq.prepare.update
Expand Down Expand Up @@ -45,3 +46,16 @@ suspend fun <R : UpdatableRecord<R>> DSLContext.updateIfChangedAndExecute(record
// SQL [update "table" set [ no fields are updated ] where "table"."pk" = $1]; syntax error at or near "["
this.updateAndExecute(record)
} else null

/**
* Inserts a [record] and sets the values of [record] from the returned values of the insert
*
* @param R the type of record
* @param record - the record to insert
* @return the inserted record, note that also the param record will have updated values
*/
suspend fun <R : UpdatableRecord<R>> DSLContext.insertAndRefreshRecord(record: UpdatableRecord<R>): UpdatableRecord<R> {
val inserted = insert(record).returning().awaitFirst()
record.from(inserted)
return record
}
26 changes: 26 additions & 0 deletions src/test/kotlin/ls/jooq/execute/RecordMutatorsTest.kt
Original file line number Diff line number Diff line change
Expand Up @@ -3,6 +3,7 @@ package ls.jooq.execute
import DBExtension
import DBTest
import io.kotest.core.spec.style.FreeSpec
import io.kotest.matchers.collections.shouldHaveSize
import io.kotest.matchers.nulls.shouldNotBeNull
import io.kotest.matchers.shouldBe
import kotlinx.coroutines.reactive.awaitSingle
Expand All @@ -14,6 +15,31 @@ class RecordMutatorsTest : FreeSpec({

val ctx = DBExtension.dslContext

"DSLContext.insertAndRefreshRecord" - {

"should insert the record" {
val record = AuthorRecord()
record.firstName = "max"
record.lastName = "irrelevant"
ctx.insertAndRefreshRecord(record)

val insertedContent = ctx.selectFrom(Tables.AUTHOR).awaitAll<AuthorRecord, _>().shouldHaveSize(1).first()
insertedContent.firstName shouldBe "max"
insertedContent.lastName shouldBe "irrelevant"
insertedContent.id.shouldNotBeNull()
}

"after the call the record should have values set from database defaults" {
val record = AuthorRecord()
record.firstName = "max"
record.lastName = "irrelevant"
ctx.insertAndRefreshRecord(record)

record.id.shouldNotBeNull()
record.created.shouldNotBeNull()
}
}

"DSLContext.create<T>()" - {

"should create a record of the given type and insert it into the DB" {
Expand Down

0 comments on commit a38ded2

Please sign in to comment.