40 lines
1.4 KiB
TypeScript
40 lines
1.4 KiB
TypeScript
import type { Collection } from "@valkyr/db";
|
|
|
|
import type { Snapshot, SnapshotsProvider } from "../../../types/adapter.ts";
|
|
|
|
export class BrowserSnapshotsProvider implements SnapshotsProvider {
|
|
constructor(readonly snapshots: Collection<Snapshot>) {}
|
|
|
|
/**
|
|
* Add snapshot state under given reducer stream.
|
|
*
|
|
* @param name - Name of the reducer the snapshot is attached to.
|
|
* @param stream - Stream the snapshot is attached to.
|
|
* @param cursor - Cursor timestamp for the last event used in the snapshot.
|
|
* @param state - State of the reduced events.
|
|
*/
|
|
async insert(name: string, stream: string, cursor: string, state: Record<string, unknown>): Promise<void> {
|
|
await this.snapshots.insertOne({ name, stream, cursor, state });
|
|
}
|
|
|
|
/**
|
|
* Get snapshot state by stream.
|
|
*
|
|
* @param name - Name of the reducer which the state was created.
|
|
* @param stream - Stream the state was reduced for.
|
|
*/
|
|
async getByStream(name: string, stream: string): Promise<Snapshot | undefined> {
|
|
return this.snapshots.findOne({ name, stream });
|
|
}
|
|
|
|
/**
|
|
* Removes a snapshot for the given reducer stream.
|
|
*
|
|
* @param name - Name of the reducer the snapshot is attached to.
|
|
* @param stream - Stream to remove from snapshots.
|
|
*/
|
|
async remove(name: string, stream: string): Promise<void> {
|
|
await this.snapshots.remove({ name, stream });
|
|
}
|
|
}
|