Files
t3code/apps/server/src/persistence/Services/ProjectionState.ts
T
3405a64dd8 bump effect to latest beta (#1866)
Co-authored-by: Claude Opus 4.6 (1M context) <[email protected]>
Co-authored-by: Julius Marminge <[email protected]>
Co-authored-by: codex <[email protected]>
2026-04-10 01:51:31 -07:00

65 lines
2.0 KiB
TypeScript

/**
* ProjectionStateRepository - Projection repository interface for projector cursors.
*
* Owns persistence operations for projection cursor state used to resume
* incremental event projection.
*
* @module ProjectionStateRepository
*/
import { IsoDateTime, NonNegativeInt } from "@t3tools/contracts";
import { Option, Schema, Context } from "effect";
import type { Effect } from "effect";
import type { ProjectionRepositoryError } from "../Errors.ts";
export const ProjectionState = Schema.Struct({
projector: Schema.String,
lastAppliedSequence: NonNegativeInt,
updatedAt: IsoDateTime,
});
export type ProjectionState = typeof ProjectionState.Type;
export const GetProjectionStateInput = Schema.Struct({
projector: Schema.String,
});
export type GetProjectionStateInput = typeof GetProjectionStateInput.Type;
/**
* ProjectionStateRepositoryShape - Service API for projector state records.
*/
export interface ProjectionStateRepositoryShape {
/**
* Insert or replace a projection cursor row.
*
* Upserts by projector name.
*/
readonly upsert: (row: ProjectionState) => Effect.Effect<void, ProjectionRepositoryError>;
/**
* Read projection cursor state for a projector key.
*/
readonly getByProjector: (
input: GetProjectionStateInput,
) => Effect.Effect<Option.Option<ProjectionState>, ProjectionRepositoryError>;
/**
* List all projector cursor rows.
*/
readonly listAll: () => Effect.Effect<ReadonlyArray<ProjectionState>, ProjectionRepositoryError>;
/**
* Read the minimum applied sequence across all projectors.
*
* Returns `null` when no projector state rows exist.
*/
readonly minLastAppliedSequence: () => Effect.Effect<number | null, ProjectionRepositoryError>;
}
/**
* ProjectionStateRepository - Service tag for projection cursor persistence.
*/
export class ProjectionStateRepository extends Context.Service<
ProjectionStateRepository,
ProjectionStateRepositoryShape
>()("t3/persistence/Services/ProjectionState/ProjectionStateRepository") {}