@mastra/pg 0.17.4 → 0.17.5-alpha.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/dist/index.js CHANGED
@@ -1503,6 +1503,20 @@ var MemoryPG = class extends MemoryStorage {
1503
1503
  this.schema = schema;
1504
1504
  this.operations = operations;
1505
1505
  }
1506
+ /**
1507
+ * Normalizes message row from database by applying createdAtZ fallback
1508
+ */
1509
+ normalizeMessageRow(row) {
1510
+ return {
1511
+ id: row.id,
1512
+ content: row.content,
1513
+ role: row.role,
1514
+ type: row.type,
1515
+ createdAt: row.createdAtZ || row.createdAt,
1516
+ threadId: row.threadId,
1517
+ resourceId: row.resourceId
1518
+ };
1519
+ }
1506
1520
  async getThreadById({ threadId }) {
1507
1521
  try {
1508
1522
  const tableName = getTableName({ indexName: TABLE_THREADS, schemaName: getSchemaName(this.schema) });
@@ -1770,11 +1784,12 @@ var MemoryPG = class extends MemoryStorage {
1770
1784
  WHERE thread_id = $${paramIdx}
1771
1785
  )
1772
1786
  SELECT
1773
- m.id,
1774
- m.content,
1775
- m.role,
1787
+ m.id,
1788
+ m.content,
1789
+ m.role,
1776
1790
  m.type,
1777
- m."createdAt",
1791
+ m."createdAt",
1792
+ m."createdAtZ",
1778
1793
  m.thread_id AS "threadId",
1779
1794
  m."resourceId"
1780
1795
  FROM ordered_messages m
@@ -1808,24 +1823,25 @@ var MemoryPG = class extends MemoryStorage {
1808
1823
  return dedupedRows;
1809
1824
  }
1810
1825
  parseRow(row) {
1811
- let content = row.content;
1826
+ const normalized = this.normalizeMessageRow(row);
1827
+ let content = normalized.content;
1812
1828
  try {
1813
- content = JSON.parse(row.content);
1829
+ content = JSON.parse(normalized.content);
1814
1830
  } catch {
1815
1831
  }
1816
1832
  return {
1817
- id: row.id,
1833
+ id: normalized.id,
1818
1834
  content,
1819
- role: row.role,
1820
- createdAt: new Date(row.createdAt),
1821
- threadId: row.threadId,
1822
- resourceId: row.resourceId,
1823
- ...row.type && row.type !== "v2" ? { type: row.type } : {}
1835
+ role: normalized.role,
1836
+ createdAt: new Date(normalized.createdAt),
1837
+ threadId: normalized.threadId,
1838
+ resourceId: normalized.resourceId,
1839
+ ...normalized.type && normalized.type !== "v2" ? { type: normalized.type } : {}
1824
1840
  };
1825
1841
  }
1826
1842
  async getMessages(args) {
1827
1843
  const { threadId, resourceId, format, selectBy } = args;
1828
- const selectStatement = `SELECT id, content, role, type, "createdAt", thread_id AS "threadId", "resourceId"`;
1844
+ const selectStatement = `SELECT id, content, role, type, "createdAt", "createdAtZ", thread_id AS "threadId", "resourceId"`;
1829
1845
  const orderByStatement = `ORDER BY "createdAt" DESC`;
1830
1846
  const limit = resolveMessageLimit({ last: selectBy?.last, defaultLimit: 40 });
1831
1847
  try {
@@ -1849,7 +1865,8 @@ var MemoryPG = class extends MemoryStorage {
1849
1865
  const queryParams = [threadId, ...excludeIds, limit];
1850
1866
  const remainingRows = await this.client.manyOrNone(query, queryParams);
1851
1867
  rows.push(...remainingRows);
1852
- const fetchedMessages = (rows || []).map((message) => {
1868
+ const fetchedMessages = (rows || []).map((row) => {
1869
+ const message = this.normalizeMessageRow(row);
1853
1870
  if (typeof message.content === "string") {
1854
1871
  try {
1855
1872
  message.content = JSON.parse(message.content);
@@ -1888,7 +1905,7 @@ var MemoryPG = class extends MemoryStorage {
1888
1905
  format
1889
1906
  }) {
1890
1907
  if (messageIds.length === 0) return [];
1891
- const selectStatement = `SELECT id, content, role, type, "createdAt", thread_id AS "threadId", "resourceId"`;
1908
+ const selectStatement = `SELECT id, content, role, type, "createdAt", "createdAtZ", thread_id AS "threadId", "resourceId"`;
1892
1909
  try {
1893
1910
  const tableName = getTableName({ indexName: TABLE_MESSAGES, schemaName: getSchemaName(this.schema) });
1894
1911
  const query = `
@@ -1897,7 +1914,10 @@ var MemoryPG = class extends MemoryStorage {
1897
1914
  ORDER BY "createdAt" DESC
1898
1915
  `;
1899
1916
  const resultRows = await this.client.manyOrNone(query, messageIds);
1900
- const list = new MessageList().add(resultRows.map(this.parseRow), "memory");
1917
+ const list = new MessageList().add(
1918
+ resultRows.map((row) => this.parseRow(row)),
1919
+ "memory"
1920
+ );
1901
1921
  if (format === `v1`) return list.get.all.v1();
1902
1922
  return list.get.all.v2();
1903
1923
  } catch (error) {
@@ -1922,7 +1942,7 @@ var MemoryPG = class extends MemoryStorage {
1922
1942
  const { page = 0, perPage: perPageInput, dateRange } = selectBy?.pagination || {};
1923
1943
  const fromDate = dateRange?.start;
1924
1944
  const toDate = dateRange?.end;
1925
- const selectStatement = `SELECT id, content, role, type, "createdAt", thread_id AS "threadId", "resourceId"`;
1945
+ const selectStatement = `SELECT id, content, role, type, "createdAt", "createdAtZ", thread_id AS "threadId", "resourceId"`;
1926
1946
  const orderByStatement = `ORDER BY "createdAt" DESC`;
1927
1947
  const messages = [];
1928
1948
  try {
@@ -1966,7 +1986,8 @@ var MemoryPG = class extends MemoryStorage {
1966
1986
  const dataQuery = `${selectStatement} FROM ${tableName} ${whereClause} ${excludeIds.length ? `AND id NOT IN (${excludeIdsParam})` : ""}${orderByStatement} LIMIT $${paramIndex++} OFFSET $${paramIndex++}`;
1967
1987
  const rows = await this.client.manyOrNone(dataQuery, [...queryParams, ...excludeIds, perPage, currentOffset]);
1968
1988
  messages.push(...rows || []);
1969
- const messagesWithParsedContent = messages.map((message) => {
1989
+ const messagesWithParsedContent = messages.map((row) => {
1990
+ const message = this.normalizeMessageRow(row);
1970
1991
  if (typeof message.content === "string") {
1971
1992
  try {
1972
1993
  return { ...message, content: JSON.parse(message.content) };
@@ -2111,7 +2132,7 @@ var MemoryPG = class extends MemoryStorage {
2111
2132
  return [];
2112
2133
  }
2113
2134
  const messageIds = messages.map((m) => m.id);
2114
- const selectQuery = `SELECT id, content, role, type, "createdAt", thread_id AS "threadId", "resourceId" FROM ${getTableName({ indexName: TABLE_MESSAGES, schemaName: getSchemaName(this.schema) })} WHERE id IN ($1:list)`;
2135
+ const selectQuery = `SELECT id, content, role, type, "createdAt", "createdAtZ", thread_id AS "threadId", "resourceId" FROM ${getTableName({ indexName: TABLE_MESSAGES, schemaName: getSchemaName(this.schema) })} WHERE id IN ($1:list)`;
2115
2136
  const existingMessagesDb = await this.client.manyOrNone(selectQuery, [messageIds]);
2116
2137
  if (existingMessagesDb.length === 0) {
2117
2138
  return [];
@@ -2186,10 +2207,11 @@ var MemoryPG = class extends MemoryStorage {
2186
2207
  }
2187
2208
  });
2188
2209
  const updatedMessages = await this.client.manyOrNone(selectQuery, [messageIds]);
2189
- return (updatedMessages || []).map((message) => {
2210
+ return (updatedMessages || []).map((row) => {
2211
+ const message = this.normalizeMessageRow(row);
2190
2212
  if (typeof message.content === "string") {
2191
2213
  try {
2192
- message.content = JSON.parse(message.content);
2214
+ return { ...message, content: JSON.parse(message.content) };
2193
2215
  } catch {
2194
2216
  }
2195
2217
  }
@@ -2835,11 +2857,13 @@ var StoreOperationsPG = class extends StoreOperations {
2835
2857
  * Set up timestamp triggers for a table to automatically manage createdAt/updatedAt
2836
2858
  */
2837
2859
  async setupTimestampTriggers(tableName) {
2838
- const fullTableName = getTableName({ indexName: tableName, schemaName: getSchemaName(this.schemaName) });
2860
+ const schemaName = getSchemaName(this.schemaName);
2861
+ const fullTableName = getTableName({ indexName: tableName, schemaName });
2862
+ const functionName = `${schemaName}.trigger_set_timestamps`;
2839
2863
  try {
2840
2864
  const triggerSQL = `
2841
- -- Create or replace the trigger function
2842
- CREATE OR REPLACE FUNCTION trigger_set_timestamps()
2865
+ -- Create or replace the trigger function in the schema
2866
+ CREATE OR REPLACE FUNCTION ${functionName}()
2843
2867
  RETURNS TRIGGER AS $$
2844
2868
  BEGIN
2845
2869
  IF TG_OP = 'INSERT' THEN
@@ -2865,7 +2889,7 @@ var StoreOperationsPG = class extends StoreOperations {
2865
2889
  CREATE TRIGGER ${tableName}_timestamps
2866
2890
  BEFORE INSERT OR UPDATE ON ${fullTableName}
2867
2891
  FOR EACH ROW
2868
- EXECUTE FUNCTION trigger_set_timestamps();
2892
+ EXECUTE FUNCTION ${functionName}();
2869
2893
  `;
2870
2894
  await this.client.none(triggerSQL);
2871
2895
  this.logger?.debug?.(`Set up timestamp triggers for table ${fullTableName}`);