@mastra/pg 1.36.0-alpha.3 → 1.36.0-alpha.4

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.
@@ -3,7 +3,7 @@ name: mastra-pg
3
3
  description: Documentation for @mastra/pg. Use when working with @mastra/pg APIs, configuration, or implementation.
4
4
  metadata:
5
5
  package: "@mastra/pg"
6
- version: "1.36.0-alpha.3"
6
+ version: "1.36.0-alpha.4"
7
7
  ---
8
8
 
9
9
  ## When to use
@@ -1,161 +1,161 @@
1
1
  {
2
- "version": "1.36.0-alpha.3",
2
+ "version": "1.36.0-alpha.4",
3
3
  "package": "@mastra/pg",
4
4
  "exports": {
5
5
  "AgentsPG": {
6
6
  "types": "dist/storage/domains/agents/index.d.ts",
7
7
  "implementation": "dist/index.js",
8
- "line": 4371
8
+ "line": 4373
9
9
  },
10
10
  "BackgroundTasksPG": {
11
11
  "types": "dist/storage/domains/background-tasks/index.d.ts",
12
12
  "implementation": "dist/index.js",
13
- "line": 5302
13
+ "line": 5304
14
14
  },
15
15
  "BlobsPG": {
16
16
  "types": "dist/storage/domains/blobs/index.d.ts",
17
17
  "implementation": "dist/index.js",
18
- "line": 5693
18
+ "line": 5695
19
19
  },
20
20
  "ChannelsPG": {
21
21
  "types": "dist/storage/domains/channels/index.d.ts",
22
22
  "implementation": "dist/index.js",
23
- "line": 5795
23
+ "line": 5797
24
24
  },
25
25
  "DatasetsPG": {
26
26
  "types": "dist/storage/domains/datasets/index.d.ts",
27
27
  "implementation": "dist/index.js",
28
- "line": 6037
28
+ "line": 6039
29
29
  },
30
30
  "ExperimentsPG": {
31
31
  "types": "dist/storage/domains/experiments/index.d.ts",
32
32
  "implementation": "dist/index.js",
33
- "line": 7294
33
+ "line": 7296
34
34
  },
35
35
  "FavoritesPG": {
36
36
  "types": "dist/storage/domains/favorites/index.d.ts",
37
37
  "implementation": "dist/index.js",
38
- "line": 8266
38
+ "line": 8268
39
39
  },
40
40
  "KnowledgePG": {
41
41
  "types": "dist/storage/domains/knowledge/index.d.ts",
42
42
  "implementation": "dist/index.js",
43
- "line": 10144
43
+ "line": 10146
44
44
  },
45
45
  "MCPClientsPG": {
46
46
  "types": "dist/storage/domains/mcp-clients/index.d.ts",
47
47
  "implementation": "dist/index.js",
48
- "line": 10301
48
+ "line": 10303
49
49
  },
50
50
  "MCPServersPG": {
51
51
  "types": "dist/storage/domains/mcp-servers/index.d.ts",
52
52
  "implementation": "dist/index.js",
53
- "line": 10870
53
+ "line": 10872
54
54
  },
55
55
  "MemoryPG": {
56
56
  "types": "dist/storage/domains/memory/index.d.ts",
57
57
  "implementation": "dist/index.js",
58
- "line": 11665
58
+ "line": 11667
59
59
  },
60
60
  "NotificationsPG": {
61
61
  "types": "dist/storage/domains/notifications/index.d.ts",
62
62
  "implementation": "dist/index.js",
63
- "line": 14562
63
+ "line": 14574
64
64
  },
65
65
  "ObservabilityPG": {
66
66
  "types": "dist/storage/domains/observability/index.d.ts",
67
67
  "implementation": "dist/index.js",
68
- "line": 14984
68
+ "line": 14996
69
69
  },
70
70
  "ObservabilityStoragePostgresVNext": {
71
71
  "types": "dist/storage/domains/observability/v-next/index.d.ts",
72
72
  "implementation": "dist/index.js",
73
- "line": 21941
73
+ "line": 21954
74
74
  },
75
75
  "PGVECTOR_PROMPT": {
76
76
  "types": "dist/vector/prompt.d.ts",
77
77
  "implementation": "dist/index.js",
78
- "line": 28617
78
+ "line": 28641
79
79
  },
80
80
  "PgFactoryStorage": {
81
81
  "types": "dist/storage/factory-storage.d.ts",
82
82
  "implementation": "dist/index.js",
83
- "line": 28103
83
+ "line": 28127
84
84
  },
85
85
  "PgVector": {
86
86
  "types": "dist/vector/index.d.ts",
87
87
  "implementation": "dist/index.js",
88
- "line": 593
88
+ "line": 591
89
89
  },
90
90
  "PoolAdapter": {
91
91
  "types": "dist/storage/client.d.ts",
92
92
  "implementation": "dist/index.js",
93
- "line": 2069
93
+ "line": 2067
94
94
  },
95
95
  "PostgresStore": {
96
96
  "types": "dist/storage/index.d.ts",
97
97
  "implementation": "dist/index.js",
98
- "line": 28318
98
+ "line": 28342
99
99
  },
100
100
  "PostgresStoreVNext": {
101
101
  "types": "dist/storage/index.d.ts",
102
102
  "implementation": "dist/index.js",
103
- "line": 28536
103
+ "line": 28560
104
104
  },
105
105
  "PromptBlocksPG": {
106
106
  "types": "dist/storage/domains/prompt-blocks/index.d.ts",
107
107
  "implementation": "dist/index.js",
108
- "line": 22391
108
+ "line": 22404
109
109
  },
110
110
  "SchedulesPG": {
111
111
  "types": "dist/storage/domains/schedules/index.d.ts",
112
112
  "implementation": "dist/index.js",
113
- "line": 23018
113
+ "line": 23033
114
114
  },
115
115
  "ScorerDefinitionsPG": {
116
116
  "types": "dist/storage/domains/scorer-definitions/index.d.ts",
117
117
  "implementation": "dist/index.js",
118
- "line": 23339
118
+ "line": 23363
119
119
  },
120
120
  "ScoresPG": {
121
121
  "types": "dist/storage/domains/scores/index.d.ts",
122
122
  "implementation": "dist/index.js",
123
- "line": 23962
123
+ "line": 23986
124
124
  },
125
125
  "SkillsPG": {
126
126
  "types": "dist/storage/domains/skills/index.d.ts",
127
127
  "implementation": "dist/index.js",
128
- "line": 24405
128
+ "line": 24429
129
129
  },
130
130
  "ThreadStatePG": {
131
131
  "types": "dist/storage/domains/thread-state/index.d.ts",
132
132
  "implementation": "dist/index.js",
133
- "line": 25096
133
+ "line": 25120
134
134
  },
135
135
  "ToolProviderConnectionsPG": {
136
136
  "types": "dist/storage/domains/tool-provider-connections/index.d.ts",
137
137
  "implementation": "dist/index.js",
138
- "line": 25270
138
+ "line": 25294
139
139
  },
140
140
  "WorkflowDefinitionsPG": {
141
141
  "types": "dist/storage/domains/workflow-definitions/index.d.ts",
142
142
  "implementation": "dist/index.js",
143
- "line": 25534
143
+ "line": 25558
144
144
  },
145
145
  "WorkflowsPG": {
146
146
  "types": "dist/storage/domains/workflows/index.d.ts",
147
147
  "implementation": "dist/index.js",
148
- "line": 25917
148
+ "line": 25941
149
149
  },
150
150
  "WorkspacesPG": {
151
151
  "types": "dist/storage/domains/workspaces/index.d.ts",
152
152
  "implementation": "dist/index.js",
153
- "line": 27203
153
+ "line": 27227
154
154
  },
155
155
  "exportSchemas": {
156
156
  "types": "dist/storage/index.d.ts",
157
157
  "implementation": "dist/index.js",
158
- "line": 28285
158
+ "line": 28309
159
159
  }
160
160
  },
161
161
  "modules": {}
package/dist/index.cjs CHANGED
@@ -273,31 +273,29 @@ function buildElemMatchConditions(value, paramIndex) {
273
273
  if (typeof value !== "object" || Array.isArray(value)) throw new Error("$elemMatch requires an object with conditions");
274
274
  const conditions = [];
275
275
  const values = [];
276
+ function buildFieldCondition(field, operator, operatorValue) {
277
+ if (operator === "$not") {
278
+ const nested = Object.entries(operatorValue);
279
+ if (nested.length === 0) throw new Error("$not requires at least one operator");
280
+ return `NOT (COALESCE(${nested.map(([nestedOp, nestedValue]) => buildFieldCondition(field, nestedOp, nestedValue)).join(" AND ")}, FALSE))`;
281
+ }
282
+ const operatorFn = FILTER_OPERATORS[operator];
283
+ if (!operatorFn) throw new Error(`Invalid operator: ${operator}`);
284
+ const scalarElement = field.startsWith("$");
285
+ const result = operatorFn(scalarElement ? "" : field, paramIndex + values.length, operatorValue);
286
+ const sql = result.sql.replaceAll("metadata->>", "elem->>").replaceAll("metadata->", "elem->").replaceAll("metadata#>>", "elem#>>").replaceAll("metadata#>", "elem#>").replaceAll("metadata ?", "elem ?");
287
+ const elementSql = scalarElement ? sql.replaceAll("elem->>''", "elem #>> '{}'").replaceAll("elem->''", "elem") : sql;
288
+ if (result.needsValue) {
289
+ const transformedValue = result.transformValue ? result.transformValue() : operatorValue;
290
+ if (operator === "$elemMatch") values.push(...transformedValue);
291
+ else values.push(transformedValue);
292
+ }
293
+ return elementSql;
294
+ }
276
295
  Object.entries(value).forEach(([field, val]) => {
277
- const nextParamIndex = paramIndex + values.length;
278
- let paramOperator;
279
- let paramKey;
280
- let paramValue;
281
- if (field.startsWith("$")) {
282
- paramOperator = field;
283
- paramKey = "";
284
- paramValue = val;
285
- } else if (typeof val === "object" && !Array.isArray(val)) {
286
- const [op, opValue] = Object.entries(val || {})[0] || [];
287
- paramOperator = op;
288
- paramKey = field;
289
- paramValue = opValue;
290
- } else {
291
- paramOperator = "$eq";
292
- paramKey = field;
293
- paramValue = val;
294
- }
295
- const operatorFn = FILTER_OPERATORS[paramOperator];
296
- if (!operatorFn) throw new Error(`Invalid operator: ${paramOperator}`);
297
- const result = operatorFn(paramKey, nextParamIndex, paramValue);
298
- const sql = result.sql.replaceAll("metadata->>", "elem->>").replaceAll("metadata->", "elem->").replaceAll("metadata#>>", "elem#>>").replaceAll("metadata#>", "elem#>");
299
- conditions.push(sql);
300
- if (result.needsValue) values.push(paramValue);
296
+ const operators = field.startsWith("$") ? [[field, val]] : typeof val === "object" && val !== null && !Array.isArray(val) ? Object.entries(val) : [["$eq", val]];
297
+ if (operators.length === 0) throw new Error(`$elemMatch field ${field} requires at least one operator`);
298
+ for (const [operator, operatorValue] of operators) conditions.push(buildFieldCondition(field, operator, operatorValue));
301
299
  });
302
300
  return {
303
301
  sql: conditions.join(" AND "),
@@ -3428,12 +3426,16 @@ var PgDB = class extends _mastra_core_base.MastraBase {
3428
3426
  }
3429
3427
  if (snapshot) {
3430
3428
  snapshot.tables.add(tableName);
3431
- if (isNewTable) snapshot.createdTables.add(tableName);
3432
- const created = this.snapshotColumns(snapshot, tableName);
3433
- for (const [name, def] of Object.entries(schema)) {
3434
- const parsedName = (0, _mastra_core_utils.parseSqlIdentifier)(name, "column name");
3435
- created.add(parsedName);
3436
- if (def.type === "timestamp") created.add(`${parsedName}Z`);
3429
+ if (isNewTable) {
3430
+ snapshot.createdTables.add(tableName);
3431
+ snapshot.columns.set(tableName, new Set(await this.getTableColumns(tableName)));
3432
+ } else {
3433
+ const created = this.snapshotColumns(snapshot, tableName);
3434
+ for (const [name, def] of Object.entries(schema)) {
3435
+ const parsedName = (0, _mastra_core_utils.parseSqlIdentifier)(name, "column name");
3436
+ created.add(parsedName);
3437
+ if (def.type === "timestamp") created.add(`${parsedName}Z`);
3438
+ }
3437
3439
  }
3438
3440
  }
3439
3441
  }
@@ -11822,15 +11824,25 @@ var MemoryPG = class MemoryPG extends _mastra_core_storage.MemoryStorage {
11822
11824
  * @param schemaPrefix - Prefix for index names (e.g. "my_schema_" or "")
11823
11825
  */
11824
11826
  static getDefaultIndexDefs(schemaPrefix) {
11825
- return [{
11826
- name: `${schemaPrefix}mastra_threads_resourceid_createdat_idx`,
11827
- table: _mastra_core_storage.TABLE_THREADS,
11828
- columns: ["resourceId", "createdAt DESC"]
11829
- }, {
11830
- name: `${schemaPrefix}mastra_messages_thread_id_createdat_idx`,
11831
- table: _mastra_core_storage.TABLE_MESSAGES,
11832
- columns: ["thread_id", "createdAt DESC"]
11833
- }];
11827
+ return [
11828
+ {
11829
+ name: `${schemaPrefix}mastra_threads_resourceid_createdat_idx`,
11830
+ table: _mastra_core_storage.TABLE_THREADS,
11831
+ columns: ["resourceId", "createdAt DESC"]
11832
+ },
11833
+ {
11834
+ name: `${schemaPrefix}mastra_threads_metadata_gin_idx`,
11835
+ table: _mastra_core_storage.TABLE_THREADS,
11836
+ columns: ["metadata"],
11837
+ method: "gin",
11838
+ opclass: "jsonb_path_ops"
11839
+ },
11840
+ {
11841
+ name: `${schemaPrefix}mastra_messages_thread_id_createdat_idx`,
11842
+ table: _mastra_core_storage.TABLE_MESSAGES,
11843
+ columns: ["thread_id", "createdAt DESC"]
11844
+ }
11845
+ ];
11834
11846
  }
11835
11847
  /**
11836
11848
  * Returns all DDL statements for this domain: tables (threads, messages, resources, OM), indexes.
@@ -21949,6 +21961,7 @@ async function dangerouslyClearTracing(client, schema) {
21949
21961
  const spanQueryFeatures = typeof _mastra_core_storage.planSpanQuery === "function" ? [
21950
21962
  "span-query",
21951
21963
  "span-query-context-fields",
21964
+ ..._mastra_core_storage.SPAN_QUERY_ORDER_FIELDS?.includes("durationMs") ? ["span-query-duration-sort"] : [],
21952
21965
  ..."model" in (_mastra_core_storage.SPAN_QUERY_FIELD_REGISTRY ?? {}) ? ["span-query-model-fields"] : []
21953
21966
  ] : [];
21954
21967
  const spanAggregateFeatures = typeof _mastra_core_storage.planSpanAggregate === "function" ? ["span-aggregate", ..."model" in (_mastra_core_storage.SPAN_AGGREGATE_DIMENSION_REGISTRY ?? {}) ? ["span-aggregate-model-dimensions"] : []] : [];
@@ -23020,6 +23033,8 @@ function rowToSchedule(row) {
23020
23033
  if (metadata !== void 0) schedule.metadata = metadata;
23021
23034
  if (row.owner_type != null) schedule.ownerType = String(row.owner_type);
23022
23035
  if (row.owner_id != null) schedule.ownerId = String(row.owner_id);
23036
+ if (row.run_at != null) schedule.runAt = Number(row.run_at);
23037
+ if (row.end_at != null) schedule.endAt = Number(row.end_at);
23023
23038
  return schedule;
23024
23039
  }
23025
23040
  function rowToTrigger(row) {
@@ -23079,6 +23094,11 @@ var SchedulesPG = class SchedulesPG extends _mastra_core_storage.SchedulesStorag
23079
23094
  tableName: _mastra_core_storage.TABLE_SCHEDULES,
23080
23095
  schema: _mastra_core_storage.TABLE_SCHEMAS[_mastra_core_storage.TABLE_SCHEDULES]
23081
23096
  });
23097
+ await this.#db.alterTable({
23098
+ tableName: _mastra_core_storage.TABLE_SCHEDULES,
23099
+ schema: _mastra_core_storage.TABLE_SCHEMAS[_mastra_core_storage.TABLE_SCHEDULES],
23100
+ ifNotExists: ["run_at", "end_at"]
23101
+ });
23082
23102
  await this.#db.createTable({
23083
23103
  tableName: _mastra_core_storage.TABLE_SCHEDULE_TRIGGERS,
23084
23104
  schema: _mastra_core_storage.TABLE_SCHEMAS[_mastra_core_storage.TABLE_SCHEDULE_TRIGGERS]
@@ -23206,7 +23226,9 @@ var SchedulesPG = class SchedulesPG extends _mastra_core_storage.SchedulesStorag
23206
23226
  updated_at: schedule.updatedAt,
23207
23227
  metadata: schedule.metadata ?? null,
23208
23228
  owner_type: schedule.ownerType ?? null,
23209
- owner_id: schedule.ownerId ?? null
23229
+ owner_id: schedule.ownerId ?? null,
23230
+ run_at: schedule.runAt ?? null,
23231
+ end_at: schedule.endAt ?? null
23210
23232
  }
23211
23233
  });
23212
23234
  return schedule;
@@ -23276,6 +23298,8 @@ var SchedulesPG = class SchedulesPG extends _mastra_core_storage.SchedulesStorag
23276
23298
  if ("metadata" in patch) push("metadata = ?::jsonb", patch.metadata != null ? toPgJson(patch.metadata) : null);
23277
23299
  if ("ownerType" in patch) push("owner_type = ?", patch.ownerType ?? null);
23278
23300
  if ("ownerId" in patch) push("owner_id = ?", patch.ownerId ?? null);
23301
+ if ("runAt" in patch) push("run_at = ?", patch.runAt ?? null);
23302
+ if ("endAt" in patch) push("end_at = ?", patch.endAt ?? null);
23279
23303
  push("updated_at = ?", Date.now());
23280
23304
  if (setClauses.length === 1) {
23281
23305
  const existing = await this.#getSchedule(this.#client, id);