@event-driven-io/emmett-postgresql 0.38.2 → 0.38.4

This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
package/dist/index.cjs CHANGED
@@ -3,7 +3,7 @@
3
3
 
4
4
  var _dumbo = require('@event-driven-io/dumbo');
5
5
 
6
- // ../emmett/dist/chunk-SJ7ERGWB.js
6
+ // ../emmett/dist/chunk-O2VMBOV4.js
7
7
  var isNumber = (val) => typeof val === "number" && val === val;
8
8
  var isBigint = (val) => typeof val === "bigint" && val === val;
9
9
  var isString = (val) => typeof val === "string";
@@ -20,6 +20,15 @@ var EmmettError = class _EmmettError extends Error {
20
20
  this.errorCode = errorCode;
21
21
  Object.setPrototypeOf(this, _EmmettError.prototype);
22
22
  }
23
+ static mapFrom(error) {
24
+ if (error instanceof _EmmettError) {
25
+ return error;
26
+ }
27
+ return new _EmmettError({
28
+ errorCode: "errorCode" in error && error.errorCode !== void 0 && error.errorCode !== null ? error.errorCode : 500,
29
+ message: _nullishCoalesce(error.message, () => ( "An unknown error occurred"))
30
+ });
31
+ }
23
32
  };
24
33
  var ConcurrencyError = class _ConcurrencyError extends EmmettError {
25
34
  constructor(current, expected, message) {
@@ -139,6 +148,7 @@ var asyncRetry = async (fn, opts) => {
139
148
  } catch (error2) {
140
149
  if (_optionalChain([opts, 'optionalAccess', _15 => _15.shouldRetryError]) && !opts.shouldRetryError(error2)) {
141
150
  bail(error2);
151
+ return void 0;
142
152
  }
143
153
  throw error2;
144
154
  }
@@ -314,6 +324,11 @@ var getCheckpoint = (message2) => {
314
324
  message2.metadata.streamPosition
315
325
  ) : null;
316
326
  };
327
+ var wasMessageHandled = (message2, checkpoint) => {
328
+ const messageCheckpoint = getCheckpoint(message2);
329
+ const checkpointBigint = checkpoint;
330
+ return messageCheckpoint !== null && messageCheckpoint !== void 0 && checkpointBigint !== null && checkpointBigint !== void 0 && messageCheckpoint <= checkpointBigint;
331
+ };
317
332
  var MessageProcessorType = {
318
333
  PROJECTOR: "projector",
319
334
  REACTOR: "reactor"
@@ -365,6 +380,7 @@ var reactor = (options) => {
365
380
  return await processingScope(async (context) => {
366
381
  let result = void 0;
367
382
  for (const message2 of messages) {
383
+ if (wasMessageHandled(message2, lastCheckpoint)) continue;
368
384
  const messageProcessingResult = await eachMessage(message2, context);
369
385
  if (checkpoints) {
370
386
  const storeCheckpointResult = await checkpoints.store(
@@ -377,7 +393,7 @@ var reactor = (options) => {
377
393
  },
378
394
  context
379
395
  );
380
- if (storeCheckpointResult && storeCheckpointResult.success) {
396
+ if (storeCheckpointResult.success) {
381
397
  lastCheckpoint = storeCheckpointResult.newCheckpoint;
382
398
  }
383
399
  }
@@ -788,9 +804,15 @@ var appendToStreamSQL = _dumbo.rawSql.call(void 0,
788
804
  v_transaction_id := pg_current_xact_id();
789
805
 
790
806
  IF v_expected_stream_position IS NULL THEN
791
- SELECT COALESCE(max(stream_position), 0) INTO v_expected_stream_position
792
- FROM ${streamsTable.name}
793
- WHERE stream_id = v_stream_id AND partition = v_partition;
807
+ SELECT COALESCE(
808
+ (SELECT stream_position
809
+ FROM ${streamsTable.name}
810
+ WHERE stream_id = v_stream_id
811
+ AND partition = v_partition
812
+ AND is_archived = FALSE
813
+ LIMIT 1),
814
+ 0
815
+ ) INTO v_expected_stream_position;
794
816
  END IF;
795
817
 
796
818
  v_next_stream_position := v_expected_stream_position + array_upper(v_messages_data, 1);
@@ -1044,9 +1066,12 @@ var streamsTableSQL = _dumbo.rawSql.call(void 0,
1044
1066
  stream_type TEXT NOT NULL,
1045
1067
  stream_metadata JSONB NOT NULL,
1046
1068
  is_archived BOOLEAN NOT NULL DEFAULT FALSE,
1047
- PRIMARY KEY (stream_id, stream_position, partition, is_archived),
1048
- UNIQUE (stream_id, partition, is_archived)
1049
- ) PARTITION BY LIST (partition);`
1069
+ PRIMARY KEY (stream_id, partition, is_archived)
1070
+ ) PARTITION BY LIST (partition);
1071
+
1072
+ CREATE UNIQUE INDEX IF NOT EXISTS idx_streams_unique
1073
+ ON ${streamsTable.name}(stream_id, partition, is_archived)
1074
+ INCLUDE (stream_position);`
1050
1075
  );
1051
1076
  var messagesTableSQL = _dumbo.rawSql.call(void 0,
1052
1077
  `
@@ -2104,12 +2129,16 @@ var getPostgreSQLEventStore = (connectionString, options = defaultPostgreSQLOpti
2104
2129
  ...options.connectionOptions ? options.connectionOptions : {}
2105
2130
  };
2106
2131
  const pool = "dumbo" in poolOptions ? poolOptions.dumbo : _dumbo.dumbo.call(void 0, poolOptions);
2107
- let migrateSchema;
2132
+ let migrateSchema = void 0;
2108
2133
  const autoGenerateSchema = _optionalChain([options, 'access', _83 => _83.schema, 'optionalAccess', _84 => _84.autoMigration]) === void 0 || _optionalChain([options, 'access', _85 => _85.schema, 'optionalAccess', _86 => _86.autoMigration]) !== "None";
2109
2134
  const ensureSchemaExists = () => {
2110
2135
  if (!autoGenerateSchema) return Promise.resolve();
2111
2136
  if (!migrateSchema) {
2112
- migrateSchema = createEventStoreSchema(pool);
2137
+ migrateSchema = createEventStoreSchema(pool).then(async () => {
2138
+ if (_optionalChain([options, 'access', _87 => _87.hooks, 'optionalAccess', _88 => _88.onAfterSchemaCreated])) {
2139
+ await options.hooks.onAfterSchemaCreated();
2140
+ }
2141
+ });
2113
2142
  }
2114
2143
  return migrateSchema;
2115
2144
  };
@@ -2135,7 +2164,7 @@ var getPostgreSQLEventStore = (connectionString, options = defaultPostgreSQLOpti
2135
2164
  },
2136
2165
  async aggregateStream(streamName, options2) {
2137
2166
  const { evolve, initialState, read } = options2;
2138
- const expectedStreamVersion = _optionalChain([read, 'optionalAccess', _87 => _87.expectedStreamVersion]);
2167
+ const expectedStreamVersion = _optionalChain([read, 'optionalAccess', _89 => _89.expectedStreamVersion]);
2139
2168
  let state = initialState();
2140
2169
  const result = await this.readStream(streamName, options2.read);
2141
2170
  const currentStreamVersion = result.currentStreamVersion;
@@ -2176,7 +2205,7 @@ var getPostgreSQLEventStore = (connectionString, options = defaultPostgreSQLOpti
2176
2205
  throw new ExpectedVersionConflictError(
2177
2206
  -1n,
2178
2207
  //TODO: Return actual version in case of error
2179
- _nullishCoalesce(_optionalChain([options2, 'optionalAccess', _88 => _88.expectedStreamVersion]), () => ( NO_CONCURRENCY_CHECK))
2208
+ _nullishCoalesce(_optionalChain([options2, 'optionalAccess', _90 => _90.expectedStreamVersion]), () => ( NO_CONCURRENCY_CHECK))
2180
2209
  );
2181
2210
  return {
2182
2211
  nextExpectedStreamVersion: appendResult.nextStreamPosition,
@@ -2196,16 +2225,22 @@ var getPostgreSQLEventStore = (connectionString, options = defaultPostgreSQLOpti
2196
2225
  ...options,
2197
2226
  connectionOptions: {
2198
2227
  connection
2228
+ },
2229
+ schema: {
2230
+ ..._nullishCoalesce(options.schema, () => ( {})),
2231
+ autoMigration: "None"
2199
2232
  }
2200
2233
  };
2201
2234
  const eventStore = getPostgreSQLEventStore(
2202
2235
  connectionString,
2203
2236
  storeOptions
2204
2237
  );
2205
- return callback({
2206
- eventStore,
2207
- close: () => Promise.resolve()
2208
- });
2238
+ return ensureSchemaExists().then(
2239
+ () => callback({
2240
+ eventStore,
2241
+ close: () => Promise.resolve()
2242
+ })
2243
+ );
2209
2244
  });
2210
2245
  }
2211
2246
  };