@@ -98,32 +98,49 @@ jobs:
9898 }
9999
100100 (async () => {
101- // Pass 1: Create stubs so all internal document references resolve
102- const stubs = sanitized.map((d) => ({ createOrReplace: { _id: d._id, _type: d._type } }));
103- for (let i = 0; i < stubs.length; i += 250) {
104- await mutateBatch(stubs.slice(i, i + 250));
101+ // Helper to chunk mutations by both count and byte size (< 1.5MB, well below Sanity's 4MB limit)
102+ async function sendChunked(items, label) {
103+ let batch = [];
104+ let batchBytes = 0;
105+ let sent = 0;
106+ const MAX_ITEMS = 50;
107+ const MAX_BYTES = 1500000;
108+
109+ for (const item of items) {
110+ const mutation = { createOrReplace: item };
111+ const itemBytes = Buffer.byteLength(JSON.stringify(mutation), "utf8");
112+ if (batch.length > 0 && (batch.length >= MAX_ITEMS || batchBytes + itemBytes > MAX_BYTES)) {
113+ await mutateBatch(batch);
114+ sent += batch.length;
115+ if (sent % 500 < batch.length) {
116+ console.log(`[${label}] Imported ${sent} / ${items.length}...`);
117+ }
118+ batch = [];
119+ batchBytes = 0;
120+ }
121+ batch.push(mutation);
122+ batchBytes += itemBytes;
123+ }
124+ if (batch.length > 0) {
125+ await mutateBatch(batch);
126+ sent += batch.length;
127+ }
128+ console.log(`[${label}] Completed (${sent} / ${items.length}).`);
105129 }
106- console.log("Pass 1 (document stubs) complete.");
130+
131+ // Pass 1: Create stubs so all internal document references resolve
132+ const stubs = sanitized.map((d) => ({ _id: d._id, _type: d._type }));
133+ await sendChunked(stubs, "Pass 1: Stubs");
107134
108135 // Pass 2: Populate full document bodies
109- for (let i = 0; i < sanitized.length; i += 100) {
110- const batch = sanitized.slice(i, i + 100).map((d) => ({ createOrReplace: d }));
111- await mutateBatch(batch);
112- if ((i / 100) % 5 === 0) {
113- console.log(`Imported ${Math.min(i + 100, sanitized.length)} / ${sanitized.length} documents...`);
114- }
115- }
136+ await sendChunked(sanitized, "Pass 2: Content Docs");
116137
117- // Pass 3 (best-effort): Import asset metadata documents
138+ // Pass 3 (best-effort): Import asset metadata documents so Studio Media Library resolves in dev
118139 const assetDocs = allDocs
119140 .filter((d) => d._type === "sanity.imageAsset" || d._type === "sanity.fileAsset")
120141 .map(({ _rev, ...rest }) => rest);
121142 try {
122- for (let i = 0; i < assetDocs.length; i += 250) {
123- const batch = assetDocs.slice(i, i + 250).map((d) => ({ createOrReplace: d }));
124- await mutateBatch(batch);
125- }
126- console.log(`Imported ${assetDocs.length} asset metadata documents.`);
143+ await sendChunked(assetDocs, "Pass 3: Asset Docs");
127144 } catch (assetErr) {
128145 console.warn("Note: Asset metadata docs skipped (served via CDN):", assetErr.message);
129146 }
0 commit comments