-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathmetadata-and-batch.ts
More file actions
45 lines (37 loc) · 953 Bytes
/
metadata-and-batch.ts
File metadata and controls
45 lines (37 loc) · 953 Bytes
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
import { z } from "zod";
import { kind, kindstore } from "kindstore";
const Task = z.object({
title: z.string(),
status: z.enum(["todo", "doing", "done"]),
updatedAt: z.number().int(),
});
const Preferences = z.object({
theme: z.enum(["light", "dark"]),
lastOpenedAt: z.number().int().optional(),
});
const db = kindstore({
filename: ":memory:",
metadata: {
preferences: Preferences,
},
schema: {
tasks: kind("tsk", Task).index("status").index("updatedAt"),
},
});
db.batch(() => {
db.tasks.put(db.tasks.newId(), {
title: "Ship docs",
status: "todo",
updatedAt: 1_710_000_000_000,
});
db.metadata.set("preferences", {
theme: "dark",
lastOpenedAt: 1_710_000_000_500,
});
});
const preferences = db.metadata.get("preferences");
const todoCount = db.raw
.query(`SELECT count(*) AS count FROM "tasks" WHERE "status" = ?`)
.get("todo");
console.log({ preferences, todoCount });
db.close();