btg: paragraphJson fix
This commit is contained in:
+21
-11
@@ -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}`);
|
||||
|
||||
Reference in New Issue
Block a user