Skip to main content

@idpass/data-collect-core / ProjectionRebuildService

Class: ProjectionRebuildService

Defined in: services/ProjectionRebuildService.ts:63

Service for rebuilding entity projections from the event log.

Replays all events from the event store in chronological order, applying each through the EventApplierService to rebuild the entity store from scratch.

This is useful when the entity store becomes corrupted, out of sync, or needs to be rebuilt after schema migrations.

Example

const service = new ProjectionRebuildService(eventStore, entityStore, eventApplierService);

const result = await service.rebuild({
onProgress: (processed, total) => {
console.log(`Progress: ${processed}/${total}`);
},
batchSize: 50,
});

console.log(`Rebuilt ${result.appliedEvents} entities in ${result.durationMs}ms`);

Constructors

Constructor

new ProjectionRebuildService(eventStore, entityStore, eventApplierService): ProjectionRebuildService

Defined in: services/ProjectionRebuildService.ts:64

Parameters

eventStore

EventStore

entityStore

EntityStore

eventApplierService

EventApplierService

Returns

ProjectionRebuildService

Methods

rebuild()

rebuild(options?): Promise<RebuildResult>

Defined in: services/ProjectionRebuildService.ts:81

Clears the entity store and rebuilds it by replaying all events.

Events are sorted by timestamp before replay to ensure consistent results. Failed events are recorded but do not stop the rebuild — the process continues with subsequent events.

Parameters

options?
onProgress?

(processed, total) => void

Optional callback invoked after each batch with current progress

batchSize?

number

Number of events to process before invoking onProgress (default 100)

Returns

Promise<RebuildResult>

Summary of the rebuild operation