@mastra/pg 1.17.0 → 1.17.1-alpha.0

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
@@ -9,7 +9,7 @@ import xxhash from 'xxhash-wasm';
9
9
  import { parse } from 'pg-connection-string';
10
10
  import { BaseFilterTranslator } from '@mastra/core/vector/filter';
11
11
  import { MastraBase } from '@mastra/core/base';
12
- import { randomUUID, createHash } from 'crypto';
12
+ import { randomUUID } from 'crypto';
13
13
  import { MessageList } from '@mastra/core/agent';
14
14
  import { coreFeatures } from '@mastra/core/features';
15
15
  import { saveScorePayloadSchema } from '@mastra/core/evals';
@@ -20226,10 +20226,6 @@ function serializeDefault(value) {
20226
20226
  if (typeof value === "string") return `'${value.replace(/'/g, "''")}'`;
20227
20227
  return String(value);
20228
20228
  }
20229
- function hashAdvisoryLockKey(key) {
20230
- const digest = createHash("sha256").update(key).digest();
20231
- return [digest.readInt32BE(0), digest.readInt32BE(4)];
20232
- }
20233
20229
  var PgFactoryStorageOps = class {
20234
20230
  #queryable;
20235
20231
  #transactionClient;
@@ -20524,21 +20520,36 @@ var PgFactoryStorage = class extends FactoryStorage {
20524
20520
  async initStorage() {
20525
20521
  await this.#pool.query("SELECT 1");
20526
20522
  }
20527
- async withTransaction(fn) {
20528
- const client = await this.#pool.connect();
20529
- try {
20530
- await client.query("BEGIN");
20523
+ async withTransaction(fn, options) {
20524
+ const maxAttempts = options?.isolationLevel === "serializable" ? 3 : 1;
20525
+ for (let attempt = 1; attempt <= maxAttempts; attempt++) {
20526
+ const client = await this.#pool.connect();
20527
+ let transactionOpen = false;
20528
+ let releaseError;
20531
20529
  try {
20530
+ await client.query(options?.isolationLevel === "serializable" ? "BEGIN ISOLATION LEVEL SERIALIZABLE" : "BEGIN");
20531
+ transactionOpen = true;
20532
20532
  const result = await fn(new PgFactoryStorageOps(client, this.#schemas, client));
20533
20533
  await client.query("COMMIT");
20534
+ transactionOpen = false;
20534
20535
  return result;
20535
20536
  } catch (error) {
20536
- await client.query("ROLLBACK");
20537
- throw error;
20537
+ if (transactionOpen) {
20538
+ try {
20539
+ await client.query("ROLLBACK");
20540
+ transactionOpen = false;
20541
+ } catch (rollbackError) {
20542
+ releaseError = rollbackError instanceof Error ? rollbackError : new Error(String(rollbackError));
20543
+ throw new AggregateError([error, rollbackError], "Factory transaction and rollback both failed");
20544
+ }
20545
+ }
20546
+ const serializationFailure = typeof error === "object" && error !== null && error.code === "40001";
20547
+ if (!serializationFailure || attempt === maxAttempts) throw error;
20548
+ } finally {
20549
+ client.release(releaseError);
20538
20550
  }
20539
- } finally {
20540
- client.release();
20541
20551
  }
20552
+ throw new Error("PgFactoryStorage: serializable transaction retry limit exceeded");
20542
20553
  }
20543
20554
  async ensureCollections(schemas) {
20544
20555
  for (const schema of schemas) {
@@ -20556,30 +20567,6 @@ var PgFactoryStorage = class extends FactoryStorage {
20556
20567
  authDatabase() {
20557
20568
  return { dialect: "postgres", pool: this.#pool };
20558
20569
  }
20559
- /**
20560
- * Run `fn` while holding a Postgres transaction-scoped advisory lock for
20561
- * `key`, serializing callers across replicas. The lock releases when the
20562
- * transaction ends (commit, rollback, or connection loss), so a crashed
20563
- * replica can never hold it forever.
20564
- */
20565
- async withDistributedLock(key, fn) {
20566
- const [k1, k2] = hashAdvisoryLockKey(key);
20567
- const client = await this.#pool.connect();
20568
- try {
20569
- await client.query("BEGIN");
20570
- await client.query("SELECT pg_advisory_xact_lock($1, $2)", [k1, k2]);
20571
- try {
20572
- const result = await fn();
20573
- await client.query("COMMIT");
20574
- return result;
20575
- } catch (error) {
20576
- await client.query("ROLLBACK");
20577
- throw error;
20578
- }
20579
- } finally {
20580
- client.release();
20581
- }
20582
- }
20583
20570
  #columnDdl(name, spec) {
20584
20571
  assertIdentifier("column", name);
20585
20572
  let ddl = `"${name}" ${COLUMN_DDL[spec.type]}`;
@@ -21015,6 +21002,6 @@ Example Complex Query:
21015
21002
  ]
21016
21003
  }`;
21017
21004
 
21018
- export { AgentsPG, BackgroundTasksPG, BlobsPG, ChannelsPG, DatasetsPG, ExperimentsPG, FavoritesPG, MCPClientsPG, MCPServersPG, MemoryPG, NotificationsPG, ObservabilityPG, ObservabilityStoragePostgresVNext, PGVECTOR_PROMPT, PgFactoryStorage, PgVector, PoolAdapter, PostgresStore, PostgresStoreVNext, PromptBlocksPG, SchedulesPG, ScorerDefinitionsPG, ScoresPG, SkillsPG, ToolProviderConnectionsPG, WorkflowsPG, WorkspacesPG, exportSchemas, hashAdvisoryLockKey };
21005
+ export { AgentsPG, BackgroundTasksPG, BlobsPG, ChannelsPG, DatasetsPG, ExperimentsPG, FavoritesPG, MCPClientsPG, MCPServersPG, MemoryPG, NotificationsPG, ObservabilityPG, ObservabilityStoragePostgresVNext, PGVECTOR_PROMPT, PgFactoryStorage, PgVector, PoolAdapter, PostgresStore, PostgresStoreVNext, PromptBlocksPG, SchedulesPG, ScorerDefinitionsPG, ScoresPG, SkillsPG, ToolProviderConnectionsPG, WorkflowsPG, WorkspacesPG, exportSchemas };
21019
21006
  //# sourceMappingURL=index.js.map
21020
21007
  //# sourceMappingURL=index.js.map