-
Notifications
You must be signed in to change notification settings - Fork 3
/
Copy pathkv.ts
44 lines (38 loc) · 1.22 KB
/
kv.ts
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
export type Reminder = {
username: string;
chatId: number;
message?: string;
replyMessageId?: number;
executionDate: Date;
};
export const kv = await Deno.openKv();
export async function getReminders(cycleDate: Date) {
const year = cycleDate.getUTCFullYear();
const month = cycleDate.getUTCMonth();
const date = cycleDate.getUTCDate();
const key = [year, month, date];
console.log("Getting reminders for", key);
const result = await kv.get<Reminder[]>(key);
if (Array.isArray(result.value)) return result.value;
return [];
}
export async function deleteReminder(cycleDate: Date) {
const year = cycleDate.getUTCFullYear();
const month = cycleDate.getUTCMonth();
const date = cycleDate.getUTCDate();
const key = [year, month, date];
await kv.delete(key);
}
export async function setReminder(cycleDate: Date, reminder: Reminder) {
const year = cycleDate.getUTCFullYear();
const month = cycleDate.getUTCMonth();
const date = cycleDate.getUTCDate();
const key = [year, month, date];
const result = await kv.get<Reminder[]>(key);
console.log({ result });
const reminders = Array.isArray(result.value)
? [...result.value, reminder]
: [reminder];
await kv.set(key, reminders);
console.log("Saved reminder", key, reminder);
}