btg: paragraphJson fix
This commit is contained in:
+1
-14
@@ -208,26 +208,13 @@ export async function getMagazineIssue(
|
|||||||
where: { book_slug: { book: bookCode, slug } },
|
where: { book_slug: { book: bookCode, slug } },
|
||||||
});
|
});
|
||||||
if (doc) {
|
if (doc) {
|
||||||
// Support both paragraphsJson (string) and paragraphs (array) formats.
|
|
||||||
// The seed script stores paragraphs as a JSON string to avoid
|
|
||||||
// MongoDB BSON wire protocol issues with large string arrays.
|
|
||||||
let paras: string[];
|
|
||||||
if (doc.paragraphsJson) {
|
|
||||||
try {
|
|
||||||
paras = JSON.parse(doc.paragraphsJson);
|
|
||||||
} catch {
|
|
||||||
paras = [];
|
|
||||||
}
|
|
||||||
} else {
|
|
||||||
paras = normalizeParagraphs(doc.paragraphs);
|
|
||||||
}
|
|
||||||
return {
|
return {
|
||||||
book: doc.book,
|
book: doc.book,
|
||||||
volume: doc.volume,
|
volume: doc.volume,
|
||||||
part: doc.part,
|
part: doc.part,
|
||||||
title: doc.title,
|
title: doc.title,
|
||||||
slug: doc.slug,
|
slug: doc.slug,
|
||||||
paragraphs: paras,
|
paragraphs: normalizeParagraphs(doc.paragraphs),
|
||||||
};
|
};
|
||||||
}
|
}
|
||||||
} catch {
|
} catch {
|
||||||
|
|||||||
@@ -224,15 +224,14 @@ model MagazineMeta {
|
|||||||
// paragraphs: array of HTML paragraph strings (all articles concatenated)
|
// paragraphs: array of HTML paragraph strings (all articles concatenated)
|
||||||
// ---------------------------------------------------------------------------
|
// ---------------------------------------------------------------------------
|
||||||
model MagazineIssue {
|
model MagazineIssue {
|
||||||
id String @id @default(auto()) @map("_id") @db.ObjectId
|
id String @id @default(auto()) @map("_id") @db.ObjectId
|
||||||
book String
|
book String
|
||||||
volume Int
|
volume Int
|
||||||
part Int
|
part Int
|
||||||
title String
|
title String
|
||||||
slug String
|
slug String
|
||||||
order Int
|
order Int
|
||||||
paragraphs String[]
|
paragraphs String[]
|
||||||
paragraphsJson String?
|
|
||||||
|
|
||||||
@@unique([book, slug])
|
@@unique([book, slug])
|
||||||
@@index([book])
|
@@index([book])
|
||||||
|
|||||||
+21
-11
@@ -107,25 +107,35 @@ async function seedOneMagazine(
|
|||||||
const rawParas = (issue.paragraphs as string[]) || [];
|
const rawParas = (issue.paragraphs as string[]) || [];
|
||||||
const cleanParas = rawParas.map((p: string) => sanitizeString(String(p)));
|
const cleanParas = rawParas.map((p: string) => sanitizeString(String(p)));
|
||||||
|
|
||||||
// Store paragraphs as a single JSON string to avoid MongoDB BSON
|
// Insert the base document without paragraphs first, then $set paragraphs.
|
||||||
// wire protocol issues with large string arrays.
|
// This avoids the MongoDB BSON wire protocol "Not null terminated string"
|
||||||
// The data layer will JSON.parse() this back into an array on read.
|
// error that occurs with large string arrays in insertOne/replaceOne.
|
||||||
const paragraphsJson = JSON.stringify(cleanParas);
|
const baseDoc = {
|
||||||
|
|
||||||
const doc = {
|
|
||||||
book: bookCode,
|
book: bookCode,
|
||||||
volume: issue.volume,
|
volume: issue.volume,
|
||||||
part: issue.part,
|
part: issue.part,
|
||||||
title: issue.title,
|
title: issue.title,
|
||||||
slug: issue.slug,
|
slug: issue.slug,
|
||||||
order: issue.order,
|
order: issue.order,
|
||||||
paragraphsJson,
|
paragraphs: [] as string[],
|
||||||
paragraphs: [] as string[], // Keep empty array for schema compat
|
|
||||||
};
|
};
|
||||||
|
|
||||||
await issuesCol.replaceOne({ book: bookCode, slug: issue.slug }, doc, {
|
await issuesCol.replaceOne(
|
||||||
upsert: true,
|
{ 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++;
|
inserted++;
|
||||||
}
|
}
|
||||||
console.log(` 📄 Issues: ${inserted}`);
|
console.log(` 📄 Issues: ${inserted}`);
|
||||||
|
|||||||
Reference in New Issue
Block a user