Guest User

Untitled

a guest
May 21st, 2025
19
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
  1. const { numItems, cursor: cursorString } = paginationOpts;
  2. const numItemsToFetch = numItems + 1;
  3.  
  4. let query = ctx.db.query("products");
  5.  
  6. const sortFieldMapping = {
  7.   "by_price": "currentPrice",
  8.   "by_popularity": "likes",
  9.   "by_date": "updateTime" // Corrected to match the schema field name
  10. };
  11. const sortFieldJsName = sortFieldMapping[sort.field];
  12.  
  13. if (cursorString) {
  14.   try {
  15.     const parsedCursor = JSON.parse(cursorString) as { cursorValue: any; cursorId: Id<"products"> };
  16.     const { cursorValue, cursorId } = parsedCursor;
  17.  
  18.     if (cursorValue === undefined || !cursorId) {
  19.       // Log and proceed without cursor if it's malformed
  20.       console.warn("Invalid cursor structure received:", cursorString);
  21.     } else {
  22.       query = query.filter(q => {
  23.         // The type assertion `as any` is used because sortFieldJsName is dynamic.
  24.         // Ensure `cursorValue` type matches the field's type.
  25.         const docSortValue = q.field(sortFieldJsName as any);
  26.         const docId = q.field("_id");
  27.  
  28.         if (sort.direction === "asc") {
  29.           return q.or(
  30.             q.gt(docSortValue, cursorValue),
  31.             q.and(q.eq(docSortValue, cursorValue), q.gt(docId, cursorId))
  32.           );
  33.         } else { // desc
  34.           return q.or(
  35.             q.lt(docSortValue, cursorValue),
  36.             q.and(q.eq(docSortValue, cursorValue), q.lt(docId, cursorId))
  37.           );
  38.         }
  39.       });
  40.     }
  41.   } catch (e) {
  42.     console.error("Failed to parse cursor string, fetching first page:", cursorString, e);
  43.     // Invalid cursor string, effectively ignore it and fetch the first page.
  44.   }
  45. }
  46.  
  47. // dbProducts will hold raw product data from the database query
  48. const dbProducts: any[] = await query.take(numItemsToFetch);
  49.  
  50. const hasMore = dbProducts.length > numItems;
  51. const pageProducts = hasMore ? dbProducts.slice(0, numItems) : dbProducts;
  52.  
  53. let newContinueCursor: string | null = null;
  54. if (hasMore) {
  55.   const lastProductOnPage = pageProducts[pageProducts.length - 1];
  56.   if (lastProductOnPage) {
  57.     // Accessing field dynamically. Ensure Product type has these fields or handle potential undefined.
  58.     const sortVal = (lastProductOnPage as any)[sortFieldJsName];
  59.     if (sortVal === undefined) {
  60.       console.warn(`Sort field '${sortFieldJsName}' is undefined for product '${lastProductOnPage._id}'. Cursor may be incorrect.`);
  61.     }
  62.     newContinueCursor = JSON.stringify({
  63.       cursorValue: sortVal,
  64.       cursorId: lastProductOnPage._id
  65.     });
  66.   }
  67. }
  68.  
  69.  
  70. return {
  71.   page: pageProducts,
  72.   isDone: !hasMore,
  73.   continueCursor: newContinueCursor || "",
  74. };
  75.  
Advertisement
Add Comment
Please, Sign In to add comment