-
Notifications
You must be signed in to change notification settings - Fork 0
/
note.server.ts
80 lines (67 loc) · 1.7 KB
/
note.server.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
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
import arc from "@architect/functions";
import cuid from "cuid";
import type { User } from "./user.server";
export type Note = {
id: ReturnType<typeof cuid>;
userId: User["id"];
title: string;
body: string;
};
type NoteItem = {
pk: User["id"];
sk: `note#${Note["id"]}`;
};
const skToId = (sk: NoteItem["sk"]): Note["id"] => sk.replace(/^note#/, "");
const idToSk = (id: Note["id"]): NoteItem["sk"] => `note#${id}`;
export async function getNote({
id,
userId,
}: Pick<Note, "id" | "userId">): Promise<Note | null> {
const db = await arc.tables();
const result = await db.note.get({ pk: userId, sk: idToSk(id) });
if (result) {
return {
userId: result.pk,
id: result.sk,
title: result.title,
body: result.body,
};
}
return null;
}
export async function getNoteListItems({
userId,
}: Pick<Note, "userId">): Promise<Array<Pick<Note, "id" | "title">>> {
const db = await arc.tables();
const result = await db.note.query({
KeyConditionExpression: "pk = :pk",
ExpressionAttributeValues: { ":pk": userId },
});
return result.Items.map((n: any) => ({
title: n.title,
id: skToId(n.sk),
}));
}
export async function createNote({
body,
title,
userId,
}: Pick<Note, "body" | "title" | "userId">): Promise<Note> {
const db = await arc.tables();
const result = await db.note.put({
pk: userId,
sk: idToSk(cuid()),
title: title,
body: body,
});
return {
id: skToId(result.sk),
userId: result.pk,
title: result.title,
body: result.body,
};
}
export async function deleteNote({ id, userId }: Pick<Note, "id" | "userId">) {
const db = await arc.tables();
return db.note.delete({ pk: userId, sk: idToSk(id) });
}