@mastra/pg 1.20.0-alpha.0 → 1.20.0-alpha.1

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
@@ -9389,6 +9389,38 @@ var MemoryPG = class MemoryPG extends MemoryStorage {
9389
9389
  throw mastraError;
9390
9390
  }
9391
9391
  }
9392
+ /**
9393
+ * Reads one page of messages together with the total row count.
9394
+ *
9395
+ * `COUNT(*) OVER ()` reports the count over the whole WHERE result on the same
9396
+ * statement as the page, so the page costs one database round-trip instead of
9397
+ * two. The page and the count also come from one snapshot, so the count always
9398
+ * describes the returned rows. A separate `COUNT(*)` runs only when the page is
9399
+ * empty and the caller asked for a page after the last row, because a window
9400
+ * function has no row to carry the count on.
9401
+ */
9402
+ async #fetchMessagePage({ selectStatement, tableName, whereClause, orderByStatement, queryParams, perPageInput, perPage, offset }) {
9403
+ const limitClause = perPageInput === false ? "" : ` LIMIT $${queryParams.length + 1} OFFSET $${queryParams.length + 2}`;
9404
+ const dataParams = perPageInput === false ? queryParams : [
9405
+ ...queryParams,
9406
+ perPage,
9407
+ offset
9408
+ ];
9409
+ const rows = await this.#db.client.manyOrNone(`${selectStatement}, COUNT(*) OVER () AS "__total" FROM ${tableName} ${whereClause} ${orderByStatement}${limitClause}`, dataParams) || [];
9410
+ if (rows.length > 0) return {
9411
+ total: Number(rows[0].__total),
9412
+ messages: rows
9413
+ };
9414
+ if (offset === 0) return {
9415
+ total: 0,
9416
+ messages: []
9417
+ };
9418
+ const countResult = await this.#db.client.one(`SELECT COUNT(*) FROM ${tableName} ${whereClause}`, queryParams);
9419
+ return {
9420
+ total: parseInt(countResult.count, 10),
9421
+ messages: []
9422
+ };
9423
+ }
9392
9424
  async listMessages(args) {
9393
9425
  const { threadId, resourceId, include, filter, perPage: perPageInput, page = 0, orderBy } = args;
9394
9426
  const threadIds = (Array.isArray(threadId) ? threadId : [threadId]).filter((id) => typeof id === "string");
@@ -9463,23 +9495,27 @@ var MemoryPG = class MemoryPG extends MemoryStorage {
9463
9495
  hasMore: false
9464
9496
  };
9465
9497
  }
9498
+ let includeFailure;
9499
+ const includePromise = include && include.length > 0 ? this._getIncludedMessages({ include }).catch((error) => {
9500
+ includeFailure = error;
9501
+ return null;
9502
+ }) : null;
9466
9503
  let total;
9467
9504
  let messages;
9468
9505
  if (metadataFilter) {
9469
9506
  const filteredRows = (await this.#db.client.manyOrNone(`${selectStatement} FROM ${tableName} ${whereClause} ${orderByStatement}`, queryParams) || []).filter((row) => storageMessageMatchesMetadataFilter(row.content, metadataFilter));
9470
9507
  total = filteredRows.length;
9471
9508
  messages = perPageInput === false ? filteredRows : filteredRows.slice(offset, offset + perPage);
9472
- } else {
9473
- const countResult = await this.#db.client.one(`SELECT COUNT(*) FROM ${tableName} ${whereClause}`, queryParams);
9474
- total = parseInt(countResult.count, 10);
9475
- const limitValue = perPageInput === false ? total : perPage;
9476
- const dataQuery = `${selectStatement} FROM ${tableName} ${whereClause} ${orderByStatement} LIMIT $${paramIndex++} OFFSET $${paramIndex++}`;
9477
- messages = [...await this.#db.client.manyOrNone(dataQuery, [
9478
- ...queryParams,
9479
- limitValue,
9480
- offset
9481
- ]) || []];
9482
- }
9509
+ } else ({total, messages} = await this.#fetchMessagePage({
9510
+ selectStatement,
9511
+ tableName,
9512
+ whereClause,
9513
+ orderByStatement,
9514
+ queryParams,
9515
+ perPageInput,
9516
+ perPage,
9517
+ offset
9518
+ }));
9483
9519
  const primaryPageCount = messages.length;
