You can not select more than 25 topics Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.

34 lines
765 B

#!/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);
}