@mastra/pg 0.1.0-alpha.13 → 0.1.0-alpha.15

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/CHANGELOG.md CHANGED
@@ -1,5 +1,25 @@
1
1
  # @mastra/pg
2
2
 
3
+ ## 0.1.0-alpha.15
4
+
5
+ ### Patch Changes
6
+
7
+ - Updated dependencies [1420ae2]
8
+ - Updated dependencies [99f1847]
9
+ - @mastra/core@0.2.0-alpha.105
10
+
11
+ ## 0.1.0-alpha.14
12
+
13
+ ### Patch Changes
14
+
15
+ - b97ca96: Tracing into default storage
16
+ - Updated dependencies [5fdc87c]
17
+ - Updated dependencies [b97ca96]
18
+ - Updated dependencies [72d1990]
19
+ - Updated dependencies [cf6d825]
20
+ - Updated dependencies [10870bc]
21
+ - @mastra/core@0.2.0-alpha.104
22
+
3
23
  ## 0.1.0-alpha.13
4
24
 
5
25
  ### Patch Changes
@@ -2,6 +2,7 @@ import { ArrayOperator } from '@mastra/core/filter';
2
2
  import { BaseFilterTranslator } from '@mastra/core/filter';
3
3
  import { BasicOperator } from '@mastra/core/filter';
4
4
  import { ElementOperator } from '@mastra/core/filter';
5
+ import { EvalRow } from '@mastra/core/storage';
5
6
  import { Filter } from '@mastra/core/filter';
6
7
  import { IndexStats } from '@mastra/core/vector';
7
8
  import { LogicalOperator } from '@mastra/core/filter';
