@mastra/pg 0.13.3 → 0.14.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/.turbo/turbo-build.log +1 -1
- package/CHANGELOG.md +39 -0
- package/dist/index.cjs +88 -29
- package/dist/index.cjs.map +1 -1
- package/dist/index.js +88 -29
- package/dist/index.js.map +1 -1
- package/dist/storage/domains/scores/index.d.ts +5 -2
- package/dist/storage/domains/scores/index.d.ts.map +1 -1
- package/dist/storage/index.d.ts +13 -6
- package/dist/storage/index.d.ts.map +1 -1
- package/dist/storage/test-utils.d.ts.map +1 -1
- package/package.json +5 -5
- package/src/storage/domains/scores/index.ts +40 -9
- package/src/storage/index.ts +70 -23
- package/src/storage/test-utils.ts +7 -0
|
@@ -1,5 +1,5 @@
|
|
|
1
1
|
import type { PaginationInfo, StoragePagination } from '@mastra/core';
|
|
2
|
-
import type { ScoreRowData } from '@mastra/core/scores';
|
|
2
|
+
import type { ScoreRowData, ScoringSource } from '@mastra/core/scores';
|
|
3
3
|
import { ScoresStorage } from '@mastra/core/storage';
|
|
4
4
|
import type { IDatabase } from 'pg-promise';
|
|
5
5
|
import type { StoreOperationsPG } from '../operations/index.js';
|
|
@@ -15,9 +15,12 @@ export declare class ScoresPG extends ScoresStorage {
|
|
|
15
15
|
getScoreById({ id }: {
|
|
16
16
|
id: string;
|
|
17
17
|
}): Promise<ScoreRowData | null>;
|
|
18
|
-
getScoresByScorerId({ scorerId, pagination, }: {
|
|
18
|
+
getScoresByScorerId({ scorerId, pagination, entityId, entityType, source, }: {
|
|
19
19
|
scorerId: string;
|
|
20
20
|
pagination: StoragePagination;
|
|
21
|
+
entityId?: string;
|
|
22
|
+
entityType?: string;
|
|
23
|
+
source?: ScoringSource;
|
|
21
24
|
}): Promise<{
|
|
22
25
|
pagination: PaginationInfo;
|
|
23
26
|
scores: ScoreRowData[];
|
|
@@ -1 +1 @@
|
|
|
1
|
-
{"version":3,"file":"index.d.ts","sourceRoot":"","sources":["../../../../src/storage/domains/scores/index.ts"],"names":[],"mappings":"AAAA,OAAO,KAAK,EAAE,cAAc,EAAE,iBAAiB,EAAE,MAAM,cAAc,CAAC;AAEtE,OAAO,KAAK,EAAE,YAAY,EAAE,MAAM,qBAAqB,CAAC;
|
|
1
|
+
{"version":3,"file":"index.d.ts","sourceRoot":"","sources":["../../../../src/storage/domains/scores/index.ts"],"names":[],"mappings":"AAAA,OAAO,KAAK,EAAE,cAAc,EAAE,iBAAiB,EAAE,MAAM,cAAc,CAAC;AAEtE,OAAO,KAAK,EAAE,YAAY,EAAE,aAAa,EAAE,MAAM,qBAAqB,CAAC;AACvE,OAAO,EAAmB,aAAa,EAAiB,MAAM,sBAAsB,CAAC;AACrF,OAAO,KAAK,EAAE,SAAS,EAAE,MAAM,YAAY,CAAC;AAC5C,OAAO,KAAK,EAAE,iBAAiB,EAAE,MAAM,eAAe,CAAC;AAqBvD,qBAAa,QAAS,SAAQ,aAAa;IAClC,MAAM,EAAE,SAAS,CAAC,EAAE,CAAC,CAAC;IAC7B,OAAO,CAAC,UAAU,CAAoB;IACtC,OAAO,CAAC,MAAM,CAAC,CAAS;gBAEZ,EACV,MAAM,EACN,UAAU,EACV,MAAM,GACP,EAAE;QACD,MAAM,EAAE,SAAS,CAAC,EAAE,CAAC,CAAC;QACtB,UAAU,EAAE,iBAAiB,CAAC;QAC9B,MAAM,CAAC,EAAE,MAAM,CAAC;KACjB;IAOK,YAAY,CAAC,EAAE,EAAE,EAAE,EAAE;QAAE,EAAE,EAAE,MAAM,CAAA;KAAE,GAAG,OAAO,CAAC,YAAY,GAAG,IAAI,CAAC;IAoBlE,mBAAmB,CAAC,EACxB,QAAQ,EACR,UAAU,EACV,QAAQ,EACR,UAAU,EACV,MAAM,GACP,EAAE;QACD,QAAQ,EAAE,MAAM,CAAC;QACjB,UAAU,EAAE,iBAAiB,CAAC;QAC9B,QAAQ,CAAC,EAAE,MAAM,CAAC;QAClB,UAAU,CAAC,EAAE,MAAM,CAAC;QACpB,MAAM,CAAC,EAAE,aAAa,CAAC;KACxB,GAAG,OAAO,CAAC;QAAE,UAAU,EAAE,cAAc,CAAC;QAAC,MAAM,EAAE,YAAY,EAAE,CAAA;KAAE,CAAC;IAiE7D,SAAS,CAAC,KAAK,EAAE,IAAI,CAAC,YAAY,EAAE,IAAI,GAAG,WAAW,GAAG,WAAW,CAAC,GAAG,OAAO,CAAC;QAAE,KAAK,EAAE,YAAY,CAAA;KAAE,CAAC;IAqDxG,gBAAgB,CAAC,EACrB,KAAK,EACL,UAAU,GACX,EAAE;QACD,KAAK,EAAE,MAAM,CAAC;QACd,UAAU,EAAE,iBAAiB,CAAC;KAC/B,GAAG,OAAO,CAAC;QAAE,UAAU,EAAE,cAAc,CAAC;QAAC,MAAM,EAAE,YAAY,EAAE,CAAA;KAAE,CAAC;IA2C7D,mBAAmB,CAAC,EACxB,QAAQ,EACR,UAAU,EACV,UAAU,GACX,EAAE;QACD,UAAU,EAAE,iBAAiB,CAAC;QAC9B,QAAQ,EAAE,MAAM,CAAC;QACjB,UAAU,EAAE,MAAM,CAAC;KACpB,GAAG,OAAO,CAAC;QAAE,UAAU,EAAE,cAAc,CAAC;QAAC,MAAM,EAAE,YAAY,EAAE,CAAA;KAAE,CAAC;CA2CpE"}
|
package/dist/storage/index.d.ts
CHANGED
|
@@ -1,6 +1,6 @@
|
|
|
1
1
|
import type { MastraMessageContentV2, MastraMessageV2 } from '@mastra/core/agent';
|
|
2
2
|
import type { MastraMessageV1, StorageThreadType } from '@mastra/core/memory';
|
|
3
|
-
import type { ScoreRowData } from '@mastra/core/scores';
|
|
3
|
+
import type { ScoreRowData, ScoringSource } from '@mastra/core/scores';
|
|
4
4
|
import { MastraStorage } from '@mastra/core/storage';
|
|
5
5
|
import type { EvalRow, PaginationInfo, StorageColumn, StorageGetMessagesArg, StorageGetTracesArg, StorageGetTracesPaginatedArg, StorageResourceType, TABLE_NAMES, WorkflowRun, WorkflowRuns, PaginationArgs, StoragePagination, StorageDomains, ThreadSortOptions } from '@mastra/core/storage';
|
|
6
6
|
import type { Trace } from '@mastra/core/telemetry';
|
|
@@ -9,6 +9,8 @@ import pgPromise from 'pg-promise';
|
|
|
9
9
|
import type { ISSLConfig } from 'pg-promise/typescript/pg-subset';
|
|
10
10
|
export type PostgresConfig = {
|
|
11
11
|
schemaName?: string;
|
|
12
|
+
max?: number;
|
|
13
|
+
idleTimeoutMillis?: number;
|
|
12
14
|
} & ({
|
|
13
15
|
host: string;
|
|
14
16
|
port: number;
|
|
@@ -20,12 +22,14 @@ export type PostgresConfig = {
|
|
|
20
22
|
connectionString: string;
|
|
21
23
|
});
|
|
22
24
|
export declare class PostgresStore extends MastraStorage {
|
|
23
|
-
|
|
24
|
-
|
|
25
|
-
private
|
|
26
|
-
private schema?;
|
|
25
|
+
#private;
|
|
26
|
+
private schema;
|
|
27
|
+
private isConnected;
|
|
27
28
|
stores: StorageDomains;
|
|
28
29
|
constructor(config: PostgresConfig);
|
|
30
|
+
init(): Promise<void>;
|
|
31
|
+
get db(): pgPromise.IDatabase<{}, import("pg-promise/typescript/pg-subset").IClient>;
|
|
32
|
+
get pgp(): pgPromise.IMain<{}, import("pg-promise/typescript/pg-subset").IClient>;
|
|
29
33
|
get supports(): {
|
|
30
34
|
selectByIncludeResourceScope: boolean;
|
|
31
35
|
resourceWorkingMemory: boolean;
|
|
@@ -182,9 +186,12 @@ export declare class PostgresStore extends MastraStorage {
|
|
|
182
186
|
getScoreById({ id: _id }: {
|
|
183
187
|
id: string;
|
|
184
188
|
}): Promise<ScoreRowData | null>;
|
|
185
|
-
getScoresByScorerId({ scorerId
|
|
189
|
+
getScoresByScorerId({ scorerId, pagination, entityId, entityType, source, }: {
|
|
186
190
|
scorerId: string;
|
|
187
191
|
pagination: StoragePagination;
|
|
192
|
+
entityId?: string;
|
|
193
|
+
entityType?: string;
|
|
194
|
+
source?: ScoringSource;
|
|
188
195
|
}): Promise<{
|
|
189
196
|
pagination: PaginationInfo;
|
|
190
197
|
scores: ScoreRowData[];
|
|
@@ -1 +1 @@
|
|
|
1
|
-
{"version":3,"file":"index.d.ts","sourceRoot":"","sources":["../../src/storage/index.ts"],"names":[],"mappings":"AAAA,OAAO,KAAK,EAAE,sBAAsB,EAAE,eAAe,EAAE,MAAM,oBAAoB,CAAC;AAElF,OAAO,KAAK,EAAE,eAAe,EAAE,iBAAiB,EAAE,MAAM,qBAAqB,CAAC;AAC9E,OAAO,KAAK,EAAE,YAAY,EAAE,MAAM,qBAAqB,CAAC;
|
|
1
|
+
{"version":3,"file":"index.d.ts","sourceRoot":"","sources":["../../src/storage/index.ts"],"names":[],"mappings":"AAAA,OAAO,KAAK,EAAE,sBAAsB,EAAE,eAAe,EAAE,MAAM,oBAAoB,CAAC;AAElF,OAAO,KAAK,EAAE,eAAe,EAAE,iBAAiB,EAAE,MAAM,qBAAqB,CAAC;AAC9E,OAAO,KAAK,EAAE,YAAY,EAAE,aAAa,EAAE,MAAM,qBAAqB,CAAC;AACvE,OAAO,EAAE,aAAa,EAAE,MAAM,sBAAsB,CAAC;AACrD,OAAO,KAAK,EACV,OAAO,EACP,cAAc,EACd,aAAa,EACb,qBAAqB,EACrB,mBAAmB,EACnB,4BAA4B,EAC5B,mBAAmB,EACnB,WAAW,EACX,WAAW,EACX,YAAY,EACZ,cAAc,EACd,iBAAiB,EACjB,cAAc,EACd,iBAAiB,EAClB,MAAM,sBAAsB,CAAC;AAC9B,OAAO,KAAK,EAAE,KAAK,EAAE,MAAM,wBAAwB,CAAC;AACpD,OAAO,KAAK,EAAE,gBAAgB,EAAE,MAAM,wBAAwB,CAAC;AAC/D,OAAO,SAAS,MAAM,YAAY,CAAC;AACnC,OAAO,KAAK,EAAE,UAAU,EAAE,MAAM,iCAAiC,CAAC;AAQlE,MAAM,MAAM,cAAc,GAAG;IAC3B,UAAU,CAAC,EAAE,MAAM,CAAC;IACpB,GAAG,CAAC,EAAE,MAAM,CAAC;IACb,iBAAiB,CAAC,EAAE,MAAM,CAAC;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AAgB,QAA6H,CAAC;AAE3J,wBAAgB,OAAO,
|
|
1
|
+
{"version":3,"file":"test-utils.d.ts","sourceRoot":"","sources":["../../src/storage/test-utils.ts"],"names":[],"mappings":"AAKA,OAAO,KAAK,EAAE,cAAc,EAAE,MAAM,GAAG,CAAC;AAExC,eAAO,MAAM,WAAW,EAAE,cAMzB,CAAC;AAEF,eAAO,MAAM,gBAAgB,QAA6H,CAAC;AAE3J,wBAAgB,OAAO,SAqWtB"}
|
package/package.json
CHANGED
|
@@ -1,6 +1,6 @@
|
|
|
1
1
|
{
|
|
2
2
|
"name": "@mastra/pg",
|
|
3
|
-
"version": "0.
|
|
3
|
+
"version": "0.14.0",
|
|
4
4
|
"description": "Postgres provider for Mastra - includes both vector and db storage capabilities",
|
|
5
5
|
"type": "module",
|
|
6
6
|
"main": "dist/index.js",
|
|
@@ -33,10 +33,10 @@
|
|
|
33
33
|
"tsup": "^8.5.0",
|
|
34
34
|
"typescript": "^5.8.3",
|
|
35
35
|
"vitest": "^3.2.4",
|
|
36
|
-
"@internal/lint": "0.0.
|
|
37
|
-
"@internal/storage-test-utils": "0.0.
|
|
38
|
-
"@mastra/core": "0.13.
|
|
39
|
-
"@internal/types-builder": "0.0.
|
|
36
|
+
"@internal/lint": "0.0.29",
|
|
37
|
+
"@internal/storage-test-utils": "0.0.25",
|
|
38
|
+
"@mastra/core": "0.13.2",
|
|
39
|
+
"@internal/types-builder": "0.0.4"
|
|
40
40
|
},
|
|
41
41
|
"peerDependencies": {
|
|
42
42
|
"@mastra/core": ">=0.13.0-0 <0.14.0-0"
|
|
@@ -1,12 +1,13 @@
|
|
|
1
1
|
import type { PaginationInfo, StoragePagination } from '@mastra/core';
|
|
2
2
|
import { ErrorCategory, ErrorDomain, MastraError } from '@mastra/core/error';
|
|
3
|
-
import type { ScoreRowData } from '@mastra/core/scores';
|
|
3
|
+
import type { ScoreRowData, ScoringSource } from '@mastra/core/scores';
|
|
4
4
|
import { safelyParseJSON, ScoresStorage, TABLE_SCORERS } from '@mastra/core/storage';
|
|
5
5
|
import type { IDatabase } from 'pg-promise';
|
|
6
6
|
import type { StoreOperationsPG } from '../operations';
|
|
7
7
|
import { getTableName } from '../utils';
|
|
8
8
|
|
|
9
9
|
function transformScoreRow(row: Record<string, any>): ScoreRowData {
|
|
10
|
+
console.log(`row is`, JSON.stringify(row, null, 2));
|
|
10
11
|
return {
|
|
11
12
|
...row,
|
|
12
13
|
input: safelyParseJSON(row.input),
|
|
@@ -50,7 +51,7 @@ export class ScoresPG extends ScoresStorage {
|
|
|
50
51
|
[id],
|
|
51
52
|
);
|
|
52
53
|
|
|
53
|
-
return transformScoreRow(result
|
|
54
|
+
return result ? transformScoreRow(result) : null;
|
|
54
55
|
} catch (error) {
|
|
55
56
|
throw new MastraError(
|
|
56
57
|
{
|
|
@@ -66,14 +67,41 @@ export class ScoresPG extends ScoresStorage {
|
|
|
66
67
|
async getScoresByScorerId({
|
|
67
68
|
scorerId,
|
|
68
69
|
pagination,
|
|
70
|
+
entityId,
|
|
71
|
+
entityType,
|
|
72
|
+
source,
|
|
69
73
|
}: {
|
|
70
74
|
scorerId: string;
|
|
71
75
|
pagination: StoragePagination;
|
|
76
|
+
entityId?: string;
|
|
77
|
+
entityType?: string;
|
|
78
|
+
source?: ScoringSource;
|
|
72
79
|
}): Promise<{ pagination: PaginationInfo; scores: ScoreRowData[] }> {
|
|
73
80
|
try {
|
|
81
|
+
const conditions: string[] = [`"scorerId" = $1`];
|
|
82
|
+
const queryParams: any[] = [scorerId];
|
|
83
|
+
let paramIndex = 2;
|
|
84
|
+
|
|
85
|
+
if (entityId) {
|
|
86
|
+
conditions.push(`"entityId" = $${paramIndex++}`);
|
|
87
|
+
queryParams.push(entityId);
|
|
88
|
+
}
|
|
89
|
+
|
|
90
|
+
if (entityType) {
|
|
91
|
+
conditions.push(`"entityType" = $${paramIndex++}`);
|
|
92
|
+
queryParams.push(entityType);
|
|
93
|
+
}
|
|
94
|
+
|
|
95
|
+
if (source) {
|
|
96
|
+
conditions.push(`"source" = $${paramIndex++}`);
|
|
97
|
+
queryParams.push(source);
|
|
98
|
+
}
|
|
99
|
+
|
|
100
|
+
const whereClause = conditions.join(' AND ');
|
|
101
|
+
|
|
74
102
|
const total = await this.client.oneOrNone<{ count: string }>(
|
|
75
|
-
`SELECT COUNT(*) FROM ${getTableName({ indexName: TABLE_SCORERS, schemaName: this.schema })} WHERE
|
|
76
|
-
|
|
103
|
+
`SELECT COUNT(*) FROM ${getTableName({ indexName: TABLE_SCORERS, schemaName: this.schema })} WHERE ${whereClause}`,
|
|
104
|
+
queryParams,
|
|
77
105
|
);
|
|
78
106
|
if (total?.count === '0' || !total?.count) {
|
|
79
107
|
return {
|
|
@@ -88,9 +116,10 @@ export class ScoresPG extends ScoresStorage {
|
|
|
88
116
|
}
|
|
89
117
|
|
|
90
118
|
const result = await this.client.manyOrNone<ScoreRowData>(
|
|
91
|
-
`SELECT * FROM ${getTableName({ indexName: TABLE_SCORERS, schemaName: this.schema })} WHERE "
|
|
92
|
-
[
|
|
119
|
+
`SELECT * FROM ${getTableName({ indexName: TABLE_SCORERS, schemaName: this.schema })} WHERE ${whereClause} ORDER BY "createdAt" DESC LIMIT $${paramIndex++} OFFSET $${paramIndex++}`,
|
|
120
|
+
[...queryParams, pagination.perPage, pagination.page * pagination.perPage],
|
|
93
121
|
);
|
|
122
|
+
|
|
94
123
|
return {
|
|
95
124
|
pagination: {
|
|
96
125
|
total: Number(total?.count) || 0,
|
|
@@ -115,7 +144,7 @@ export class ScoresPG extends ScoresStorage {
|
|
|
115
144
|
async saveScore(score: Omit<ScoreRowData, 'id' | 'createdAt' | 'updatedAt'>): Promise<{ score: ScoreRowData }> {
|
|
116
145
|
try {
|
|
117
146
|
// Generate ID like other storage implementations
|
|
118
|
-
const
|
|
147
|
+
const id = crypto.randomUUID();
|
|
119
148
|
|
|
120
149
|
const {
|
|
121
150
|
scorer,
|
|
@@ -130,10 +159,12 @@ export class ScoresPG extends ScoresStorage {
|
|
|
130
159
|
...rest
|
|
131
160
|
} = score;
|
|
132
161
|
|
|
162
|
+
console.log(`saving score with id: ${id}`);
|
|
163
|
+
|
|
133
164
|
await this.operations.insert({
|
|
134
165
|
tableName: TABLE_SCORERS,
|
|
135
166
|
record: {
|
|
136
|
-
id
|
|
167
|
+
id,
|
|
137
168
|
...rest,
|
|
138
169
|
input: JSON.stringify(input) || '',
|
|
139
170
|
output: JSON.stringify(output) || '',
|
|
@@ -149,7 +180,7 @@ export class ScoresPG extends ScoresStorage {
|
|
|
149
180
|
},
|
|
150
181
|
});
|
|
151
182
|
|
|
152
|
-
const scoreFromDb = await this.getScoreById({ id
|
|
183
|
+
const scoreFromDb = await this.getScoreById({ id });
|
|
153
184
|
return { score: scoreFromDb! };
|
|
154
185
|
} catch (error) {
|
|
155
186
|
throw new MastraError(
|
package/src/storage/index.ts
CHANGED
|
@@ -1,7 +1,7 @@
|
|
|
1
1
|
import type { MastraMessageContentV2, MastraMessageV2 } from '@mastra/core/agent';
|
|
2
2
|
import { ErrorCategory, ErrorDomain, MastraError } from '@mastra/core/error';
|
|
3
3
|
import type { MastraMessageV1, StorageThreadType } from '@mastra/core/memory';
|
|
4
|
-
import type { ScoreRowData } from '@mastra/core/scores';
|
|
4
|
+
import type { ScoreRowData, ScoringSource } from '@mastra/core/scores';
|
|
5
5
|
import { MastraStorage } from '@mastra/core/storage';
|
|
6
6
|
import type {
|
|
7
7
|
EvalRow,
|
|
@@ -32,6 +32,8 @@ import { WorkflowsPG } from './domains/workflows';
|
|
|
32
32
|
|
|
33
33
|
export type PostgresConfig = {
|
|
34
34
|
schemaName?: string;
|
|
35
|
+
max?: number;
|
|
36
|
+
idleTimeoutMillis?: number;
|
|
35
37
|
} & (
|
|
36
38
|
| {
|
|
37
39
|
host: string;
|
|
@@ -47,10 +49,11 @@ export type PostgresConfig = {
|
|
|
47
49
|
);
|
|
48
50
|
|
|
49
51
|
export class PostgresStore extends MastraStorage {
|
|
50
|
-
|
|
51
|
-
|
|
52
|
-
|
|
53
|
-
private schema
|
|
52
|
+
#db?: pgPromise.IDatabase<{}>;
|
|
53
|
+
#pgp?: pgPromise.IMain;
|
|
54
|
+
#config: PostgresConfig;
|
|
55
|
+
private schema: string;
|
|
56
|
+
private isConnected: boolean = false;
|
|
54
57
|
|
|
55
58
|
stores: StorageDomains;
|
|
56
59
|
|
|
@@ -78,10 +81,11 @@ export class PostgresStore extends MastraStorage {
|
|
|
78
81
|
}
|
|
79
82
|
}
|
|
80
83
|
super({ name: 'PostgresStore' });
|
|
81
|
-
this.pgp = pgPromise();
|
|
82
84
|
this.schema = config.schemaName || 'public';
|
|
83
|
-
this
|
|
84
|
-
|
|
85
|
+
this.#config = {
|
|
86
|
+
max: config.max,
|
|
87
|
+
idleTimeoutMillis: config.idleTimeoutMillis,
|
|
88
|
+
...(`connectionString` in config
|
|
85
89
|
? { connectionString: config.connectionString }
|
|
86
90
|
: {
|
|
87
91
|
host: config.host,
|
|
@@ -90,17 +94,37 @@ export class PostgresStore extends MastraStorage {
|
|
|
90
94
|
user: config.user,
|
|
91
95
|
password: config.password,
|
|
92
96
|
ssl: config.ssl,
|
|
93
|
-
},
|
|
97
|
+
}),
|
|
98
|
+
};
|
|
99
|
+
this.stores = {} as StorageDomains;
|
|
100
|
+
} catch (e) {
|
|
101
|
+
throw new MastraError(
|
|
102
|
+
{
|
|
103
|
+
id: 'MASTRA_STORAGE_PG_STORE_INITIALIZATION_FAILED',
|
|
104
|
+
domain: ErrorDomain.STORAGE,
|
|
105
|
+
category: ErrorCategory.USER,
|
|
106
|
+
},
|
|
107
|
+
e,
|
|
94
108
|
);
|
|
109
|
+
}
|
|
110
|
+
}
|
|
111
|
+
|
|
112
|
+
async init(): Promise<void> {
|
|
113
|
+
if (this.isConnected) {
|
|
114
|
+
return;
|
|
115
|
+
}
|
|
95
116
|
|
|
96
|
-
|
|
117
|
+
try {
|
|
118
|
+
this.isConnected = true;
|
|
119
|
+
this.#pgp = pgPromise();
|
|
120
|
+
this.#db = this.#pgp(this.#config);
|
|
97
121
|
|
|
98
|
-
const operations = new StoreOperationsPG({ client: this
|
|
99
|
-
const scores = new ScoresPG({ client: this
|
|
100
|
-
const traces = new TracesPG({ client: this
|
|
101
|
-
const workflows = new WorkflowsPG({ client: this
|
|
102
|
-
const legacyEvals = new LegacyEvalsPG({ client: this
|
|
103
|
-
const memory = new MemoryPG({ client: this
|
|
122
|
+
const operations = new StoreOperationsPG({ client: this.#db, schemaName: this.schema });
|
|
123
|
+
const scores = new ScoresPG({ client: this.#db, operations, schema: this.schema });
|
|
124
|
+
const traces = new TracesPG({ client: this.#db, operations, schema: this.schema });
|
|
125
|
+
const workflows = new WorkflowsPG({ client: this.#db, operations, schema: this.schema });
|
|
126
|
+
const legacyEvals = new LegacyEvalsPG({ client: this.#db, schema: this.schema });
|
|
127
|
+
const memory = new MemoryPG({ client: this.#db, schema: this.schema, operations });
|
|
104
128
|
|
|
105
129
|
this.stores = {
|
|
106
130
|
operations,
|
|
@@ -110,18 +134,35 @@ export class PostgresStore extends MastraStorage {
|
|
|
110
134
|
legacyEvals,
|
|
111
135
|
memory,
|
|
112
136
|
};
|
|
113
|
-
|
|
137
|
+
|
|
138
|
+
await super.init();
|
|
139
|
+
} catch (error) {
|
|
140
|
+
this.isConnected = false;
|
|
114
141
|
throw new MastraError(
|
|
115
142
|
{
|
|
116
|
-
id: '
|
|
143
|
+
id: 'MASTRA_STORAGE_POSTGRES_STORE_INIT_FAILED',
|
|
117
144
|
domain: ErrorDomain.STORAGE,
|
|
118
|
-
category: ErrorCategory.
|
|
145
|
+
category: ErrorCategory.THIRD_PARTY,
|
|
119
146
|
},
|
|
120
|
-
|
|
147
|
+
error,
|
|
121
148
|
);
|
|
122
149
|
}
|
|
123
150
|
}
|
|
124
151
|
|
|
152
|
+
public get db() {
|
|
153
|
+
if (!this.#db) {
|
|
154
|
+
throw new Error(`PostgresStore: Store is not initialized, please call "init()" first.`);
|
|
155
|
+
}
|
|
156
|
+
return this.#db;
|
|
157
|
+
}
|
|
158
|
+
|
|
159
|
+
public get pgp() {
|
|
160
|
+
if (!this.#pgp) {
|
|
161
|
+
throw new Error(`PostgresStore: Store is not initialized, please call "init()" first.`);
|
|
162
|
+
}
|
|
163
|
+
return this.#pgp;
|
|
164
|
+
}
|
|
165
|
+
|
|
125
166
|
public get supports() {
|
|
126
167
|
return {
|
|
127
168
|
selectByIncludeResourceScope: true,
|
|
@@ -380,13 +421,19 @@ export class PostgresStore extends MastraStorage {
|
|
|
380
421
|
}
|
|
381
422
|
|
|
382
423
|
async getScoresByScorerId({
|
|
383
|
-
scorerId
|
|
384
|
-
pagination
|
|
424
|
+
scorerId,
|
|
425
|
+
pagination,
|
|
426
|
+
entityId,
|
|
427
|
+
entityType,
|
|
428
|
+
source,
|
|
385
429
|
}: {
|
|
386
430
|
scorerId: string;
|
|
387
431
|
pagination: StoragePagination;
|
|
432
|
+
entityId?: string;
|
|
433
|
+
entityType?: string;
|
|
434
|
+
source?: ScoringSource;
|
|
388
435
|
}): Promise<{ pagination: PaginationInfo; scores: ScoreRowData[] }> {
|
|
389
|
-
return this.stores.scores.getScoresByScorerId({ scorerId
|
|
436
|
+
return this.stores.scores.getScoresByScorerId({ scorerId, pagination, entityId, entityType, source });
|
|
390
437
|
}
|
|
391
438
|
|
|
392
439
|
async saveScore(_score: ScoreRowData): Promise<{ score: ScoreRowData }> {
|
|
@@ -21,6 +21,7 @@ export function pgTests() {
|
|
|
21
21
|
describe('PG specific tests', () => {
|
|
22
22
|
beforeAll(async () => {
|
|
23
23
|
store = new PostgresStore(TEST_CONFIG);
|
|
24
|
+
await store.init();
|
|
24
25
|
});
|
|
25
26
|
afterAll(async () => {
|
|
26
27
|
try {
|
|
@@ -363,6 +364,12 @@ export function pgTests() {
|
|
|
363
364
|
it('does not throw on non-empty connection string', () => {
|
|
364
365
|
expect(() => new PostgresStore({ connectionString })).not.toThrow();
|
|
365
366
|
});
|
|
367
|
+
it('throws if store is not initialized', () => {
|
|
368
|
+
expect(() => new PostgresStore(validConfig).db.any('SELECT 1')).toThrow(
|
|
369
|
+
/PostgresStore: Store is not initialized/,
|
|
370
|
+
);
|
|
371
|
+
expect(() => new PostgresStore(validConfig).pgp).toThrow(/PostgresStore: Store is not initialized/);
|
|
372
|
+
});
|
|
366
373
|
});
|
|
367
374
|
});
|
|
368
375
|
}
|