Created
December 19, 2023 19:29
-
-
Save martpet/40b423bc432ce17f9eee033000c345ae to your computer and use it in GitHub Desktop.
Deno KV secondary index
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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