char/avril

(wip) why'd you have to go and make things so complicaaaaated?

git clone https://git.t4t.associates/char/avril

Charlotte Somadd `avril:kv` module78f4748

main
4.0 KiB139 linesraw
1import {
2  openKv,
3  type KvAccess,
4  type KvAtomic,
5  type KvAtomicResult,
6  type KvDatabase,
7  type KvEntry,
8  type KvKey,
9  type KvKeyspace,
10  type KvListOptions,
11  type KvOpenOptions,
12  type KvPathUrl,
13  type KvValue,
14  type KvVersionstamp,
15} from "avril:kv";
16
17const access: KvAccess = "exclusive";
18const options: KvOpenOptions = { access, create: true };
19const url: KvPathUrl = { href: "file:///tmp/kv.redb" };
20
21const db: KvDatabase = await openKv("/tmp/kv.redb", options);
22const same: KvDatabase = await openKv(url);
23void same;
24
25const space: KvKeyspace = db.keyspace("events");
26const spaceName: string = space.name;
27void spaceName;
28
29const key: KvKey = "users:42";
30const bytesKey: KvKey = new Uint8Array([1, 2, 3]);
31void bytesKey;
32
33const raw: Uint8Array | null = await space.get(key);
34void raw;
35const text: string | null = await space.getText(key);
36void text;
37
38const batch: (Uint8Array | null)[] = await space.getMany([key, "other"]);
39void batch;
40const textBatch: (string | null)[] = await space.getTextMany([key, "other"]);
41void textBatch;
42
43const entry: KvEntry | null = await space.entry(key);
44if (entry !== null) {
45  const stamp: bigint = entry.versionstamp;
46  const owner: string = entry.keyspace;
47  const back: Uint8Array = entry.key;
48  const stored: Uint8Array = entry.value;
49  void stamp;
50  void owner;
51  void back;
52  void stored;
53}
54
55const entries: (KvEntry | null)[] = await space.entries([key, "other"]);
56void entries;
57
58const written: { readonly versionstamp: KvVersionstamp } = await space.set(key, "v1");
59void written;
60const writtenBytes: { readonly versionstamp: KvVersionstamp } = await space.set(
61  new Uint8Array([9]),
62  new Uint8Array([8]),
63);
64void writtenBytes;
65await space.delete(key);
66await space.clear();
67
68const listOptions: KvListOptions = { prefix: "user:", limit: 10, reverse: true };
69for await (const listed of space.list(listOptions)) {
70  const again: KvEntry = listed;
71  void again;
72}
73for await (const viaBytes of space.list({ prefix: new Uint8Array([117]) })) {
74  const again: KvEntry = viaBytes;
75  void again;
76}
77
78const names: string[] = await db.keyspaces();
79void names;
80
81const result: KvAtomicResult = await db.atomic((tx: KvAtomic) => {
82  tx.check(space, key, null);
83  tx.set(space, key, "1");
84  tx.delete(space, "gone");
85});
86if (entry !== null) {
87  await db.atomic((tx) => tx.check(space, key, entry.versionstamp));
88}
89if (result.ok) {
90  const stamp: KvVersionstamp | null = result.versionstamp;
91  void stamp;
92}
93
94const value: KvValue = "text";
95void value;
96
97const genericDefault: KvEntry = entry as KvEntry;
98void genericDefault;
99
100// @ts-expect-error get returns bytes, not an entry object.
101const asEntry: { value: unknown } = await space.get(key);
102void asEntry;
103// @ts-expect-error getText returns a string, not an entry object.
104const asTextEntry: { value: Uint8Array } = await space.getText(key);
105void asTextEntry;
106// @ts-expect-error openKv requires a path.
107await openKv(42);
108// @ts-expect-error keyspace names must be strings.
109db.keyspace(1);
110// @ts-expect-error set rejects numbers.
111await space.set("k", 42);
112// @ts-expect-error set rejects objects.
113await space.set("k", { nested: true });
114// @ts-expect-error get rejects numbers.
115await space.get(42);
116// @ts-expect-error versionstamp is a bigint, not a number.
117const numberStamp: KvVersionstamp = 42;
118void numberStamp;
119// @ts-expect-error versionstamp is a bigint, not a string.
120const stringStamp: KvVersionstamp = "1";
121void stringStamp;
122if (entry !== null) {
123  // @ts-expect-error versionstamps do not narrow to strings.
124  const asString: string = entry.versionstamp;
125  // @ts-expect-error versionstamps do not narrow to numbers.
126  const asNumber: number = entry.versionstamp;
127  void asString;
128  void asNumber;
129}
130await db.atomic((tx) => {
131  // @ts-expect-error check expects a bigint stamp or null.
132  tx.check(space, key, "1");
133});
134// @ts-expect-error list limit must be a number.
135space.list({ limit: "many" });
136// @ts-expect-error list options must be an object.
137space.list(42);
138// @ts-expect-error access must be a valid mode.
139await openKv("/tmp/kv.redb", { access: "sometimes" });