-
Notifications
You must be signed in to change notification settings - Fork 1
Expand file tree
/
Copy pathdb.ts
More file actions
54 lines (47 loc) · 1.16 KB
/
db.ts
File metadata and controls
54 lines (47 loc) · 1.16 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
const db = await Deno.openKv();
export async function getAllSessions() {
const entries = await db.list({ prefix: [] });
let sessions = [];
for await (const entry of entries) {
if (Date.now() <= entry.value.expires) {
sessions.push(entry.value);
}
}
return sessions;
}
export async function getNewSessionId() {
let n = 1;
while ((await db.get(["session" + n])).value != null) {
n += 1;
}
return "session" + n;
}
export async function getSessionData(id) {
let session = (await db.get([id])).value;
if (session != null && Date.now() > session.expires) {
return null;
}
return session;
}
export async function setSessionData(id, data) {
await db.set([id], data);
return;
}
export async function deleteSessionData(id) {
await db.delete([id]);
return;
}
export async function clearAllData() {
const entries = db.list({ prefix: [] });
for await (const entry of entries) {
db.delete(entry.key);
}
}
export async function cleanupExpiredSessions() {
const entries = db.list({ prefix: [] });
for await (const entry of entries) {
if (Date.now() > entry.value.expires) {
deleteSessionData(entry.key[0]);
}
}
}