@@ -88,6 +89,18 @@ declare class PostgresStore extends MastraStorage {
88
89
  private db;
89
90
  private pgp;
90
91
  constructor(config: PostgresConfig);
92
+ getEvalsByAgentName(agentName: string, type?: 'test' | 'live'): Promise<EvalRow[]>;
93
+ batchInsert({ tableName, records }: {
94
+ tableName: TABLE_NAMES;
95
+ records: Record<string, any>[];
96
+ }): Promise<void>;
97
+ getTraces({ name, scope, page, perPage, attributes, }: {
98
+ name?: string;
99
+ scope?: string;
100
+ page: number;
101
+ perPage: number;
102
+ attributes?: Record<string, string>;
103
+ }): Promise<any[]>;
91
104
  createTable({ tableName, schema, }: {
92
105
  tableName: TABLE_NAMES;
93
106
  schema: Record<string, StorageColumn>;
package/dist/index.js CHANGED
@@ -516,6 +516,86 @@ var PostgresStore = class extends MastraStorage {
516
516
  }
517
517
  );
518
518
  }
519
+ getEvalsByAgentName(agentName, type) {
520
+ throw new Error("Method not implemented.");
521
+ }
522
+ async batchInsert({ tableName, records }) {
523
+ try {
524
+ await this.db.query("BEGIN");
525
+ for (const record of records) {
526
+ await this.insert({ tableName, record });
527
+ }
528
+ await this.db.query("COMMIT");
529
+ } catch (error) {
530
+ console.error(`Error inserting into ${tableName}:`, error);
531
+ await this.db.query("ROLLBACK");
532
+ throw error;
533
+ }
534
+ }
535
+ async getTraces({
536
+ name,
537
+ scope,
538
+ page,
539
+ perPage,
540
+ attributes
541
+ }) {
542
+ let idx = 1;
543
+ const limit = perPage;
544
+ const offset = page * perPage;
545
+ const args = [];
546
+ const conditions = [];
547
+ if (name) {
548
+ conditions.push(`name LIKE CONCAT($${idx++}, '%')`);
549
+ }
550
+ if (scope) {
551
+ conditions.push(`scope = $${idx++}`);
552
+ }
553
+ if (attributes) {
554
+ Object.keys(attributes).forEach((key) => {
555
+ conditions.push(`attributes->>'${key}' = $${idx++}`);
556
+ });
557
+ }
558
+ const whereClause = conditions.length > 0 ? `WHERE ${conditions.join(" AND ")}` : "";
559
+ if (name) {
560
+ args.push(name);
561
+ }
562
+ if (scope) {
563
+ args.push(scope);
564
+ }
565
+ if (attributes) {
566
+ for (const [_key, value] of Object.entries(attributes)) {
567
+ args.push(value);
568
+ }
569
+ }
570
+ console.log(
571
+ "QUERY",
572
+ `SELECT * FROM ${MastraStorage.TABLE_TRACES} ${whereClause} ORDER BY "createdAt" DESC LIMIT ${limit} OFFSET ${offset}`,
573
+ args
574
+ );
575
+ const result = await this.db.manyOrNone(
576
+ `SELECT * FROM ${MastraStorage.TABLE_TRACES} ${whereClause} ORDER BY "createdAt" DESC LIMIT ${limit} OFFSET ${offset}`,
577
+ args
578
+ );
579
+ if (!result) {
580
+ return [];
581
+ }
582
+ return result.map((row) => ({
583
+ id: row.id,
584
+ parentSpanId: row.parentSpanId,
585
+ traceId: row.traceId,
586
+ name: row.name,
587
+ scope: row.scope,
588
+ kind: row.kind,
589
+ status: row.status,
590
+ events: row.events,
591
+ links: row.links,
592
+ attributes: row.attributes,
593
+ startTime: row.startTime,
594
+ endTime: row.endTime,
595
+ other: row.other,
596
+ createdAt: row.createdAt
597
+ }));
598
+ }
519
599
  async createTable({
520
600
  tableName,
521
601
  schema
package/package.json CHANGED
@@ -1,6 +1,6 @@
1
1
  {
2
2
  "name": "@mastra/pg",
3
- "version": "0.1.0-alpha.13",
3
+ "version": "0.1.0-alpha.15",
4
4
  "description": "Postgres provider for Mastra - includes both vector and db storage capabilities",
5
5
  "type": "module",
6
6
  "main": "dist/index.js",
@@ -17,7 +17,7 @@
17
17
  "dependencies": {
18
18
  "pg": "^8.13.1",
19
19
  "pg-promise": "^11.5.4",
20
- "@mastra/core": "^0.2.0-alpha.103"
20
+ "@mastra/core": "^0.2.0-alpha.105"
21
21
  },
22
22
  "devDependencies": {
23
23
  "@microsoft/api-extractor": "^7.49.2",
@@ -1,8 +1,22 @@
1
1
  import { type MessageType, type StorageThreadType } from '@mastra/core/memory';
2
- import { MastraStorage, type StorageColumn, type StorageGetMessagesArg, type TABLE_NAMES } from '@mastra/core/storage';
2
+ import {
3
+ MastraStorage,
4
+ type EvalRow,
5
+ type StorageColumn,
6
+ type StorageGetMessagesArg,
7
+ type TABLE_NAMES,
8
+ } from '@mastra/core/storage';
3
9
  import { type WorkflowRunState } from '@mastra/core/workflows';
4
10
  import pgPromise from 'pg-promise';
5
11
 
12
+ function safelyParseJSON(json: string): any {
13
+ try {
14
+ return JSON.parse(json);
15
+ } catch (e) {
16
+ return {};
17
+ }
18
+ }
19
+
6
20
  export type PostgresConfig =
7
21
  | {
8
22
  host: string;
@@ -35,6 +49,120 @@ export class PostgresStore extends MastraStorage {
35
49
  );
36
50
  }
37
51
 
52
+ getEvalsByAgentName(agentName: string, type?: 'test' | 'live'): Promise<EvalRow[]> {
53
+ throw new Error('Method not implemented.');
54
+ }
55
+
56
+ async batchInsert({ tableName, records }: { tableName: TABLE_NAMES; records: Record<string, any>[] }): Promise<void> {
57
+ try {
58
+ await this.db.query('BEGIN');
59
+ for (const record of records) {
60
+ await this.insert({ tableName, record });
61
+ }
62
+ await this.db.query('COMMIT');
63
+ } catch (error) {
64
+ console.error(`Error inserting into ${tableName}:`, error);
65
+ await this.db.query('ROLLBACK');
66
+ throw error;
67
+ }
68
+ }
69
+
70
+ async getTraces({
71
+ name,
72
+ scope,
73
+ page,
74
+ perPage,
75
+ attributes,
76
+ }: {
77
+ name?: string;
78
+ scope?: string;
79
+ page: number;
80
+ perPage: number;
81
+ attributes?: Record<string, string>;
82
+ }): Promise<any[]> {
83
+ let idx = 1;
84
+ const limit = perPage;
85
+ const offset = page * perPage;
86
+
87
+ const args: (string | number)[] = [];
88
+
89
+ const conditions: string[] = [];
90
+ if (name) {
91
+ conditions.push(`name LIKE CONCAT(\$${idx++}, '%')`);
92
+ }
93
+ if (scope) {
94
+ conditions.push(`scope = \$${idx++}`);
95
+ }
96
+ if (attributes) {
97
+ Object.keys(attributes).forEach(key => {
98
+ conditions.push(`attributes->>'${key}' = \$${idx++}`);
99
+ });
100
+ }
101
+
102
+ const whereClause = conditions.length > 0 ? `WHERE ${conditions.join(' AND ')}` : '';
103
+
104
+ if (name) {
105
+ args.push(name);
106
+ }
107
+
108
+ if (scope) {
109
+ args.push(scope);
110
+ }
111
+
112
+ if (attributes) {
113
+ for (const [_key, value] of Object.entries(attributes)) {
114
+ args.push(value);
115
+ }
116
+ }
117
+
118
+ console.log(
119
+ 'QUERY',
120
+ `SELECT * FROM ${MastraStorage.TABLE_TRACES} ${whereClause} ORDER BY "createdAt" DESC LIMIT ${limit} OFFSET ${offset}`,
121
+ args,
122
+ );
123
+
124
+ const result = await this.db.manyOrNone<{
125
+ id: string;
126
+ parentSpanId: string;
127
+ traceId: string;
128
+ name: string;
129
+ scope: string;
130
+ kind: string;
131
+ events: any[];
132
+ links: any[];
133
+ status: any;
134
+ attributes: Record<string, any>;
135
+ startTime: string;
136
+ endTime: string;
137
+ other: any;
138
+ createdAt: string;
139
+ }>(
140
+ `SELECT * FROM ${MastraStorage.TABLE_TRACES} ${whereClause} ORDER BY "createdAt" DESC LIMIT ${limit} OFFSET ${offset}`,
141
+ args,
142
+ );
143
+
144
+ if (!result) {
145
+ return [];
146
+ }
147
+
148
+ return result.map(row => ({
149
+ id: row.id,
150
+ parentSpanId: row.parentSpanId,
151
+ traceId: row.traceId,
152
+ name: row.name,
153
+ scope: row.scope,
154
+ kind: row.kind,
155
+ status: row.status,
156
+ events: row.events,
157
+ links: row.links,
158
+ attributes: row.attributes,
159
+ startTime: row.startTime,
160
+ endTime: row.endTime,
161
+ other: row.other,
162
+ createdAt: row.createdAt,
163
+ })) as any;
164
+ }
165
+
38
166
  async createTable({
39
167
  tableName,
40
168
  schema,