btg: paragraphJson fix

This commit is contained in:
gosangam
2026-05-04 00:30:13 +05:30
parent e55ab47b66
commit 004ff6ca2f
3 changed files with 30 additions and 34 deletions
+21 -11
View File
@@ -107,25 +107,35 @@ async function seedOneMagazine(
const rawParas = (issue.paragraphs as string[]) || [];
const cleanParas = rawParas.map((p: string) => sanitizeString(String(p)));
// Store paragraphs as a single JSON string to avoid MongoDB BSON
// wire protocol issues with large string arrays.
// The data layer will JSON.parse() this back into an array on read.
const paragraphsJson = JSON.stringify(cleanParas);
const doc = {
// Insert the base document without paragraphs first, then $set paragraphs.
// This avoids the MongoDB BSON wire protocol "Not null terminated string"
// error that occurs with large string arrays in insertOne/replaceOne.
const baseDoc = {
book: bookCode,
volume: issue.volume,
part: issue.part,
title: issue.title,
slug: issue.slug,
order: issue.order,
paragraphsJson,
paragraphs: [] as string[], // Keep empty array for schema compat
paragraphs: [] as string[],
};
await issuesCol.replaceOne({ book: bookCode, slug: issue.slug }, doc, {
upsert: true,
});
await issuesCol.replaceOne(
{ book: bookCode, slug: issue.slug as string },
baseDoc,
{ upsert: true },
);
// Now set paragraphs in batches using $push to avoid BSON limits
const BATCH_SIZE = 200;
for (let bi = 0; bi < cleanParas.length; bi += BATCH_SIZE) {
const batch = cleanParas.slice(bi, bi + BATCH_SIZE);
await issuesCol.updateOne(
{ book: bookCode, slug: issue.slug as string },
{ $push: { paragraphs: { $each: batch } } },
);
}
inserted++;
}
console.log(` 📄 Issues: ${inserted}`);