Skip to content

How can my SyncManager pull deletes? #1020

@MechMel

Description

@MechMel

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

Metadata

Metadata

Assignees

No one assigned

    Labels

    No labels
    No labels

    Projects

    No projects

    Milestone

    No milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions