diff --git a/bun.lockb b/bun.lockb index 0b8476f..3958dbf 100755 Binary files a/bun.lockb and b/bun.lockb differ diff --git a/packages/server/package.json b/packages/server/package.json index 8b048d8..a2c3140 100644 --- a/packages/server/package.json +++ b/packages/server/package.json @@ -9,5 +9,8 @@ "@thilawyn/thilalib": "^0.1.4", "@todo-tests/common": "workspace:*", "effect": "^3.4.5" + }, + "devDependencies": { + "bun-types": "^1.1.17" } } diff --git a/packages/server/src/TodoRepository.ts b/packages/server/src/TodoRepository.ts index 086a099..6557696 100644 --- a/packages/server/src/TodoRepository.ts +++ b/packages/server/src/TodoRepository.ts @@ -1,5 +1,6 @@ import { Todo } from "@todo-tests/common/data" import { Array, Context, Data, Effect, Layer, Option, Ref, SubscriptionRef } from "effect" +import crypto from "node:crypto" export class TodoRepository extends Context.Tag("TodoRepository")() {} @@ -13,17 +14,17 @@ export const TodoRepositoryLive = Layer.effect(TodoRepository, export class TodoRepositoryService { constructor( - readonly ref: SubscriptionRef.SubscriptionRef<(Todo & { id: Option.Some })[]> + readonly todos: SubscriptionRef.SubscriptionRef<(Todo & { id: Option.Some })[]> ) {} get(id: string) { - return this.ref.get.pipe( + return this.todos.get.pipe( Effect.map(Array.findFirst(todo => todo.id.value === id)) ) } getIndex(id: string) { - return this.ref.get.pipe( + return this.todos.get.pipe( Effect.map(Array.findFirstIndex(todo => todo.id.value === id)) ) } @@ -33,10 +34,10 @@ export class TodoRepositoryService { if (Option.isSome(todo.id)) return yield* Effect.fail(new TodoHasID({ todo })) - yield* Ref.update(this.ref, todos => + yield* Ref.update(this.todos, todos => Array.append(todos, new Todo({ ...todo, - id: Option.some(""), + id: Option.some(crypto.randomUUID()), }) as Todo & { id: Option.Some }) ) }) @@ -47,7 +48,7 @@ export class TodoRepositoryService { if (Option.isNone(todo.id)) return yield* Effect.fail(new TodoHasNoID({ todo })) - yield* Ref.update(this.ref, Array.replace( + yield* Ref.update(this.todos, Array.replace( yield* yield* this.getIndex(todo.id.value), todo as Todo & { id: Option.Some }, )) @@ -59,7 +60,7 @@ export class TodoRepositoryService { if (Option.isNone(todo.id)) return yield* Effect.fail(new TodoHasNoID({ todo })) - yield* Ref.update(this.ref, Array.remove( + yield* Ref.update(this.todos, Array.remove( yield* yield* this.getIndex(todo.id.value) )) }) diff --git a/packages/server/src/index.ts b/packages/server/src/index.ts index 2e6ff7b..8e8afd8 100644 --- a/packages/server/src/index.ts +++ b/packages/server/src/index.ts @@ -7,7 +7,7 @@ const main = Effect.gen(function*() { yield* createDefaultTodos const todos = yield* TodoRepository - console.log(yield* todos.ref.get) + console.log(yield* todos.todos.get) }) const runnableMain = main.pipe(