Skip to content

Instantly share code, notes, and snippets.

@martpet
Created December 19, 2023 19:29
Show Gist options
  • Save martpet/40b423bc432ce17f9eee033000c345ae to your computer and use it in GitHub Desktop.
Save martpet/40b423bc432ce17f9eee033000c345ae to your computer and use it in GitHub Desktop.
Deno KV secondary index
export async function setUser(user) {
const primaryKey = ["users", user.id];
const byColorKey = ["users_by_favorite_color", user.favoriteColor, user.id];
const oldUser = await getUser(user.id);
const atomic = kv.atomic();
if (oldUser) {
if (oldUser.favoriteColor !== user.favoriteColor) {
atomic.delete(byColorKey);
}
}
await atomic
.set(primaryKey, user)
.set(byColorKey, user)
.commit();
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment