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

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.
@@ -3,7 +3,7 @@ name: mastra-pg
3
3
  description: Documentation for @mastra/pg. Use when working with @mastra/pg APIs, configuration, or implementation.
4
4
  metadata:
5
5
  package: "@mastra/pg"
6
- version: "1.36.0-alpha.4"
6
+ version: "1.36.0-alpha.6"
7
7
  ---
8
8
 
9
9
  ## When to use
@@ -1,86 +1,86 @@
1
1
  {
2
- "version": "1.36.0-alpha.4",
2
+ "version": "1.36.0-alpha.6",
3
3
  "package": "@mastra/pg",
4
4
  "exports": {
5
5
  "AgentsPG": {
6
6
  "types": "dist/storage/domains/agents/index.d.ts",
7
7
  "implementation": "dist/index.js",
8
- "line": 4373
8
+ "line": 4404
9
9
  },
10
10
  "BackgroundTasksPG": {
11
11
  "types": "dist/storage/domains/background-tasks/index.d.ts",
12
12
  "implementation": "dist/index.js",
13
- "line": 5304
13
+ "line": 5335
14
14
  },
15
15
  "BlobsPG": {
16
16
  "types": "dist/storage/domains/blobs/index.d.ts",
17
17
  "implementation": "dist/index.js",
18
- "line": 5695
18
+ "line": 5726
19
19
  },
20
20
  "ChannelsPG": {
21
21
  "types": "dist/storage/domains/channels/index.d.ts",
22
22
  "implementation": "dist/index.js",
23
- "line": 5797
23
+ "line": 5828
24
24
  },
25
25
  "DatasetsPG": {
26
26
  "types": "dist/storage/domains/datasets/index.d.ts",
27
27
  "implementation": "dist/index.js",
28
- "line": 6039
28
+ "line": 6070
29
29
  },
30
30
  "ExperimentsPG": {
31
31
  "types": "dist/storage/domains/experiments/index.d.ts",
32
32
  "implementation": "dist/index.js",
33
- "line": 7296
33
+ "line": 7327
34
34
  },
35
35
  "FavoritesPG": {
36
36
  "types": "dist/storage/domains/favorites/index.d.ts",
37
37
  "implementation": "dist/index.js",
38
- "line": 8268
38
+ "line": 8299
39
39
  },
40
40
  "KnowledgePG": {
41
41
  "types": "dist/storage/domains/knowledge/index.d.ts",
42
42
  "implementation": "dist/index.js",
43
- "line": 10146
43
+ "line": 10177
44
44
  },
45
45
  "MCPClientsPG": {
46
46
  "types": "dist/storage/domains/mcp-clients/index.d.ts",
47
47
  "implementation": "dist/index.js",
48
- "line": 10303
48
+ "line": 10334
49
49
  },
50
50
  "MCPServersPG": {
51
51
  "types": "dist/storage/domains/mcp-servers/index.d.ts",
52
52
  "implementation": "dist/index.js",
53
- "line": 10872
53
+ "line": 10903
54
54
  },
55
55
  "MemoryPG": {
56
56
  "types": "dist/storage/domains/memory/index.d.ts",
57
57
  "implementation": "dist/index.js",
58
- "line": 11667
58
+ "line": 11698
59
59
  },
60
60
  "NotificationsPG": {
61
61
  "types": "dist/storage/domains/notifications/index.d.ts",
62
62
  "implementation": "dist/index.js",
63
- "line": 14574
63
+ "line": 14605
64
64
  },
65
65
  "ObservabilityPG": {
66
66
  "types": "dist/storage/domains/observability/index.d.ts",
67
67
  "implementation": "dist/index.js",
68
- "line": 14996
68
+ "line": 15027
69
69
  },
70
70
  "ObservabilityStoragePostgresVNext": {
71
71
  "types": "dist/storage/domains/observability/v-next/index.d.ts",
72
72
  "implementation": "dist/index.js",
73
- "line": 21954
73
+ "line": 21993
74
74
  },
75
75
  "PGVECTOR_PROMPT": {
76
76
  "types": "dist/vector/prompt.d.ts",
77
77
  "implementation": "dist/index.js",
78
- "line": 28641
78
+ "line": 28697
79
79
  },
80
80
  "PgFactoryStorage": {
81
81
  "types": "dist/storage/factory-storage.d.ts",
82
82
  "implementation": "dist/index.js",
83
- "line": 28127
83
+ "line": 28177
84
84
  },
85
85
  "PgVector": {
86
86
  "types": "dist/vector/index.d.ts",
@@ -95,67 +95,67 @@
95
95
  "PostgresStore": {
96
96
  "types": "dist/storage/index.d.ts",
97
97
  "implementation": "dist/index.js",
98
- "line": 28342
98
+ "line": 28397
99
99
  },
100
100
  "PostgresStoreVNext": {
101
101
  "types": "dist/storage/index.d.ts",
102
102
  "implementation": "dist/index.js",
103
- "line": 28560
103
+ "line": 28616
104
104
  },
105
105
  "PromptBlocksPG": {
106
106
  "types": "dist/storage/domains/prompt-blocks/index.d.ts",
107
107
  "implementation": "dist/index.js",
108
- "line": 22404
108
+ "line": 22443
109
109
  },
110
110
  "SchedulesPG": {
111
111
  "types": "dist/storage/domains/schedules/index.d.ts",
112
112
  "implementation": "dist/index.js",
113
- "line": 23033
113
+ "line": 23072
114
114
  },
115
115
  "ScorerDefinitionsPG": {
116
116
  "types": "dist/storage/domains/scorer-definitions/index.d.ts",
117
117
  "implementation": "dist/index.js",
118
- "line": 23363
118
+ "line": 23402
119
119
  },
120
120
  "ScoresPG": {
121
121
  "types": "dist/storage/domains/scores/index.d.ts",
122
122
  "implementation": "dist/index.js",
123
- "line": 23986
123
+ "line": 24025
124
124
  },
125
125
  "SkillsPG": {
126
126
  "types": "dist/storage/domains/skills/index.d.ts",
127
127
  "implementation": "dist/index.js",
128
- "line": 24429
128
+ "line": 24468
129
129
  },
130
130
  "ThreadStatePG": {
131
131
  "types": "dist/storage/domains/thread-state/index.d.ts",
132
132
  "implementation": "dist/index.js",
133
- "line": 25120
133
+ "line": 25159
134
134
  },
135
135
  "ToolProviderConnectionsPG": {
136
136
  "types": "dist/storage/domains/tool-provider-connections/index.d.ts",
137
137
  "implementation": "dist/index.js",
138
- "line": 25294
138
+ "line": 25333
139
139
  },
140
140
  "WorkflowDefinitionsPG": {
141
141
  "types": "dist/storage/domains/workflow-definitions/index.d.ts",
142
142
  "implementation": "dist/index.js",
143
- "line": 25558
143
+ "line": 25597
144
144
  },
145
145
  "WorkflowsPG": {
146
146
  "types": "dist/storage/domains/workflows/index.d.ts",
147
147
  "implementation": "dist/index.js",
148
- "line": 25941
148
+ "line": 25980
149
149
  },
150
150
  "WorkspacesPG": {
151
151
  "types": "dist/storage/domains/workspaces/index.d.ts",
152
152
  "implementation": "dist/index.js",
153
- "line": 27227
153
+ "line": 27266
154
154
  },
155
155
  "exportSchemas": {
156
156
  "types": "dist/storage/index.d.ts",
157
157
  "implementation": "dist/index.js",
158
- "line": 28309
158
+ "line": 28364
159
159
  }
160
160
  },
161
161
  "modules": {}
package/dist/index.cjs CHANGED
@@ -2089,8 +2089,10 @@ function truncateQuery(query, maxLength = 100) {
2089
2089
  */
2090
2090
  var PoolAdapter = class {
2091
2091
  $pool;
2092
- constructor($pool) {
2092
+ onRollbackError;
2093
+ constructor($pool, onRollbackError) {
2093
2094
  this.$pool = $pool;
2095
+ this.onRollbackError = onRollbackError;
2094
2096
  }
2095
2097
  connect() {
2096
2098
  return this.$pool.connect();
@@ -2127,8 +2129,14 @@ var PoolAdapter = class {
2127
2129
  }
2128
2130
  async tx(callback) {
2129
2131
  const client = await this.$pool.connect();
2132
+ let destroyClient = false;
2130
2133
  try {
2131
- await client.query("BEGIN");
2134
+ try {
2135
+ await client.query("BEGIN");
2136
+ } catch (error) {
2137
+ destroyClient = true;
2138
+ throw error;
2139
+ }
2132
2140
  const txClient = new TransactionClient(client);
2133
2141
  try {
2134
2142
  const result = await callback(txClient);
@@ -2140,12 +2148,16 @@ var PoolAdapter = class {
2140
2148
  try {
2141
2149
  await client.query("ROLLBACK");
2142
2150
  } catch (rollbackError) {
2143
- console.error("Transaction rollback failed:", rollbackError);
2151
+ destroyClient = true;
2152
+ try {
2153
+ if (this.onRollbackError) await this.onRollbackError(rollbackError);
2154
+ else console.error("Failed to rollback transaction:", rollbackError);
2155
+ } catch {}
2144
2156
  }
2145
2157
  throw error;
2146
2158
  }
2147
2159
  } finally {
2148
- client.release();
2160
+ client.release(destroyClient || void 0);
2149
2161
  }
2150
2162
  }
2151
2163
  };
@@ -2252,6 +2264,7 @@ var TransactionClient = class {
2252
2264
  var PinnedClientAdapter = class {
2253
2265
  $pool;
2254
2266
  pinnedClient;
2267
+ onRollbackError;
2255
2268
  /**
2256
2269
  * Serialization tail. Domain init() methods fire via Promise.all in
2257
2270
  * MastraCompositeStore.#runInit(), so without our own gate every domain's
@@ -2262,9 +2275,14 @@ var PinnedClientAdapter = class {
2262
2275
  * the reliance on pg's internal queue.
2263
2276
  */
2264
2277
  #tail = Promise.resolve();
2265
- constructor($pool, pinnedClient) {
2278
+ #rollbackError;
2279
+ constructor($pool, pinnedClient, onRollbackError) {
2266
2280
  this.$pool = $pool;
2267
2281
  this.pinnedClient = pinnedClient;
2282
+ this.onRollbackError = onRollbackError;
2283
+ }
2284
+ get shouldDestroyClient() {
2285
+ return this.#rollbackError !== void 0;
2268
2286
  }
2269
2287
  /**
2270
2288
  * Run `fn` after any previously enqueued work on this pinned client has
@@ -2272,7 +2290,11 @@ var PinnedClientAdapter = class {
2272
2290
  * resume on the next caller's turn.
2273
2291
  */
2274
2292
  #enqueue(fn) {
2275
- const next = this.#tail.then(fn, fn);
2293
+ const run = async () => {
2294
+ if (this.shouldDestroyClient) throw this.#rollbackError;
2295
+ return fn();
2296
+ };
2297
+ const next = this.#tail.then(run, run);
2276
2298
  this.#tail = next.catch(() => void 0);
2277
2299
  return next;
2278
2300
  }
@@ -2324,7 +2346,12 @@ var PinnedClientAdapter = class {
2324
2346
  }
2325
2347
  tx(callback) {
2326
2348
  return this.#enqueue(async () => {
2327
- await this.pinnedClient.query("BEGIN");
2349
+ try {
2350
+ await this.pinnedClient.query("BEGIN");
2351
+ } catch (error) {
2352
+ this.#rollbackError = error;
2353
+ throw error;
2354
+ }
2328
2355
  const txClient = new TransactionClient(this.pinnedClient);
2329
2356
  try {
2330
2357
  const result = await callback(txClient);
@@ -2336,7 +2363,11 @@ var PinnedClientAdapter = class {
2336
2363
  try {
2337
2364
  await this.pinnedClient.query("ROLLBACK");
2338
2365
  } catch (rollbackError) {
2339
- console.error("Transaction rollback failed:", rollbackError);
2366
+ this.#rollbackError = rollbackError;
2367
+ try {
2368
+ if (this.onRollbackError) await this.onRollbackError(rollbackError);
2369
+ else console.error("Failed to rollback transaction:", rollbackError);
2370
+ } catch {}
2340
2371
  }
2341
2372
  throw error;
2342
2373
  }
@@ -19391,13 +19422,13 @@ const timingFeatures = [
19391
19422
  * condition for rows level with it. Empty values sort last in both directions, so they are past
19392
19423
  * every value and level only with each other.
19393
19424
  */
19394
- function keysetSortBounds(field, direction, sortValue, values) {
19425
+ function keysetSortBounds(field, direction, sortValue, values, stringSort = false) {
19395
19426
  if (sortValue === null) return {
19396
19427
  past: [],
19397
19428
  level: `${field} IS NULL`
19398
19429
  };
19399
19430
  values.push(sortValue);
19400
- const parameter = `$${values.length}::${typeof sortValue === "number" ? "float8" : "timestamptz"}`;
19431
+ const parameter = `$${values.length}::${stringSort ? "text COLLATE \"C\"" : typeof sortValue === "number" ? "float8" : "timestamptz"}`;
19401
19432
  return {
19402
19433
  past: [`${field} ${direction === "asc" ? ">" : "<"} ${parameter}`, `${field} IS NULL`],
19403
19434
  level: `${field} = ${parameter}`
@@ -20445,10 +20476,14 @@ function selectedKeys(rows, values) {
20445
20476
  return `"${field}" ${field === "traceId" || field === "spanId" ? "=" : "IS NOT DISTINCT FROM"} $${values.length}::text`;
20446
20477
  }).join(" AND ")})`).join(" OR ");
20447
20478
  }
20479
+ function sortColumn(plan) {
20480
+ const column = `s."${plan.orderBy.field}"`;
20481
+ return plan.orderBy.field === "model" || plan.orderBy.field === "provider" ? `${column} COLLATE "C"` : column;
20482
+ }
20448
20483
  function afterCursor(plan, values) {
20449
20484
  if (!plan.cursor) return "TRUE";
20450
20485
  const cursor = plan.cursor;
20451
- const { past: terms, level } = keysetSortBounds(`s."${plan.orderBy.field}"`, plan.orderBy.direction, cursor.sortValue, values);
20486
+ const { past: terms, level } = keysetSortBounds(sortColumn(plan), plan.orderBy.direction, cursor.sortValue, values, plan.orderBy.field === "model" || plan.orderBy.field === "provider");
20452
20487
  const equal = [level];
20453
20488
  for (const key of identityFields) {
20454
20489
  const ref = `s."${key}" COLLATE "C"`;
@@ -20529,10 +20564,12 @@ function compilePostgresSpanQuery(schema, plan) {
20529
20564
  "endedAt",
20530
20565
  "durationMs",
20531
20566
  "status",
20532
- "version"
20567
+ "version",
20568
+ "model",
20569
+ "provider"
20533
20570
  ].map((field) => `s."${field}"`).join(", ")}
20534
20571
  FROM selected s WHERE ${after}
20535
- ORDER BY s."${plan.orderBy.field}" ${plan.orderBy.direction === "asc" ? "ASC" : "DESC"} NULLS LAST, ${identityOrder}
20572
+ ORDER BY ${sortColumn(plan)} ${plan.orderBy.direction === "asc" ? "ASC" : "DESC"} NULLS LAST, ${identityOrder}
20536
20573
  LIMIT $${values.length}`,
20537
20574
  values
20538
20575
  };
@@ -20567,6 +20604,7 @@ async function querySpans(client, schema, plan, timeoutMs) {
20567
20604
  const rows = await tx.any(query.text, query.values);
20568
20605
  const selected = rows.map(({ version: _version, startedAt, endedAt, timeToFirstChunkMs, timeToFirstTokenMs, outcome, stopReason, ...row }) => ({
20569
20606
  ...row,
20607
+ ...(plan.orderBy.field === "model" || plan.orderBy.field === "provider") && { cursorSortValue: row[plan.orderBy.field] },
20570
20608
  ...timeToFirstChunkSupported && { timeToFirstChunkMs },
20571
20609
  ...timeToFirstTokenSupported && { timeToFirstTokenMs },
20572
20610
  ...spanQueryOutcomeFieldsSupported && {
@@ -21962,6 +22000,7 @@ const spanQueryFeatures = typeof _mastra_core_storage.planSpanQuery === "functio
21962
22000
  "span-query",
21963
22001
  "span-query-context-fields",
21964
22002
  ..._mastra_core_storage.SPAN_QUERY_ORDER_FIELDS?.includes("durationMs") ? ["span-query-duration-sort"] : [],
22003
+ ..._mastra_core_storage.SPAN_QUERY_ORDER_FIELDS?.includes("model") && _mastra_core_storage.SPAN_QUERY_ORDER_FIELDS?.includes("provider") ? ["span-query-model-provider-sort"] : [],
21965
22004
  ..."model" in (_mastra_core_storage.SPAN_QUERY_FIELD_REGISTRY ?? {}) ? ["span-query-model-fields"] : []
21966
22005
  ] : [];
21967
22006
  const spanAggregateFeatures = typeof _mastra_core_storage.planSpanAggregate === "function" ? ["span-aggregate", ..."model" in (_mastra_core_storage.SPAN_AGGREGATE_DIMENSION_REGISTRY ?? {}) ? ["span-aggregate-model-dimensions"] : []] : [];
@@ -28124,18 +28163,29 @@ var PgFactoryStorageOps = class {
28124
28163
  };
28125
28164
  if (this.#transactionClient) return run(this.#transactionClient);
28126
28165
  const client = await this.#queryable.connect();
28166
+ let releaseError;
28127
28167
  try {
28128
- await client.query("BEGIN");
28168
+ try {
28169
+ await client.query("BEGIN");
28170
+ } catch (error) {
28171
+ releaseError = error instanceof Error ? error : new Error(String(error));
28172
+ throw error;
28173
+ }
28129
28174
  try {
28130
28175
  const result = await run(client);
28131
28176
  await client.query("COMMIT");
28132
28177
  return result;
28133
28178
  } catch (error) {
28134
- await client.query("ROLLBACK");
28179
+ try {
28180
+ await client.query("ROLLBACK");
28181
+ } catch (rollbackError) {
28182
+ releaseError = rollbackError instanceof Error ? rollbackError : new Error(String(rollbackError));
28183
+ throw new AggregateError([error, rollbackError], "Factory transaction and rollback both failed");
28184
+ }
28135
28185
  throw error;
28136
28186
  }
28137
28187
  } finally {
28138
- client.release();
28188
+ client.release(releaseError);
28139
28189
  }
28140
28190
  }
28141
28191
  };
@@ -28192,7 +28242,12 @@ var PgFactoryStorage = class extends _mastra_core_storage.FactoryStorage {
28192
28242
  let transactionOpen = false;
28193
28243
  let releaseError;
28194
28244
  try {
28195
- await client.query(options?.isolationLevel === "serializable" ? "BEGIN ISOLATION LEVEL SERIALIZABLE" : "BEGIN");
28245
+ try {
28246
+ await client.query(options?.isolationLevel === "serializable" ? "BEGIN ISOLATION LEVEL SERIALIZABLE" : "BEGIN");
28247
+ } catch (error) {
28248
+ releaseError = error instanceof Error ? error : new Error(String(error));
28249
+ throw error;
28250
+ }
28196
28251
  transactionOpen = true;
28197
28252
  const result = await fn(new PgFactoryStorageOps(client, this.#schemas, client));
28198
28253
  await client.query("COMMIT");
@@ -28394,8 +28449,9 @@ var PostgresStore = class extends _mastra_core_storage.MastraCompositeStore {
28394
28449
  this.#ownsWritePool = true;
28395
28450
  }
28396
28451
  this.#readPool = config.readPool ?? this.#writePool;
28397
- this.#db = new RoutingDbClient(new PoolAdapter(this.#writePool));
28398
- this.#readDb = this.#readPool === this.#writePool ? this.#db : new PoolAdapter(this.#readPool);
28452
+ const onRollbackError = (error) => this.logger?.error?.("Transaction rollback failed", { error });
28453
+ this.#db = new RoutingDbClient(new PoolAdapter(this.#writePool, onRollbackError));
28454
+ this.#readDb = this.#readPool === this.#writePool ? this.#db : new PoolAdapter(this.#readPool, onRollbackError);
28399
28455
  const domainConfig = {
28400
28456
  client: this.#db,
28401
28457
  readClient: this.#readDb,
@@ -28464,7 +28520,7 @@ var PostgresStore = class extends _mastra_core_storage.MastraCompositeStore {
28464
28520
  let pinned;
28465
28521
  try {
28466
28522
  pinnedClient = await this.#writePool.connect();
28467
- pinned = new PinnedClientAdapter(this.#writePool, pinnedClient);
28523
+ pinned = new PinnedClientAdapter(this.#writePool, pinnedClient, (error) => this.logger?.error?.("Transaction rollback failed", { error }));
28468
28524
  this.#db.pin(pinned);
28469
28525
  for (let attempt = 1; attempt <= 3; attempt++) {
28470
28526
  this.#db.setSchemaSnapshot(await loadSchemaSnapshot(pinned, this.schema));
@@ -28489,7 +28545,7 @@ var PostgresStore = class extends _mastra_core_storage.MastraCompositeStore {
28489
28545
  this.#db.setSchemaSnapshot(null);
28490
28546
  if (pinnedClient) {
28491
28547
  this.#db.unpin();
28492
- pinnedClient.release();
28548
+ pinnedClient.release(pinned?.shouldDestroyClient || void 0);
28493
28549
  }
28494
28550
  }
28495
28551
  }