Skip to content
Myr

feat: Use KV for blog example (#60)

Before adoption: this commit came in with the history the project adopted, so no landing records it.

authored by Yusuke Wada · committed by GitHub · parent fbde907 · Browse files · 8 files+72 added−42 removed
This commit

State

Before adoption: this commit came in with the history the project adopted, so no landing records it.

Diff stats

8 files+72 added−42 removed

  • tests:+1 added−0 removed
  • other:+71 added−42 removed

8 files+72 added−42 removed

modified:example/blog/src/controller.test.ts+1 added
modified:example/blog/src/controller.ts+13 added-10 removed
modified:example/blog/src/index.ts+2 added
modified:example/blog/src/model.ts+39 added-21 removed
1declare const BLOG_EXAMPLE: KVNamespace2const PREFIX = 'v1:post:'34declare global {5 interface Crypto {6 randomUUID(): string7 }
9 unmodified lines
17 title: string18 body: string19}2018const posts: { [key: string]: Post } = {}21const generateID = (key: string) => {22 return `${PREFIX}${key}`23}2420export const getPosts = (): Post[] => {21 return Object.values(posts)25export const getPosts = async (): Promise<Post[]> => {26 const list = await BLOG_EXAMPLE.list({ prefix: PREFIX })27 const keys = list.keys2829 const posts: Post[] = []3031 for (const key of keys) {32 const value = await BLOG_EXAMPLE.get(key.name)33 if (!value) continue34 const post: Post = JSON.parse(value)35 posts.push(post)36 }3738 return posts39}4024export const getPost = (id: string): Post | undefined => {25 return posts[id]41export const getPost = async (id: string): Promise<Post | undefined> => {42 const value = await BLOG_EXAMPLE.get(generateID(id))43 if (!value) return44 const post: Post = JSON.parse(value)45 return post46}4728export const createPost = (param: Param): Post | undefined => {48export const createPost = async (param: Param): Promise<Post | undefined> => {49 if (!(param.title && param.body)) return50 const id = crypto.randomUUID()51 const newPost: Post = { id: id, title: param.title, body: param.body }32 posts[id] = newPost52 await BLOG_EXAMPLE.put(generateID(id), JSON.stringify(newPost))53 return newPost54}5536export const updatePost = (id: string, param: Param): boolean => {56export const updatePost = async (id: string, param: Param): Promise<boolean> => {57 if (!(param.title && param.body)) return false38 const post = posts[id]39 if (post) {40 post.title = param.title41 post.body = param.body42 return true43 }44 return false58 const post = await getPost(id)59 if (!post) return false60 post.title = param.title61 post.body = param.body62 await BLOG_EXAMPLE.put(generateID(id), JSON.stringify(post))63 return true64}6547export const deletePost = (id: string): boolean => {48 if (posts[id]) {49 delete posts[id]50 return true51 }52 return false66export const deletePost = async (id: string): Promise<boolean> => {67 const post = await getPost(id)68 if (!post) return false69 await BLOG_EXAMPLE.delete(generateID(id))70 return true71}
modified:example/blog/.gitignore+2 added-1 removed
1dist2node_modules3worker4package-lock.json5yarn.lock5yarn.lock6wrangler.toml
modified:example/blog/README.md+2 added-1 removed
added:example/blog/wrangler.example.toml+13 added
deleted:example/blog/wrangler.toml-9 removed