Deno KV Plus â Transactions that { ok: true } đ
Upgrade your Deno KV code with confidence and peace of mind.
⨠Features
đĄī¸ Safety First
Never worry about updates being overwritten by other updates.
Trust in the fact that calculations are always based on the most up-to-date data.
đĻž Unwavering Reliability
Our system tries the same transaction/calculation until the write is successful without interference.
Every attempt fetches the freshest data and recalculates if the data changes.
Update process can be aborted if values change and the update is no longer valid (e.g. insufficient balance to complete a transfer).
Zero dependencies for ultimate compatibility.
đ A quick demo
import{ withSafeAtomics }from"https://deno.land/x/kv/mod.ts";// Create a KV instance with atomic supportconst kv =withSafeAtomics(awaitDeno.openKv());// Increment a visit counterconst{ ok, error }=await kv.setSafeAtomic(["view_counter"],(value, abort)=> value +1,);if(ok)console.log("đ");elseconsole.log("âšī¸", error);
đŦ Usage
setSafeAtomic
A convenient version of setSafeAtomicMany for updating just one key. This function ensures atomic updates and retries whenever necessary. Use setSafeAtomic when you're updating a single entry and want to guarantee atomic updates.
đ Starter template
import{ withSafeAtomics }from"https://deno.land/x/kv/mod.ts";const kv =withSafeAtomics(awaitDeno.openKv());const{ ok, error }=await kv.setAtomic(["this is one","and the same","example key"],(value, abort)=> value,);
đ Arguments
Argument
Type
Description
key
any[]
The key to fetch and update.
updateFn
(value: any, abort: (reason?: string) => void) => any
A function that takes the current value and returns the updated value. Can be called multiple times.
retryCount
number
The number of times to retry the update function if the value changes.
đĻ Return object
Property
Type
Description
ok
boolean
Whether the update was successful.
error
string
The error message if the update failed or was aborted.
A function that takes an array of values and returns an array of values to update. Can be called multiple times.
retryCount
number
The number of times to retry the update function if the values change.
đĻ Return object
Property
Type
Description
ok
boolean
Whether the update was successful.
error
string
The error message if the update failed or was aborted.
đ Complex example
import{ withSafeAtomics }from"https://deno.land/x/kv/mod.ts";// Create a KV instance with atomicMany supportconst kv =withSafeAtomics(awaitDeno.openKv());// Send 50 monies from Chad to Stacyconst{ ok, error }=await kv.setSafeAtomicMany([["balance","chad"],["balance","stacy"],],(values, abort)=>{const[chadBalance, stacyBalance]= values;if(chadBalance <50){// Oh no, Chad doesn't have enough money anymore. Abort!returnabort("Chad has too little money to send 50 monies to Stacy.");}// Return array of updated valuesreturn[chadBalance -50, stacyBalance +50];},);if(ok){console.log("Transaction successful!");}else{console.log("Transaction failed:", error);}
đą Contributing
We appreciate your contributions! Please open an issue first to discuss any changes before submitting a Pull Request to avoid any disappointment.