Dexie.Observable is an addon for Dexie.js that significantly enhances IndexedDB functionality by enabling cross-window and cross-instance database change observation. It introduces an event mechanism, similar to `localStorage`'s `onstorage`, specifically for IndexedDB, allowing web applications to react in real-time to data modifications, irrespective of the originating tab or worker. This ensures views remain synchronized and up-to-date across all connected clients. The package is an integral part of the Dexie.js ecosystem, fully compatible with Dexie v4.x, and typically releases in alignment with Dexie.js's maintenance and feature updates. Although the package version provided is `4.0.1-beta.13`, it functions within the broader stable Dexie v4 environment. Furthermore, it forms the foundational layer for `Dexie.Syncable.js`, which provides robust two-way data replication capabilities with remote servers.
npm install dexie-observableVerified import paths — ran on the pinned version, not inferred.
This example demonstrates how to initialize Dexie with the observable addon, define a store using UUID primary keys, and subscribe to the `db.on('changes')` event to react to database modifications, including those from other browser windows.
Add a new version to your database schema, even if it's an empty `db.version(X).stores({});` entry, after your existing schema. This triggers the necessary upgrade logic.Use `import 'dexie-observable';` after `import Dexie from 'dexie';`. Any functionality, like `db.on('changes')`, will then be available on your Dexie database instance.Monitor the Dexie.js GitHub releases for a stable `dexie-observable` v4.x release, or use with caution in production environments, ensuring thorough testing.
To use a custom UUID generator, assign your function to `Dexie.createUUID = myCustomUUIDFunction;` before initializing your database. Ensure your custom function returns a unique string suitable for primary keys.
Ensure `import Dexie from 'dexie';` is followed by `import 'dexie-observable';` in your module. Also, make sure you are calling `db.on()` on an instance of Dexie created *after* the import.
If using an existing database, ensure you have added a new, empty version to your schema definition (`db.version(X).stores({});`) to trigger the addon's installation. Verify browser compatibility with cross-window IndexedDB events.