Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

KV: atomic operation check to check if key exists #19025

Open
lucacasonato opened this issue May 7, 2023 · 2 comments
Open

KV: atomic operation check to check if key exists #19025

lucacasonato opened this issue May 7, 2023 · 2 comments
Labels
ext/kv Related to "Deno.openKv()" API

Comments

@lucacasonato
Copy link
Member

Right now to check whether a given key exists, you have to do:

const res = await kv.get(["users", userId]);
if (res.value === null) throw new Error();
await kv.atomic()
  .check(res)
  .set(["posts", 1], { userId, content: "..." })
  .commit();

It would be great if you could do:

await kv.atomic()
  .check({ key: ["users", userId], versionstamp: "any" })
  .set(["posts", 1], { userId, content: "..." })
  .commit();

Alternative API ideas:

.check({ key, exists: true })

// and then we also support

.check({ key, exists: false }) // same as .check({ key, versionstamp: null })
@lucacasonato lucacasonato changed the title KV: atomic operation check to check if _any_ versionstamp is set KV: atomic operation check to check if key exists May 7, 2023
@lucacasonato lucacasonato added the ext/kv Related to "Deno.openKv()" API label May 7, 2023
@iuioiua
Copy link
Contributor

iuioiua commented May 8, 2023

Another idea:

.exists(key, true) // if key exists (maybe have 2nd param be `true` by default)
.exists(key, false) // if key doesn't exist

@WilcoKruijer
Copy link

Tangentially related, it'd also be useful to be able to check if a key's versionstamp is lower than a given versionstamp.

The use case being: wanting to know if key X has been modified since the last edit of key Y.

// Maybe something like
type CheckOperation = 'lt' | 'eq' | 'exists';

.check({ key, versionstamp, operation: 'lt' })

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
ext/kv Related to "Deno.openKv()" API
Projects
None yet
Development

No branches or pull requests

3 participants