Automatically synchronize browser database with RESTful web service including AWS S3 and Google Cloud Storage.
This article refer to old
sync
module. See Entity for new way of synchronization.
Synchronizing with RESTful service is pretty straight forward. The following example use Checkvist Open API, which follow REST principles for obtaining and updating the data. Currently the API does not expose medata data on entity request header. For entity ‘checklists’, the resource URI and sample data for key 190078 is:
https://beta.checkvist.com/checklists/190078.json
{
id: 190078
name: "test sync app"
public: false
updated_at: "2013/06/08 22:57:09 +0000"
user_count: 1
user_updated_at: null
}
Store schema is declared as follow:
var schema = {
stores: [{
name: 'checklists',
keyPath: 'id',
Sync: {
format: 'json',
transport: service,
Options: {
baseUri: '/',
delimiter: '/'
}
}
}] };
The Sync
attribute value specify synchronization information of the store. HTTP requests are made by HTTP transport
service object specified by transport
attribute in Sync
options. A HTTP transport service is an object which has a method of request
,
which take argument object having fields of
Response object has the following fields
It is similar to as defined in Google Javascript client library.
The following code snippet show how to make HTTP transport service:
/**
* Send request to checkvist server.
* @link https://developers.google.com/api-client-library/javascript/reference/referencedocs#gapiclientrequest
* @param {Object} args request arguments, compatible with Google javascript
* client library.
*/
CheckvistApp.prototype.request = function(args) {
var path = args.path;
var method = args.method || 'GET';
var params = args.params || {};
params['username'] = this.session.username;
if (this.session.token) {
params['token'] = this.session.token;
}
var host = 'beta.checkvist.com';
var url = 'https://' + host + path + '.json';
var query = [];
for (var q in params) {
query.push(q + '=' + encodeURIComponent(params[q]));
}
url += '?' + query.join('&');
var req = new XMLHttpRequest();
req.open(method, url, false);
// req.withCredentials = true;
var me = this;
req.onload = function(e) {
var raw = {
body: req.responseText,
status: req.status,
statusText: req.statusText,
headers: {}
};
var header_lines = req.getAllResponseHeaders().split('\n');
for (var i = 0; i < header_lines.length; i++) {
var idx = header_lines[i].indexOf(':');
if (idx > 0) {
var name = header_lines[i].substr(0, idx).toLowerCase();
var value = header_lines[i].substr(idx + 1).trim();
raw.headers[name] = value;
}
}
args.callback(JSON.parse(req.responseText), raw);
};
console.info('sending ' + url);
req.send();
};
A complete offline application can be found Checkvist sync demo app.
YDN-DB have build in synchronization support for AWS S3 (Amazon simple storage service), ATOM syndicate format, OData, GData, GCS (Google cloud storage).
<pre>var schema = { stores: [
{
name: 'st',
Sync: {
format: 's3'
transport: gapi.client
}
} ] };</pre>