@mastra/cloudflare-d1 0.11.1-alpha.1 → 0.11.1-alpha.2

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.
@@ -169,6 +169,12 @@ declare class D1Store extends MastraStorage {
169
169
  tableName: TABLE_NAMES;
170
170
  records: Record<string, any>[];
171
171
  }): Promise<void>;
172
+ /**
173
+ * Upsert multiple records in a batch operation
174
+ * @param tableName The table to insert into
175
+ * @param records The records to insert
176
+ */
177
+ private batchUpsert;
172
178
  /**
173
179
  * @deprecated use getTracesPaginated instead
174
180
  */
@@ -169,6 +169,12 @@ declare class D1Store extends MastraStorage {
169
169
  tableName: TABLE_NAMES;
170
170
  records: Record<string, any>[];
171
171
  }): Promise<void>;
172
+ /**
173
+ * Upsert multiple records in a batch operation
174
+ * @param tableName The table to insert into
175
+ * @param records The records to insert
176
+ */
177
+ private batchUpsert;
172
178
  /**
173
179
  * @deprecated use getTracesPaginated instead
174
180
  */
package/dist/index.cjs CHANGED
@@ -862,7 +862,7 @@ var D1Store = class extends storage.MastraStorage {
862
862
  };
863
863
  });
864
864
  await Promise.all([
865
- this.batchInsert({
865
+ this.batchUpsert({
866
866
  tableName: storage.TABLE_MESSAGES,
867
867
  records: messagesToInsert
868
868
  }),
@@ -1172,6 +1172,61 @@ var D1Store = class extends storage.MastraStorage {
1172
1172
  );
1173
1173
  }
1174
1174
  }
1175
+ /**
1176
+ * Upsert multiple records in a batch operation
1177
+ * @param tableName The table to insert into
1178
+ * @param records The records to insert
1179
+ */
1180
+ async batchUpsert({
1181
+ tableName,
1182
+ records
1183
+ }) {
1184
+ if (records.length === 0) return;
1185
+ const fullTableName = this.getTableName(tableName);
1186
+ try {
1187
+ const batchSize = 50;
1188
+ for (let i = 0; i < records.length; i += batchSize) {
1189
+ const batch = records.slice(i, i + batchSize);
1190
+ const recordsToInsert = batch;
1191
+ if (recordsToInsert.length > 0) {
1192
+ const firstRecord = recordsToInsert[0];
1193
+ const columns = Object.keys(firstRecord || {});
1194
+ for (const record of recordsToInsert) {
1195
+ const values = columns.map((col) => {
1196
+ if (!record) return null;
1197
+ const value = typeof col === "string" ? record[col] : null;
1198
+ return this.serializeValue(value);
1199
+ });
1200
+ const recordToUpsert = columns.reduce(
1201
+ (acc, col) => {
1202
+ if (col !== "createdAt") acc[col] = `excluded.${col}`;
1203
+ return acc;
1204
+ },
1205
+ {}
1206
+ );
1207
+ const query = createSqlBuilder().insert(fullTableName, columns, values, ["id"], recordToUpsert);
1208
+ const { sql, params } = query.build();
1209
+ await this.executeQuery({ sql, params });
1210
+ }
1211
+ }
1212
+ this.logger.debug(
1213
+ `Processed batch ${Math.floor(i / batchSize) + 1} of ${Math.ceil(records.length / batchSize)}`
1214
+ );
1215
+ }
1216
+ this.logger.debug(`Successfully batch upserted ${records.length} records into ${tableName}`);
1217
+ } catch (error$1) {
1218
+ throw new error.MastraError(
1219
+ {
1220
+ id: "CLOUDFLARE_D1_STORAGE_BATCH_UPSERT_ERROR",
1221
+ domain: error.ErrorDomain.STORAGE,
1222
+ category: error.ErrorCategory.THIRD_PARTY,
1223
+ text: `Failed to batch upsert into ${tableName}: ${error$1 instanceof Error ? error$1.message : String(error$1)}`,
1224
+ details: { tableName }
1225
+ },
1226
+ error$1
1227
+ );
1228
+ }
1229
+ }
1175
1230
  /**
1176
1231
  * @deprecated use getTracesPaginated instead
1177
1232
  */
package/dist/index.js CHANGED
@@ -856,7 +856,7 @@ var D1Store = class extends MastraStorage {
856
856
  };
857
857
  });
