From 06b903bbdf11ac39f972c0e676362f2fd915e824 Mon Sep 17 00:00:00 2001 From: Daniel Date: Thu, 25 Jul 2024 18:12:09 +0800 Subject: [PATCH] fix(indexer): handle db update when children is null instead of array --- src/Database/Inscriptions/Methods.ts | 22 ++++++++++++++++++++-- 1 file changed, 20 insertions(+), 2 deletions(-) diff --git a/src/Database/Inscriptions/Methods.ts b/src/Database/Inscriptions/Methods.ts index 57b25cf9..f211b516 100644 --- a/src/Database/Inscriptions/Methods.ts +++ b/src/Database/Inscriptions/Methods.ts @@ -53,7 +53,17 @@ async function insertMany(inscriptions: Inscription[], chunkSize = getChunkSize( for (const inscription of chunk) { if (inscription.parents) { for (const parent of inscription.parents) { - promises.push(collection.updateOne({ id: parent }, { $addToSet: { children: inscription.id } })); + promises.push( + collection.updateOne({ id: parent }, [ + { + $set: { + children: { + $ifNull: [{ $concatArrays: ["$children", [inscription.id]] }, [inscription.id]], + }, + }, + }, + ]), + ); } } } @@ -65,7 +75,15 @@ async function insertOne(inscription: Inscription) { // Check for parents in Inscriptions and update children if (inscription.parents) { for (const parent of inscription.parents) { - await collection.updateOne({ id: parent }, { $addToSet: { children: inscription.id } }); + await collection.updateOne({ id: parent }, [ + { + $set: { + children: { + $ifNull: [{ $concatArrays: ["$children", [inscription.id]] }, [inscription.id]], + }, + }, + }, + ]); } } return collection.updateOne({ id: inscription.id }, { $set: inscription }, { upsert: true });