-
-
Notifications
You must be signed in to change notification settings - Fork 680
db.syncable.connect()
db.syncable.connect (
protocol: string,
url: string,
options: Object) : Promise<void>| protocol : String | Name of protocol. An Implementation of [ISyncProtocol](Dexie.Syncable.ISyncProtocol) must have been registered using [Dexie.Syncable.registerProtocol()](Dexie.Syncable.registerProtocol()) |
| url : String | URL to connect to |
| options : Object | Options that the registered protocol can handle. Options are specific to each ISyncProtocol implementation |
Connect to given URL using given protocol and options. Returned Promise will resolve when sync-protocol has called its onSuccess() callback. If sync-protocol calls onError(), the returned promise will reject but the underlying framework may continue to try connecting in the background. If you want to stop this, call db.syncable.disconnect (url) in your catch clause.
Once connected, you will never have to call connect() again. Not even after a reboot. The framework will keep in sync forever with this node until a call to disconnect(), delete() happens, or if an irrepairable error occur on the node (not network down, but a fatal error).
The very first time that connect() is called upon an URL, the framework will start syncing the entire indexedDB database towards the sync protocol implementation. If this is not desired, supply option {initialUpload: false} in your connect call.
Calling connect() when the node is already connected, will not generate any sync operation but result in an immediate resolve of returned Promise.
import Dexie from 'dexie';
import 'dexie-observable';
import 'dexie-syncable';
//
// 1. Register Your Protocol(s)!
//
Dexie.Syncable.registerProtocol("myProtocol", {
sync: function (...) {...}; // An ISyncProtocol implementation.
});
//
// 2. Declare Your Database.
//
var db = new Dexie('mydb');
db.version(1).stores({foo: 'id'});
//
// 3. Call db.syncable.connect().
//
db.syncable.connect("myProtocol", "https://remote-server/...", {options...}).catch(err => {
console.error (`Failed to open database: ${err.stack || err});
});
//
// 4. Start using the database as normal.
//
db.foo.toArray().then(foos => {
// This promise will not resolve before an initial sync has taken place.
// For sure, your data will be populated from server when you get here.
// Very First Time - you need to be online to get here!
// After that: you can get here even if you are offline.
}).catch(err => {
// May happen if this was you app's initial load and you were offline.
});Dexie.js - minimalistic and bullet proof indexedDB library