@mastra/pg 0.10.2-alpha.0 → 0.10.2-alpha.1
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 +7 -7
- package/CHANGELOG.md +12 -0
- package/dist/_tsup-dts-rollup.d.cts +30 -40
- package/dist/_tsup-dts-rollup.d.ts +30 -40
- package/dist/index.cjs +226 -196
- package/dist/index.js +226 -196
- package/package.json +3 -3
- package/src/storage/index.test.ts +208 -190
- package/src/storage/index.ts +283 -318
package/dist/index.js
CHANGED
|
@@ -992,18 +992,23 @@ var PostgresStore = class extends MastraStorage {
|
|
|
992
992
|
throw error;
|
|
993
993
|
}
|
|
994
994
|
}
|
|
995
|
+
/**
|
|
996
|
+
* @deprecated use getTracesPaginated instead
|
|
997
|
+
*/
|
|
995
998
|
async getTraces(args) {
|
|
996
|
-
|
|
997
|
-
|
|
998
|
-
|
|
999
|
-
|
|
1000
|
-
|
|
1001
|
-
|
|
1002
|
-
|
|
1003
|
-
|
|
1004
|
-
|
|
1005
|
-
|
|
1006
|
-
} = args;
|
|
999
|
+
if (args.fromDate || args.toDate) {
|
|
1000
|
+
args.dateRange = {
|
|
1001
|
+
start: args.fromDate,
|
|
1002
|
+
end: args.toDate
|
|
1003
|
+
};
|
|
1004
|
+
}
|
|
1005
|
+
const result = await this.getTracesPaginated(args);
|
|
1006
|
+
return result.traces;
|
|
1007
|
+
}
|
|
1008
|
+
async getTracesPaginated(args) {
|
|
1009
|
+
const { name, scope, page = 0, perPage: perPageInput, attributes, filters, dateRange } = args;
|
|
1010
|
+
const fromDate = dateRange?.start;
|
|
1011
|
+
const toDate = dateRange?.end;
|
|
1007
1012
|
const perPage = perPageInput !== void 0 ? perPageInput : 100;
|
|
1008
1013
|
const currentOffset = page * perPage;
|
|
1009
1014
|
const queryParams = [];
|
|
@@ -1043,7 +1048,7 @@ var PostgresStore = class extends MastraStorage {
|
|
|
1043
1048
|
const countQuery = `SELECT COUNT(*) FROM ${this.getTableName(TABLE_TRACES)} ${whereClause}`;
|
|
1044
1049
|
const countResult = await this.db.one(countQuery, queryParams);
|
|
1045
1050
|
const total = parseInt(countResult.count, 10);
|
|
1046
|
-
if (total === 0
|
|
1051
|
+
if (total === 0) {
|
|
1047
1052
|
return {
|
|
1048
1053
|
traces: [],
|
|
1049
1054
|
total: 0,
|
|
@@ -1051,10 +1056,10 @@ var PostgresStore = class extends MastraStorage {
|
|
|
1051
1056
|
perPage,
|
|
1052
1057
|
hasMore: false
|
|
1053
1058
|
};
|
|
1054
|
-
} else if (total === 0) {
|
|
1055
|
-
return [];
|
|
1056
1059
|
}
|
|
1057
|
-
const dataQuery = `SELECT * FROM ${this.getTableName(
|
|
1060
|
+
const dataQuery = `SELECT * FROM ${this.getTableName(
|
|
1061
|
+
TABLE_TRACES
|
|
1062
|
+
)} ${whereClause} ORDER BY "createdAt" DESC LIMIT $${paramIndex++} OFFSET $${paramIndex++}`;
|
|
1058
1063
|
const finalQueryParams = [...queryParams, perPage, currentOffset];
|
|
1059
1064
|
const rows = await this.db.manyOrNone(dataQuery, finalQueryParams);
|
|
1060
1065
|
const traces = rows.map((row) => ({
|
|
@@ -1073,17 +1078,13 @@ var PostgresStore = class extends MastraStorage {
|
|
|
1073
1078
|
other: row.other,
|
|
1074
1079
|
createdAt: row.createdAt
|
|
1075
1080
|
}));
|
|
1076
|
-
|
|
1077
|
-
|
|
1078
|
-
|
|
1079
|
-
|
|
1080
|
-
|
|
1081
|
-
|
|
1082
|
-
|
|
1083
|
-
};
|
|
1084
|
-
} else {
|
|
1085
|
-
return traces;
|
|
1086
|
-
}
|
|
1081
|
+
return {
|
|
1082
|
+
traces,
|
|
1083
|
+
total,
|
|
1084
|
+
page,
|
|
1085
|
+
perPage,
|
|
1086
|
+
hasMore: currentOffset + traces.length < total
|
|
1087
|
+
};
|
|
1087
1088
|
}
|
|
1088
1089
|
async setupSchema() {
|
|
1089
1090
|
if (!this.schema || this.schemaSetupComplete) {
|
|
@@ -1162,6 +1163,48 @@ var PostgresStore = class extends MastraStorage {
|
|
|
1162
1163
|
throw error;
|
|
1163
1164
|
}
|
|
1164
1165
|
}
|
|
1166
|
+
getDefaultValue(type) {
|
|
1167
|
+
switch (type) {
|
|
1168
|
+
case "timestamp":
|
|
1169
|
+
return "DEFAULT NOW()";
|
|
1170
|
+
case "jsonb":
|
|
1171
|
+
return "DEFAULT '{}'::jsonb";
|
|
1172
|
+
default:
|
|
1173
|
+
return super.getDefaultValue(type);
|
|
1174
|
+
}
|
|
1175
|
+
}
|
|
1176
|
+
/**
|
|
1177
|
+
* Alters table schema to add columns if they don't exist
|
|
1178
|
+
* @param tableName Name of the table
|
|
1179
|
+
* @param schema Schema of the table
|
|
1180
|
+
* @param ifNotExists Array of column names to add if they don't exist
|
|
1181
|
+
*/
|
|
1182
|
+
async alterTable({
|
|
1183
|
+
tableName,
|
|
1184
|
+
schema,
|
|
1185
|
+
ifNotExists
|
|
1186
|
+
}) {
|
|
1187
|
+
const fullTableName = this.getTableName(tableName);
|
|
1188
|
+
try {
|
|
1189
|
+
for (const columnName of ifNotExists) {
|
|
1190
|
+
if (schema[columnName]) {
|
|
1191
|
+
const columnDef = schema[columnName];
|
|
1192
|
+
const sqlType = this.getSqlType(columnDef.type);
|
|
1193
|
+
const nullable = columnDef.nullable === false ? "NOT NULL" : "";
|
|
1194
|
+
const defaultValue = columnDef.nullable === false ? this.getDefaultValue(columnDef.type) : "";
|
|
1195
|
+
const parsedColumnName = parseSqlIdentifier(columnName, "column name");
|
|
1196
|
+
const alterSql = `ALTER TABLE ${fullTableName} ADD COLUMN IF NOT EXISTS "${parsedColumnName}" ${sqlType} ${nullable} ${defaultValue}`.trim();
|
|
1197
|
+
await this.db.none(alterSql);
|
|
1198
|
+
this.logger?.debug?.(`Ensured column ${parsedColumnName} exists in table ${fullTableName}`);
|
|
1199
|
+
}
|
|
1200
|
+
}
|
|
1201
|
+
} catch (error) {
|
|
1202
|
+
this.logger?.error?.(
|
|
1203
|
+
`Error altering table ${tableName}: ${error instanceof Error ? error.message : String(error)}`
|
|
1204
|
+
);
|
|
1205
|
+
throw new Error(`Failed to alter table ${tableName}: ${error}`);
|
|
1206
|
+
}
|
|
1207
|
+
}
|
|
1165
1208
|
async clearTable({ tableName }) {
|
|
1166
1209
|
try {
|
|
1167
1210
|
await this.db.none(`TRUNCATE TABLE ${this.getTableName(tableName)} CASCADE`);
|
|
@@ -1237,58 +1280,65 @@ var PostgresStore = class extends MastraStorage {
|
|
|
1237
1280
|
throw error;
|
|
1238
1281
|
}
|
|
1239
1282
|
}
|
|
1283
|
+
/**
|
|
1284
|
+
* @deprecated use getThreadsByResourceIdPaginated instead
|
|
1285
|
+
*/
|
|
1240
1286
|
async getThreadsByResourceId(args) {
|
|
1241
|
-
const { resourceId
|
|
1287
|
+
const { resourceId } = args;
|
|
1242
1288
|
try {
|
|
1243
1289
|
const baseQuery = `FROM ${this.getTableName(TABLE_THREADS)} WHERE "resourceId" = $1`;
|
|
1244
1290
|
const queryParams = [resourceId];
|
|
1245
|
-
|
|
1246
|
-
|
|
1247
|
-
|
|
1248
|
-
|
|
1249
|
-
|
|
1250
|
-
|
|
1251
|
-
|
|
1252
|
-
|
|
1253
|
-
|
|
1254
|
-
|
|
1255
|
-
|
|
1256
|
-
|
|
1257
|
-
|
|
1258
|
-
|
|
1259
|
-
|
|
1260
|
-
|
|
1261
|
-
|
|
1262
|
-
|
|
1263
|
-
|
|
1264
|
-
|
|
1265
|
-
|
|
1266
|
-
|
|
1267
|
-
|
|
1268
|
-
|
|
1291
|
+
const dataQuery = `SELECT id, "resourceId", title, metadata, "createdAt", "updatedAt" ${baseQuery} ORDER BY "createdAt" DESC`;
|
|
1292
|
+
const rows = await this.db.manyOrNone(dataQuery, queryParams);
|
|
1293
|
+
return (rows || []).map((thread) => ({
|
|
1294
|
+
...thread,
|
|
1295
|
+
metadata: typeof thread.metadata === "string" ? JSON.parse(thread.metadata) : thread.metadata,
|
|
1296
|
+
createdAt: thread.createdAt,
|
|
1297
|
+
updatedAt: thread.updatedAt
|
|
1298
|
+
}));
|
|
1299
|
+
} catch (error) {
|
|
1300
|
+
this.logger.error(`Error getting threads for resource ${resourceId}:`, error);
|
|
1301
|
+
return [];
|
|
1302
|
+
}
|
|
1303
|
+
}
|
|
1304
|
+
async getThreadsByResourceIdPaginated(args) {
|
|
1305
|
+
const { resourceId, page = 0, perPage: perPageInput } = args;
|
|
1306
|
+
try {
|
|
1307
|
+
const baseQuery = `FROM ${this.getTableName(TABLE_THREADS)} WHERE "resourceId" = $1`;
|
|
1308
|
+
const queryParams = [resourceId];
|
|
1309
|
+
const perPage = perPageInput !== void 0 ? perPageInput : 100;
|
|
1310
|
+
const currentOffset = page * perPage;
|
|
1311
|
+
const countQuery = `SELECT COUNT(*) ${baseQuery}`;
|
|
1312
|
+
const countResult = await this.db.one(countQuery, queryParams);
|
|
1313
|
+
const total = parseInt(countResult.count, 10);
|
|
1314
|
+
if (total === 0) {
|
|
1269
1315
|
return {
|
|
1270
|
-
threads,
|
|
1271
|
-
total,
|
|
1316
|
+
threads: [],
|
|
1317
|
+
total: 0,
|
|
1272
1318
|
page,
|
|
1273
1319
|
perPage,
|
|
1274
|
-
hasMore:
|
|
1320
|
+
hasMore: false
|
|
1275
1321
|
};
|
|
1276
|
-
} else {
|
|
1277
|
-
const dataQuery = `SELECT id, "resourceId", title, metadata, "createdAt", "updatedAt" ${baseQuery} ORDER BY "createdAt" DESC`;
|
|
1278
|
-
const rows = await this.db.manyOrNone(dataQuery, queryParams);
|
|
1279
|
-
return (rows || []).map((thread) => ({
|
|
1280
|
-
...thread,
|
|
1281
|
-
metadata: typeof thread.metadata === "string" ? JSON.parse(thread.metadata) : thread.metadata,
|
|
1282
|
-
createdAt: thread.createdAt,
|
|
1283
|
-
updatedAt: thread.updatedAt
|
|
1284
|
-
}));
|
|
1285
1322
|
}
|
|
1323
|
+
const dataQuery = `SELECT id, "resourceId", title, metadata, "createdAt", "updatedAt" ${baseQuery} ORDER BY "createdAt" DESC LIMIT $2 OFFSET $3`;
|
|
1324
|
+
const rows = await this.db.manyOrNone(dataQuery, [...queryParams, perPage, currentOffset]);
|
|
1325
|
+
const threads = (rows || []).map((thread) => ({
|
|
1326
|
+
...thread,
|
|
1327
|
+
metadata: typeof thread.metadata === "string" ? JSON.parse(thread.metadata) : thread.metadata,
|
|
1328
|
+
createdAt: thread.createdAt,
|
|
1329
|
+
// Assuming already Date objects or ISO strings
|
|
1330
|
+
updatedAt: thread.updatedAt
|
|
1331
|
+
}));
|
|
1332
|
+
return {
|
|
1333
|
+
threads,
|
|
1334
|
+
total,
|
|
1335
|
+
page,
|
|
1336
|
+
perPage,
|
|
1337
|
+
hasMore: currentOffset + threads.length < total
|
|
1338
|
+
};
|
|
1286
1339
|
} catch (error) {
|
|
1287
1340
|
this.logger.error(`Error getting threads for resource ${resourceId}:`, error);
|
|
1288
|
-
|
|
1289
|
-
return { threads: [], total: 0, page, perPage: perPageInput || 100, hasMore: false };
|
|
1290
|
-
}
|
|
1291
|
-
return [];
|
|
1341
|
+
return { threads: [], total: 0, page, perPage: perPageInput || 100, hasMore: false };
|
|
1292
1342
|
}
|
|
1293
1343
|
}
|
|
1294
1344
|
async saveThread({ thread }) {
|
|
@@ -1369,68 +1419,15 @@ var PostgresStore = class extends MastraStorage {
|
|
|
1369
1419
|
}
|
|
1370
1420
|
}
|
|
1371
1421
|
async getMessages(args) {
|
|
1372
|
-
const { threadId, format,
|
|
1422
|
+
const { threadId, format, selectBy } = args;
|
|
1373
1423
|
const selectStatement = `SELECT id, content, role, type, "createdAt", thread_id AS "threadId"`;
|
|
1374
1424
|
const orderByStatement = `ORDER BY "createdAt" DESC`;
|
|
1375
1425
|
try {
|
|
1376
|
-
|
|
1377
|
-
|
|
1378
|
-
|
|
1379
|
-
|
|
1380
|
-
|
|
1381
|
-
let paramIndex = 2;
|
|
1382
|
-
if (fromDate) {
|
|
1383
|
-
conditions.push(`"createdAt" >= $${paramIndex++}`);
|
|
1384
|
-
queryParams.push(fromDate);
|
|
1385
|
-
}
|
|
1386
|
-
if (toDate) {
|
|
1387
|
-
conditions.push(`"createdAt" <= $${paramIndex++}`);
|
|
1388
|
-
queryParams.push(toDate);
|
|
1389
|
-
}
|
|
1390
|
-
const whereClause = conditions.length > 0 ? `WHERE ${conditions.join(" AND ")}` : "";
|
|
1391
|
-
const countQuery = `SELECT COUNT(*) FROM ${this.getTableName(TABLE_MESSAGES)} ${whereClause}`;
|
|
1392
|
-
const countResult = await this.db.one(countQuery, queryParams);
|
|
1393
|
-
const total = parseInt(countResult.count, 10);
|
|
1394
|
-
if (total === 0) {
|
|
1395
|
-
return {
|
|
1396
|
-
messages: [],
|
|
1397
|
-
total: 0,
|
|
1398
|
-
page,
|
|
1399
|
-
perPage,
|
|
1400
|
-
hasMore: false
|
|
1401
|
-
};
|
|
1402
|
-
}
|
|
1403
|
-
const dataQuery = `${selectStatement} FROM ${this.getTableName(TABLE_MESSAGES)} ${whereClause} ${orderByStatement} LIMIT $${paramIndex++} OFFSET $${paramIndex++}`;
|
|
1404
|
-
const rows = await this.db.manyOrNone(dataQuery, [...queryParams, perPage, currentOffset]);
|
|
1405
|
-
const fetchedMessages = (rows || []).map((message) => {
|
|
1406
|
-
if (typeof message.content === "string") {
|
|
1407
|
-
try {
|
|
1408
|
-
message.content = JSON.parse(message.content);
|
|
1409
|
-
} catch {
|
|
1410
|
-
}
|
|
1411
|
-
}
|
|
1412
|
-
if (message.type === "v2") delete message.type;
|
|
1413
|
-
return message;
|
|
1414
|
-
});
|
|
1415
|
-
const messagesToReturn = format === "v2" ? fetchedMessages.map(
|
|
1416
|
-
(m) => ({
|
|
1417
|
-
...m,
|
|
1418
|
-
content: m.content || { format: 2, parts: [{ type: "text", text: "" }] }
|
|
1419
|
-
})
|
|
1420
|
-
) : fetchedMessages;
|
|
1421
|
-
return {
|
|
1422
|
-
messages: messagesToReturn,
|
|
1423
|
-
total,
|
|
1424
|
-
page,
|
|
1425
|
-
perPage,
|
|
1426
|
-
hasMore: currentOffset + fetchedMessages.length < total
|
|
1427
|
-
};
|
|
1428
|
-
} else {
|
|
1429
|
-
let rows = [];
|
|
1430
|
-
const include = selectBy?.include || [];
|
|
1431
|
-
if (include.length) {
|
|
1432
|
-
rows = await this.db.manyOrNone(
|
|
1433
|
-
`
|
|
1426
|
+
let rows = [];
|
|
1427
|
+
const include = selectBy?.include || [];
|
|
1428
|
+
if (include.length) {
|
|
1429
|
+
rows = await this.db.manyOrNone(
|
|
1430
|
+
`
|
|
1434
1431
|
WITH ordered_messages AS (
|
|
1435
1432
|
SELECT
|
|
1436
1433
|
*,
|
|
@@ -1460,53 +1457,103 @@ var PostgresStore = class extends MastraStorage {
|
|
|
1460
1457
|
)
|
|
1461
1458
|
ORDER BY m."createdAt" ASC
|
|
1462
1459
|
`,
|
|
1463
|
-
|
|
1464
|
-
|
|
1465
|
-
|
|
1466
|
-
|
|
1467
|
-
|
|
1468
|
-
|
|
1469
|
-
|
|
1470
|
-
|
|
1471
|
-
|
|
1472
|
-
|
|
1473
|
-
|
|
1474
|
-
|
|
1475
|
-
|
|
1476
|
-
|
|
1477
|
-
|
|
1478
|
-
|
|
1479
|
-
|
|
1480
|
-
|
|
1481
|
-
|
|
1482
|
-
|
|
1460
|
+
// Keep ASC for final sorting after fetching context
|
|
1461
|
+
[
|
|
1462
|
+
threadId,
|
|
1463
|
+
include.map((i) => i.id),
|
|
1464
|
+
Math.max(0, ...include.map((i) => i.withPreviousMessages || 0)),
|
|
1465
|
+
// Ensure non-negative
|
|
1466
|
+
Math.max(0, ...include.map((i) => i.withNextMessages || 0))
|
|
1467
|
+
// Ensure non-negative
|
|
1468
|
+
]
|
|
1469
|
+
);
|
|
1470
|
+
} else {
|
|
1471
|
+
const limit = typeof selectBy?.last === `number` ? selectBy.last : 40;
|
|
1472
|
+
if (limit === 0 && selectBy?.last !== false) ; else {
|
|
1473
|
+
let query = `${selectStatement} FROM ${this.getTableName(
|
|
1474
|
+
TABLE_MESSAGES
|
|
1475
|
+
)} WHERE thread_id = $1 ${orderByStatement}`;
|
|
1476
|
+
const queryParams = [threadId];
|
|
1477
|
+
if (limit !== void 0 && selectBy?.last !== false) {
|
|
1478
|
+
query += ` LIMIT $2`;
|
|
1479
|
+
queryParams.push(limit);
|
|
1483
1480
|
}
|
|
1481
|
+
rows = await this.db.manyOrNone(query, queryParams);
|
|
1484
1482
|
}
|
|
1485
|
-
const fetchedMessages = (rows || []).map((message) => {
|
|
1486
|
-
if (typeof message.content === "string") {
|
|
1487
|
-
try {
|
|
1488
|
-
message.content = JSON.parse(message.content);
|
|
1489
|
-
} catch {
|
|
1490
|
-
}
|
|
1491
|
-
}
|
|
1492
|
-
if (message.type === "v2") delete message.type;
|
|
1493
|
-
return message;
|
|
1494
|
-
});
|
|
1495
|
-
const sortedMessages = fetchedMessages.sort(
|
|
1496
|
-
(a, b) => new Date(a.createdAt).getTime() - new Date(b.createdAt).getTime()
|
|
1497
|
-
);
|
|
1498
|
-
return format === "v2" ? sortedMessages.map(
|
|
1499
|
-
(m) => ({ ...m, content: m.content || { format: 2, parts: [{ type: "text", text: "" }] } })
|
|
1500
|
-
) : sortedMessages;
|
|
1501
1483
|
}
|
|
1484
|
+
const fetchedMessages = (rows || []).map((message) => {
|
|
1485
|
+
if (typeof message.content === "string") {
|
|
1486
|
+
try {
|
|
1487
|
+
message.content = JSON.parse(message.content);
|
|
1488
|
+
} catch {
|
|
1489
|
+
}
|
|
1490
|
+
}
|
|
1491
|
+
if (message.type === "v2") delete message.type;
|
|
1492
|
+
return message;
|
|
1493
|
+
});
|
|
1494
|
+
const sortedMessages = fetchedMessages.sort(
|
|
1495
|
+
(a, b) => new Date(a.createdAt).getTime() - new Date(b.createdAt).getTime()
|
|
1496
|
+
);
|
|
1497
|
+
return format === "v2" ? sortedMessages.map(
|
|
1498
|
+
(m) => ({ ...m, content: m.content || { format: 2, parts: [{ type: "text", text: "" }] } })
|
|
1499
|
+
) : sortedMessages;
|
|
1502
1500
|
} catch (error) {
|
|
1503
1501
|
this.logger.error("Error getting messages:", error);
|
|
1504
|
-
if (page !== void 0) {
|
|
1505
|
-
return { messages: [], total: 0, page, perPage: perPageInput || 40, hasMore: false };
|
|
1506
|
-
}
|
|
1507
1502
|
return [];
|
|
1508
1503
|
}
|
|
1509
1504
|
}
|
|
1505
|
+
async getMessagesPaginated(args) {
|
|
1506
|
+
const { threadId, format, selectBy } = args;
|
|
1507
|
+
const { page = 0, perPage: perPageInput, dateRange } = selectBy?.pagination || {};
|
|
1508
|
+
const fromDate = dateRange?.start;
|
|
1509
|
+
const toDate = dateRange?.end;
|
|
1510
|
+
const selectStatement = `SELECT id, content, role, type, "createdAt", thread_id AS "threadId"`;
|
|
1511
|
+
const orderByStatement = `ORDER BY "createdAt" DESC`;
|
|
1512
|
+
try {
|
|
1513
|
+
const perPage = perPageInput !== void 0 ? perPageInput : 40;
|
|
1514
|
+
const currentOffset = page * perPage;
|
|
1515
|
+
const conditions = [`thread_id = $1`];
|
|
1516
|
+
const queryParams = [threadId];
|
|
1517
|
+
let paramIndex = 2;
|
|
1518
|
+
if (fromDate) {
|
|
1519
|
+
conditions.push(`"createdAt" >= $${paramIndex++}`);
|
|
1520
|
+
queryParams.push(fromDate);
|
|
1521
|
+
}
|
|
1522
|
+
if (toDate) {
|
|
1523
|
+
conditions.push(`"createdAt" <= $${paramIndex++}`);
|
|
1524
|
+
queryParams.push(toDate);
|
|
1525
|
+
}
|
|
1526
|
+
const whereClause = conditions.length > 0 ? `WHERE ${conditions.join(" AND ")}` : "";
|
|
1527
|
+
const countQuery = `SELECT COUNT(*) FROM ${this.getTableName(TABLE_MESSAGES)} ${whereClause}`;
|
|
1528
|
+
const countResult = await this.db.one(countQuery, queryParams);
|
|
1529
|
+
const total = parseInt(countResult.count, 10);
|
|
1530
|
+
if (total === 0) {
|
|
1531
|
+
return {
|
|
1532
|
+
messages: [],
|
|
1533
|
+
total: 0,
|
|
1534
|
+
page,
|
|
1535
|
+
perPage,
|
|
1536
|
+
hasMore: false
|
|
1537
|
+
};
|
|
1538
|
+
}
|
|
1539
|
+
const dataQuery = `${selectStatement} FROM ${this.getTableName(
|
|
1540
|
+
TABLE_MESSAGES
|
|
1541
|
+
)} ${whereClause} ${orderByStatement} LIMIT $${paramIndex++} OFFSET $${paramIndex++}`;
|
|
1542
|
+
const rows = await this.db.manyOrNone(dataQuery, [...queryParams, perPage, currentOffset]);
|
|
1543
|
+
const list = new MessageList().add(rows || [], "memory");
|
|
1544
|
+
const messagesToReturn = format === `v2` ? list.get.all.v2() : list.get.all.v1();
|
|
1545
|
+
return {
|
|
1546
|
+
messages: messagesToReturn,
|
|
1547
|
+
total,
|
|
1548
|
+
page,
|
|
1549
|
+
perPage,
|
|
1550
|
+
hasMore: currentOffset + rows.length < total
|
|
1551
|
+
};
|
|
1552
|
+
} catch (error) {
|
|
1553
|
+
this.logger.error("Error getting messages:", error);
|
|
1554
|
+
return { messages: [], total: 0, page, perPage: perPageInput || 40, hasMore: false };
|
|
1555
|
+
}
|
|
1556
|
+
}
|
|
1510
1557
|
async saveMessages({
|
|
1511
1558
|
messages,
|
|
1512
1559
|
format
|
|
@@ -1717,8 +1764,10 @@ var PostgresStore = class extends MastraStorage {
|
|
|
1717
1764
|
async close() {
|
|
1718
1765
|
this.pgp.end();
|
|
1719
1766
|
}
|
|
1720
|
-
async getEvals(options) {
|
|
1721
|
-
const { agentName, type, page, perPage
|
|
1767
|
+
async getEvals(options = {}) {
|
|
1768
|
+
const { agentName, type, page = 0, perPage = 100, dateRange } = options;
|
|
1769
|
+
const fromDate = dateRange?.start;
|
|
1770
|
+
const toDate = dateRange?.end;
|
|
1722
1771
|
const conditions = [];
|
|
1723
1772
|
const queryParams = [];
|
|
1724
1773
|
let paramIndex = 1;
|
|
@@ -1743,45 +1792,26 @@ var PostgresStore = class extends MastraStorage {
|
|
|
1743
1792
|
const countQuery = `SELECT COUNT(*) FROM ${this.getTableName(TABLE_EVALS)} ${whereClause}`;
|
|
1744
1793
|
const countResult = await this.db.one(countQuery, queryParams);
|
|
1745
1794
|
const total = parseInt(countResult.count, 10);
|
|
1746
|
-
|
|
1747
|
-
let currentOffset;
|
|
1748
|
-
let currentPage = page;
|
|
1749
|
-
let currentPerPage = perPage;
|
|
1750
|
-
let hasMore = false;
|
|
1751
|
-
if (limit !== void 0 && offset !== void 0) {
|
|
1752
|
-
currentLimit = limit;
|
|
1753
|
-
currentOffset = offset;
|
|
1754
|
-
currentPage = void 0;
|
|
1755
|
-
currentPerPage = void 0;
|
|
1756
|
-
hasMore = currentOffset + currentLimit < total;
|
|
1757
|
-
} else if (page !== void 0 && perPage !== void 0) {
|
|
1758
|
-
currentLimit = perPage;
|
|
1759
|
-
currentOffset = page * perPage;
|
|
1760
|
-
hasMore = currentOffset + currentLimit < total;
|
|
1761
|
-
} else {
|
|
1762
|
-
currentLimit = perPage || 100;
|
|
1763
|
-
currentOffset = (page || 0) * currentLimit;
|
|
1764
|
-
if (page === void 0) currentPage = 0;
|
|
1765
|
-
if (currentPerPage === void 0) currentPerPage = currentLimit;
|
|
1766
|
-
hasMore = currentOffset + currentLimit < total;
|
|
1767
|
-
}
|
|
1795
|
+
const currentOffset = page * perPage;
|
|
1768
1796
|
if (total === 0) {
|
|
1769
1797
|
return {
|
|
1770
1798
|
evals: [],
|
|
1771
1799
|
total: 0,
|
|
1772
|
-
page
|
|
1773
|
-
perPage
|
|
1800
|
+
page,
|
|
1801
|
+
perPage,
|
|
1774
1802
|
hasMore: false
|
|
1775
1803
|
};
|
|
1776
1804
|
}
|
|
1777
|
-
const dataQuery = `SELECT * FROM ${this.getTableName(
|
|
1778
|
-
|
|
1805
|
+
const dataQuery = `SELECT * FROM ${this.getTableName(
|
|
1806
|
+
TABLE_EVALS
|
|
1807
|
+
)} ${whereClause} ORDER BY created_at DESC LIMIT $${paramIndex++} OFFSET $${paramIndex++}`;
|
|
1808
|
+
const rows = await this.db.manyOrNone(dataQuery, [...queryParams, perPage, currentOffset]);
|
|
1779
1809
|
return {
|
|
1780
1810
|
evals: rows?.map((row) => this.transformEvalRow(row)) ?? [],
|
|
1781
1811
|
total,
|
|
1782
|
-
page
|
|
1783
|
-
perPage
|
|
1784
|
-
hasMore
|
|
1812
|
+
page,
|
|
1813
|
+
perPage,
|
|
1814
|
+
hasMore: currentOffset + (rows?.length ?? 0) < total
|
|
1785
1815
|
};
|
|
1786
1816
|
}
|
|
1787
1817
|
};
|
package/package.json
CHANGED
|
@@ -1,6 +1,6 @@
|
|
|
1
1
|
{
|
|
2
2
|
"name": "@mastra/pg",
|
|
3
|
-
"version": "0.10.2-alpha.
|
|
3
|
+
"version": "0.10.2-alpha.1",
|
|
4
4
|
"description": "Postgres provider for Mastra - includes both vector and db storage capabilities",
|
|
5
5
|
"type": "module",
|
|
6
6
|
"main": "dist/index.js",
|
|
@@ -32,10 +32,10 @@
|
|
|
32
32
|
"eslint": "^9.28.0",
|
|
33
33
|
"tsup": "^8.5.0",
|
|
34
34
|
"typescript": "^5.8.2",
|
|
35
|
-
"vitest": "^3.
|
|
35
|
+
"vitest": "^3.2.2",
|
|
36
36
|
"@internal/lint": "0.0.10",
|
|
37
37
|
"@internal/storage-test-utils": "0.0.6",
|
|
38
|
-
"@mastra/core": "0.10.4-alpha.
|
|
38
|
+
"@mastra/core": "0.10.4-alpha.1"
|
|
39
39
|
},
|
|
40
40
|
"peerDependencies": {
|
|
41
41
|
"@mastra/core": "^0.10.2-alpha.0"
|