858
858
  await Promise.all([
859
- this.batchInsert({
859
+ this.batchUpsert({
860
860
  tableName: TABLE_MESSAGES,
861
861
  records: messagesToInsert
862
862
  }),
@@ -1166,6 +1166,61 @@ var D1Store = class extends MastraStorage {
1166
1166
  );
1167
1167
  }
1168
1168
  }
1169
+ /**
1170
+ * Upsert multiple records in a batch operation
1171
+ * @param tableName The table to insert into
1172
+ * @param records The records to insert
1173
+ */
1174
+ async batchUpsert({
1175
+ tableName,
1176
+ records
1177
+ }) {
1178
+ if (records.length === 0) return;
1179
+ const fullTableName = this.getTableName(tableName);
1180
+ try {
1181
+ const batchSize = 50;
1182
+ for (let i = 0; i < records.length; i += batchSize) {
1183
+ const batch = records.slice(i, i + batchSize);
1184
+ const recordsToInsert = batch;
1185
+ if (recordsToInsert.length > 0) {
1186
+ const firstRecord = recordsToInsert[0];
1187
+ const columns = Object.keys(firstRecord || {});
1188
+ for (const record of recordsToInsert) {
1189
+ const values = columns.map((col) => {
1190
+ if (!record) return null;
1191
+ const value = typeof col === "string" ? record[col] : null;
1192
+ return this.serializeValue(value);
1193
+ });
1194
+ const recordToUpsert = columns.reduce(
1195
+ (acc, col) => {
1196
+ if (col !== "createdAt") acc[col] = `excluded.${col}`;
1197
+ return acc;
1198
+ },
1199
+ {}
1200
+ );
1201
+ const query = createSqlBuilder().insert(fullTableName, columns, values, ["id"], recordToUpsert);
1202
+ const { sql, params } = query.build();
1203
+ await this.executeQuery({ sql, params });
1204
+ }
1205
+ }
1206
+ this.logger.debug(
1207
+ `Processed batch ${Math.floor(i / batchSize) + 1} of ${Math.ceil(records.length / batchSize)}`
1208
+ );
1209
+ }
1210
+ this.logger.debug(`Successfully batch upserted ${records.length} records into ${tableName}`);
1211
+ } catch (error) {
1212
+ throw new MastraError(
1213
+ {
1214
+ id: "CLOUDFLARE_D1_STORAGE_BATCH_UPSERT_ERROR",
1215
+ domain: ErrorDomain.STORAGE,
1216
+ category: ErrorCategory.THIRD_PARTY,
1217
+ text: `Failed to batch upsert into ${tableName}: ${error instanceof Error ? error.message : String(error)}`,
1218
+ details: { tableName }
1219
+ },
1220
+ error
1221
+ );
1222
+ }
1223
+ }
1169
1224
  /**
1170
1225
  * @deprecated use getTracesPaginated instead
1171
1226
  */
package/package.json CHANGED
@@ -1,6 +1,6 @@
1
1
  {
2
2
  "name": "@mastra/cloudflare-d1",
3
- "version": "0.11.1-alpha.1",
3
+ "version": "0.11.1-alpha.2",
4
4
  "description": "D1 provider for Mastra - includes db storage capabilities",
5
5
  "type": "module",
6
6
  "files": [
@@ -25,18 +25,18 @@
25
25
  "cloudflare": "^4.3.0"
26
26
  },
27
27
  "devDependencies": {
28
- "@cloudflare/workers-types": "^4.20250610.0",
28
+ "@cloudflare/workers-types": "^4.20250620.0",
29
29
  "@microsoft/api-extractor": "^7.52.8",
30
30
  "@types/node": "^20.19.0",
31
31
  "dotenv": "^16.5.0",
32
- "eslint": "^9.28.0",
32
+ "eslint": "^9.29.0",
33
33
  "miniflare": "^4.20250604.1",
34
34
  "tsup": "^8.5.0",
35
35
  "typescript": "^5.8.3",
36
36
  "vitest": "^3.2.3",
37
- "@internal/storage-test-utils": "0.0.9",
38
37
  "@internal/lint": "0.0.13",
39
- "@mastra/core": "0.10.7-alpha.1"
38
+ "@mastra/core": "0.10.7-alpha.2",
39
+ "@internal/storage-test-utils": "0.0.9"
40
40
  },
41
41
  "peerDependencies": {
42
42
  "@mastra/core": ">=0.10.4-0 <0.11.0"