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

@ -1,5 +1,27 @@
import { z } from "zod/v4-mini";
import { defineEntity, idSchema, type Id } from "~/shared/types/common";
import { idSchema, type Id } from "~/shared/types/common";
export const apiEntitySchema = z.object({
id: idSchema,
updatedAt: z.string(),
deleted: z.optional(z.literal(false)),
});
export type ApiEntity = z.infer<typeof apiEntitySchema>;
export function defineApiEntity<T extends {}>(fields: T) {
return z.extend(apiEntitySchema, fields);
}
export function tombstonable<T extends z.ZodMiniObject>(entitySchema: T) {
return z.discriminatedUnion("deleted", [entitySchema, apiTombstoneSchema]);
}
export const apiTombstoneSchema = z.object({
id: idSchema,
updatedAt: z.string(),
deleted: z.literal(true),
});
export type ApiTombstone = z.infer<typeof apiTombstoneSchema>;
export const apiUserTypeSchema = z.union([
z.literal("anonymous"),
@ -47,7 +69,7 @@ export interface ApiSession {
push: boolean,
}
export const apiScheduleLocationSchema = defineEntity({
export const apiScheduleLocationSchema = defineApiEntity({
name: z.string(),
description: z.optional(z.string()),
});
@ -63,7 +85,7 @@ export const apiScheduleEventSlotSchema = z.object({
});
export type ApiScheduleEventSlot = z.infer<typeof apiScheduleEventSlotSchema>;
export const apiScheduleEventSchema = defineEntity({
export const apiScheduleEventSchema = defineApiEntity({
name: z.string(),
crew: z.optional(z.boolean()),
host: z.optional(z.string()),
@ -74,7 +96,7 @@ export const apiScheduleEventSchema = defineEntity({
});
export type ApiScheduleEvent = z.infer<typeof apiScheduleEventSchema>;
export const apiScheduleRoleSchema = defineEntity({
export const apiScheduleRoleSchema = defineApiEntity({
name: z.string(),
description: z.optional(z.string()),
});
@ -88,7 +110,7 @@ export const apiScheduleShiftSlotSchema = z.object({
});
export type ApiScheduleShiftSlot = z.infer<typeof apiScheduleShiftSlotSchema>;
export const apiScheduleShiftSchema = defineEntity({
export const apiScheduleShiftSchema = defineApiEntity({
roleId: idSchema,
name: z.string(),
description: z.optional(z.string()),
@ -96,16 +118,16 @@ export const apiScheduleShiftSchema = defineEntity({
});
export type ApiScheduleShift = z.infer<typeof apiScheduleShiftSchema>;
export const apiScheduleSchema = defineEntity({
export const apiScheduleSchema = defineApiEntity({
id: z.literal(111),
locations: z.optional(z.array(apiScheduleLocationSchema)),
events: z.optional(z.array(apiScheduleEventSchema)),
roles: z.optional(z.array(apiScheduleRoleSchema)),
shifts: z.optional(z.array(apiScheduleShiftSchema)),
locations: z.optional(z.array(tombstonable(apiScheduleLocationSchema))),
events: z.optional(z.array(tombstonable(apiScheduleEventSchema))),
roles: z.optional(z.array(tombstonable(apiScheduleRoleSchema))),
shifts: z.optional(z.array(tombstonable(apiScheduleShiftSchema))),
});
export type ApiSchedule = z.infer<typeof apiScheduleSchema>;
export const apiUserSchema = defineEntity({
export const apiUserSchema = defineApiEntity({
type: apiUserTypeSchema,
name: z.optional(z.string()),
});
@ -126,13 +148,13 @@ export interface ApiAccountUpdate {
export interface ApiScheduleUpdate {
type: "schedule-update",
updatedFrom?: string,
data: ApiSchedule,
data: ApiSchedule | ApiTombstone,
}
export interface ApiUserUpdate {
type: "user-update",
updatedFrom?: string,
data: ApiUser,
data: ApiUser | ApiTombstone,
}
export type ApiEvent =

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]);
}

View file

@ -1,6 +1,6 @@
import type { Entity } from "~/shared/types/common";
import type { ApiEntity, ApiTombstone } from "~/shared/types/api";
export function applyUpdatesToArray<T extends Entity>(updates: T[], entities: T[]) {
export function applyUpdatesToArray<T extends ApiEntity | ApiTombstone>(updates: T[], entities: T[]) {
const idMap = new Map(entities.map((e, i) => [e.id, i]));
for (const update of updates) {
const index = idMap.get(update.id);