@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/CHANGELOG.md +11 -0
- package/dist/index.cjs +49 -25
- package/dist/index.cjs.map +1 -1
- package/dist/index.js +49 -25
- package/dist/index.js.map +1 -1
- package/dist/storage/domains/memory/index.d.ts +4 -0
- package/dist/storage/domains/memory/index.d.ts.map +1 -1
- package/dist/storage/domains/operations/index.d.ts.map +1 -1
- package/dist/storage/test-utils.d.ts.map +1 -1
- package/package.json +3 -3
package/CHANGELOG.md
CHANGED
|
@@ -1,5 +1,16 @@
|
|
|
1
1
|
# @mastra/pg
|
|
2
2
|
|
|
3
|
+
## 0.17.5-alpha.0
|
|
4
|
+
|
|
5
|
+
### Patch Changes
|
|
6
|
+
|
|
7
|
+
- Use the tz version of the timestamp column in when fetching messages. ([#8944](https://github.com/mastra-ai/mastra/pull/8944))
|
|
8
|
+
|
|
9
|
+
- Avoid conflicts in pg fn naming when creating new tables on storage.init() ([#8946](https://github.com/mastra-ai/mastra/pull/8946))
|
|
10
|
+
|
|
11
|
+
- Updated dependencies [[`c67ca32`](https://github.com/mastra-ai/mastra/commit/c67ca32e3c2cf69bfc146580770c720220ca44ac), [`dbc9e12`](https://github.com/mastra-ai/mastra/commit/dbc9e1216ba575ba59ead4afb727a01215f7de4f), [`99e41b9`](https://github.com/mastra-ai/mastra/commit/99e41b94957cdd25137d3ac12e94e8b21aa01b68), [`c28833c`](https://github.com/mastra-ai/mastra/commit/c28833c5b6d8e10eeffd7f7d39129d53b8bca240), [`f053e89`](https://github.com/mastra-ai/mastra/commit/f053e89160dbd0bd3333fc3492f68231b5c7c349), [`9a1a485`](https://github.com/mastra-ai/mastra/commit/9a1a4859b855e37239f652bf14b1ecd1029b8c4e), [`9257233`](https://github.com/mastra-ai/mastra/commit/9257233c4ffce09b2bedc2a9adbd70d7a83fa8e2), [`0f1a4c9`](https://github.com/mastra-ai/mastra/commit/0f1a4c984fb4b104b2f0b63ba18c9fa77f567700), [`2db6160`](https://github.com/mastra-ai/mastra/commit/2db6160e2022ff8827c15d30157e684683b934b5), [`8aeea37`](https://github.com/mastra-ai/mastra/commit/8aeea37efdde347c635a67fed56794943b7f74ec), [`02fe153`](https://github.com/mastra-ai/mastra/commit/02fe15351d6021d214da48ec982a0e9e4150bcee), [`74567b3`](https://github.com/mastra-ai/mastra/commit/74567b3d237ae3915cd0bca3cf55fa0a64e4e4a4), [`15a1733`](https://github.com/mastra-ai/mastra/commit/15a1733074cee8bd37370e1af34cd818e89fa7ac), [`fc2a774`](https://github.com/mastra-ai/mastra/commit/fc2a77468981aaddc3e77f83f0c4ad4a4af140da), [`4e08933`](https://github.com/mastra-ai/mastra/commit/4e08933625464dfde178347af5b6278fcf34188e)]:
|
|
12
|
+
- @mastra/core@0.21.2-alpha.0
|
|
13
|
+
|
|
3
14
|
## 0.17.4
|
|
4
15
|
|
|
5
16
|
### Patch Changes
|
package/dist/index.cjs
CHANGED
|
@@ -1529,6 +1529,20 @@ var MemoryPG = class extends storage.MemoryStorage {
|
|
|
1529
1529
|
this.schema = schema;
|
|
1530
1530
|
this.operations = operations;
|
|
1531
1531
|
}
|
|
1532
|
+
/**
|
|
1533
|
+
* Normalizes message row from database by applying createdAtZ fallback
|
|
1534
|
+
*/
|
|
1535
|
+
normalizeMessageRow(row) {
|
|
1536
|
+
return {
|
|
1537
|
+
id: row.id,
|
|
1538
|
+
content: row.content,
|
|
1539
|
+
role: row.role,
|
|
1540
|
+
type: row.type,
|
|
1541
|
+
createdAt: row.createdAtZ || row.createdAt,
|
|
1542
|
+
threadId: row.threadId,
|
|
1543
|
+
resourceId: row.resourceId
|
|
1544
|
+
};
|
|
1545
|
+
}
|
|
1532
1546
|
async getThreadById({ threadId }) {
|
|
1533
1547
|
try {
|
|
1534
1548
|
const tableName = getTableName({ indexName: storage.TABLE_THREADS, schemaName: getSchemaName(this.schema) });
|
|
@@ -1796,11 +1810,12 @@ var MemoryPG = class extends storage.MemoryStorage {
|
|
|
1796
1810
|
WHERE thread_id = $${paramIdx}
|
|
1797
1811
|
)
|
|
1798
1812
|
SELECT
|
|
1799
|
-
m.id,
|
|
1800
|
-
m.content,
|
|
1801
|
-
m.role,
|
|
1813
|
+
m.id,
|
|
1814
|
+
m.content,
|
|
1815
|
+
m.role,
|
|
1802
1816
|
m.type,
|
|
1803
|
-
m."createdAt",
|
|
1817
|
+
m."createdAt",
|
|
1818
|
+
m."createdAtZ",
|
|
1804
1819
|
m.thread_id AS "threadId",
|
|
1805
1820
|
m."resourceId"
|
|
1806
1821
|
FROM ordered_messages m
|
|
@@ -1834,24 +1849,25 @@ var MemoryPG = class extends storage.MemoryStorage {
|
|
|
1834
1849
|
return dedupedRows;
|
|
1835
1850
|
}
|
|
1836
1851
|
parseRow(row) {
|
|
1837
|
-
|
|
1852
|
+
const normalized = this.normalizeMessageRow(row);
|
|
1853
|
+
let content = normalized.content;
|
|
1838
1854
|
try {
|
|
1839
|
-
content = JSON.parse(
|
|
1855
|
+
content = JSON.parse(normalized.content);
|
|
1840
1856
|
} catch {
|
|
1841
1857
|
}
|
|
1842
1858
|
return {
|
|
1843
|
-
id:
|
|
1859
|
+
id: normalized.id,
|
|
1844
1860
|
content,
|
|
1845
|
-
role:
|
|
1846
|
-
createdAt: new Date(
|
|
1847
|
-
threadId:
|
|
1848
|
-
resourceId:
|
|
1849
|
-
...
|
|
1861
|
+
role: normalized.role,
|
|
1862
|
+
createdAt: new Date(normalized.createdAt),
|
|
1863
|
+
threadId: normalized.threadId,
|
|
1864
|
+
resourceId: normalized.resourceId,
|
|
1865
|
+
...normalized.type && normalized.type !== "v2" ? { type: normalized.type } : {}
|
|
1850
1866
|
};
|
|
1851
1867
|
}
|
|
1852
1868
|
async getMessages(args) {
|
|
1853
1869
|
const { threadId, resourceId, format, selectBy } = args;
|
|
1854
|
-
const selectStatement = `SELECT id, content, role, type, "createdAt", thread_id AS "threadId", "resourceId"`;
|
|
1870
|
+
const selectStatement = `SELECT id, content, role, type, "createdAt", "createdAtZ", thread_id AS "threadId", "resourceId"`;
|
|
1855
1871
|
const orderByStatement = `ORDER BY "createdAt" DESC`;
|
|
1856
1872
|
const limit = storage.resolveMessageLimit({ last: selectBy?.last, defaultLimit: 40 });
|
|
1857
1873
|
try {
|
|
@@ -1875,7 +1891,8 @@ var MemoryPG = class extends storage.MemoryStorage {
|
|
|
1875
1891
|
const queryParams = [threadId, ...excludeIds, limit];
|
|
1876
1892
|
const remainingRows = await this.client.manyOrNone(query, queryParams);
|
|
1877
1893
|
rows.push(...remainingRows);
|
|
1878
|
-
const fetchedMessages = (rows || []).map((
|
|
1894
|
+
const fetchedMessages = (rows || []).map((row) => {
|
|
1895
|
+
const message = this.normalizeMessageRow(row);
|
|
1879
1896
|
if (typeof message.content === "string") {
|
|
1880
1897
|
try {
|
|
1881
1898
|
message.content = JSON.parse(message.content);
|
|
@@ -1914,7 +1931,7 @@ var MemoryPG = class extends storage.MemoryStorage {
|
|
|
1914
1931
|
format
|
|
1915
1932
|
}) {
|
|
1916
1933
|
if (messageIds.length === 0) return [];
|
|
1917
|
-
const selectStatement = `SELECT id, content, role, type, "createdAt", thread_id AS "threadId", "resourceId"`;
|
|
1934
|
+
const selectStatement = `SELECT id, content, role, type, "createdAt", "createdAtZ", thread_id AS "threadId", "resourceId"`;
|
|
1918
1935
|
try {
|
|
1919
1936
|
const tableName = getTableName({ indexName: storage.TABLE_MESSAGES, schemaName: getSchemaName(this.schema) });
|
|
1920
1937
|
const query = `
|
|
@@ -1923,7 +1940,10 @@ var MemoryPG = class extends storage.MemoryStorage {
|
|
|
1923
1940
|
ORDER BY "createdAt" DESC
|
|
1924
1941
|
`;
|
|
1925
1942
|
const resultRows = await this.client.manyOrNone(query, messageIds);
|
|
1926
|
-
const list = new agent.MessageList().add(
|
|
1943
|
+
const list = new agent.MessageList().add(
|
|
1944
|
+
resultRows.map((row) => this.parseRow(row)),
|
|
1945
|
+
"memory"
|
|
1946
|
+
);
|
|
1927
1947
|
if (format === `v1`) return list.get.all.v1();
|
|
1928
1948
|
return list.get.all.v2();
|
|
1929
1949
|
} catch (error$1) {
|
|
@@ -1948,7 +1968,7 @@ var MemoryPG = class extends storage.MemoryStorage {
|
|
|
1948
1968
|
const { page = 0, perPage: perPageInput, dateRange } = selectBy?.pagination || {};
|
|
1949
1969
|
const fromDate = dateRange?.start;
|
|
1950
1970
|
const toDate = dateRange?.end;
|
|
1951
|
-
const selectStatement = `SELECT id, content, role, type, "createdAt", thread_id AS "threadId", "resourceId"`;
|
|
1971
|
+
const selectStatement = `SELECT id, content, role, type, "createdAt", "createdAtZ", thread_id AS "threadId", "resourceId"`;
|
|
1952
1972
|
const orderByStatement = `ORDER BY "createdAt" DESC`;
|
|
1953
1973
|
const messages = [];
|
|
1954
1974
|
try {
|
|
@@ -1992,7 +2012,8 @@ var MemoryPG = class extends storage.MemoryStorage {
|
|
|
1992
2012
|
const dataQuery = `${selectStatement} FROM ${tableName} ${whereClause} ${excludeIds.length ? `AND id NOT IN (${excludeIdsParam})` : ""}${orderByStatement} LIMIT $${paramIndex++} OFFSET $${paramIndex++}`;
|
|
1993
2013
|
const rows = await this.client.manyOrNone(dataQuery, [...queryParams, ...excludeIds, perPage, currentOffset]);
|
|
1994
2014
|
messages.push(...rows || []);
|
|
1995
|
-
const messagesWithParsedContent = messages.map((
|
|
2015
|
+
const messagesWithParsedContent = messages.map((row) => {
|
|
2016
|
+
const message = this.normalizeMessageRow(row);
|
|
1996
2017
|
if (typeof message.content === "string") {
|
|
1997
2018
|
try {
|
|
1998
2019
|
return { ...message, content: JSON.parse(message.content) };
|
|
@@ -2137,7 +2158,7 @@ var MemoryPG = class extends storage.MemoryStorage {
|
|
|
2137
2158
|
return [];
|
|
2138
2159
|
}
|
|
2139
2160
|
const messageIds = messages.map((m) => m.id);
|
|
2140
|
-
const selectQuery = `SELECT id, content, role, type, "createdAt", thread_id AS "threadId", "resourceId" FROM ${getTableName({ indexName: storage.TABLE_MESSAGES, schemaName: getSchemaName(this.schema) })} WHERE id IN ($1:list)`;
|
|
2161
|
+
const selectQuery = `SELECT id, content, role, type, "createdAt", "createdAtZ", thread_id AS "threadId", "resourceId" FROM ${getTableName({ indexName: storage.TABLE_MESSAGES, schemaName: getSchemaName(this.schema) })} WHERE id IN ($1:list)`;
|
|
2141
2162
|
const existingMessagesDb = await this.client.manyOrNone(selectQuery, [messageIds]);
|
|
2142
2163
|
if (existingMessagesDb.length === 0) {
|
|
2143
2164
|
return [];
|
|
@@ -2212,10 +2233,11 @@ var MemoryPG = class extends storage.MemoryStorage {
|
|
|
2212
2233
|
}
|
|
2213
2234
|
});
|
|
2214
2235
|
const updatedMessages = await this.client.manyOrNone(selectQuery, [messageIds]);
|
|
2215
|
-
return (updatedMessages || []).map((
|
|
2236
|
+
return (updatedMessages || []).map((row) => {
|
|
2237
|
+
const message = this.normalizeMessageRow(row);
|
|
2216
2238
|
if (typeof message.content === "string") {
|
|
2217
2239
|
try {
|
|
2218
|
-
message
|
|
2240
|
+
return { ...message, content: JSON.parse(message.content) };
|
|
2219
2241
|
} catch {
|
|
2220
2242
|
}
|
|
2221
2243
|
}
|
|
@@ -2861,11 +2883,13 @@ var StoreOperationsPG = class extends storage.StoreOperations {
|
|
|
2861
2883
|
* Set up timestamp triggers for a table to automatically manage createdAt/updatedAt
|
|
2862
2884
|
*/
|
|
2863
2885
|
async setupTimestampTriggers(tableName) {
|
|
2864
|
-
const
|
|
2886
|
+
const schemaName = getSchemaName(this.schemaName);
|
|
2887
|
+
const fullTableName = getTableName({ indexName: tableName, schemaName });
|
|
2888
|
+
const functionName = `${schemaName}.trigger_set_timestamps`;
|
|
2865
2889
|
try {
|
|
2866
2890
|
const triggerSQL = `
|
|
2867
|
-
-- Create or replace the trigger function
|
|
2868
|
-
CREATE OR REPLACE FUNCTION
|
|
2891
|
+
-- Create or replace the trigger function in the schema
|
|
2892
|
+
CREATE OR REPLACE FUNCTION ${functionName}()
|
|
2869
2893
|
RETURNS TRIGGER AS $$
|
|
2870
2894
|
BEGIN
|
|
2871
2895
|
IF TG_OP = 'INSERT' THEN
|
|
@@ -2891,7 +2915,7 @@ var StoreOperationsPG = class extends storage.StoreOperations {
|
|
|
2891
2915
|
CREATE TRIGGER ${tableName}_timestamps
|
|
2892
2916
|
BEFORE INSERT OR UPDATE ON ${fullTableName}
|
|
2893
2917
|
FOR EACH ROW
|
|
2894
|
-
EXECUTE FUNCTION
|
|
2918
|
+
EXECUTE FUNCTION ${functionName}();
|
|
2895
2919
|
`;
|
|
2896
2920
|
await this.client.none(triggerSQL);
|
|
2897
2921
|
this.logger?.debug?.(`Set up timestamp triggers for table ${fullTableName}`);
|