Repository navigation
Expand file tree
/
Copy pathpage-state.ts
More file actions
109 lines (103 loc) · 3.03 KB
/
Copy pathpage-state.ts
File metadata and controls
109 lines (103 loc) · 3.03 KB
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
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
import type { DB } from "@db/index.js";
import { fail } from "@core/shared/errors.js";
import {
normalizePageStateValue,
parsePageStateKey,
} from "@core/modules/page-state.js";
export type PageStateItem = {
key: string;
value: unknown;
version: number;
updatedAt: string;
};
function present(row: { key: string; value: string; version: number; updated_at: string }): PageStateItem {
return {
key: row.key,
value: JSON.parse(row.value),
version: row.version,
updatedAt: row.updated_at,
};
}
export async function readPageState(db: DB, userId: string, key: string) {
const parsed = parsePageStateKey(key);
if (!parsed) fail(400, "不支持的页面状态");
const row = await db
.selectFrom("user_page_state")
.selectAll()
.where("user_id", "=", userId)
.where("key", "=", parsed)
.executeTakeFirst();
return row ? present(row) : null;
}
export async function listPageState(db: DB, userId: string, prefix: string) {
if (prefix !== "ui." && prefix !== "ai.")
fail(400, "不支持的页面状态");
const rows = await db
.selectFrom("user_page_state")
.selectAll()
.where("user_id", "=", userId)
.where("key", "like", `${prefix}%`)
.orderBy("key")
.execute();
return rows.map(present);
}
export async function writePageState(
db: DB,
userId: string,
key: string,
value: unknown,
version: number,
options?: { force?: boolean },
): Promise<{ item: PageStateItem | null; conflict: boolean }> {
const parsed = parsePageStateKey(key);
if (!parsed) fail(400, "不支持的页面状态");
let normalized: unknown;
try {
normalized = normalizePageStateValue(parsed, value);
} catch (error) {
fail(400, error instanceof Error ? error.message : "页面状态无效");
}
const now = new Date().toISOString();
const existing = await db
.selectFrom("user_page_state")
.selectAll()
.where("user_id", "=", userId)
.where("key", "=", parsed)
.executeTakeFirst();
if (existing && !options?.force && existing.version !== version)
return { item: present(existing), conflict: true };
const nextVersion = (existing?.version ?? 0) + 1;
const stored = JSON.stringify(normalized);
if (existing) {
await db
.updateTable("user_page_state")
.set({ value: stored, version: nextVersion, updated_at: now })
.where("user_id", "=", userId)
.where("key", "=", parsed)
.execute();
} else {
await db
.insertInto("user_page_state")
.values({
user_id: userId,
key: parsed,
value: stored,
version: nextVersion,
updated_at: now,
})
.execute();
}
return {
item: { key: parsed, value: normalized, version: nextVersion, updatedAt: now },
conflict: false,
};
}
export async function clearPageState(db: DB, userId: string, key: string) {
const parsed = parsePageStateKey(key);
if (!parsed) fail(400, "不支持的页面状态");
await db
.deleteFrom("user_page_state")
.where("user_id", "=", userId)
.where("key", "=", parsed)
.execute();
}