@mastra/pg 0.14.1 → 0.14.2

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/dist/index.js CHANGED
@@ -1543,6 +1543,22 @@ var MemoryPG = class extends MemoryStorage {
1543
1543
  });
1544
1544
  return dedupedRows;
1545
1545
  }
1546
+ parseRow(row) {
1547
+ let content = row.content;
1548
+ try {
1549
+ content = JSON.parse(row.content);
1550
+ } catch {
1551
+ }
1552
+ return {
1553
+ id: row.id,
1554
+ content,
1555
+ role: row.role,
1556
+ createdAt: new Date(row.createdAt),
1557
+ threadId: row.threadId,
1558
+ resourceId: row.resourceId,
1559
+ ...row.type && row.type !== "v2" ? { type: row.type } : {}
1560
+ };
1561
+ }
1546
1562
  async getMessages(args) {
1547
1563
  const { threadId, format, selectBy } = args;
1548
1564
  const selectStatement = `SELECT id, content, role, type, "createdAt", thread_id AS "threadId", "resourceId"`;
@@ -1601,6 +1617,40 @@ var MemoryPG = class extends MemoryStorage {
1601
1617
  return [];
1602
1618
  }
1603
1619
  }
1620
+ async getMessagesById({
1621
+ messageIds,
1622
+ format
1623
+ }) {
1624
+ if (messageIds.length === 0) return [];
1625
+ const selectStatement = `SELECT id, content, role, type, "createdAt", thread_id AS "threadId", "resourceId"`;
1626
+ try {
1627
+ const tableName = getTableName({ indexName: TABLE_MESSAGES, schemaName: getSchemaName(this.schema) });
1628
+ const query = `
1629
+ ${selectStatement} FROM ${tableName}
1630
+ WHERE id IN (${messageIds.map((_, i) => `$${i + 1}`).join(", ")})
1631
+ ORDER BY "createdAt" DESC
1632
+ `;
1633
+ const resultRows = await this.client.manyOrNone(query, messageIds);
1634
+ const list = new MessageList().add(resultRows.map(this.parseRow), "memory");
1635
+ if (format === `v1`) return list.get.all.v1();
1636
+ return list.get.all.v2();
1637
+ } catch (error) {
1638
+ const mastraError = new MastraError(
1639
+ {
1640
+ id: "MASTRA_STORAGE_PG_STORE_GET_MESSAGES_BY_ID_FAILED",
1641
+ domain: ErrorDomain.STORAGE,
1642
+ category: ErrorCategory.THIRD_PARTY,
1643
+ details: {
1644
+ messageIds: JSON.stringify(messageIds)
1645
+ }
1646
+ },
1647
+ error
1648
+ );
1649
+ this.logger?.error?.(mastraError.toString());
1650
+ this.logger?.trackException(mastraError);
1651
+ return [];
1652
+ }
1653
+ }
1604
1654
  async getMessagesPaginated(args) {
1605
1655
  const { threadId, format, selectBy } = args;
1606
1656
  const { page = 0, perPage: perPageInput, dateRange } = selectBy?.pagination || {};
@@ -3054,6 +3104,12 @@ var PostgresStore = class extends MastraStorage {
3054
3104
  async getMessages(args) {
3055
3105
  return this.stores.memory.getMessages(args);
3056
3106
  }
3107
+ async getMessagesById({
3108
+ messageIds,
3109
+ format
3110
+ }) {
3111
+ return this.stores.memory.getMessagesById({ messageIds, format });
3112
+ }
3057
3113
  async getMessagesPaginated(args) {
3058
3114
  return this.stores.memory.getMessagesPaginated(args);
3059
3115
  }