@mastra/pg 1.0.0-beta.13 → 1.0.0-beta.14

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
@@ -3195,27 +3195,52 @@ var MemoryPG = class _MemoryPG extends MemoryStorage {
3195
3195
  );
3196
3196
  }
3197
3197
  }
3198
- async listThreadsByResourceId(args) {
3199
- const { resourceId, page = 0, perPage: perPageInput, orderBy } = args;
3200
- if (page < 0) {
3198
+ async listThreads(args) {
3199
+ const { page = 0, perPage: perPageInput, orderBy, filter } = args;
3200
+ try {
3201
+ this.validatePaginationInput(page, perPageInput ?? 100);
3202
+ } catch (error) {
3201
3203
  throw new MastraError({
3202
- id: createStorageErrorId("PG", "LIST_THREADS_BY_RESOURCE_ID", "INVALID_PAGE"),
3204
+ id: createStorageErrorId("PG", "LIST_THREADS", "INVALID_PAGE"),
3203
3205
  domain: ErrorDomain.STORAGE,
3204
3206
  category: ErrorCategory.USER,
3205
- text: "Page number must be non-negative",
3206
- details: {
3207
- resourceId,
3208
- page
3209
- }
3207
+ text: error instanceof Error ? error.message : "Invalid pagination parameters",
3208
+ details: { page, ...perPageInput !== void 0 && { perPage: perPageInput } }
3210
3209
  });
3211
3210
  }
3212
- const { field, direction } = this.parseOrderBy(orderBy);
3213
3211
  const perPage = normalizePerPage(perPageInput, 100);
3212
+ try {
3213
+ this.validateMetadataKeys(filter?.metadata);
3214
+ } catch (error) {
3215
+ throw new MastraError({
3216
+ id: createStorageErrorId("PG", "LIST_THREADS", "INVALID_METADATA_KEY"),
3217
+ domain: ErrorDomain.STORAGE,
3218
+ category: ErrorCategory.USER,
3219
+ text: error instanceof Error ? error.message : "Invalid metadata key",
3220
+ details: { metadataKeys: filter?.metadata ? Object.keys(filter.metadata).join(", ") : "" }
3221
+ });
3222
+ }
3223
+ const { field, direction } = this.parseOrderBy(orderBy);
3214
3224
  const { offset, perPage: perPageForResponse } = calculatePagination(page, perPageInput, perPage);
3215
3225
  try {
3216
3226
  const tableName = getTableName3({ indexName: TABLE_THREADS, schemaName: getSchemaName3(this.#schema) });
3217
- const baseQuery = `FROM ${tableName} WHERE "resourceId" = $1`;
3218
- const queryParams = [resourceId];
3227
+ const whereClauses = [];
3228
+ const queryParams = [];
3229
+ let paramIndex = 1;
3230
+ if (filter?.resourceId) {
3231
+ whereClauses.push(`"resourceId" = $${paramIndex}`);
3232
+ queryParams.push(filter.resourceId);
3233
+ paramIndex++;
3234
+ }
3235
+ if (filter?.metadata && Object.keys(filter.metadata).length > 0) {
3236
+ for (const [key, value] of Object.entries(filter.metadata)) {
3237
+ whereClauses.push(`metadata::jsonb @> $${paramIndex}::jsonb`);
3238
+ queryParams.push(JSON.stringify({ [key]: value }));
3239
+ paramIndex++;
3240
+ }
3241
+ }
3242
+ const whereClause = whereClauses.length > 0 ? `WHERE ${whereClauses.join(" AND ")}` : "";
3243
+ const baseQuery = `FROM ${tableName} ${whereClause}`;
3219
3244
  const countQuery = `SELECT COUNT(*) ${baseQuery}`;
3220
3245
  const countResult = await this.#db.client.one(countQuery, queryParams);
3221
3246
  const total = parseInt(countResult.count, 10);
@@ -3229,7 +3254,7 @@ var MemoryPG = class _MemoryPG extends MemoryStorage {
3229
3254
  };
3230
3255
  }
3231
3256
  const limitValue = perPageInput === false ? total : perPage;
3232
- const dataQuery = `SELECT id, "resourceId", title, metadata, "createdAt", "createdAtZ", "updatedAt", "updatedAtZ" ${baseQuery} ORDER BY "${field}" ${direction} LIMIT $2 OFFSET $3`;
3257
+ const dataQuery = `SELECT id, "resourceId", title, metadata, "createdAt", "createdAtZ", "updatedAt", "updatedAtZ" ${baseQuery} ORDER BY "${field}" ${direction} LIMIT $${paramIndex} OFFSET $${paramIndex + 1}`;
3233
3258
  const rows = await this.#db.client.manyOrNone(
3234
3259
  dataQuery,
3235
3260
  [...queryParams, limitValue, offset]
@@ -3253,11 +3278,12 @@ var MemoryPG = class _MemoryPG extends MemoryStorage {
3253
3278
  } catch (error) {
3254
3279
  const mastraError = new MastraError(
3255
3280
  {
3256
- id: createStorageErrorId("PG", "LIST_THREADS_BY_RESOURCE_ID", "FAILED"),
3281
+ id: createStorageErrorId("PG", "LIST_THREADS", "FAILED"),
3257
3282
  domain: ErrorDomain.STORAGE,
3258
3283
  category: ErrorCategory.THIRD_PARTY,
3259
3284
  details: {
3260
- resourceId,
3285
+ ...filter?.resourceId && { resourceId: filter.resourceId },
3286
+ hasMetadataFilter: !!filter?.metadata,
3261
3287
  page
3262
3288
  }
3263
3289
  },