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 * 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 /** Insert or replace projector cursors in one statement. Empty batches do nothing. */
41 readonly upsertMany: (
42 rows: ReadonlyArray<ProjectionState>,
43 ) => Effect.Effect<void, ProjectionRepositoryError>;
44
45 /**
46 * Read projection cursor state for a projector key.
47 */
48 readonly getByProjector: (
49 input: GetProjectionStateInput,
50 ) => Effect.Effect<Option.Option<ProjectionState>, ProjectionRepositoryError>;
51
52 /**
53 * List all projector cursor rows.
54 */
55 readonly listAll: () => Effect.Effect<ReadonlyArray<ProjectionState>, ProjectionRepositoryError>;
56}
57
58/**
59 * ProjectionStateRepository - Service tag for projection cursor persistence.
60 */
61export class ProjectionStateRepository extends Context.Service<
62 ProjectionStateRepository,
63 ProjectionStateRepositoryShape
64>()("t3/persistence/Services/ProjectionState/ProjectionStateRepository") {}