本地数据库和server端数据库比对思路
async function fetchServerDataPage(startId, endId, pageSize) {
// 获取 id 大于 startId 的数据 小于endId
return await fetch(`/server-data?startId=${startId}&endId=${endId}&size=${pageSize}`).then(res => res.json());
}
async function fetchLocalDataPage(startId, endId, pageSize) {
// 获取 id 大于 startId 的数据 小于endId
return await fetch(`/local-data?startId=${startId}&endId=${endId}&size=${pageSize}`).then(res => res.json());
}
function diffPage(serverData, localData) {
const patches = [];
const serverMap = new Map();
serverData.forEach(item => serverMap.set(item.id, item));
localData.forEach(localItem => {
const serverItem = serverMap.get(localItem.id);
if (!serverItem) {
patches.push({type: 'remove', id: localItem.id});
} else if (serverItem.value !== localItem.value) {
patches.push({type: 'update', id: localItem.id, newValue: serverItem.value});
}
serverMap.delete(localItem.id);
});
serverMap.forEach((serverItem, id) => {
patches.push({type: 'add', id, value: serverItem.value});
});
return patches;
}
async function diffAllData(pageSize) {
let startId = 0;
let endId = 50;
let serverData, localData;
let allPatches = [];
while (true) {
// 获取下一页数据
[serverData, localData] = await Promise.all([fetchServerDataPage(startId, endId, pageSize), fetchLocalDataPage(startId, endId, pageSize)]);
// 如果两边都没有数据了,终止循环
if (serverData.length === 0 && localData.length === 0) {
break;
}
// 比对当前页的数据
const patches = diffPage(serverData, localData);
allPatches = allPatches.concat(patches);
if (serverData.length > 0 || localData.length > 0) {
startId = endId;
endId += 50;
}
}
return allPatches;
}
// 调用 diffAllData 函数进行比对处理
const pageSize = 100; // 每页的数据量
diffAllData(pageSize).then(allPatches => {
console.log(allPatches);
});
本作品采用《CC 协议》,转载必须注明作者和本文链接