From 9440d71d5edc7154533a693937bd4c302d0ff7f8 Mon Sep 17 00:00:00 2001 From: Max Nowack Date: Wed, 11 Dec 2024 16:03:27 +0100 Subject: [PATCH] fix: upsert data when applying to snapshot --- packages/signaldb/src/SyncManager/getSnapshot.ts | 15 +++++++++++++-- 1 file changed, 13 insertions(+), 2 deletions(-) diff --git a/packages/signaldb/src/SyncManager/getSnapshot.ts b/packages/signaldb/src/SyncManager/getSnapshot.ts index d884972f..f3d3d0c9 100644 --- a/packages/signaldb/src/SyncManager/getSnapshot.ts +++ b/packages/signaldb/src/SyncManager/getSnapshot.ts @@ -14,10 +14,21 @@ export default function getSnapshot, IdType>( if (data.items != null) return data.items const items = lastSnapshot || [] - data.changes.added.forEach(item => items.push(item)) + data.changes.added.forEach((item) => { + const index = items.findIndex(i => i.id === item.id) + if (index !== -1) { + items[index] = item + } else { + items.push(item) + } + }) data.changes.modified.forEach((item) => { const index = items.findIndex(i => i.id === item.id) - if (index !== -1) items[index] = item + if (index !== -1) { + items[index] = item + } else { + items.push(item) + } }) data.changes.removed.forEach((item) => { const index = items.findIndex(i => i.id === item.id)