-
I'm tying to make a firestore SyncManager, and I'm not sure how Signal DB expects me to tell it when docs are deleted. Most other local first tools use tombstones, but SignalDB seems to be performing actual deletes. I also don't see a way to emit delete events in the SyncManager's pull function. What is the right way to approach this? import { SyncManager } from 'signaldb'
import { initializeApp } from 'firebase/app'
import { getDatabase, ref, get, set, remove, update, onChildAdded, onChildChanged, onChildRemoved } from 'firebase/database'
initializeApp({
databaseURL: 'https://signaldb-d7e71-default-rtdb.firebaseio.com',
})
const db = getDatabase()
const syncManager = new SyncManager({
onError: (options, error) => {
// eslint-disable-next-line no-console
console.error(options, error)
},
registerRemoteChange({ name }, onChange) {
const handleChange = () => {
void onChange()
}
onChildAdded(ref(db, name), () => { void handleChange() })
onChildChanged(ref(db, name), () => { void handleChange() })
onChildRemoved(ref(db, name), () => { void handleChange() })
},
async pull({ name }) {
const snapshot = await get(ref(db, name))
const items = await snapshot.val() as Record<string, any> | null
return { items: Object.values(items ?? {}) }
},
async push({ name }, { changes }) {
await Promise.all([
...changes.added.map(async (item) => {
await set(ref(db, `${name}/${item.id}`), item)
}),
...changes.modified.map(async (item) => {
await update(ref(db, `${name}/${item.id}`), item)
}),
...changes.removed.map(async (item) => {
await remove(ref(db, `${name}/${item.id}`))
}),
])
},
})
export default syncManager |
Beta Was this translation helpful? Give feedback.
Replies: 1 comment
-
If the
|
Beta Was this translation helpful? Give feedback.
If the
pull
method returns an object with theitems
property, the content of the SignalDB collection will be replaced with this data. This means, if the response doesn't contain a specific item. It will be deleted locally. You could also return an object with achanges
property, if you want to control this by yourself.