-
-
Save wperron/9e184dc583fa9264fe8d9b6a5653c7c6 to your computer and use it in GitHub Desktop.
Keep a persisted sequence of numbers
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
#!/usr/bin/env deno run --unstable-kv --allow-read --allow-write | |
// deno-lint-ignore-file prefer-const | |
import { parseArgs } from "jsr:@std/cli"; | |
let db = await Deno.openKv("~/.incr/incr.db"); | |
if (import.meta.main) { | |
let args = parseArgs(Deno.args, {boolean: ["list"]}); | |
if (args["list"]) { | |
let iter = db.list({ prefix: [] }); | |
for await (const res of iter) { | |
console.log(`${res.key}: ${res.value}`); | |
} | |
Deno.exit(0); | |
} | |
let ns = args["_"][0] ?? "_global"; | |
let old = await db.get([ns]); | |
let nextVal = old.value + 1; | |
let n = await db.atomic() | |
.check(old) | |
.set([ns], nextVal) | |
.commit(); | |
if (n.ok === false) { | |
console.error("failed to commit result to Deno KV"); | |
Deno.exit(1); | |
} | |
console.log(nextVal); | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment