@mastra/pg 0.3.1-alpha.2 → 0.3.1-alpha.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.
@@ -1,23 +1,23 @@
1
1
 
2
- > @mastra/pg@0.3.1-alpha.2 build /home/runner/work/mastra/mastra/stores/pg
2
+ > @mastra/pg@0.3.1-alpha.4 build /home/runner/work/mastra/mastra/stores/pg
3
3
  > tsup src/index.ts --format esm,cjs --experimental-dts --clean --treeshake=smallest --splitting
4
4
 
5
5
  CLI Building entry: src/index.ts
6
6
  CLI Using tsconfig: tsconfig.json
7
7
  CLI tsup v8.4.0
8
8
  TSC Build start
9
- TSC ⚡️ Build success in 9212ms
9
+ TSC ⚡️ Build success in 11471ms
10
10
  DTS Build start
11
11
  CLI Target: es2022
12
- Analysis will use the bundled TypeScript version 5.8.2
12
+ Analysis will use the bundled TypeScript version 5.8.3
13
13
  Writing package typings: /home/runner/work/mastra/mastra/stores/pg/dist/_tsup-dts-rollup.d.ts
14
- Analysis will use the bundled TypeScript version 5.8.2
14
+ Analysis will use the bundled TypeScript version 5.8.3
15
15
  Writing package typings: /home/runner/work/mastra/mastra/stores/pg/dist/_tsup-dts-rollup.d.cts
16
- DTS ⚡️ Build success in 11600ms
16
+ DTS ⚡️ Build success in 12539ms
17
17
  CLI Cleaning output folder
18
18
  ESM Build start
19
19
  CJS Build start
20
- CJS dist/index.cjs 48.63 KB
21
- CJS ⚡️ Build success in 1328ms
22
- ESM dist/index.js 48.21 KB
23
- ESM ⚡️ Build success in 1328ms
20
+ ESM dist/index.js 50.33 KB
21
+ ESM ⚡️ Build success in 1488ms
22
+ CJS dist/index.cjs 50.78 KB
23
+ CJS ⚡️ Build success in 1489ms
package/CHANGELOG.md CHANGED
@@ -1,5 +1,22 @@
1
1
  # @mastra/pg
2
2
 
3
+ ## 0.3.1-alpha.4
4
+
5
+ ### Patch Changes
6
+
7
+ - 479f490: [MASTRA-3131] Add getWorkflowRunByID and add resourceId as filter for getWorkflowRuns
8
+ - Updated dependencies [e4943b8]
9
+ - Updated dependencies [479f490]
10
+ - @mastra/core@0.9.1-alpha.4
11
+
12
+ ## 0.3.1-alpha.3
13
+
14
+ ### Patch Changes
15
+
16
+ - 6a76949: add PgVector pgPoolOptions constructor parameter
17
+ - Updated dependencies [6262bd5]
18
+ - @mastra/core@0.9.1-alpha.3
19
+
3
20
  ## 0.3.1-alpha.2
4
21
 
5
22
  ### Patch Changes
@@ -14,6 +14,7 @@ import type { MessageType } from '@mastra/core/memory';
14
14
  import type { NumericOperator } from '@mastra/core/vector/filter';
15
15
  import type { OperatorSupport } from '@mastra/core/vector/filter';
16
16
  import type { ParamsToArgs } from '@mastra/core/vector';
17
+ import pg from 'pg';
17
18
  import type { QueryResult } from '@mastra/core/vector';
18
19
  import type { QueryVectorArgs } from '@mastra/core/vector';
19
20
  import type { QueryVectorParams } from '@mastra/core/vector';
@@ -24,6 +25,8 @@ import type { StorageThreadType } from '@mastra/core/memory';
24
25
  import type { TABLE_NAMES } from '@mastra/core/storage';
25
26
  import type { UpsertVectorParams } from '@mastra/core/vector';
26
27
  import type { VectorFilter } from '@mastra/core/vector/filter';
28
+ import type { WorkflowRun } from '@mastra/core/storage';
29
+ import type { WorkflowRuns } from '@mastra/core/storage';
27
30
  import type { WorkflowRunState } from '@mastra/core/workflows';
28
31
 
