From aba88d157cccccfc0fe116a9c6a7babfb735e26d Mon Sep 17 00:00:00 2001 From: Kevin Whinnery Date: Thu, 14 Sep 2023 11:01:59 -0500 Subject: [PATCH] corrections noticed in KV content --- kv/manual/data_modeling_typescript.md | 31 ++++++++++++++++++++------- 1 file changed, 23 insertions(+), 8 deletions(-) diff --git a/kv/manual/data_modeling_typescript.md b/kv/manual/data_modeling_typescript.md index 69b2d7b6b..9958a76e0 100644 --- a/kv/manual/data_modeling_typescript.md +++ b/kv/manual/data_modeling_typescript.md @@ -48,8 +48,7 @@ export interface Post { } ``` -This object model describes a blog post, authors, and an enumerated list of tags -that could be applied to blog posts. +This object model describes a blog post and an associated author. With Deno KV, you can use these TypeScript interfaces like [data transfer objects (DTOs)](https://martinfowler.com/bliki/LocalDTO.html) - a @@ -89,10 +88,24 @@ const ac = r.value as Author; console.log(ac.fullName); ``` +You can also specify an optional +[type parameter](https://deno.land/api?s=Deno.Kv&p=prototype.get&unstable) for +`get`: + +```ts +import { Author } from "./model.ts"; + +const kv = await Deno.openKv(); + +const r = await kv.get(["authors", "acdoyle"]); + +console.log(r.value.fullName); +``` + For simpler data structures, this technique may be sufficient. But often, you -will want or need to apply some business logic when creating your domain -objects. When this need arises, you can develop a set of pure functions that can -operate on your DTOs. +will want or need to apply some business logic when creating or accessing your +domain objects. When this need arises, you can develop a set of pure functions +that can operate on your DTOs. ## Encapsulating business logic with a service layer @@ -144,6 +157,8 @@ export async function getPost(slug: string): Promise { This thin layer uses a `RawPost` interface, which extends the actual `Post` interface, to include some additional data that is used to reference data at -another index (the associated `Author` object). By putting the `get` and `set` -operations on `Post` objects, we can apply additional business logic to getting -and setting these values. +another index (the associated `Author` object). + +The `savePost` and `getPost` functions take the place of a direct Deno KV `get` +or `set` operation, so that they can properly serialize and "hydrate" model +objects for us with appropriate types and associations.