Security News
Opengrep Emerges as Open Source Alternative Amid Semgrep Licensing Controversy
Opengrep forks Semgrep to preserve open source SAST in response to controversial licensing changes.
dexie-observable
Advanced tools
Addon to Dexie that makes it possible to observe database changes no matter if they occur on other db instance or other window.
Observe changes to database - even when they happen in another browser window.
npm install dexie --save
npm install dexie-observable --save
import Dexie from 'dexie';
import 'dexie-observable';
// Use Dexie as normally - but you can also subscribe to db.on('changes').
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.
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.
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.
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.
Subscribe to any database changes no matter if they occur locally or in other browser window.
Parameters to your callback:
changes : Array<DatabaseChange> | Array of changes that have occured in database (locally or in other window) since last time event was triggered, or the time of starting subscribing to changes. |
partial: Boolean | True in case the array does not contain all changes. In this case, your callback will soon be called again with the additional changes and partial=false when all changes are delivered. |
<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>
FAQs
Addon to Dexie that makes it possible to observe database changes no matter if they occur on other db instance or other window.
The npm package dexie-observable receives a total of 7,390 weekly downloads. As such, dexie-observable popularity was classified as popular.
We found that dexie-observable demonstrated a not healthy version release cadence and project activity because the last version was released a year ago. It has 1 open source maintainer collaborating on the project.
Did you know?
Socket for GitHub automatically highlights issues in each pull request and monitors the health of all your open source dependencies. Discover the contents of your packages and block harmful activity before you install or update your dependencies.
Security News
Opengrep forks Semgrep to preserve open source SAST in response to controversial licensing changes.
Security News
Critics call the Node.js EOL CVE a misuse of the system, sparking debate over CVE standards and the growing noise in vulnerability databases.
Security News
cURL and Go security teams are publicly rejecting CVSS as flawed for assessing vulnerabilities and are calling for more accurate, context-aware approaches.