diff --git a/flowchart-sync.drawio b/flowchart-sync.drawio index 5e0bc04..4e25f73 100644 --- a/flowchart-sync.drawio +++ b/flowchart-sync.drawio @@ -1,6 +1,6 @@ - + - + @@ -8,140 +8,88 @@ - + - + - + - - - - + - + - - - - + - - + + - - - - + + - - + + - + - - - - - - - - - - - - - - - - - - + + + + - + - + - - - - + - + - + - - - - - - - - - - - - - - - - - - - - - - - - - - - - + @@ -150,46 +98,112 @@ - + - - + + - + - + - + - + - - + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + - + - - + + - + - - + + diff --git a/flowchart-sync.webp b/flowchart-sync.webp index e7bf272..a996f00 100644 Binary files a/flowchart-sync.webp and b/flowchart-sync.webp differ diff --git a/lib/syncFromRemote.js b/lib/syncFromRemote.js index eb507fa..ab43a1a 100644 --- a/lib/syncFromRemote.js +++ b/lib/syncFromRemote.js @@ -30,41 +30,59 @@ export default async function syncFromRemote(table, path, api, remoteTable, loca // Loop remote docs for (let remoteDoc of remoteDocs) { + + // Deletion flag set + if (remoteDoc.$deleted) { - // Find local doc - const localDoc = await table.get(remoteDoc.id) + // Get local doc + const localDoc = await table.get(remoteDoc.id) + + // Local doc exists + if (localDoc) { - // Local doc exists - if (localDoc) { + // Remote doc is newer + if (remoteDoc.$updated > localDoc.$updated) { - // Remote doc is newer - if (remoteDoc.$updated > localDoc.$updated) { + // Update local doc to avoid re-sync + await table.update(remoteDoc.id, { $deleted: 1, $updated: 'keep', $synchronized: Date.now() }) - // Get full remote doc - remoteDoc = await api.read(remoteTable, remoteDoc.id) + // Delete local doc + await table.delete(remoteDoc.id) - // Define updated local doc - const updatedLocalDoc = { ...remoteDoc, $synchronized: Date.now() } + } - // Update local doc - await table.update(localDoc.id, updatedLocalDoc) - debug('sync from remote > update doc', updatedLocalDoc) + } - // Deletion flag set - if (remoteDoc.$deleted) { + } - // Delete local doc - await table.delete(remoteDoc.id) + // Deletion flag is not set + if (!remoteDoc.$deleted) { + + // Get local doc + const localDoc = await table.get(remoteDoc.id) - } + // Local doc exists + if (localDoc) { + + // Remote doc is newer + if (remoteDoc.$updated > localDoc.$updated) { + // Get full remote doc + remoteDoc = await api.read(remoteTable, remoteDoc.id) + + // Define updated local doc + const updatedLocalDoc = { ...remoteDoc, $synchronized: Date.now() } + + // Update local doc + await table.update(localDoc.id, updatedLocalDoc) + debug('sync from remote > update doc', updatedLocalDoc) + + } + } - // Local doc does not exist - } else { - - // No deletion flag - if (!remoteDoc.$deleted) { + // Local doc does not exist + if (!localDoc) { // Get full remote doc remoteDoc = await api.read(remoteTable, remoteDoc.id) @@ -76,15 +94,21 @@ export default async function syncFromRemote(table, path, api, remoteTable, loca await table.add(newLocalDoc) debug('sync from remote > create doc', newLocalDoc) + } - } + } } + } + // Not found not synchronized remote docs - } else { + if (!remoteDocs.length) { + + // Mark sync as completed syncCompleted = true + } } diff --git a/package-lock.json b/package-lock.json index f5ca6e4..22b6295 100644 --- a/package-lock.json +++ b/package-lock.json @@ -1,12 +1,12 @@ { "name": "dexie-mysql-sync", - "version": "6.0.0", + "version": "6.0.1", "lockfileVersion": 3, "requires": true, "packages": { "": { "name": "dexie-mysql-sync", - "version": "6.0.0", + "version": "6.0.1", "license": "MIT", "dependencies": { "js-crud-api": "^0.4.0", diff --git a/package.json b/package.json index 7fbe4c9..290e598 100644 --- a/package.json +++ b/package.json @@ -1,6 +1,6 @@ { "name": "dexie-mysql-sync", - "version": "6.0.0", + "version": "6.0.1", "description": "", "main": "lib/index.js", "type": "module",