Skip to content
This repository has been archived by the owner on Sep 24, 2024. It is now read-only.

Commit

Permalink
fixing projections snapshots and cleaning up tests
Browse files Browse the repository at this point in the history
  • Loading branch information
DominikBB committed Apr 23, 2024
1 parent 5adae65 commit cba4003
Show file tree
Hide file tree
Showing 7 changed files with 11 additions and 17 deletions.
5 changes: 2 additions & 3 deletions .vscode/launch.json
Original file line number Diff line number Diff line change
Expand Up @@ -30,9 +30,8 @@
"type": "node",
"request": "launch",
"name": "Debug AVA test file",
"runtimeExecutable": "yarn",
"runtimeArgs": ["run", "ava"],
"args": ["${file}"],
"program": "${workspaceFolder}/node_modules/ava/cli.js",
"args": ["--serial", "${file}"],
"outputCapture": "std",
"console": "integratedTerminal", // optional
"skipFiles": ["<node_internals>/**/*.js"]
Expand Down
2 changes: 0 additions & 2 deletions src/@types/LittleEsEventMetadata.d.ts
Original file line number Diff line number Diff line change
Expand Up @@ -11,5 +11,3 @@ export type LittleEsEventMetadata = {
readonly is: "PrivateEvent" | "PublicEvent" | "NamedProjection" | "GlobalProjection" | "AggregateSnapshot";
};
};

export const ID_SEPARATOR = "_";
2 changes: 1 addition & 1 deletion src/lib/aggregate.spec.ts
Original file line number Diff line number Diff line change
Expand Up @@ -6,11 +6,11 @@ import anyTest from 'ava';

import { Aggregate } from '../@types/Aggregate';
import { LittleEsEvent } from '../@types/LittleEsEvent';
import { ID_SEPARATOR } from '../@types/LittleEsEventMetadata';
import { Snapshot } from '../@types/Snapshot';

import { createAggregate } from './aggregate';
import { aggregateEventHandlers, commandHandlers, mockPersistanceHandler, Product, ProductCommand, ProductEvent } from './testdata.spec';
import { ID_SEPARATOR } from './util';

const test = anyTest as TestInterface<{
events: LittleEsEvent<ProductEvent>[],
Expand Down
3 changes: 1 addition & 2 deletions src/lib/aggregate.ts
Original file line number Diff line number Diff line change
Expand Up @@ -4,11 +4,10 @@ import { Command } from "../@types/Command";
import { CommandHandler } from "../@types/CommandHandler";
import { EventHandler } from "../@types/EventHandler";
import { EventStoreResult, LittleEsEvent } from "../@types/LittleEsEvent";
import { ID_SEPARATOR } from "../@types/LittleEsEventMetadata";
import { PersistanceHandler } from "../@types/PersistanceHandler";
import { PublishingHandler } from "../@types/PublishingHandler";

import { hydrateAggregate, SafeArray } from "./util";
import { hydrateAggregate, ID_SEPARATOR, SafeArray } from "./util";

/**
* Configuration options for the Aggregate.
Expand Down
8 changes: 3 additions & 5 deletions src/lib/projection.ts
Original file line number Diff line number Diff line change
Expand Up @@ -60,7 +60,6 @@ export function createNamedProjection<TPROJECTION, TEVENT extends BaseEvent>(

return {
get: getNamedProjectionWorkflow(
opt.projectionName,
opt.persistanceHandler.getProjection(opt.projectionName),
hydrateProjectionFromSnapshot(opt.defaultProjection, opt.eventHandler, opt.snapshot?.schemaVersion),
snapshotProjection(opt.persistanceHandler, opt.snapshot)
Expand Down Expand Up @@ -111,7 +110,6 @@ export function createGlobalProjection<TPROJECTION, TEVENT extends BaseEvent>(
}

const getNamedProjectionWorkflow = <TPROJECTION, TEVENT extends BaseEvent>(
projectionName: string,
retrieveAggregateEvents: (id?: string) => Promise<EventStoreResult<PersistedProjection<TPROJECTION, TEVENT>>>,
hydrateProjection: (state: PersistedProjection<TPROJECTION, TEVENT>) => TPROJECTION,
snapshotProjection: (projectionName: string, state: TPROJECTION, latestEventId: string, lastSnapshotEventId: string) => Promise<EventStoreResult<null>>,
Expand All @@ -124,10 +122,10 @@ const getNamedProjectionWorkflow = <TPROJECTION, TEVENT extends BaseEvent>(

if (SafeArray(existingEventsResult.data.events)) {
await snapshotProjection(
projectionName,
id,
projection,
existingEventsResult.data.events.slice(-1)[0].id,
existingEventsResult.data.snapshot?.lastConsideredEvent ?? "a_1"
existingEventsResult.data.snapshot?.lastConsideredEvent ?? "1_a"
)
};

Expand All @@ -151,7 +149,7 @@ const getGlobalProjectionWorkflow = <TPROJECTION, TEVENT extends BaseEvent>(
projectionName,
projection,
existingEventsResult.data.events.slice(-1)[0].id,
existingEventsResult.data.snapshot?.lastConsideredEvent ?? "a_1"
existingEventsResult.data.snapshot?.lastConsideredEvent ?? "1_a"
)
};

Expand Down
3 changes: 1 addition & 2 deletions src/lib/testdata.spec.ts
Original file line number Diff line number Diff line change
Expand Up @@ -83,8 +83,7 @@ export const mockPersistanceHandler = <TAGGREGATE, TEVENT extends BaseEvent>(c:

const maybePreviousSnapshot = (i?: string) => {
if (!i) return 0
if (id) return validateEventId(i) ? extractEventSequenceId(i) : 0
return 0
return validateEventId(i) ? extractEventSequenceId(i) : 0
}

const snapshot = c.context.snapshots.find(snapshot => snapshot.name === searchOn)
Expand Down
5 changes: 3 additions & 2 deletions src/lib/util.ts
Original file line number Diff line number Diff line change
@@ -1,7 +1,6 @@
import { BaseEvent } from "../@types/BaseEvent";
import { EventHandler } from "../@types/EventHandler";
import { EventStoreResult, LittleEsEvent } from "../@types/LittleEsEvent";
import { ID_SEPARATOR } from "../@types/LittleEsEventMetadata";
import { PersistanceHandler } from "../@types/PersistanceHandler";
import { PersistedProjection } from "../@types/PersistedAggregate";

Expand Down Expand Up @@ -48,5 +47,7 @@ export const snapshotProjection = <TPROJECTION, TEVENT extends BaseEvent>(persis
const hasValidSnapshot = <TPROJECTION, TEVENT extends BaseEvent>(state: PersistedProjection<TPROJECTION, TEVENT>, currentSchemaVersion: number | undefined) =>
state.snapshot && currentSchemaVersion && !isEmpty(state.snapshot) && state.snapshot.schemaVersion === currentSchemaVersion;

export const validateEventId = (id: string) => id.split(ID_SEPARATOR).length === 2 && parseInt(id.split(ID_SEPARATOR)[0]) > 0;
export const validateEventId = (id: string) => id.split(ID_SEPARATOR).length >= 2 && parseInt(id.split(ID_SEPARATOR)[0]) > 0;
export const extractEventSequenceId = (id: string) => parseInt(id.split(ID_SEPARATOR)[0]);

export const ID_SEPARATOR = "_";

0 comments on commit cba4003

Please sign in to comment.