-
Notifications
You must be signed in to change notification settings - Fork 0
/
service.ts
50 lines (40 loc) · 1.01 KB
/
service.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
const kv = await Deno.openKv();
type Post = {
id: string;
title: string;
content: string;
}
export const EmptyPost = {
id: "",
title: "",
content: "",
}
export async function searchPosts(key: string) {
const posts = await getPosts()
return posts
.filter(it => it.title.indexOf(key) > -1)
}
export async function getPost(id: string) {
return (await kv.get(["posts", id])).value;
}
export async function getPosts() {
const posts = [] as Post[];
const entries = kv.list({ prefix: ["posts"] });
for await (const entry of entries) {
posts.push(entry.value);
}
return posts;
}
export async function createPost(post: Partial<Post>) {
const id = crypto.randomUUID();
kv.set(["posts", id], {...post, id});
}
export async function updatePost(data: Partial<Post>) {
const post = await getPost(data.id!);
post.title = data.title ?? "";
post.content = data.content ?? "";
kv.set(["posts", data.id!], {...post});
}
export async function deletePost(id: string) {
kv.delete(["posts", id]);
}