put()

Creates or updates the specified document in storage.

uniface.datastore.put(Doc)

Example: uniface.datastore.put({_id:'songs_dsp',title:'Heroes'})

Arguments

Doc—a pure JSON object, that is, a collection of name/value pairs. It must contain:

  • The document identifier: _id:String.
  • Any other data to be stored: NameOfData:String.

Note: If you try to store non-JSON data (for example, Date objects) you may see inconsistent results.

Return Value

Returns a JavaScript Promise object with the response. This includes a revision marker _rev:String.

Otherwise, an error is returned. For more information, see Promises and Errors.

Description

Whenever a data object is created or updated, a new version of the document is created. It is distinguished from other versions by a revision marker that is automatically added to the document as a field _rev:String. The revision marker contains a unique random number that is updated each time the data object is updated, and is added to the response.

When updating Doc, you must specify the revision to update: _rev:String. For more information, see Creating and Updating Data.

Creating a document in offline storage

When you create a new document in client-side storage, you only need to specify the document identifier and the rest of the data to be stored:

Uniface.datastore.put({
  _id: 'songs_dsp',
  title: 'Heroes'
 }).then(function (response) {
  // handle response
 }).catch(function (err) {
  console.log(err);
 });

Updating a document in offline storage

When updating an existing document, you must first retrieve it using uniface.datastore.get, so that you have the revision number. You can then update the document, providing the document identifier, the revision, and the data to be modified:

uniface.datastore.get('songs_dsp').then(function(doc) {
  return uniface.datastore.put({
    _id: 'songs_dsp',
    _rev: doc._rev,
    title: "Let's Dance"
  });
 }).then(function(response) {
  // handle response
 }).catch(function (err) {
  console.log(err);
 });

Related Topics