Created
November 8, 2022 19:18
-
-
Save BetterProgramming/6b2fa73574f0fcef108e68d5d613c77f to your computer and use it in GitHub Desktop.
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
const ANIMALS_KV_KEY_ID = 'all-animals' | |
export async function getAllAnimals(env: Env): Promise<any[]> { | |
let animals = await env.ANIMALS.get(ANIMALS_KV_KEY_ID) | |
if (animals === null) { | |
return [] | |
} | |
return JSON.parse(animals); | |
} | |
export async function updateAnimals(animals: any[], env: Env) { | |
await env.ANIMALS.put(ANIMALS_KV_KEY_ID, JSON.stringify(animals)) | |
} | |
export async function addAnimal(animal: Object, env: Env) { | |
let animals = await getAllAnimals(env) | |
animals.push(animal); | |
await updateAnimals(animals, env) | |
return animal; | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment