修复云端数据一致性问题

This commit is contained in:
2026-07-29 11:03:44 +08:00
parent a3dd080b6f
commit 17727ab1dd
12 changed files with 389 additions and 41 deletions
+18 -20
View File
@@ -1,5 +1,6 @@
const DB_COLLECTION = 'plank_data'
const ENV_ID = 'cloudbase-d1g56kl2q8f4f7d8a'
const { deleteAllPages } = require('./cloud-delete')
let _db = null
let _pushTimer = null
@@ -214,7 +215,7 @@ const _doPush = async () => {
const clearAll = async () => {
const db = getDb()
if (!db) return
if (!db) throw new Error('云端服务不可用')
// Ensure _openid is cached before trying to delete — without it we
// can't reliably locate the user's doc (add() only returns _id).
@@ -223,29 +224,26 @@ const clearAll = async () => {
}
if (!_openid) {
console.log('[cloud] clearAll: cannot fetch openid, giving up')
return
throw new Error('无法识别当前用户')
}
// Always locate by _openid (never by cached _docId alone) so we're
// guaranteed to target OUR doc under the custom permission rule.
const mine = await db.collection(DB_COLLECTION)
.where({ _openid: _openid })
.limit(1)
.get()
if (mine && mine.data && mine.data.length > 0) {
const myDocId = mine.data[0]._id
try {
await db.collection(DB_COLLECTION).doc(myDocId).remove()
console.log('[cloud] clearAll: removed doc', myDocId)
} catch (e) {
console.log('[cloud] clearAll: remove failed:', e.message || e)
// Repeatedly fetch the first page and delete it. This avoids skip()'s
// shifting-offset bug and removes legacy duplicate docs as well.
await deleteAllPages(
async () => {
const mine = await db.collection(DB_COLLECTION)
.where({ _openid: _openid })
.limit(100)
.get()
return (mine && mine.data) || []
},
async (id) => {
await db.collection(DB_COLLECTION).doc(id).remove()
console.log('[cloud] clearAll: removed doc', id)
}
} else {
console.log('[cloud] clearAll: no doc found for openid')
}
)
// Always invalidate cached ids so the next _doPush starts fresh
// instead of trying to update a now-deleted or foreign doc.
// Invalidate cached ids so the next _doPush starts fresh.
_docId = null
}