| {% include anchor.html title="Sync a database" hash="sync" %} |
| |
| {% highlight js %} |
| var sync = PouchDB.sync(src, target, [options]) |
| {% endhighlight %} |
| |
| Sync data from `src` to `target` and `target` to `src`. This is a convenience method for bidirectional data replication. |
| |
| In other words, this code: |
| |
| {% highlight js %} |
| PouchDB.replicate('mydb', 'http://localhost:5984/mydb'); |
| PouchDB.replicate('http://localhost:5984/mydb', 'mydb'); |
| {% endhighlight %} |
| |
| |
| is equivalent to this code: |
| |
| {% highlight js %} |
| PouchDB.sync('mydb', 'http://localhost:5984/mydb'); |
| {% endhighlight %} |
| |
| |
| ### Options |
| |
| Please refer to [replicate()](api.html#replication) for documentation on options, as `sync()` is just a convenience method that entails bidirectional replication. |
| |
| #### Example Usage: |
| {% highlight js %} |
| var sync = PouchDB.sync('mydb', 'http://localhost:5984/mydb', { |
| live: true, |
| retry: true |
| }).on('change', function (info) { |
| // handle change |
| }).on('paused', function () { |
| // replication paused (e.g. user went offline) |
| }).on('active', function () { |
| // replicate resumed (e.g. user went back online) |
| }).on('denied', function (info) { |
| // a document failed to replicate, e.g. due to permissions |
| }).on('complete', function (info) { |
| // handle complete |
| }).on('error', function (err) { |
| // handle error |
| }); |
| |
| sync.cancel(); // whenever you want to cancel |
| {% endhighlight %} |
| |
| There is also a shorthand for syncing given existing PouchDB objects. This behaves the same as `PouchDB.sync()`: |
| |
| {% highlight js %} |
| db.sync(remoteDB, [options]); |
| {% endhighlight %} |
| |
| For any further details, please refer to [replicate()](api.html#replication). |
| |
| |