@event-driven-io/emmett-postgresql 0.38.3 → 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
@@ -804,9 +804,15 @@ var appendToStreamSQL = _dumbo.rawSql.call(void 0,
804
804
  v_transaction_id := pg_current_xact_id();
805
805
 
806
806
  IF v_expected_stream_position IS NULL THEN
807
- SELECT COALESCE(max(stream_position), 0) INTO v_expected_stream_position
808
- FROM ${streamsTable.name}
809
- 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;
810
816
  END IF;
811
817
 
812
818
  v_next_stream_position := v_expected_stream_position + array_upper(v_messages_data, 1);
@@ -1060,9 +1066,12 @@ var streamsTableSQL = _dumbo.rawSql.call(void 0,
1060
1066
  stream_type TEXT NOT NULL,
1061
1067
  stream_metadata JSONB NOT NULL,
1062
1068
  is_archived BOOLEAN NOT NULL DEFAULT FALSE,
1063
- PRIMARY KEY (stream_id, stream_position, partition, is_archived),
1064
- UNIQUE (stream_id, partition, is_archived)
1065
- ) 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);`
1066
1075
  );
1067
1076
  var messagesTableSQL = _dumbo.rawSql.call(void 0,
1068
1077
  `
@@ -2120,12 +2129,16 @@ var getPostgreSQLEventStore = (connectionString, options = defaultPostgreSQLOpti
2120
2129
  ...options.connectionOptions ? options.connectionOptions : {}
2121
2130
  };
2122
2131
  const pool = "dumbo" in poolOptions ? poolOptions.dumbo : _dumbo.dumbo.call(void 0, poolOptions);
2123
- let migrateSchema;
2132
+ let migrateSchema = void 0;
2124
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";
2125
2134
  const ensureSchemaExists = () => {
2126
2135
  if (!autoGenerateSchema) return Promise.resolve();
2127
2136
  if (!migrateSchema) {
2128
- 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
+ });
2129
2142
  }
2130
2143
  return migrateSchema;
2131
2144
  };
@@ -2151,7 +2164,7 @@ var getPostgreSQLEventStore = (connectionString, options = defaultPostgreSQLOpti
2151
2164
  },
2152
2165
  async aggregateStream(streamName, options2) {
2153
2166
  const { evolve, initialState, read } = options2;
2154
- const expectedStreamVersion = _optionalChain([read, 'optionalAccess', _87 => _87.expectedStreamVersion]);
2167
+ const expectedStreamVersion = _optionalChain([read, 'optionalAccess', _89 => _89.expectedStreamVersion]);
2155
2168
  let state = initialState();
2156
2169
  const result = await this.readStream(streamName, options2.read);
2157
2170
  const currentStreamVersion = result.currentStreamVersion;
@@ -2192,7 +2205,7 @@ var getPostgreSQLEventStore = (connectionString, options = defaultPostgreSQLOpti
2192
2205
  throw new ExpectedVersionConflictError(
2193
2206
  -1n,
2194
2207
  //TODO: Return actual version in case of error
2195
- _nullishCoalesce(_optionalChain([options2, 'optionalAccess', _88 => _88.expectedStreamVersion]), () => ( NO_CONCURRENCY_CHECK))
2208
+ _nullishCoalesce(_optionalChain([options2, 'optionalAccess', _90 => _90.expectedStreamVersion]), () => ( NO_CONCURRENCY_CHECK))
2196
2209
  );
2197
2210
  return {
2198
2211
  nextExpectedStreamVersion: appendResult.nextStreamPosition,
@@ -2212,16 +2225,22 @@ var getPostgreSQLEventStore = (connectionString, options = defaultPostgreSQLOpti
2212
2225
  ...options,
2213
2226
  connectionOptions: {
2214
2227
  connection
2228
+ },
2229
+ schema: {
2230
+ ..._nullishCoalesce(options.schema, () => ( {})),
2231
+ autoMigration: "None"
2215
2232
  }
2216
2233
  };
2217
2234
  const eventStore = getPostgreSQLEventStore(
2218
2235
  connectionString,
2219
2236
  storeOptions
2220
2237
  );
2221
- return callback({
2222
- eventStore,
2223
- close: () => Promise.resolve()
2224
- });
2238
+ return ensureSchemaExists().then(
2239
+ () => callback({
2240
+ eventStore,
2241
+ close: () => Promise.resolve()
2242
+ })
2243
+ );
2225
2244
  });
2226
2245
  }
2227
2246
  };