Refactor base types for entities and tombstones

Rename the base Entity type to ApiEntity, and the base EntityToombstone
to ApiTombstone to better reflect the reality that its only used in the
API interface and that the client and server types uses its own base if
any.

Remove EntityLiving and pull EntityTombstone out of of the base entity
type so that the types based on ApiEntity are always living entities and
if it's possible for it to contain tombstone this will be explicitly
told with the type including a union with ApiTombstone.

Refactor the types of the ClientEntity and ClientMap to better reflect
the types of the entities it stores and converts to/from.
This commit is contained in:
Hornwitser 2025-06-24 15:19:11 +02:00
parent e3ff872b5c
commit 985b8e0950
13 changed files with 107 additions and 102 deletions

View file

@ -2,27 +2,3 @@ import { z } from "zod/v4-mini";
export const idSchema = z.number();
export type Id = z.infer<typeof idSchema>;
export const entityLivingSchema = z.object({
id: idSchema,
updatedAt: z.string(),
deleted: z.optional(z.literal(false)),
});
export type EntityLiving = z.infer<typeof entityLivingSchema>;
export const entityToombstoneSchema = z.object({
id: idSchema,
updatedAt: z.string(),
deleted: z.literal(true),
});
export type EntityToombstone = z.infer<typeof entityToombstoneSchema>;
export const entitySchema = z.discriminatedUnion("deleted", [entityLivingSchema, entityToombstoneSchema]);
export type Entity = z.infer<typeof entitySchema>;
export type Living<T extends Entity> = Extract<T, { deleted?: false }>;
export type Tombstone<T extends Entity> = Extract<T, { deleted: true }>;
export function defineEntity<T extends {}>(fields: T) {
return z.discriminatedUnion("deleted", [z.extend(entityLivingSchema, fields), entityToombstoneSchema]);
}