Todo optional ID

This commit is contained in:
Julien Valverdé
2024-06-28 14:58:17 +02:00
parent b49b4b19dc
commit b2098550e1
2 changed files with 8 additions and 4 deletions

View File

@@ -9,7 +9,7 @@ export class Todo
_kind: Kind("Todo"),
_tag: Tag("Todo"),
id: S.String,
id: S.OptionFromSelf(S.String),
title: S.String,
content: S.String,
@@ -26,6 +26,7 @@ export class Todo
export const JsonifiableTodo = Todo.pipe(Jsonifiable(S.Struct({
...Todo.fields,
id: S.OptionFromNullOr(S.String),
due: S.OptionFromNullOr(S.DateFromString),
createdAt: S.DateFromString,
updatedAt: S.DateFromString,

View File

@@ -1,4 +1,4 @@
import { Equal } from "effect"
import { Equal, Option } from "effect"
export interface Identifiable<
@@ -6,7 +6,7 @@ export interface Identifiable<
ID,
> {
readonly _kind: Kind
readonly id: ID
readonly id: Option.Option<ID>
}
export module Identifiable {
@@ -17,6 +17,9 @@ export module Identifiable {
that: Identifiable<Kind, ID>,
to: Identifiable<Kind, ID>,
): boolean {
return Equal.equals(that.id, to.id)
// Two elements can only be equal if they both have a defined ID
return Option.isSome(that.id) && Option.isSome(to.id)
? Equal.equals(that.id, to.id)
: false
}
}