Search code examples
dexie

How to subscribe to changes in Dexie.js + vanilla JS


This is client side code. I see there are examples for React, but I am not using React. I am using preact + @preact/signals for client side state.

What is an API for subscriptions to a query in vanilla JS?


Solution

  • In https://dexie.org/docs/liveQuery()#vanilla-js it's examplified with vanilla JS (module style). If you're after a script-style, it would be:

    
    const friendsObservable = Dexie.liveQuery (
      () => db.friends
        .where('age')
        .between(50, 75)
        .toArray()
    );
    
    // Subscribe
    const subscription = friendsObservable.subscribe({
      next: result => console.log("Got result:", JSON.stringify(result)),
      error: error => console.error(error)
    });
    
    // Unsubscribe
    subscription.unsubscribe();