From fc772c62f904753aad95e56ac1002ed9b0d5749a Mon Sep 17 00:00:00 2001 From: Daniel Flanagan Date: Fri, 5 Jan 2024 21:10:38 -0600 Subject: [PATCH] Pentagon seems nice? --- .helix/languages.toml | 2 +- deno.json | 3 +- main.ts | 1 + models.ts | 66 ++++++++++++++++++++++++++++++++++++------- routes/_app.tsx | 2 +- routes/index.tsx | 20 +++++++------ 6 files changed, 72 insertions(+), 22 deletions(-) diff --git a/.helix/languages.toml b/.helix/languages.toml index b05ee97..51ddcf4 100644 --- a/.helix/languages.toml +++ b/.helix/languages.toml @@ -1,6 +1,6 @@ [language-server.deno] command = "deno" -args = ["lsp"] +args = ["lsp", "--unstable-kv"] config.hostInfo = "helix" [[language]] diff --git a/deno.json b/deno.json index bf818c8..acc90d9 100644 --- a/deno.json +++ b/deno.json @@ -2,7 +2,7 @@ "lock": false, "tasks": { "check": "deno fmt --check && deno lint && deno check **/*.ts && deno check **/*.tsx", - "cli": "echo \"import '\\$fresh/src/dev/cli.ts'\" | deno run --unstable -A -", + "cli": "echo \"import '\\$fresh/src/dev/cli.ts'\" | deno run -A -", "manifest": "deno task cli manifest $(pwd)", "start": "deno run -A --watch=static/,routes/ dev.ts", "build": "deno run -A dev.ts build", @@ -41,5 +41,6 @@ "semiColons": false, "singleQuote": true }, + "unstable": ["kv"], "nodeModulesDir": true } diff --git a/main.ts b/main.ts index 13d2f71..11ebba6 100644 --- a/main.ts +++ b/main.ts @@ -3,6 +3,7 @@ /// /// /// +/// import '$std/dotenv/load.ts' diff --git a/models.ts b/models.ts index 4329fdd..57e3839 100644 --- a/models.ts +++ b/models.ts @@ -1,13 +1,59 @@ -type Name = string +import { z } from 'https://deno.land/x/zod@v3.21.4/mod.ts' +import { createPentagon } from 'https://deno.land/x/pentagon@v0.1.5/mod.ts' +import { ulid } from 'https://deno.land/x/ulid@v0.3.0/mod.ts' -export interface FamilyMember { - name: Name - avatarUrl?: URL +const kv = await Deno.openKv() + +const User = z.object({ + id: z.string().ulid().describe('primary'), + createdAt: z.date(), + + name: z.string(), + avatarUrl: z.string().nullable(), +}) +export type User = z.infer + +const Todo = z.object({ + id: z.string().ulid().describe('primary'), + createdAt: z.date(), + + description: z.string(), + emoji: z.string().nullable(), + + doneAt: z.date().nullable(), + assigneeUserId: z.string().ulid().nullable(), +}) +export type Todo = z.infer + +export const db = createPentagon(kv, { + users: { + schema: User, + relations: { + assignedTodos: ['todos', [Todo], 'id', 'assigneeUserId'], + }, + }, + todos: { + schema: Todo, + relations: { + assignee: ['users', User, 'assigneeUserId', 'id'], + }, + }, +}) + +const daddy: User = { + id: ulid(), + createdAt: new Date(), + name: 'Daddy', + avatarUrl: null, +} +const todo: Todo = { + emoji: null, + id: ulid(), + createdAt: new Date(), + description: 'Test Todo', + doneAt: null, + assigneeUserId: daddy.id, } -export interface ToDo { - emoji?: string - description: string - doneAt?: Date - assigneeName?: Name -} +db.todos.create({ data: todo }) +db.users.create({ data: daddy }) diff --git a/routes/_app.tsx b/routes/_app.tsx index d716935..9f87bdf 100644 --- a/routes/_app.tsx +++ b/routes/_app.tsx @@ -8,7 +8,7 @@ export default function App({ Component }: PageProps) { homeman-deno - + meow diff --git a/routes/index.tsx b/routes/index.tsx index 583a9a0..c53f48a 100644 --- a/routes/index.tsx +++ b/routes/index.tsx @@ -1,25 +1,27 @@ -import { Handlers } from '$fresh/server.ts' +import { Handlers, PageProps } from '$fresh/server.ts' import { useSignal } from '@preact/signals' import Counter from '../islands/Counter.tsx' -import { FamilyMember, ToDo } from '$models' +import { db, Todo, User } from '$models' interface Data { - familyMembers: FamilyMember[] - todos: ToDo[] + users: User[] + unassignedTodos: Todo[] } export const handler: Handlers = { async GET(_req, ctx) { - const resp = await ctx.render() - resp.headers.set('X-Custom-Header', 'Hello') - return resp + const users = await db.users.findMany({ include: { assignedTodos: true } }) + const unassignedTodos = await db.todos.findMany({ + where: { assigneeUserId: null }, + }) + return ctx.render({ users, unassignedTodos }) }, } -export default function Home() { +export default function Home(props: PageProps) { const count = useSignal(3) return ( -
+