Learn about the different concepts that are required to work with external data.
For the actual API examples, see JSONStore examples.
Many systems use the term pull to refer to getting data from an external source.
All of the following code examples are written in pseudocode that looks similar to JavaScript.
app.get('/people', function (req, res) {
var people = database.getAll('people');
res.json(people);
});
[{id: 0, name: 'carlos', ssn: '111-22-3333'},
{id: 1, name: 'mike', ssn: '111-44-3333'},
{id: 2, name: 'dgonz' ssn: '111-55-3333')]
function getPeople () {
var input = {
method : 'get',
path : '/people'
};
return MFP.Server.invokeHttp(input);
}
var adapter = 'people';
var procedure = 'getPeople';
var resource = new WLResourceRequest('/adapters' + '/' + adapter + '/' + procedure, WLResourceRequest.GET);
resource.send()
.then(function (responseFromAdapter) {
// ...
});
$.ajax({
type: 'GET',
url: 'http://example.org/people',
})
.then(function (responseFromEndpoint) {
// ...
});
JSONStore provides a way to track local changes. It enables some APIs to mark documents as dirty. The API records the last operation that was performed on the document, and when the document was marked as dirty. You can then use this information to implement features like data synchronization.
['id', 'ssn']
as
the replace criteria, pass the following array as the input data:[{id: 1, ssn: '111-22-3333', name: 'Carlos'}]
and
the people collection already contains the following
document:{_id: 1,json: {id: 1, ssn: '111-22-3333', name: 'Carlitos'}}
The change operation
locates a document that matches exactly the following query:{id: 1, ssn: '111-22-3333'}
Then
the change operation performs
a replacement with the input data and the collection contains:{_id: 1, json: {id:1, ssn: '111-22-3333', name: 'Carlos'}}
The
name was changed from Carlitos to Carlos.
If more than one document matches the replace criteria, then all documents
that match are replaced with the respective input data..then(function (responseFromAdapter) {
var accessor = WL.JSONStore.get('people');
var data = responseFromAdapter.responseJSON;
var changeOptions = {
replaceCriteria : ['id', 'ssn'],
addNew : true,
markDirty : false
};
return accessor.change(data, changeOptions);
})
.then(function() {
// ...
})
var accessor = WL.JSONStore.get('people')
accessor.add(data, {markDirty: true})
accessor.replace(doc, {markDirty: true})
Similarly, you can remove a document, and opt to mark the removal as dirty or not. Documents that are removed and marked dirty do not show up when you use the find API. However, they are still inside the collection until you use the markClean API, which physically removes the documents from the collection. If the document is not marked as dirty, it is physically removed from the collection.
accessor.remove(doc, {markDirty: true})
Many systems use the term push to refer to sending data to an external source.
All of the following code examples are written in pseudocode that looks similar to JavaScript.
var accessor = WL.JSONStore.get('people');
accessor.getAllDirty()
.then(function (dirtyDocs) {
// ...
});
[{_id: 1,
json: {id: 1, ssn: '111-22-3333', name: 'Carlos'},
_operation: 'add',
_dirty: '1395774961,12902'}]
.then(function (dirtyDocs) {
var adapter = 'people',
procedure = 'updatePeople';
var resource = new WLResourceRequest('/adapters/' + adapter + '/' + procedure, WLResourceRequest.GET)
resource.setQueryParameter('params', [dirtyDocs]);
return resource.send();
})
.then(function (responseFromAdapter) {
// ...
})
function updatePeople (dirtyDocs) {
var input = {
method : 'post',
path : '/people',
body: {
contentType : 'application/json',
content : JSON.stringify(dirtyDocs)
}
};
return MFP.Server.invokeHttp(input);
}
Instead of relaying the output from the getAllDirty API
on the client, you might have to update the payload to
match a format that is expected by the backend. You might
have to split the replacements, removals, and inclusions into
separate backend API calls.var len = dirtyDocs.length;
var arrayOfPromises = [];
var adapter = 'people';
var procedure = 'addPerson';
var resource;
while (len--) {
var currentDirtyDoc = dirtyDocs[len];
switch (currentDirtyDoc._operation) {
case 'add':
case 'store':
resource = new WLResourceRequest('/adapters/people/addPerson', WLResourceRequest.GET);
resource.setQueryParameter('params', [currentDirtyDoc]);
arrayOfPromises.push(resource.send());
break;
case 'replace':
case 'refresh':
resource = new WLResourceRequest('/adapters/people/replacePerson', WLResourceRequest.GET);
resource.setQueryParameter('params', [currentDirtyDoc]);
arrayOfPromises.push(resource.send());
break;
case 'remove':
case 'erase':
resource = new WLResourceRequest('/adapters/people/removePerson', WLResourceRequest.GET);
resource.setQueryParameter('params', [currentDirtyDoc]);
arrayOfPromises.push(resource.send());
}
}
$.when.apply(this, arrayOfPromises)
.then(function () {
var len = arguments.length;
while (len--) {
// Look at the responses in arguments[len]
}
});
.then(function (dirtyDocs) {
return $.ajax({
type: 'POST',
url: 'http://example.org/updatePeople',
data: dirtyDocs
});
})
.then(function (responseFromEndpoint) {
// ...
});
.then(function (responseFromAdapter) {
if (responseFromAdapter is successful) {
WL.JSONStore.get('people').markClean(dirtyDocs);
}
})
.then(function () {
// ...
})
After documents are marked as clean, they do not show
up in the output from the getAllDirty API.