Deno KV equivalent of 'returning'
hello, in sqlite there is a way to get the new value set on an upsert like so
insert into counters (name, counter) values (?, 0)
on conflict (name) do update set counter = counter + 1
returning counter;
is there a way an equivalent on KV? At the moment what I'm looking at is looping with a get and then try set or retry.
1 Reply
In Deno KV, there isn't a direct equivalent to the 'returning' clause in SQL. The documentation does not provide a way to return the new value directly after an update or insert operation in a single atomic transaction.
However, you can achieve similar functionality by performing a
get
operation after the set
operation. Here's an example:
In this example, we're using an optimistic locking strategy. We first get the current counter value, then try to increment it. If another process has updated the counter in the meantime, the check
operation will fail, and we'll retry the whole process. Once the update is successful, we get the new counter value.
Please note that this approach involves two separate operations (set
and get
), and the counter value could potentially change between these operations if it's being updated concurrently by another process. If you need to ensure that the counter value doesn't change between the set
and get
operations, you would need to implement additional synchronization logic.