Skip to content

Commit

Permalink
Fixed mapping of optimistic concurrency for PostgreSQL event store
Browse files Browse the repository at this point in the history
Bumped to 0.12.1
  • Loading branch information
oskardudycz committed Jul 11, 2024
1 parent c6500e2 commit 207b3b6
Show file tree
Hide file tree
Showing 9 changed files with 52 additions and 27 deletions.
26 changes: 13 additions & 13 deletions src/package-lock.json

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

2 changes: 1 addition & 1 deletion src/package.json
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
{
"name": "@event-driven-io/core",
"version": "0.12.0",
"version": "0.12.1",
"description": "Emmett - Event Sourcing development made simple",
"engines": {
"node": ">=20.11.1"
Expand Down
4 changes: 2 additions & 2 deletions src/packages/emmett-esdb/package.json
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
{
"name": "@event-driven-io/emmett-esdb",
"version": "0.12.0",
"version": "0.12.1",
"description": "Emmett - EventStoreDB - Event Sourcing development made simple",
"scripts": {
"build": "tsup",
Expand Down Expand Up @@ -50,7 +50,7 @@
"@event-driven-io/emmett-testcontainers": "^0.5.0"
},
"peerDependencies": {
"@event-driven-io/emmett": "0.12.0",
"@event-driven-io/emmett": "0.12.1",
"@eventstore/db-client": "^6.1.0"
}
}
4 changes: 2 additions & 2 deletions src/packages/emmett-expressjs/package.json
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
{
"name": "@event-driven-io/emmett-expressjs",
"version": "0.12.0",
"version": "0.12.1",
"description": "Emmett - Event Sourcing development made simple",
"scripts": {
"build": "tsup",
Expand Down Expand Up @@ -48,7 +48,7 @@
"dependencies": {},
"devDependencies": {},
"peerDependencies": {
"@event-driven-io/emmett": "0.12.0",
"@event-driven-io/emmett": "0.12.1",
"@types/express": "4.17.21",
"@types/supertest": "6.0.2",
"express": "4.19.2",
Expand Down
4 changes: 2 additions & 2 deletions src/packages/emmett-fastify/package.json
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
{
"name": "@event-driven-io/emmett-fastify",
"version": "0.12.0",
"version": "0.12.1",
"description": "Emmett - Event Sourcing development made simple",
"scripts": {
"build": "tsup",
Expand Down Expand Up @@ -52,7 +52,7 @@
"dependencies": {},
"devDependencies": {},
"peerDependencies": {
"@event-driven-io/emmett": "^0.12.0",
"@event-driven-io/emmett": "^0.12.1",
"fastify": "4.26.2",
"@fastify/compress": "7.0.0",
"@fastify/etag": "5.1.0",
Expand Down
4 changes: 2 additions & 2 deletions src/packages/emmett-postgresql/package.json
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
{
"name": "@event-driven-io/emmett-postgresql",
"version": "0.12.0",
"version": "0.12.1",
"description": "Emmett - PostgreSQL - Event Sourcing development made simple",
"scripts": {
"build": "tsup",
Expand Down Expand Up @@ -52,7 +52,7 @@
"@event-driven-io/emmett-testcontainers": "^0.5.0"
},
"peerDependencies": {
"@event-driven-io/emmett": "0.12.0",
"@event-driven-io/emmett": "0.12.1",
"@types/pg": "^8.11.6",
"@types/pg-format": "^1.0.5",
"pg": "^8.12.0",
Expand Down
Original file line number Diff line number Diff line change
@@ -1,7 +1,11 @@
import {
JSONParser,
NO_CONCURRENCY_CHECK,
STREAM_DOES_NOT_EXIST,
STREAM_EXISTS,
type AppendToStreamOptions,
type Event,
type ExpectedStreamVersion,
} from '@event-driven-io/emmett';
import pg from 'pg';
import { v4 as uuid } from 'uuid';
Expand Down Expand Up @@ -125,7 +129,11 @@ export const appendToStream = (
streamName,
streamType,
events,
options,
{
expectedStreamVersion: toExpectedVersion(
options?.expectedStreamVersion,
),
},
);
} catch (error) {
if (!isOptimisticConcurrencyError(error)) throw error;
Expand Down Expand Up @@ -162,6 +170,22 @@ export const appendToStream = (
};
});

const toExpectedVersion = (
expected: ExpectedStreamVersion | undefined,
): bigint | null => {
if (expected === undefined) return null;

if (expected === NO_CONCURRENCY_CHECK) return null;

// TODO: this needs to be fixed
if (expected == STREAM_DOES_NOT_EXIST) return null;

// TODO: this needs to be fixed
if (expected == STREAM_EXISTS) return null;

return expected as bigint;
};

const isOptimisticConcurrencyError = (error: unknown): boolean =>
error instanceof Error && 'code' in error && error.code === '23505';

Expand All @@ -177,7 +201,8 @@ const appendEventsRaw = (
streamId: string,
streamType: string,
events: Event[],
options?: AppendToStreamOptions & {
options?: {
expectedStreamVersion: bigint | null;
partition?: string;
},
): Promise<AppendEventSqlResult> =>
Expand Down
4 changes: 2 additions & 2 deletions src/packages/emmett-testcontainers/package.json
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
{
"name": "@event-driven-io/emmett-testcontainers",
"version": "0.12.0",
"version": "0.12.1",
"description": "Emmett - TestContainers - Event Sourcing development made simple",
"scripts": {
"build": "tsup",
Expand Down Expand Up @@ -46,7 +46,7 @@
"dist"
],
"dependencies": {
"@event-driven-io/emmett": "0.12.0",
"@event-driven-io/emmett": "0.12.1",
"testcontainers": "^10.7.2"
},
"devDependencies": {
Expand Down
2 changes: 1 addition & 1 deletion src/packages/emmett/package.json
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
{
"name": "@event-driven-io/emmett",
"version": "0.12.0",
"version": "0.12.1",
"description": "Emmett - Event Sourcing development made simple",
"scripts": {
"build": "tsup",
Expand Down

0 comments on commit 207b3b6

Please sign in to comment.