DataStore
#
DescriptionThe DataStore
interface of the Data Store API represents a retrieved set of data, and includes standard properties for accessing the store's name, owner, etc., methods for reading, modifying and syncing data, and the onchange event handler for reacting to changes to the data.
#
PropertiesDataStore.name
read-only
Returns the name of the current data store.DataStore.owner
read-only
Returns the name of the app that owns the data store.DataStore.readOnly
read-only
Returns a boolean that signifies whether the data store is read only or not.DataStore.revisionId
read-only
Returns the current revision ID of the data store.
#
Event handlersDataStore.onchange
read-only
Fired when a change is made to the data store, by any app that has permission to modify it.
#
MethodsDataStore.get()
Returns a specific record (or list of records) from the data store.DataStore.add()
Adds a new record to the data store; if the record you are attempting to add already exists, it will throw an exception.DataStore.put()
Updates an existing record in the data store.DataStore.remove()
Deletes a record (or list of records) from the data storeDataStore.clear()
Deletes all records from the data store, leaving it empty.DataStore.getLength()
Returns the number of records currently in the data store.DataStore.sync()
Opens a cursor that allows you to step through any changes that have taken place in the data store going back to a particular revision ID, and run code in response to different types of change.
#
ExampleIn the following example, we use navigator.getDataStores to return a list of DataStore
objects representing data stores on the device called contacts. Since there is only one such data store, we can access it inside the outer promise using stores[0]
. The next promise uses DataStore.getLength
to return the number of records in the store. If the value is 0, we populate the data store with records contained in the contactsInit
object; if there is already data in the store, we run DataStore.sync
to loop through any additions since the code last accessed the data store and update the data display as necessary.