Skip to content

Commit

Permalink
Swapped order of handling event stream and event storage
Browse files Browse the repository at this point in the history
  • Loading branch information
dave authored and oskardudycz committed Jan 9, 2025
1 parent c473f94 commit e8e0b3c
Showing 1 changed file with 18 additions and 18 deletions.
36 changes: 18 additions & 18 deletions src/packages/emmett-sqlite/src/eventStore/schema/appendToStream.ts
Original file line number Diff line number Diff line change
Expand Up @@ -115,24 +115,6 @@ const appendEventsRaw = async (
);
}

const { sqlString, values } = buildEventInsertQuery(
events,
expectedStreamVersion,
streamId,
streamType,
options?.partition?.toString() ?? defaultTag,
);

const returningId = await db.querySingle<{
global_position: string;
} | null>(sqlString, values);

if (returningId?.global_position == null) {
throw new Error('Could not find global position');
}

globalPosition = BigInt(returningId.global_position);

let position: { stream_position: string } | null;

if (expectedStreamVersion === 0n) {
Expand Down Expand Up @@ -192,6 +174,24 @@ const appendEventsRaw = async (
};
}
}

const { sqlString, values } = buildEventInsertQuery(
events,
expectedStreamVersion,
streamId,
streamType,
options?.partition?.toString() ?? defaultTag,
);

const returningId = await db.querySingle<{
global_position: string;
} | null>(sqlString, values);

if (returningId?.global_position == null) {
throw new Error('Could not find global position');
}

globalPosition = BigInt(returningId.global_position);
} catch (err: unknown) {
if (isSQLiteError(err) && isOptimisticConcurrencyError(err)) {
return {
Expand Down

0 comments on commit e8e0b3c

Please sign in to comment.