-
-
Notifications
You must be signed in to change notification settings - Fork 664
Dexie.Observable.js
Enables immediate reaction to database changes - local from other browser window.
Dexie.Observable is an add-on to Dexie.js that provides a new event: ´db.on('changes')´. 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 other 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.
Another difference from the CRUD hooks is that the changes event is triggered after the changes are done and transaction has committed.
I third difference is that if lots of changes takes place on a short time, this event wont be triggered until there have been no changes taking place during the last 25 milliseconds.
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 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.js"></script>
<script src="Dexie.Observable.js"></script> <!-- Enable DB observation -->
<script>
var db = new Dexie("Observable-test"); // Still using new Dexie() to create db instance.
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>
Dexie.js - minimalistic and bullet proof indexedDB library