@mastra/cloudflare-d1 1.0.2 → 1.0.3

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
@@ -1203,12 +1203,28 @@ var MemoryStorageD1 = class extends MemoryStorage {
1203
1203
  const targetMap = new Map(
1204
1204
  targetResult.map((r) => [r.id, { threadId: r.thread_id, createdAt: r.createdAt }])
1205
1205
  );
1206
+ const MAX_UNION_TERMS = 5;
1206
1207
  const unionQueries = [];
1207
- const params = [];
1208
+ const unionParams = [];
1209
+ const allMessages = [];
1210
+ const flushBatch = async () => {
1211
+ if (unionQueries.length === 0) return;
1212
+ const sql = unionQueries.length === 1 ? unionQueries[0] : `${unionQueries.join(" UNION ALL ")} ORDER BY createdAt ASC, id ASC`;
1213
+ const result = await this.#db.executeQuery({ sql, params: unionParams });
1214
+ if (Array.isArray(result)) {
1215
+ allMessages.push(...result);
1216
+ }
1217
+ unionQueries.length = 0;
1218
+ unionParams.length = 0;
1219
+ };
1208
1220
  for (const inc of include) {
1209
1221
  const { id, withPreviousMessages = 0, withNextMessages = 0 } = inc;
1210
1222
  const target = targetMap.get(id);
1211
1223
  if (!target) continue;
1224
+ const termsNeeded = withNextMessages > 0 ? 2 : 1;
1225
+ if (unionQueries.length + termsNeeded > MAX_UNION_TERMS) {
1226
+ await flushBatch();
1227
+ }
1212
1228
  unionQueries.push(`SELECT * FROM (
1213
1229
  SELECT id, content, role, type, createdAt, thread_id AS threadId, resourceId
1214
1230
  FROM ${tableName}
@@ -1217,7 +1233,7 @@ var MemoryStorageD1 = class extends MemoryStorage {
1217
1233
  ORDER BY createdAt DESC, id DESC
1218
1234
  LIMIT ?
1219
1235
  )`);
1220
- params.push(target.threadId, target.createdAt, withPreviousMessages + 1);
1236
+ unionParams.push(target.threadId, target.createdAt, withPreviousMessages + 1);
1221
1237
  if (withNextMessages > 0) {
1222
1238
  unionQueries.push(`SELECT * FROM (
1223
1239
  SELECT id, content, role, type, createdAt, thread_id AS threadId, resourceId
@@ -1227,22 +1243,12 @@ var MemoryStorageD1 = class extends MemoryStorage {
1227
1243
  ORDER BY createdAt ASC, id ASC
1228
1244
  LIMIT ?
1229
1245
  )`);
1230
- params.push(target.threadId, target.createdAt, withNextMessages);
1246
+ unionParams.push(target.threadId, target.createdAt, withNextMessages);
1231
1247
  }
1232
1248
  }
1233
- if (unionQueries.length === 0) return null;
1234
- let finalQuery;
1235
- if (unionQueries.length === 1) {
1236
- finalQuery = unionQueries[0];
1237
- } else {
1238
- finalQuery = `${unionQueries.join(" UNION ALL ")} ORDER BY createdAt ASC, id ASC`;
1239
- }
1240
- const messages = await this.#db.executeQuery({ sql: finalQuery, params });
1241
- if (!Array.isArray(messages)) {
1242
- return [];
1243
- }
1249
+ await flushBatch();
1244
1250
  const seen = /* @__PURE__ */ new Set();
1245
- const processedMessages = messages.filter((message) => {
1251
+ const processedMessages = allMessages.filter((message) => {
1246
1252
  const id = message.id;
1247
1253
  if (seen.has(id)) return false;
1248
1254
  seen.add(id);