Open History to see the last commit
1/**
2 * ProjectionStateRepository - Projection repository interface for projector cursors.
3 *
4 * Owns persistence operations for projection cursor state used to resume
5 * incremental event projection.
6 *
7 * @module ProjectionStateRepository
8 */
9import { IsoDateTime, NonNegativeInt } from "@t3tools/contracts";
10import { Option, Schema, ServiceMap } from "effect";
11import type { Effect } from "effect";
12
13import type { ProjectionRepositoryError } from "../Errors.ts";
14
15export const ProjectionState = Schema.Struct({
16 projector: Schema.String,
17 lastAppliedSequence: NonNegativeInt,
18 updatedAt: IsoDateTime,
19});
20export type ProjectionState = typeof ProjectionState.Type;
21
22export const GetProjectionStateInput = Schema.Struct({
23 projector: Schema.String,
24});
25export type GetProjectionStateInput = typeof GetProjectionStateInput.Type;
26
27/**
28 * ProjectionStateRepositoryShape - Service API for projector state records.
29 */
30export interface ProjectionStateRepositoryShape {
31 /**
32 * Insert or replace a projection cursor row.
33 *
34 * Upserts by projector name.
35 */
36 readonly upsert: (row: ProjectionState) => Effect.Effect<void, ProjectionRepositoryError>;
37
38 /**
39 * Read projection cursor state for a projector key.
40 */
41 readonly getByProjector: (
42 input: GetProjectionStateInput,
43 ) => Effect.Effect<Option.Option<ProjectionState>, ProjectionRepositoryError>;
44
45 /**
46 * List all projector cursor rows.
47 */
48 readonly listAll: () => Effect.Effect<ReadonlyArray<ProjectionState>, ProjectionRepositoryError>;
49
50 /**
51 * Read the minimum applied sequence across all projectors.
52 *
53 * Returns `null` when no projector state rows exist.
54 */
55 readonly minLastAppliedSequence: () => Effect.Effect<number | null, ProjectionRepositoryError>;
56}
57
58/**
59 * ProjectionStateRepository - Service tag for projection cursor persistence.
60 */
61export class ProjectionStateRepository extends ServiceMap.Service<
62 ProjectionStateRepository,
63 ProjectionStateRepositoryShape
64>()("t3/persistence/Services/ProjectionState/ProjectionStateRepository") {}