@mastra/dynamodb 0.14.0-alpha.0 → 0.14.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.cjs CHANGED
@@ -1123,6 +1123,20 @@ var MemoryStorageDynamoDB = class extends storage.MemoryStorage {
1123
1123
  // transformed by the ElectroDB entity getters.
1124
1124
  };
1125
1125
  }
1126
+ // Helper function to transform and sort threads
1127
+ transformAndSortThreads(rawThreads, orderBy, sortDirection) {
1128
+ return rawThreads.map((data) => ({
1129
+ ...data,
1130
+ // Convert date strings back to Date objects for consistency
1131
+ createdAt: typeof data.createdAt === "string" ? new Date(data.createdAt) : data.createdAt,
1132
+ updatedAt: typeof data.updatedAt === "string" ? new Date(data.updatedAt) : data.updatedAt
1133
+ })).sort((a, b) => {
1134
+ const fieldA = orderBy === "createdAt" ? a.createdAt : a.updatedAt;
1135
+ const fieldB = orderBy === "createdAt" ? b.createdAt : b.updatedAt;
1136
+ const comparison = fieldA.getTime() - fieldB.getTime();
1137
+ return sortDirection === "DESC" ? -comparison : comparison;
1138
+ });
1139
+ }
1126
1140
  async getThreadById({ threadId }) {
1127
1141
  this.logger.debug("Getting thread by ID", { threadId });
1128
1142
  try {
@@ -1151,21 +1165,20 @@ var MemoryStorageDynamoDB = class extends storage.MemoryStorage {
1151
1165
  );
1152
1166
  }
1153
1167
  }
1154
- async getThreadsByResourceId({ resourceId }) {
1155
- this.logger.debug("Getting threads by resource ID", { resourceId });
1168
+ /**
1169
+ * @deprecated use getThreadsByResourceIdPaginated instead for paginated results.
1170
+ */
1171
+ async getThreadsByResourceId(args) {
1172
+ const resourceId = args.resourceId;
1173
+ const orderBy = this.castThreadOrderBy(args.orderBy);
1174
+ const sortDirection = this.castThreadSortDirection(args.sortDirection);
1175
+ this.logger.debug("Getting threads by resource ID", { resourceId, orderBy, sortDirection });
1156
1176
  try {
1157
1177
  const result = await this.service.entities.thread.query.byResource({ entity: "thread", resourceId }).go();
1158
1178
  if (!result.data.length) {
1159
1179
  return [];
1160
1180
  }
1161
- return result.data.map((data) => ({
1162
- ...data,
1163
- // Convert date strings back to Date objects for consistency
1164
- createdAt: typeof data.createdAt === "string" ? new Date(data.createdAt) : data.createdAt,
1165
- updatedAt: typeof data.updatedAt === "string" ? new Date(data.updatedAt) : data.updatedAt
1166
- // metadata: data.metadata ? JSON.parse(data.metadata) : undefined, // REMOVED by AI
1167
- // metadata is already transformed by the entity's getter
1168
- }));
1181
+ return this.transformAndSortThreads(result.data, orderBy, sortDirection);
1169
1182
  } catch (error$1) {
1170
1183
  throw new error.MastraError(
1171
1184
  {
@@ -1422,11 +1435,19 @@ var MemoryStorageDynamoDB = class extends storage.MemoryStorage {
1422
1435
  }
1423
1436
  async getThreadsByResourceIdPaginated(args) {
1424
1437
  const { resourceId, page = 0, perPage = 100 } = args;
1425
- this.logger.debug("Getting threads by resource ID with pagination", { resourceId, page, perPage });
1438
+ const orderBy = this.castThreadOrderBy(args.orderBy);
1439
+ const sortDirection = this.castThreadSortDirection(args.sortDirection);
1440
+ this.logger.debug("Getting threads by resource ID with pagination", {
1441
+ resourceId,
1442
+ page,
1443
+ perPage,
1444
+ orderBy,
1445
+ sortDirection
1446
+ });
1426
1447
  try {
1427
1448
  const query = this.service.entities.thread.query.byResource({ entity: "thread", resourceId });
1428
1449
  const results = await query.go();
1429
- const allThreads = results.data;
1450
+ const allThreads = this.transformAndSortThreads(results.data, orderBy, sortDirection);
1430
1451
  const startIndex = page * perPage;
1431
1452
  const endIndex = startIndex + perPage;
1432
1453
  const paginatedThreads = allThreads.slice(startIndex, endIndex);
@@ -2891,8 +2912,8 @@ var DynamoDBStore = class extends storage.MastraStorage {
2891
2912
  async getThreadById({ threadId }) {
2892
2913
  return this.stores.memory.getThreadById({ threadId });
2893
2914
  }
2894
- async getThreadsByResourceId({ resourceId }) {
2895
- return this.stores.memory.getThreadsByResourceId({ resourceId });
2915
+ async getThreadsByResourceId(args) {
2916
+ return this.stores.memory.getThreadsByResourceId(args);
2896
2917
  }
2897
2918
  async saveThread({ thread }) {
2898
2919
  return this.stores.memory.saveThread({ thread });