@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.
package/dist/index.js CHANGED
@@ -250,31 +250,29 @@ function buildElemMatchConditions(value, paramIndex) {
250
250
  if (typeof value !== "object" || Array.isArray(value)) throw new Error("$elemMatch requires an object with conditions");
251
251
  const conditions = [];
252
252
  const values = [];
253
+ function buildFieldCondition(field, operator, operatorValue) {
254
+ if (operator === "$not") {
255
+ const nested = Object.entries(operatorValue);
256
+ if (nested.length === 0) throw new Error("$not requires at least one operator");
257
+ return `NOT (COALESCE(${nested.map(([nestedOp, nestedValue]) => buildFieldCondition(field, nestedOp, nestedValue)).join(" AND ")}, FALSE))`;
258
+ }
259
+ const operatorFn = FILTER_OPERATORS[operator];
260
+ if (!operatorFn) throw new Error(`Invalid operator: ${operator}`);
261
+ const scalarElement = field.startsWith("$");
262
+ const result = operatorFn(scalarElement ? "" : field, paramIndex + values.length, operatorValue);
263
+ const sql = result.sql.replaceAll("metadata->>", "elem->>").replaceAll("metadata->", "elem->").replaceAll("metadata#>>", "elem#>>").replaceAll("metadata#>", "elem#>").replaceAll("metadata ?", "elem ?");
264
+ const elementSql = scalarElement ? sql.replaceAll("elem->>''", "elem #>> '{}'").replaceAll("elem->''", "elem") : sql;
265
+ if (result.needsValue) {
266
+ const transformedValue = result.transformValue ? result.transformValue() : operatorValue;
267
+ if (operator === "$elemMatch") values.push(...transformedValue);
268
+ else values.push(transformedValue);
269
+ }
270
+ return elementSql;
271
+ }
253
272
  Object.entries(value).forEach(([field, val]) => {
254
- const nextParamIndex = paramIndex + values.length;
255
- let paramOperator;
256
- let paramKey;
257
- let paramValue;
258
- if (field.startsWith("$")) {
259
- paramOperator = field;
260
- paramKey = "";
261
- paramValue = val;
262
- } else if (typeof val === "object" && !Array.isArray(val)) {
263
- const [op, opValue] = Object.entries(val || {})[0] || [];
264
- paramOperator = op;
265
- paramKey = field;
266
- paramValue = opValue;
267
- } else {
268
- paramOperator = "$eq";
269
- paramKey = field;
270
- paramValue = val;
271
- }
272
- const operatorFn = FILTER_OPERATORS[paramOperator];
273
- if (!operatorFn) throw new Error(`Invalid operator: ${paramOperator}`);
274
- const result = operatorFn(paramKey, nextParamIndex, paramValue);
275
- const sql = result.sql.replaceAll("metadata->>", "elem->>").replaceAll("metadata->", "elem->").replaceAll("metadata#>>", "elem#>>").replaceAll("metadata#>", "elem#>");
276
- conditions.push(sql);
277
- if (result.needsValue) values.push(paramValue);
273
+ const operators = field.startsWith("$") ? [[field, val]] : typeof val === "object" && val !== null && !Array.isArray(val) ? Object.entries(val) : [["$eq", val]];
274
+ if (operators.length === 0) throw new Error(`$elemMatch field ${field} requires at least one operator`);
275
+ for (const [operator, operatorValue] of operators) conditions.push(buildFieldCondition(field, operator, operatorValue));
278
276
  });
279
277
  return {
280
278
  sql: conditions.join(" AND "),
@@ -3405,12 +3403,16 @@ var PgDB = class extends MastraBase {
3405
3403
  }
3406
3404
  if (snapshot) {
3407
3405
  snapshot.tables.add(tableName);
3408
- if (isNewTable) snapshot.createdTables.add(tableName);
3409
- const created = this.snapshotColumns(snapshot, tableName);
3410
- for (const [name, def] of Object.entries(schema)) {
3411
- const parsedName = parseSqlIdentifier(name, "column name");
3412
- created.add(parsedName);
3413
- if (def.type === "timestamp") created.add(`${parsedName}Z`);
3406
+ if (isNewTable) {
3407
+ snapshot.createdTables.add(tableName);
3408
+ snapshot.columns.set(tableName, new Set(await this.getTableColumns(tableName)));
3409
+ } else {
3410
+ const created = this.snapshotColumns(snapshot, tableName);
3411
+ for (const [name, def] of Object.entries(schema)) {
3412
+ const parsedName = parseSqlIdentifier(name, "column name");
3413
+ created.add(parsedName);
3414
+ if (def.type === "timestamp") created.add(`${parsedName}Z`);
3415
+ }
3414
3416
  }
3415
3417
  }
3416
3418
  }
@@ -11799,15 +11801,25 @@ var MemoryPG = class MemoryPG extends MemoryStorage {
11799
11801
  * @param schemaPrefix - Prefix for index names (e.g. "my_schema_" or "")
11800
11802
  */
11801
11803
  static getDefaultIndexDefs(schemaPrefix) {
11802
- return [{
11803
- name: `${schemaPrefix}mastra_threads_resourceid_createdat_idx`,
11804
- table: TABLE_THREADS,
11805
- columns: ["resourceId", "createdAt DESC"]
11806
- }, {
11807
- name: `${schemaPrefix}mastra_messages_thread_id_createdat_idx`,
11808
- table: TABLE_MESSAGES,
11809
- columns: ["thread_id", "createdAt DESC"]
11810
- }];
11804
+ return [
11805
+ {
11806
+ name: `${schemaPrefix}mastra_threads_resourceid_createdat_idx`,
11807
+ table: TABLE_THREADS,
11808
+ columns: ["resourceId", "createdAt DESC"]
11809
+ },
11810
+ {
11811
+ name: `${schemaPrefix}mastra_threads_metadata_gin_idx`,
11812
+ table: TABLE_THREADS,
11813
+ columns: ["metadata"],
11814
+ method: "gin",
11815
+ opclass: "jsonb_path_ops"
11816
+ },
11817
+ {
11818
+ name: `${schemaPrefix}mastra_messages_thread_id_createdat_idx`,
11819
+ table: TABLE_MESSAGES,
11820
+ columns: ["thread_id", "createdAt DESC"]
11821
+ }
11822
+ ];
11811
11823
  }
11812
11824
  /**
11813
11825
  * Returns all DDL statements for this domain: tables (threads, messages, resources, OM), indexes.
@@ -21926,6 +21938,7 @@ async function dangerouslyClearTracing(client, schema) {
21926
21938
  const spanQueryFeatures = typeof coreStorage.planSpanQuery === "function" ? [
21927
21939
  "span-query",
21928
21940
  "span-query-context-fields",
21941
+ ...coreStorage.SPAN_QUERY_ORDER_FIELDS?.includes("durationMs") ? ["span-query-duration-sort"] : [],
21929
21942
  ..."model" in (coreStorage.SPAN_QUERY_FIELD_REGISTRY ?? {}) ? ["span-query-model-fields"] : []
21930
21943
  ] : [];
21931
21944
  const spanAggregateFeatures = typeof coreStorage.planSpanAggregate === "function" ? ["span-aggregate", ..."model" in (coreStorage.SPAN_AGGREGATE_DIMENSION_REGISTRY ?? {}) ? ["span-aggregate-model-dimensions"] : []] : [];
@@ -22997,6 +23010,8 @@ function rowToSchedule(row) {
22997
23010
  if (metadata !== void 0) schedule.metadata = metadata;
22998
23011
  if (row.owner_type != null) schedule.ownerType = String(row.owner_type);
22999
23012
  if (row.owner_id != null) schedule.ownerId = String(row.owner_id);
23013
+ if (row.run_at != null) schedule.runAt = Number(row.run_at);
23014
+ if (row.end_at != null) schedule.endAt = Number(row.end_at);
23000
23015
  return schedule;
23001
23016
  }
23002
23017
  function rowToTrigger(row) {
@@ -23056,6 +23071,11 @@ var SchedulesPG = class SchedulesPG extends SchedulesStorage {
23056
23071
  tableName: TABLE_SCHEDULES,
23057
23072
  schema: TABLE_SCHEMAS[TABLE_SCHEDULES]
23058
23073
  });
23074
+ await this.#db.alterTable({
23075
+ tableName: TABLE_SCHEDULES,
23076
+ schema: TABLE_SCHEMAS[TABLE_SCHEDULES],
23077
+ ifNotExists: ["run_at", "end_at"]
23078
+ });
23059
23079
  await this.#db.createTable({
23060
23080
  tableName: TABLE_SCHEDULE_TRIGGERS,
23061
23081
  schema: TABLE_SCHEMAS[TABLE_SCHEDULE_TRIGGERS]
@@ -23183,7 +23203,9 @@ var SchedulesPG = class SchedulesPG extends SchedulesStorage {
23183
23203
  updated_at: schedule.updatedAt,
23184
23204
  metadata: schedule.metadata ?? null,
23185
23205
  owner_type: schedule.ownerType ?? null,
23186
- owner_id: schedule.ownerId ?? null
23206
+ owner_id: schedule.ownerId ?? null,
23207
+ run_at: schedule.runAt ?? null,
23208
+ end_at: schedule.endAt ?? null
23187
23209
  }
23188
23210
  });
23189
23211
  return schedule;
@@ -23253,6 +23275,8 @@ var SchedulesPG = class SchedulesPG extends SchedulesStorage {
23253
23275
  if ("metadata" in patch) push("metadata = ?::jsonb", patch.metadata != null ? toPgJson(patch.metadata) : null);
23254
23276
  if ("ownerType" in patch) push("owner_type = ?", patch.ownerType ?? null);
23255
23277
  if ("ownerId" in patch) push("owner_id = ?", patch.ownerId ?? null);
23278
+ if ("runAt" in patch) push("run_at = ?", patch.runAt ?? null);
23279
+ if ("endAt" in patch) push("end_at = ?", patch.endAt ?? null);
23256
23280
  push("updated_at = ?", Date.now());
23257
23281
  if (setClauses.length === 1) {
23258
23282
  const existing = await this.#getSchedule(this.#client, id);