npm package discovery and stats viewer.

Discover Tips

  • General search

    [free text search, go nuts!]

  • Package details

    pkg:[package-name]

  • User packages

    @[username]

Sponsor

Optimize Toolset

I’ve always been into building performant and accessible sites, but lately I’ve been taking it extremely seriously. So much so that I’ve been building a tool to help me optimize and monitor the sites that I build to make sure that I’m making an attempt to offer the best experience to those who visit them. If you’re into performant, accessible and SEO friendly sites, you might like it too! You can check it out at Optimize Toolset.

About

Hi, 👋, I’m Ryan Hefner  and I built this site for me, and you! The goal of this site was to provide an easy way for me to check the stats on my npm packages, both for prioritizing issues and updates, and to give me a little kick in the pants to keep up on stuff.

As I was building it, I realized that I was actually using the tool to build the tool, and figured I might as well put this out there and hopefully others will find it to be a fast and useful way to search and browse npm packages as I have.

If you’re interested in other things I’m working on, follow me on Twitter or check out the open source projects I’ve been publishing on GitHub.

I am also working on a Twitter bot for this site to tweet the most popular, newest, random packages from npm. Please follow that account now and it will start sending out packages soon–ish.

Open Software & Tools

This site wouldn’t be possible without the immense generosity and tireless efforts from the people who make contributions to the world and share their work via open source initiatives. Thank you 🙏

© 2024 – Pkg Stats / Ryan Hefner

dexie-observable

v4.0.1-beta.13

Published

Addon to Dexie that makes it possible to observe database changes no matter if they occur on other db instance or other window.

Downloads

52,583

Readme

Dexie.Observable.js

Observe changes to database - even when they happen in another browser window.

Install

npm install dexie --save
npm install dexie-observable --save

Use

import Dexie from 'dexie';
import 'dexie-observable';

// Use Dexie as normally - but you can also subscribe to db.on('changes').

Usage with existing DB

In case you want to use Dexie.Observable with your existing database, you will have to do a schema upgrade. Without it Dexie.Observable will not be able to properly work.

import Dexie from 'dexie';
import 'dexie-observable';

var db = new Dexie('myExistingDb');
db.version(1).stores(... existing schema ...);

// Now, add another version, just to trigger an upgrade for Dexie.Observable
db.version(2).stores({}); // No need to add / remove tables. This is just to allow the addon to install its tables.

Dependency Tree

Source

Dexie.Observable.js

Description

Dexie.Observable is an add-on to Dexie.js makes it possible to listen for changes on the database even if the changes are made in a foreign window. The addon provides a "storage" event for IndexedDB, much like the storage event (onstorage) for localStorage.

In contrary to the Dexie CRUD hooks, this event reacts not only on changes made on the current db instance but also on changes occurring on db instances in other browser windows. This enables a Web Apps to react to database changes and update their views accordingly.

Dexie.Observable is also the base of Dexie.Syncable.js - an add-on that enables two-way replication with a remote server.

Extended Methods, Properties and Events

UUID key generator

When defining your stores in Version.stores() you may use the $$ (double dollar) prefix to your primary key. This will make it auto-generated to a UUID string. See sample below.

Dexie.Observable.createUUID()

A static method added to Dexie that creates a UUID. This method is used internally when using the $$ prefix to primary keys. To change the format of $$ primary keys, just override Dexie.createUUID by setting it to your desired function instead.

db.on('changes') event

Subscribe to any database changes no matter if they occur locally or in other browser window.

Parameters to your callback:

Example (here we're using plain ES6 script tags):

<html>
    <head>
    <script src="dexie.min.js"></script>
    <script src="dexie-observable.min.js"></script> <!-- Enable DB observation -->
    <script>
        var db = new Dexie("ObservableTest");
        db.version(1).stores({
            friends: "$$uuid,name"
        });
        db.on('changes', function (changes) {
            changes.forEach(function (change) {
                switch (change.type) {
                    case 1: // CREATED
                        console.log('An object was created: ' + JSON.stringify(change.obj);
                        break;
                    case 2: // UPDATED
                        console.log('An object with key ' + change.key + ' was updated with modifications: ' + JSON.stringify(change.mods));
                        break;
                    case 3: // DELETED
                        console.log('An object was deleted: ' + JSON.stringify(change.oldObj);
                        break;
            });
        });
        db.open();
        // Make an initial put() - will result in a CREATE-change:
        db.friends.put({name: "Kalle"}).then(function(primKey) {
            // Call put() with existing primary key - will result in an UPDATE-change:
            db.friends.put({uuid: primKey, name: "Olle"}).then (function () {
                // Call delete() will result in a DELETE-change:
                db.friends.delete(primKey);
            });
        });

        // Result that will be logged:
        // An object was created: {"uuid": "23bada36-d27a-4e78-a978-1ab3c4129cd0", name: "Kalle"}
        // An object with key: 23bada36-d27a-4e78-a978-1ab3c4129cd0 was updated with modifications: {"name": "Olle"}
        // An object was deleted: {"uuid": "23bada36-d27a-4e78-a978-1ab3c4129cd0", name: "Olle"}
    </script>
    </head>
    <body>
    </body>
</html>