29
32
  export declare const baseTestConfigs: {
@@ -212,6 +215,7 @@ declare class PgVector extends MastraVector {
212
215
  constructor(config: {
213
216
  connectionString: string;
214
217
  schemaName?: string;
218
+ pgPoolOptions?: Omit<pg.PoolConfig, 'connectionString'>;
215
219
  });
216
220
  private getMutexByName;
217
221
  private getTableName;
@@ -319,7 +323,7 @@ declare class PostgresStore extends MastraStorage {
319
323
  deleteThread({ threadId }: {
320
324
  threadId: string;
321
325
  }): Promise<void>;
322
- getMessages<T = unknown>({ threadId, selectBy }: StorageGetMessagesArg): Promise<T>;
326
+ getMessages<T = unknown>({ threadId, selectBy }: StorageGetMessagesArg): Promise<T[]>;
323
327
  saveMessages({ messages }: {
324
328
  messages: MessageType[];
325
329
  }): Promise<MessageType[]>;
@@ -332,22 +336,20 @@ declare class PostgresStore extends MastraStorage {
332
336
  workflowName: string;
333
337
  runId: string;
334
338
  }): Promise<WorkflowRunState | null>;
335
- getWorkflowRuns({ workflowName, fromDate, toDate, limit, offset, }?: {
339
+ private hasColumn;
340
+ private parseWorkflowRun;
341
+ getWorkflowRuns({ workflowName, fromDate, toDate, limit, offset, resourceId, }?: {
336
342
  workflowName?: string;
337
343
  fromDate?: Date;
338
344
  toDate?: Date;
339
345
  limit?: number;
340
346
  offset?: number;
341
- }): Promise<{
342
- runs: Array<{
343
- workflowName: string;
344
- runId: string;
345
- snapshot: WorkflowRunState | string;
346
- createdAt: Date;
347
- updatedAt: Date;
348
- }>;
349
- total: number;
350
- }>;
347
+ resourceId?: string;
348
+ }): Promise<WorkflowRuns>;
349
+ getWorkflowRunById({ runId, workflowName, }: {
350
+ runId: string;
351
+ workflowName?: string;
352
+ }): Promise<WorkflowRun | null>;
351
353
  close(): Promise<void>;
352
354
  }
353
355
  export { PostgresStore }
@@ -14,6 +14,7 @@ import type { MessageType } from '@mastra/core/memory';
14
14
  import type { NumericOperator } from '@mastra/core/vector/filter';
15
15
  import type { OperatorSupport } from '@mastra/core/vector/filter';
16
16
  import type { ParamsToArgs } from '@mastra/core/vector';
17
+ import pg from 'pg';
17
18
  import type { QueryResult } from '@mastra/core/vector';
18
19
  import type { QueryVectorArgs } from '@mastra/core/vector';
19
20
  import type { QueryVectorParams } from '@mastra/core/vector';
@@ -24,6 +25,8 @@ import type { StorageThreadType } from '@mastra/core/memory';
24
25
  import type { TABLE_NAMES } from '@mastra/core/storage';
25
26
  import type { UpsertVectorParams } from '@mastra/core/vector';
26
27
  import type { VectorFilter } from '@mastra/core/vector/filter';
28
+ import type { WorkflowRun } from '@mastra/core/storage';
29
+ import type { WorkflowRuns } from '@mastra/core/storage';
27
30
  import type { WorkflowRunState } from '@mastra/core/workflows';
28
31
 
29
32
  export declare const baseTestConfigs: {
@@ -212,6 +215,7 @@ declare class PgVector extends MastraVector {
212
215
  constructor(config: {
213
216
  connectionString: string;
214
217
  schemaName?: string;
218
+ pgPoolOptions?: Omit<pg.PoolConfig, 'connectionString'>;
215
219
  });
216
220
  private getMutexByName;
217
221
  private getTableName;
@@ -319,7 +323,7 @@ declare class PostgresStore extends MastraStorage {
319
323
  deleteThread({ threadId }: {
320
324
  threadId: string;
321
325
  }): Promise<void>;
322
- getMessages<T = unknown>({ threadId, selectBy }: StorageGetMessagesArg): Promise<T>;
326
+ getMessages<T = unknown>({ threadId, selectBy }: StorageGetMessagesArg): Promise<T[]>;
323
327
  saveMessages({ messages }: {
324
328
  messages: MessageType[];
325
329
  }): Promise<MessageType[]>;
@@ -332,22 +336,20 @@ declare class PostgresStore extends MastraStorage {
332
336
  workflowName: string;
333
337
  runId: string;
334
338
  }): Promise<WorkflowRunState | null>;
335
- getWorkflowRuns({ workflowName, fromDate, toDate, limit, offset, }?: {
339
+ private hasColumn;
340
+ private parseWorkflowRun;
341
+ getWorkflowRuns({ workflowName, fromDate, toDate, limit, offset, resourceId, }?: {
336
342
  workflowName?: string;
337
343
  fromDate?: Date;
338
344
  toDate?: Date;
339
345
  limit?: number;
340
346
  offset?: number;
341
- }): Promise<{
342
- runs: Array<{
343
- workflowName: string;
344
- runId: string;
345
- snapshot: WorkflowRunState | string;
346
- createdAt: Date;
347
- updatedAt: Date;
348
- }>;
349
- total: number;
350
- }>;
347
+ resourceId?: string;
348
+ }): Promise<WorkflowRuns>;
349
+ getWorkflowRunById({ runId, workflowName, }: {
350
+ runId: string;
351
+ workflowName?: string;
352
+ }): Promise<WorkflowRun | null>;
351
353
  close(): Promise<void>;
352
354
  }
353
355
  export { PostgresStore }
package/dist/index.cjs CHANGED
@@ -342,22 +342,34 @@ var PgVector = class extends vector.MastraVector {
342
342
  vectorExtensionInstalled = void 0;
343
343
  schemaSetupComplete = void 0;
344
344
  constructor(config) {
345
- const connectionString = typeof config === "string" ? config : config.connectionString;
346
- if (!connectionString || typeof connectionString !== "string" || connectionString.trim() === "") {
345
+ let connectionString;
346
+ let pgPoolOptions;
347
+ let schemaName;
348
+ if (typeof config === "string") {
349
+ connectionString = config;
350
+ schemaName = void 0;
351
+ pgPoolOptions = void 0;
352
+ } else {
353
+ connectionString = config.connectionString;
354
+ schemaName = config.schemaName;
355
+ pgPoolOptions = config.pgPoolOptions;
356
+ }
357
+ if (!connectionString || connectionString.trim() === "") {
347
358
  throw new Error(
348
359
  "PgVector: connectionString must be provided and cannot be empty. Passing an empty string may cause fallback to local Postgres defaults."
349
360
  );
350
361
  }
351
362
  super();
352
- this.schema = typeof config === "string" ? void 0 : config.schemaName;
363
+ this.schema = schemaName;
353
364
  const basePool = new pg__default.default.Pool({
354
365
  connectionString,
355
366
  max: 20,
356
367
  // Maximum number of clients in the pool
357
368
  idleTimeoutMillis: 3e4,
358
369
  // Close idle connections after 30 seconds
359
- connectionTimeoutMillis: 2e3
370
+ connectionTimeoutMillis: 2e3,
360
371
  // Fail fast if can't connect
372
+ ...pgPoolOptions
361
373
  });
362
374
  const telemetry = this.__getTelemetry();
363
375
  this.pool = telemetry?.traceClass(basePool, {
@@ -1404,66 +1416,128 @@ var PostgresStore = class extends storage.MastraStorage {
1404
1416
  throw error;
1405
1417
  }
1406
1418
  }
1419
+ async hasColumn(table, column) {
1420
+ const schema = this.schema || "public";
1421
+ const result = await this.db.oneOrNone(
1422
+ `SELECT 1 FROM information_schema.columns WHERE table_schema = $1 AND table_name = $2 AND (column_name = $3 OR column_name = $4)`,
1423
+ [schema, table, column, column.toLowerCase()]
1424
+ );
1425
+ return !!result;
1426
+ }
1427
+ parseWorkflowRun(row) {
1428
+ let parsedSnapshot = row.snapshot;
1429
+ if (typeof parsedSnapshot === "string") {
1430
+ try {
1431
+ parsedSnapshot = JSON.parse(row.snapshot);
1432
+ } catch (e) {
1433
+ console.warn(`Failed to parse snapshot for workflow ${row.workflow_name}: ${e}`);
1434
+ }
1435
+ }
1436
+ return {
1437
+ workflowName: row.workflow_name,
1438
+ runId: row.run_id,
1439
+ snapshot: parsedSnapshot,
1440
+ createdAt: row.createdAt,
1441
+ updatedAt: row.updatedAt,
1442
+ resourceId: row.resourceId
1443
+ };
1444
+ }
1407
1445
  async getWorkflowRuns({
1408
1446
  workflowName,
1409
1447
  fromDate,
1410
1448
  toDate,
1411
1449
  limit,
1412
- offset
1450
+ offset,
1451
+ resourceId
1413
1452
  } = {}) {
1414
- const conditions = [];
1415
- const values = [];
1416
- let paramIndex = 1;
1417
- if (workflowName) {
1418
- conditions.push(`workflow_name = $${paramIndex}`);
1419
- values.push(workflowName);
1420
- paramIndex++;
1421
- }
1422
- if (fromDate) {
1423
- conditions.push(`"createdAt" >= $${paramIndex}`);
1424
- values.push(fromDate);
1425
- paramIndex++;
1426
- }
1427
- if (toDate) {
1428
- conditions.push(`"createdAt" <= $${paramIndex}`);
1429
- values.push(toDate);
1430
- paramIndex++;
1431
- }
1432
- const whereClause = conditions.length > 0 ? `WHERE ${conditions.join(" AND ")}` : "";
1433
- let total = 0;
1434
- if (limit !== void 0 && offset !== void 0) {
1435
- const countResult = await this.db.one(
1436
- `SELECT COUNT(*) as count FROM ${this.getTableName(storage.TABLE_WORKFLOW_SNAPSHOT)} ${whereClause}`,
1437
- values
1438
- );
1439
- total = Number(countResult.count);
1440
- }
1441
- const query = `
1453
+ try {
1454
+ const conditions = [];
1455
+ const values = [];
1456
+ let paramIndex = 1;
1457
+ if (workflowName) {
1458
+ conditions.push(`workflow_name = $${paramIndex}`);
1459
+ values.push(workflowName);
1460
+ paramIndex++;
1461
+ }
1462
+ if (resourceId) {
1463
+ const hasResourceId = await this.hasColumn(storage.TABLE_WORKFLOW_SNAPSHOT, "resourceId");
1464
+ if (hasResourceId) {
1465
+ conditions.push(`"resourceId" = $${paramIndex}`);
1466
+ values.push(resourceId);
1467
+ paramIndex++;
1468
+ } else {
1469
+ console.warn(`[${storage.TABLE_WORKFLOW_SNAPSHOT}] resourceId column not found. Skipping resourceId filter.`);
1470
+ }
1471
+ }
1472
+ if (fromDate) {
1473
+ conditions.push(`"createdAt" >= $${paramIndex}`);
1474
+ values.push(fromDate);
1475
+ paramIndex++;
1476
+ }
1477
+ if (toDate) {
1478
+ conditions.push(`"createdAt" <= $${paramIndex}`);
1479
+ values.push(toDate);
1480
+ paramIndex++;
1481
+ }
1482
+ const whereClause = conditions.length > 0 ? `WHERE ${conditions.join(" AND ")}` : "";
1483
+ let total = 0;
1484
+ if (limit !== void 0 && offset !== void 0) {
1485
+ const countResult = await this.db.one(
1486
+ `SELECT COUNT(*) as count FROM ${this.getTableName(storage.TABLE_WORKFLOW_SNAPSHOT)} ${whereClause}`,
1487
+ values
1488
+ );
1489
+ total = Number(countResult.count);
1490
+ }
1491
+ const query = `
1442
1492
  SELECT * FROM ${this.getTableName(storage.TABLE_WORKFLOW_SNAPSHOT)}
1443
1493
  ${whereClause}
1444
1494
  ORDER BY "createdAt" DESC
1445
1495
  ${limit !== void 0 && offset !== void 0 ? ` LIMIT $${paramIndex} OFFSET $${paramIndex + 1}` : ""}
1446
1496
  `;
1447
- const queryValues = limit !== void 0 && offset !== void 0 ? [...values, limit, offset] : values;
1448
- const result = await this.db.manyOrNone(query, queryValues);
1449
- const runs = (result || []).map((row) => {
1450
- let parsedSnapshot = row.snapshot;
1451
- if (typeof parsedSnapshot === "string") {
1452
- try {
1453
- parsedSnapshot = JSON.parse(row.snapshot);
1454
- } catch (e) {
1455
- console.warn(`Failed to parse snapshot for workflow ${row.workflow_name}: ${e}`);
1456
- }
1497
+ const queryValues = limit !== void 0 && offset !== void 0 ? [...values, limit, offset] : values;
1498
+ const result = await this.db.manyOrNone(query, queryValues);
1499
+ const runs = (result || []).map((row) => {
1500
+ return this.parseWorkflowRun(row);
1501
+ });
1502
+ return { runs, total: total || runs.length };
1503
+ } catch (error) {
1504
+ console.error("Error getting workflow runs:", error);
1505
+ throw error;
1506
+ }
1507
+ }
1508
+ async getWorkflowRunById({
1509
+ runId,
1510
+ workflowName
1511
+ }) {
1512
+ try {
1513
+ const conditions = [];
1514
+ const values = [];
1515
+ let paramIndex = 1;
1516
+ if (runId) {
1517
+ conditions.push(`run_id = $${paramIndex}`);
1518
+ values.push(runId);
1519
+ paramIndex++;
1457
1520
  }
1458
- return {
1459
- workflowName: row.workflow_name,
1460
- runId: row.run_id,
1461
- snapshot: parsedSnapshot,
1462
- createdAt: row.createdAt,
1463
- updatedAt: row.updatedAt
1464
- };
1465
- });
1466
- return { runs, total: total || runs.length };
1521
+ if (workflowName) {
1522
+ conditions.push(`workflow_name = $${paramIndex}`);
1523
+ values.push(workflowName);
1524
+ paramIndex++;
1525
+ }
1526
+ const whereClause = conditions.length > 0 ? `WHERE ${conditions.join(" AND ")}` : "";
1527
+ const query = `
1528
+ SELECT * FROM ${this.getTableName(storage.TABLE_WORKFLOW_SNAPSHOT)}
1529
+ ${whereClause}
1530
+ `;
1531
+ const queryValues = values;
1532
+ const result = await this.db.oneOrNone(query, queryValues);
1533
+ if (!result) {
1534
+ return null;
1535
+ }
1536
+ return this.parseWorkflowRun(result);
1537
+ } catch (error) {
1538
+ console.error("Error getting workflow run by ID:", error);
1539
+ throw error;
1540
+ }
1467
1541
  }
1468
1542
  async close() {
1469
1543
  this.pgp.end();
package/dist/index.js CHANGED
@@ -334,22 +334,34 @@ var PgVector = class extends MastraVector {
334
334
  vectorExtensionInstalled = void 0;
335
335
  schemaSetupComplete = void 0;
336
336
  constructor(config) {
337
- const connectionString = typeof config === "string" ? config : config.connectionString;
338
- if (!connectionString || typeof connectionString !== "string" || connectionString.trim() === "") {
337
+ let connectionString;
338
+ let pgPoolOptions;
339
+ let schemaName;
340
+ if (typeof config === "string") {
341
+ connectionString = config;
342
+ schemaName = void 0;
343
+ pgPoolOptions = void 0;
344
+ } else {
345
+ connectionString = config.connectionString;
346
+ schemaName = config.schemaName;
347
+ pgPoolOptions = config.pgPoolOptions;
348
+ }
349
+ if (!connectionString || connectionString.trim() === "") {
339
350
  throw new Error(
340
351
  "PgVector: connectionString must be provided and cannot be empty. Passing an empty string may cause fallback to local Postgres defaults."
341
352
  );
342
353
  }
343
354
  super();
344
- this.schema = typeof config === "string" ? void 0 : config.schemaName;
355
+ this.schema = schemaName;
345
356
  const basePool = new pg.Pool({
346
357
  connectionString,
347
358
  max: 20,
348
359
  // Maximum number of clients in the pool
349
360
  idleTimeoutMillis: 3e4,
350
361
  // Close idle connections after 30 seconds
351
- connectionTimeoutMillis: 2e3
362
+ connectionTimeoutMillis: 2e3,
352
363
  // Fail fast if can't connect
364
+ ...pgPoolOptions
353
365
  });
354
366
  const telemetry = this.__getTelemetry();
355
367
  this.pool = telemetry?.traceClass(basePool, {
@@ -1396,66 +1408,128 @@ var PostgresStore = class extends MastraStorage {
1396
1408
  throw error;
1397
1409
  }
1398
1410
  }
1411
+ async hasColumn(table, column) {
1412
+ const schema = this.schema || "public";
1413
+ const result = await this.db.oneOrNone(
1414
+ `SELECT 1 FROM information_schema.columns WHERE table_schema = $1 AND table_name = $2 AND (column_name = $3 OR column_name = $4)`,
1415
+ [schema, table, column, column.toLowerCase()]
1416
+ );
1417
+ return !!result;
1418
+ }
1419
+ parseWorkflowRun(row) {
1420
+ let parsedSnapshot = row.snapshot;
1421
+ if (typeof parsedSnapshot === "string") {
1422
+ try {
1423
+ parsedSnapshot = JSON.parse(row.snapshot);
1424
+ } catch (e) {
1425
+ console.warn(`Failed to parse snapshot for workflow ${row.workflow_name}: ${e}`);
1426
+ }
1427
+ }
1428
+ return {
1429
+ workflowName: row.workflow_name,
1430
+ runId: row.run_id,
1431
+ snapshot: parsedSnapshot,
1432
+ createdAt: row.createdAt,
1433
+ updatedAt: row.updatedAt,
1434
+ resourceId: row.resourceId
1435
+ };
1436
+ }
1399
1437
  async getWorkflowRuns({
1400
1438
  workflowName,
1401
1439
  fromDate,
1402
1440
  toDate,
1403
1441
  limit,
1404
- offset
1442
+ offset,
1443
+ resourceId
1405
1444
  } = {}) {
1406
- const conditions = [];
1407
- const values = [];
1408
- let paramIndex = 1;
1409
- if (workflowName) {
1410
- conditions.push(`workflow_name = $${paramIndex}`);
1411
- values.push(workflowName);
1412
- paramIndex++;
1413
- }
1414
- if (fromDate) {
1415
- conditions.push(`"createdAt" >= $${paramIndex}`);
1416
- values.push(fromDate);
1417
- paramIndex++;
1418
- }
1419
- if (toDate) {
1420
- conditions.push(`"createdAt" <= $${paramIndex}`);
1421
- values.push(toDate);
1422
- paramIndex++;
1423
- }
1424
- const whereClause = conditions.length > 0 ? `WHERE ${conditions.join(" AND ")}` : "";
1425
- let total = 0;
1426
- if (limit !== void 0 && offset !== void 0) {
1427
- const countResult = await this.db.one(
1428
- `SELECT COUNT(*) as count FROM ${this.getTableName(TABLE_WORKFLOW_SNAPSHOT)} ${whereClause}`,
1429
- values
1430
- );
1431
- total = Number(countResult.count);
1432
- }
1433
- const query = `
1445
+ try {
1446
+ const conditions = [];
1447
+ const values = [];
1448
+ let paramIndex = 1;
1449
+ if (workflowName) {
1450
+ conditions.push(`workflow_name = $${paramIndex}`);
1451
+ values.push(workflowName);
1452
+ paramIndex++;
1453
+ }
1454
+ if (resourceId) {
1455
+ const hasResourceId = await this.hasColumn(TABLE_WORKFLOW_SNAPSHOT, "resourceId");
1456
+ if (hasResourceId) {
1457
+ conditions.push(`"resourceId" = $${paramIndex}`);
1458
+ values.push(resourceId);
1459
+ paramIndex++;
1460
+ } else {
1461
+ console.warn(`[${TABLE_WORKFLOW_SNAPSHOT}] resourceId column not found. Skipping resourceId filter.`);
1462
+ }
1463
+ }
1464
+ if (fromDate) {
1465
+ conditions.push(`"createdAt" >= $${paramIndex}`);
1466
+ values.push(fromDate);
1467
+ paramIndex++;
1468
+ }
1469
+ if (toDate) {
1470
+ conditions.push(`"createdAt" <= $${paramIndex}`);
1471
+ values.push(toDate);
1472
+ paramIndex++;
1473
+ }
1474
+ const whereClause = conditions.length > 0 ? `WHERE ${conditions.join(" AND ")}` : "";
1475
+ let total = 0;
1476
+ if (limit !== void 0 && offset !== void 0) {
1477
+ const countResult = await this.db.one(
1478
+ `SELECT COUNT(*) as count FROM ${this.getTableName(TABLE_WORKFLOW_SNAPSHOT)} ${whereClause}`,
1479
+ values
1480
+ );
1481
+ total = Number(countResult.count);
1482
+ }
1483
+ const query = `
1434
1484
  SELECT * FROM ${this.getTableName(TABLE_WORKFLOW_SNAPSHOT)}
1435
1485
  ${whereClause}
1436
1486
  ORDER BY "createdAt" DESC
1437
1487
  ${limit !== void 0 && offset !== void 0 ? ` LIMIT $${paramIndex} OFFSET $${paramIndex + 1}` : ""}
1438
1488
  `;
1439
- const queryValues = limit !== void 0 && offset !== void 0 ? [...values, limit, offset] : values;
1440
- const result = await this.db.manyOrNone(query, queryValues);
1441
- const runs = (result || []).map((row) => {
1442
- let parsedSnapshot = row.snapshot;
1443
- if (typeof parsedSnapshot === "string") {
1444
- try {
1445
- parsedSnapshot = JSON.parse(row.snapshot);
1446
- } catch (e) {
1447
- console.warn(`Failed to parse snapshot for workflow ${row.workflow_name}: ${e}`);
1448
- }
1489
+ const queryValues = limit !== void 0 && offset !== void 0 ? [...values, limit, offset] : values;
1490
+ const result = await this.db.manyOrNone(query, queryValues);
1491
+ const runs = (result || []).map((row) => {
1492
+ return this.parseWorkflowRun(row);
1493
+ });
1494
+ return { runs, total: total || runs.length };
1495
+ } catch (error) {
1496
+ console.error("Error getting workflow runs:", error);
1497
+ throw error;
1498
+ }
1499
+ }
1500
+ async getWorkflowRunById({
1501
+ runId,
1502
+ workflowName
1503
+ }) {
1504
+ try {
1505
+ const conditions = [];
1506
+ const values = [];
1507
+ let paramIndex = 1;
1508
+ if (runId) {
1509
+ conditions.push(`run_id = $${paramIndex}`);
1510
+ values.push(runId);
1511
+ paramIndex++;
1449
1512
  }
1450
- return {
1451
- workflowName: row.workflow_name,
1452
- runId: row.run_id,
1453
- snapshot: parsedSnapshot,
1454
- createdAt: row.createdAt,
1455
- updatedAt: row.updatedAt
1456
- };
1457
- });
1458
- return { runs, total: total || runs.length };
1513
+ if (workflowName) {
1514
+ conditions.push(`workflow_name = $${paramIndex}`);
1515
+ values.push(workflowName);
1516
+ paramIndex++;
1517
+ }
1518
+ const whereClause = conditions.length > 0 ? `WHERE ${conditions.join(" AND ")}` : "";
1519
+ const query = `
1520
+ SELECT * FROM ${this.getTableName(TABLE_WORKFLOW_SNAPSHOT)}
1521
+ ${whereClause}
1522
+ `;
1523
+ const queryValues = values;
1524
+ const result = await this.db.oneOrNone(query, queryValues);
1525
+ if (!result) {
1526
+ return null;
1527
+ }
1528
+ return this.parseWorkflowRun(result);
1529
+ } catch (error) {
1530
+ console.error("Error getting workflow run by ID:", error);
1531
+ throw error;
1532
+ }
1459
1533
  }
1460
1534
  async close() {
1461
1535
  this.pgp.end();
package/package.json CHANGED
@@ -1,6 +1,6 @@
1
1
  {
2
2
  "name": "@mastra/pg",
3
- "version": "0.3.1-alpha.2",
3
+ "version": "0.3.1-alpha.4",
4
4
  "description": "Postgres provider for Mastra - includes both vector and db storage capabilities",
5
5
  "type": "module",
6
6
  "main": "dist/index.js",
@@ -24,16 +24,16 @@
24
24
  "pg": "^8.13.3",
25
25
  "pg-promise": "^11.11.0",
26
26
  "xxhash-wasm": "^1.1.0",
27
- "@mastra/core": "^0.9.1-alpha.2"
27
+ "@mastra/core": "^0.9.1-alpha.4"
28
28
  },
29
29
  "devDependencies": {
30
- "@microsoft/api-extractor": "^7.52.1",
30
+ "@microsoft/api-extractor": "^7.52.5",
31
31
  "@types/node": "^20.17.27",
32
32
  "@types/pg": "^8.11.11",
33
33
  "eslint": "^9.23.0",
34
34
  "tsup": "^8.4.0",
35
35
  "typescript": "^5.8.2",
36
- "vitest": "^3.0.9",
36
+ "vitest": "^3.1.2",
37
37
  "@internal/lint": "0.0.2"
38
38
  },
39
39
  "scripts": {