From 4275d51fafe741c851e2d404ab8ae1bc261299b4 Mon Sep 17 00:00:00 2001 From: Daniel Flanagan Date: Sun, 21 Jan 2024 15:52:46 -0600 Subject: [PATCH] WIP --- db.ts | 5 +---- islands/Routine.tsx | 9 +-------- playground.ts | 12 +++++++----- routes/api/tasks.ts | 19 ++++++++----------- 4 files changed, 17 insertions(+), 28 deletions(-) diff --git a/db.ts b/db.ts index 58b2f3b..a012630 100644 --- a/db.ts +++ b/db.ts @@ -2,7 +2,7 @@ import { createPentagon, TableDefinition, } from 'https://deno.land/x/pentagon@v0.1.5/mod.ts' -import { TaskModel, TodoModel, UserModel } from '@homeman/models.ts' +import { TodoModel, UserModel } from '@homeman/models.ts' export const kv = await Deno.openKv('homeman.db') @@ -19,9 +19,6 @@ export const schema: Record = { assignee: ['users', UserModel, 'assigneeUserId', 'id'], }, }, - tasks: { - schema: TaskModel, - }, } export const db = createPentagon(kv, schema) diff --git a/islands/Routine.tsx b/islands/Routine.tsx index 1f465f2..37821b5 100644 --- a/islands/Routine.tsx +++ b/islands/Routine.tsx @@ -1,16 +1,9 @@ -import { - DailyPhase, - DailyPhaseModel, - DoneTask, - Task, - toPhase, -} from '@homeman/models.ts' +import { DailyPhase, DailyPhaseModel, Task, toPhase } from '@homeman/models.ts' import { useSignal } from '@preact/signals' import { excitement } from '@homeman/common.ts' export interface Props { tasks: Task[] - done: DoneTask[] } interface TaskWithIndex extends Task { diff --git a/playground.ts b/playground.ts index 0d386a1..6877363 100644 --- a/playground.ts +++ b/playground.ts @@ -1,5 +1,5 @@ import { DailyPhase, TaskModel } from '@homeman/models.ts' -import { db } from '@homeman/db.ts' +import { kv } from '@homeman/db.ts' import { ulid } from 'https://deno.land/x/ulid@v0.3.0/mod.ts' const hardcoded: Record = { @@ -41,6 +41,7 @@ const hardcoded: Record = { ['👨‍⚖️', 'Sleep!'], ], } +console.log({ kv }) Object.entries(hardcoded).forEach(async ([tphase, phaseTasks]) => { const phase = tphase as DailyPhase for (const [emoji, description] of phaseTasks) { @@ -52,9 +53,10 @@ Object.entries(hardcoded).forEach(async ([tphase, phaseTasks]) => { doneAt: null, }) console.log(task) - const result = await db.tasks.create({ - data: task, - }) - console.log('after create:', result) + try { + await kv.set(['task', task.id], task) + } catch (e) { + console.error('error creating:', e) + } } }) diff --git a/routes/api/tasks.ts b/routes/api/tasks.ts index 69fa4c7..2c786a2 100644 --- a/routes/api/tasks.ts +++ b/routes/api/tasks.ts @@ -1,6 +1,6 @@ import { Handlers } from '$fresh/server.ts' import { Task, TaskModel } from '@homeman/models.ts' -import { db, kv } from '@homeman/db.ts' +import { kv } from '@homeman/db.ts' import { ulid } from 'https://deno.land/x/ulid@v0.3.0/mod.ts' import { z } from 'https://deno.land/x/zod@v3.21.4/mod.ts' @@ -9,16 +9,17 @@ type TaskPayload = z.infer async function createOrUpdate(task: TaskPayload) { if (!task.id || task.id === '') { - const newTask: Task = { + const newTask: Task = TaskModel.parse({ ...task, id: ulid(), - } - const result = await db.tasks.create({ data: newTask }) + }) + const result = await kv.set(['task', newTask.id], newTask) await kv.set(['last_task_updated'], newTask.id) return result } else { - const result = await db.tasks.update({ where: { id: task.id }, data: task }) - await kv.set(['last_task_updated'], task.id) + const newTask: Task = TaskModel.parse(task) + const result = await kv.set(['task', task.id], newTask) + await kv.set(['last_task_updated'], newTask.id) return result } } @@ -42,17 +43,13 @@ export const handler: Handlers = { emoji: form.get('emoji')?.toString() || null, doneAt: form.get('doneAt')?.toString() || null, description: form.get('description')?.toString(), - assigneeUserId: form.get('assigneeUserId')?.toString() || null, + phase: form.get('phase')?.toString() || null, }) if (!id) { delete task.id } - if (!task.assigneeUserId) { - delete task.id - } - await createOrUpdate(task) const url = new URL(req.url)