Skip to content
Source
Sign inSign up
pingdotgg/t3code
Open History to see the last commit

ProjectionState.ts

66 lines2.1 KB

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 * as Option from "effect/Option";
11import * as Schema from "effect/Schema";
12import * as Context from "effect/Context";
13import type * as Effect from "effect/Effect";
14​
15import type { ProjectionRepositoryError } from "../Errors.ts";
16​
17export const ProjectionState = Schema.Struct({
18 projector: Schema.String,
19 lastAppliedSequence: NonNegativeInt,
20 updatedAt: IsoDateTime,
21});
22export type ProjectionState = typeof ProjectionState.Type;
23​
24export const GetProjectionStateInput = Schema.Struct({
25 projector: Schema.String,
26});
27export type GetProjectionStateInput = typeof GetProjectionStateInput.Type;
28​
29/**
30 * ProjectionStateRepositoryShape - Service API for projector state records.
31 */
32export interface ProjectionStateRepositoryShape {
33 /**
34 * Insert or replace a projection cursor row.
35 *
36 * Upserts by projector name.
37 */
38 readonly upsert: (row: ProjectionState) => Effect.Effect<void, ProjectionRepositoryError>;
39​
40 /**
41 * Read projection cursor state for a projector key.
42 */
43 readonly getByProjector: (
44 input: GetProjectionStateInput,
45 ) => Effect.Effect<Option.Option<ProjectionState>, ProjectionRepositoryError>;
46​
47 /**
48 * List all projector cursor rows.
49 */
50 readonly listAll: () => Effect.Effect<ReadonlyArray<ProjectionState>, ProjectionRepositoryError>;
51​
52 /**
53 * Read the minimum applied sequence across all projectors.
54 *
55 * Returns `null` when no projector state rows exist.
56 */
57 readonly minLastAppliedSequence: () => Effect.Effect<number | null, ProjectionRepositoryError>;
59​
60/**
61 * ProjectionStateRepository - Service tag for projection cursor persistence.
62 */
63export class ProjectionStateRepository extends Context.Service<
64 ProjectionStateRepository,
65 ProjectionStateRepositoryShape
66>()("t3/persistence/Services/ProjectionState/ProjectionStateRepository") {}