WIP
This commit is contained in:
parent
beb488b3d6
commit
4275d51faf
5
db.ts
5
db.ts
|
@ -2,7 +2,7 @@ import {
|
||||||
createPentagon,
|
createPentagon,
|
||||||
TableDefinition,
|
TableDefinition,
|
||||||
} from 'https://deno.land/x/pentagon@v0.1.5/mod.ts'
|
} 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')
|
export const kv = await Deno.openKv('homeman.db')
|
||||||
|
|
||||||
|
@ -19,9 +19,6 @@ export const schema: Record<string, TableDefinition> = {
|
||||||
assignee: ['users', UserModel, 'assigneeUserId', 'id'],
|
assignee: ['users', UserModel, 'assigneeUserId', 'id'],
|
||||||
},
|
},
|
||||||
},
|
},
|
||||||
tasks: {
|
|
||||||
schema: TaskModel,
|
|
||||||
},
|
|
||||||
}
|
}
|
||||||
|
|
||||||
export const db = createPentagon(kv, schema)
|
export const db = createPentagon(kv, schema)
|
||||||
|
|
|
@ -1,16 +1,9 @@
|
||||||
import {
|
import { DailyPhase, DailyPhaseModel, Task, toPhase } from '@homeman/models.ts'
|
||||||
DailyPhase,
|
|
||||||
DailyPhaseModel,
|
|
||||||
DoneTask,
|
|
||||||
Task,
|
|
||||||
toPhase,
|
|
||||||
} from '@homeman/models.ts'
|
|
||||||
import { useSignal } from '@preact/signals'
|
import { useSignal } from '@preact/signals'
|
||||||
import { excitement } from '@homeman/common.ts'
|
import { excitement } from '@homeman/common.ts'
|
||||||
|
|
||||||
export interface Props {
|
export interface Props {
|
||||||
tasks: Task[]
|
tasks: Task[]
|
||||||
done: DoneTask[]
|
|
||||||
}
|
}
|
||||||
|
|
||||||
interface TaskWithIndex extends Task {
|
interface TaskWithIndex extends Task {
|
||||||
|
|
|
@ -1,5 +1,5 @@
|
||||||
import { DailyPhase, TaskModel } from '@homeman/models.ts'
|
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'
|
import { ulid } from 'https://deno.land/x/ulid@v0.3.0/mod.ts'
|
||||||
|
|
||||||
const hardcoded: Record<DailyPhase, [string, string][]> = {
|
const hardcoded: Record<DailyPhase, [string, string][]> = {
|
||||||
|
@ -41,6 +41,7 @@ const hardcoded: Record<DailyPhase, [string, string][]> = {
|
||||||
['👨⚖️', 'Sleep!'],
|
['👨⚖️', 'Sleep!'],
|
||||||
],
|
],
|
||||||
}
|
}
|
||||||
|
console.log({ kv })
|
||||||
Object.entries(hardcoded).forEach(async ([tphase, phaseTasks]) => {
|
Object.entries(hardcoded).forEach(async ([tphase, phaseTasks]) => {
|
||||||
const phase = tphase as DailyPhase
|
const phase = tphase as DailyPhase
|
||||||
for (const [emoji, description] of phaseTasks) {
|
for (const [emoji, description] of phaseTasks) {
|
||||||
|
@ -52,9 +53,10 @@ Object.entries(hardcoded).forEach(async ([tphase, phaseTasks]) => {
|
||||||
doneAt: null,
|
doneAt: null,
|
||||||
})
|
})
|
||||||
console.log(task)
|
console.log(task)
|
||||||
const result = await db.tasks.create({
|
try {
|
||||||
data: task,
|
await kv.set(['task', task.id], task)
|
||||||
})
|
} catch (e) {
|
||||||
console.log('after create:', result)
|
console.error('error creating:', e)
|
||||||
|
}
|
||||||
}
|
}
|
||||||
})
|
})
|
||||||
|
|
|
@ -1,6 +1,6 @@
|
||||||
import { Handlers } from '$fresh/server.ts'
|
import { Handlers } from '$fresh/server.ts'
|
||||||
import { Task, TaskModel } from '@homeman/models.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 { 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'
|
import { z } from 'https://deno.land/x/zod@v3.21.4/mod.ts'
|
||||||
|
|
||||||
|
@ -9,16 +9,17 @@ type TaskPayload = z.infer<typeof TaskPayload>
|
||||||
|
|
||||||
async function createOrUpdate(task: TaskPayload) {
|
async function createOrUpdate(task: TaskPayload) {
|
||||||
if (!task.id || task.id === '') {
|
if (!task.id || task.id === '') {
|
||||||
const newTask: Task = {
|
const newTask: Task = TaskModel.parse({
|
||||||
...task,
|
...task,
|
||||||
id: ulid(),
|
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)
|
await kv.set(['last_task_updated'], newTask.id)
|
||||||
return result
|
return result
|
||||||
} else {
|
} else {
|
||||||
const result = await db.tasks.update({ where: { id: task.id }, data: task })
|
const newTask: Task = TaskModel.parse(task)
|
||||||
await kv.set(['last_task_updated'], task.id)
|
const result = await kv.set(['task', task.id], newTask)
|
||||||
|
await kv.set(['last_task_updated'], newTask.id)
|
||||||
return result
|
return result
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
@ -42,17 +43,13 @@ export const handler: Handlers<Task | null> = {
|
||||||
emoji: form.get('emoji')?.toString() || null,
|
emoji: form.get('emoji')?.toString() || null,
|
||||||
doneAt: form.get('doneAt')?.toString() || null,
|
doneAt: form.get('doneAt')?.toString() || null,
|
||||||
description: form.get('description')?.toString(),
|
description: form.get('description')?.toString(),
|
||||||
assigneeUserId: form.get('assigneeUserId')?.toString() || null,
|
phase: form.get('phase')?.toString() || null,
|
||||||
})
|
})
|
||||||
|
|
||||||
if (!id) {
|
if (!id) {
|
||||||
delete task.id
|
delete task.id
|
||||||
}
|
}
|
||||||
|
|
||||||
if (!task.assigneeUserId) {
|
|
||||||
delete task.id
|
|
||||||
}
|
|
||||||
|
|
||||||
await createOrUpdate(task)
|
await createOrUpdate(task)
|
||||||
|
|
||||||
const url = new URL(req.url)
|
const url = new URL(req.url)
|
||||||
|
|
Loading…
Reference in a new issue