Describe the bug
The current read-modify-write pattern for updates is not atomic and can cause
race conditions leading to data loss. Consider making the update operation either through a transactional adapter method or by implementing a
locking/queuing mechanism in the SyncEngine.
async function handleUpdate(table, id, partialData) {
// 1. Read existing data from storage
const existing = await local.adapter.findOne(table, id);
// 2. Merge changes in application memory
const merged = { ...existing, ...partialData };
// Another update could happen here, making `existing` stale.
// 3. Write the merged data back to storage
await local.adapter.update(table, id, merged);
}
Possible Solution(s):
async function handleUpdate(table, id, partialData) {
// Delegate the update to an atomic operation in the adapter.
// The adapter is responsible for ensuring the read-modify-write
// cycle is atomic, preventing race conditions.
await local.adapter.atomicPartialUpdate(table, id, partialData);
}
// Example implementation in the adapter layer:
class LocalAdapter {
atomicPartialUpdate(table, id, partialData) {
// This logic should be executed within a transaction
// provided by the underlying database (e.g., IndexedDB).
const record = this.findOne(table, id);
const merged = { ...record, ...partialData };
this.update(table, id, merged);
}
}
Environment:
- sveltekit-sync version: affects all current versions
- SvelteKit version:
- Node version:
- Browser (if applicable):
Additional context
Any other relevant information.
Describe the bug
The current read-modify-write pattern for updates is not atomic and can cause
race conditions leading to data loss. Consider making the update operation either through a transactional adapter method or by implementing a
locking/queuing mechanism in the SyncEngine.
Possible Solution(s):
Environment:
Additional context
Any other relevant information.