@mastra/pg 1.36.0-alpha.4 → 1.36.0-alpha.5

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.js CHANGED
@@ -2066,8 +2066,10 @@ function truncateQuery(query, maxLength = 100) {
2066
2066
  */
2067
2067
  var PoolAdapter = class {
2068
2068
  $pool;
2069
- constructor($pool) {
2069
+ onRollbackError;
2070
+ constructor($pool, onRollbackError) {
2070
2071
  this.$pool = $pool;
2072
+ this.onRollbackError = onRollbackError;
2071
2073
  }
2072
2074
  connect() {
2073
2075
  return this.$pool.connect();
@@ -2104,8 +2106,14 @@ var PoolAdapter = class {
2104
2106
  }
2105
2107
  async tx(callback) {
2106
2108
  const client = await this.$pool.connect();
2109
+ let destroyClient = false;
2107
2110
  try {
2108
- await client.query("BEGIN");
2111
+ try {
2112
+ await client.query("BEGIN");
2113
+ } catch (error) {
2114
+ destroyClient = true;
2115
+ throw error;
2116
+ }
2109
2117
  const txClient = new TransactionClient(client);
2110
2118
  try {
2111
2119
  const result = await callback(txClient);
@@ -2117,12 +2125,16 @@ var PoolAdapter = class {
2117
2125
  try {
2118
2126
  await client.query("ROLLBACK");
2119
2127
  } catch (rollbackError) {
2120
- console.error("Transaction rollback failed:", rollbackError);
2128
+ destroyClient = true;
2129
+ try {
2130
+ if (this.onRollbackError) await this.onRollbackError(rollbackError);
2131
+ else console.error("Failed to rollback transaction:", rollbackError);
2132
+ } catch {}
2121
2133
  }
2122
2134
  throw error;
2123
2135
  }
2124
2136
  } finally {
2125
- client.release();
2137
+ client.release(destroyClient || void 0);
2126
2138
  }
2127
2139
  }
2128
2140
  };
@@ -2229,6 +2241,7 @@ var TransactionClient = class {
2229
2241
  var PinnedClientAdapter = class {
2230
2242
  $pool;
2231
2243
  pinnedClient;
2244
+ onRollbackError;
2232
2245
  /**
2233
2246
  * Serialization tail. Domain init() methods fire via Promise.all in
2234
2247
  * MastraCompositeStore.#runInit(), so without our own gate every domain's
@@ -2239,9 +2252,14 @@ var PinnedClientAdapter = class {
2239
2252
  * the reliance on pg's internal queue.
2240
2253
  */
2241
2254
  #tail = Promise.resolve();
2242
- constructor($pool, pinnedClient) {
2255
+ #rollbackError;
2256
+ constructor($pool, pinnedClient, onRollbackError) {
2243
2257
  this.$pool = $pool;
2244
2258
  this.pinnedClient = pinnedClient;
2259
+ this.onRollbackError = onRollbackError;
2260
+ }
2261
+ get shouldDestroyClient() {
2262
+ return this.#rollbackError !== void 0;
2245
2263
  }
2246
2264
  /**
2247
2265
  * Run `fn` after any previously enqueued work on this pinned client has
@@ -2249,7 +2267,11 @@ var PinnedClientAdapter = class {
2249
2267
  * resume on the next caller's turn.
2250
2268
  */
2251
2269
  #enqueue(fn) {
2252
- const next = this.#tail.then(fn, fn);
2270
+ const run = async () => {
2271
+ if (this.shouldDestroyClient) throw this.#rollbackError;
2272
+ return fn();
2273
+ };
2274
+ const next = this.#tail.then(run, run);
2253
2275
  this.#tail = next.catch(() => void 0);
2254
2276
  return next;
2255
2277
  }
@@ -2301,7 +2323,12 @@ var PinnedClientAdapter = class {
2301
2323
  }
2302
2324
  tx(callback) {
2303
2325
  return this.#enqueue(async () => {
2304
- await this.pinnedClient.query("BEGIN");
2326
+ try {
2327
+ await this.pinnedClient.query("BEGIN");
2328
+ } catch (error) {
2329
+ this.#rollbackError = error;
2330
+ throw error;
2331
+ }
2305
2332
  const txClient = new TransactionClient(this.pinnedClient);
2306
2333
  try {
2307
2334
  const result = await callback(txClient);
@@ -2313,7 +2340,11 @@ var PinnedClientAdapter = class {
2313
2340
  try {
2314
2341
  await this.pinnedClient.query("ROLLBACK");
2315
2342
  } catch (rollbackError) {
2316
- console.error("Transaction rollback failed:", rollbackError);
2343
+ this.#rollbackError = rollbackError;
2344
+ try {
2345
+ if (this.onRollbackError) await this.onRollbackError(rollbackError);
2346
+ else console.error("Failed to rollback transaction:", rollbackError);
2347
+ } catch {}
2317
2348
  }
2318
2349
  throw error;
2319
2350
  }
@@ -28101,18 +28132,29 @@ var PgFactoryStorageOps = class {
28101
28132
  };
28102
28133
  if (this.#transactionClient) return run(this.#transactionClient);
28103
28134
  const client = await this.#queryable.connect();
28135
+ let releaseError;
28104
28136
  try {
28105
- await client.query("BEGIN");
28137
+ try {
28138
+ await client.query("BEGIN");
28139
+ } catch (error) {
28140
+ releaseError = error instanceof Error ? error : new Error(String(error));
28141
+ throw error;
28142
+ }
28106
28143
  try {
28107
28144
  const result = await run(client);
28108
28145
  await client.query("COMMIT");
28109
28146
  return result;
28110
28147
  } catch (error) {
28111
- await client.query("ROLLBACK");
28148
+ try {
28149
+ await client.query("ROLLBACK");
28150
+ } catch (rollbackError) {
28151
+ releaseError = rollbackError instanceof Error ? rollbackError : new Error(String(rollbackError));
28152
+ throw new AggregateError([error, rollbackError], "Factory transaction and rollback both failed");
28153
+ }
28112
28154
  throw error;
28113
28155
  }
28114
28156
  } finally {
28115
- client.release();
28157
+ client.release(releaseError);
28116
28158
  }
28117
28159
  }
28118
28160
  };
@@ -28169,7 +28211,12 @@ var PgFactoryStorage = class extends FactoryStorage {
28169
28211
  let transactionOpen = false;
28170
28212
  let releaseError;
28171
28213
  try {
28172
- await client.query(options?.isolationLevel === "serializable" ? "BEGIN ISOLATION LEVEL SERIALIZABLE" : "BEGIN");
28214
+ try {
28215
+ await client.query(options?.isolationLevel === "serializable" ? "BEGIN ISOLATION LEVEL SERIALIZABLE" : "BEGIN");
28216
+ } catch (error) {
28217
+ releaseError = error instanceof Error ? error : new Error(String(error));
28218
+ throw error;
28219
+ }
28173
28220
  transactionOpen = true;
28174
28221
  const result = await fn(new PgFactoryStorageOps(client, this.#schemas, client));
28175
28222
  await client.query("COMMIT");
@@ -28371,8 +28418,9 @@ var PostgresStore = class extends MastraCompositeStore {
28371
28418
  this.#ownsWritePool = true;
28372
28419
  }
28373
28420
  this.#readPool = config.readPool ?? this.#writePool;
28374
- this.#db = new RoutingDbClient(new PoolAdapter(this.#writePool));
28375
- this.#readDb = this.#readPool === this.#writePool ? this.#db : new PoolAdapter(this.#readPool);
28421
+ const onRollbackError = (error) => this.logger?.error?.("Transaction rollback failed", { error });
28422
+ this.#db = new RoutingDbClient(new PoolAdapter(this.#writePool, onRollbackError));
28423
+ this.#readDb = this.#readPool === this.#writePool ? this.#db : new PoolAdapter(this.#readPool, onRollbackError);
28376
28424
  const domainConfig = {
28377
28425
  client: this.#db,
28378
28426
  readClient: this.#readDb,
@@ -28441,7 +28489,7 @@ var PostgresStore = class extends MastraCompositeStore {
28441
28489
  let pinned;
28442
28490
  try {
28443
28491
  pinnedClient = await this.#writePool.connect();
28444
- pinned = new PinnedClientAdapter(this.#writePool, pinnedClient);
28492
+ pinned = new PinnedClientAdapter(this.#writePool, pinnedClient, (error) => this.logger?.error?.("Transaction rollback failed", { error }));
28445
28493
  this.#db.pin(pinned);
28446
28494
  for (let attempt = 1; attempt <= 3; attempt++) {
28447
28495
  this.#db.setSchemaSnapshot(await loadSchemaSnapshot(pinned, this.schema));
@@ -28466,7 +28514,7 @@ var PostgresStore = class extends MastraCompositeStore {
28466
28514
  this.#db.setSchemaSnapshot(null);
28467
28515
  if (pinnedClient) {
28468
28516
  this.#db.unpin();
28469
- pinnedClient.release();
28517
+ pinnedClient.release(pinned?.shouldDestroyClient || void 0);
28470
28518
  }
28471
28519
  }
28472
28520
  }