9484
9520
  if (total === 0 && messages.length === 0 && (!include || include.length === 0)) return {
9485
9521
  messages: [],
@@ -9490,7 +9526,8 @@ var MemoryPG = class MemoryPG extends MemoryStorage {
9490
9526
  };
9491
9527
  const messageIds = new Set(messages.map((m) => m.id));
9492
9528
  if (include && include.length > 0) {
9493
- const includeMessages = await this._getIncludedMessages({ include });
9529
+ const includeMessages = await includePromise;
9530
+ if (includeFailure) throw includeFailure;
9494
9531
  if (includeMessages) {
9495
9532
  for (const includeMsg of includeMessages) if (!messageIds.has(includeMsg.id)) {
9496
9533
  messages.push(includeMsg);
@@ -9598,23 +9635,27 @@ var MemoryPG = class MemoryPG extends MemoryStorage {
9598
9635
  hasMore: false
9599
9636
  };
9600
9637
  }
9638
+ let includeFailure;
9639
+ const includePromise = include && include.length > 0 ? this._getIncludedMessages({ include }).catch((error) => {
9640
+ includeFailure = error;
9641
+ return null;
9642
+ }) : null;
9601
9643
  let total;
9602
9644
  let messages;
9603
9645
  if (metadataFilter) {
9604
9646
  const filteredRows = (await this.#db.client.manyOrNone(`${selectStatement} FROM ${tableName} ${whereClause} ${orderByStatement}`, queryParams) || []).filter((row) => storageMessageMatchesMetadataFilter(row.content, metadataFilter));
9605
9647
  total = filteredRows.length;
9606
9648
  messages = perPageInput === false ? filteredRows : filteredRows.slice(offset, offset + perPage);
9607
- } else {
9608
- const countResult = await this.#db.client.one(`SELECT COUNT(*) FROM ${tableName} ${whereClause}`, queryParams);
9609
- total = parseInt(countResult.count, 10);
9610
- const limitValue = perPageInput === false ? total : perPage;
9611
- const dataQuery = `${selectStatement} FROM ${tableName} ${whereClause} ${orderByStatement} LIMIT $${paramIndex++} OFFSET $${paramIndex++}`;
9612
- messages = [...await this.#db.client.manyOrNone(dataQuery, [
9613
- ...queryParams,
9614
- limitValue,
9615
- offset
9616
- ]) || []];
9617
- }
9649
+ } else ({total, messages} = await this.#fetchMessagePage({
9650
+ selectStatement,
9651
+ tableName,
9652
+ whereClause,
9653
+ orderByStatement,
9654
+ queryParams,
9655
+ perPageInput,
9656
+ perPage,
9657
+ offset
9658
+ }));
9618
9659
  if (total === 0 && messages.length === 0 && (!include || include.length === 0)) return {
9619
9660
  messages: [],
9620
9661
  total: 0,
@@ -9624,7 +9665,8 @@ var MemoryPG = class MemoryPG extends MemoryStorage {
9624
9665
  };
9625
9666
  const messageIds = new Set(messages.map((m) => m.id));
9626
9667
  if (include && include.length > 0) {
9627
- const includeMessages = await this._getIncludedMessages({ include });
9668
+ const includeMessages = await includePromise;
9669
+ if (includeFailure) throw includeFailure;
9628
9670
  if (includeMessages) {
9629
9671
  for (const includeMsg of includeMessages) if (!messageIds.has(includeMsg.id)) {
9630
9672
  messages.push(includeMsg);