TodoRepository work
This commit is contained in:
@@ -2,7 +2,8 @@
|
|||||||
"name": "@todo-tests/common",
|
"name": "@todo-tests/common",
|
||||||
"type": "module",
|
"type": "module",
|
||||||
"exports": {
|
"exports": {
|
||||||
"./data": "./src/data/index.ts"
|
"./data": "./src/data/index.ts",
|
||||||
|
"./traits": "./src/traits/index.ts"
|
||||||
},
|
},
|
||||||
"private": true,
|
"private": true,
|
||||||
"dependencies": {
|
"dependencies": {
|
||||||
|
|||||||
32
packages/common/src/data/Todo.class.ts
Normal file
32
packages/common/src/data/Todo.class.ts
Normal file
@@ -0,0 +1,32 @@
|
|||||||
|
import { Schema as S } from "@effect/schema"
|
||||||
|
import { Jsonifiable, Kind, Tag } from "@thilawyn/thilalib/effect/schema"
|
||||||
|
import { Class } from "@thilawyn/thilalib/effect/schema/class"
|
||||||
|
import type { Identifiable } from "../traits"
|
||||||
|
|
||||||
|
|
||||||
|
export class Todo
|
||||||
|
extends Class<Todo>("Todo")({
|
||||||
|
_kind: Kind("Todo"),
|
||||||
|
_tag: Tag("Todo"),
|
||||||
|
|
||||||
|
id: S.String,
|
||||||
|
title: S.String,
|
||||||
|
content: S.String,
|
||||||
|
|
||||||
|
due: S.OptionFromSelf(S.DateFromSelf),
|
||||||
|
completed: S.Boolean,
|
||||||
|
|
||||||
|
createdAt: S.DateFromSelf,
|
||||||
|
updatedAt: S.DateFromSelf,
|
||||||
|
})
|
||||||
|
implements Identifiable<"Todo", string>
|
||||||
|
{}
|
||||||
|
|
||||||
|
|
||||||
|
export const JsonifiableTodo = Todo.pipe(Jsonifiable(S.Struct({
|
||||||
|
...Todo.fields,
|
||||||
|
|
||||||
|
due: S.OptionFromNullOr(S.DateFromString),
|
||||||
|
createdAt: S.DateFromString,
|
||||||
|
updatedAt: S.DateFromString,
|
||||||
|
})))
|
||||||
@@ -1,28 +0,0 @@
|
|||||||
import { Schema as S } from "@effect/schema"
|
|
||||||
import { Jsonifiable, Kind, Tag } from "@thilawyn/thilalib/effect/schema"
|
|
||||||
import { Class } from "@thilawyn/thilalib/effect/schema/class"
|
|
||||||
|
|
||||||
|
|
||||||
export class Todo extends Class<Todo>("Todo")({
|
|
||||||
_kind: Kind("Todo"),
|
|
||||||
_tag: Tag("Todo"),
|
|
||||||
|
|
||||||
id: S.String,
|
|
||||||
title: S.String,
|
|
||||||
content: S.String,
|
|
||||||
|
|
||||||
due: S.OptionFromSelf(S.DateFromSelf),
|
|
||||||
completed: S.Boolean,
|
|
||||||
|
|
||||||
createdAt: S.DateFromSelf,
|
|
||||||
updatedAt: S.DateFromSelf,
|
|
||||||
}) {}
|
|
||||||
|
|
||||||
|
|
||||||
export const JsonifiableTodo = Todo.pipe(Jsonifiable(S.Struct({
|
|
||||||
...Todo.fields,
|
|
||||||
|
|
||||||
due: S.OptionFromNullOr(S.DateFromString),
|
|
||||||
createdAt: S.DateFromString,
|
|
||||||
updatedAt: S.DateFromString,
|
|
||||||
})))
|
|
||||||
@@ -1 +1 @@
|
|||||||
export * from "./Todo"
|
export * from "./Todo.class"
|
||||||
|
|||||||
22
packages/common/src/traits/Identifiable.trait.ts
Normal file
22
packages/common/src/traits/Identifiable.trait.ts
Normal file
@@ -0,0 +1,22 @@
|
|||||||
|
import { Equal } from "effect"
|
||||||
|
|
||||||
|
|
||||||
|
export interface Identifiable<
|
||||||
|
Kind extends string,
|
||||||
|
ID,
|
||||||
|
> {
|
||||||
|
readonly _kind: Kind
|
||||||
|
readonly id: ID
|
||||||
|
}
|
||||||
|
|
||||||
|
export module Identifiable {
|
||||||
|
export function equals<
|
||||||
|
Kind extends string,
|
||||||
|
ID,
|
||||||
|
>(
|
||||||
|
that: Identifiable<Kind, ID>,
|
||||||
|
to: Identifiable<Kind, ID>,
|
||||||
|
): boolean {
|
||||||
|
return Equal.equals(that.id, to.id)
|
||||||
|
}
|
||||||
|
}
|
||||||
1
packages/common/src/traits/index.ts
Normal file
1
packages/common/src/traits/index.ts
Normal file
@@ -0,0 +1 @@
|
|||||||
|
export * from "./Identifiable.trait"
|
||||||
@@ -1,9 +1,33 @@
|
|||||||
import { Todo } from "@todo-tests/common/data"
|
import { Todo } from "@todo-tests/common/data"
|
||||||
import { Array, Context, Effect, Option, Ref } from "effect"
|
import { Array, Context, Effect, Option, Ref, SubscriptionRef } from "effect"
|
||||||
|
|
||||||
|
|
||||||
export class TodoRepository extends Context.Tag("TodoRepository")<TodoRepository, Ref.Ref<Todo[]>>() {}
|
export class TodoRepository extends Context.Tag("TodoRepository")<TodoRepository, Ref.Ref<Todo[]>>() {}
|
||||||
|
|
||||||
|
export class TodoRepositoryService {
|
||||||
|
constructor(
|
||||||
|
readonly ref: SubscriptionRef.SubscriptionRef<Todo[]>
|
||||||
|
) {}
|
||||||
|
|
||||||
|
get(id: string) {
|
||||||
|
return this.ref.get.pipe(
|
||||||
|
Effect.map(Array.findFirst(todo => todo.id === id))
|
||||||
|
)
|
||||||
|
}
|
||||||
|
getIndex(id: string) {
|
||||||
|
return this.ref.get.pipe(
|
||||||
|
Effect.map(Array.findFirstIndex(todo => todo.id === id))
|
||||||
|
)
|
||||||
|
}
|
||||||
|
|
||||||
|
update(todo: Todo) {
|
||||||
|
return Effect.gen(this, function*() {
|
||||||
|
const todoWithSameIDIndex = yield* this.getIndex(todo.id)
|
||||||
|
|
||||||
|
})
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
|
||||||
export const createDefaultTodos = TodoRepository.pipe(Effect.flatMap(repo =>
|
export const createDefaultTodos = TodoRepository.pipe(Effect.flatMap(repo =>
|
||||||
Ref.update(repo, todos =>
|
Ref.update(repo, todos =>
|
||||||
|
|||||||
Reference in New Issue
Block a user