Use openCollections to open one or more JSONStore collections
Starting or provisioning a collections means creating the persistent storage that contains the collection and documents, if it does not exists.
If the persistent storage is encrypted and a correct password is passed, the necessary security procedures to make the data accessible are run.
For optional features that you can enable at initialization time, see Security, Multiple User Support, and MobileFirst Adapter Integration in the second part of this module
Get
Use getCollectionByName to create an accessor to the collection. You must call openCollections before you call getCollectionByName.
The variable collection can now be used to perform operations on the people collection such as add, find, and replace
Add
Use addData to store data as documents inside a collection
Find
Use findDocuments to locate a document inside a collection by using a query. Use findAllDocuments to retrieve all the documents inside a collection. Use findDocumentById to search by the document unique identifier.
Replace
Use replaceDocument to modify documents inside a collection. The field that you use to perform the replacement is _id, the document unique identifier.
This examples assumes that the document {_id: 1, json: {name: 'yoel', age: 23} } is in the collection
Remove
Use removeDocumentById to delete a document from a collection.
Documents are not erased from the collection until you call markDocumentClean. For more information, see the MobileFirst Adapter Integration section later in this tutorial
Remove Collection
Use removeCollection to delete all the documents that are stored inside a collection. This operation is similar to dropping a table in database terms
Destroy
Use destroy to remove the following data:
All documents
All collections
All Stores "See Multiple User Support later in this tutorial"
All JSONStore metadata and security artifacts "See Security later in this tutorial"
Advanced Usage
Security
You can secure all the collections in a store by passing a JSONStoreInitOptions object with a password to the openCollections function. If no password is passed, the documents of all the collections in the store are not encrypted.
Some security metadata is stored in shared preferences (Android);
The store is encrypted with a 256-bit Advanced Encryption Standard (AES) key. All keys are strengthened with Password-Based Key Derivation Function 2 (PBKDF2).
Use closeAll to lock access to all the collections until you call openCollections again. If you think of openCollections as a login function you can think of closeAll as the corresponding logout function.
Use changePassword to change the password.
Multiple User Support
You can create multiple stores that contain different collections in a single MobileFirst application. The openCollections function can take an options object with a username. If no username is given, the default username is jsonstore
MobileFirst Adapter Integration
This section assumes that you are familiar with MobileFirst adapters. MobileFirst Adapter Integration is optional and provides ways to send data from a collection to an adapter and get data from an adapter into a collection.
You can achieve these goals by using functions such as WLClient.invokeProcedure or your own instance of an HttpClient if you need more flexibility.
Adapter Implementation
Create a MobileFirst adapter and name it "People". Define it's procedures addPerson, getPeople, pushPeople, removePerson, and replacePerson.
Load data from MobileFirst Adapter
To load data from a MobileFirst Adapter use WLClient.invokeProcedure.
Get Push Required (Dirty Documents)
Calling findAllDirtyDocuments returns and array of so called "dirty documents", which are documents that have local modifications that do not exist on the back-end system.
To prevent JSONStore from marking the documents as "dirty", pass the option options.setMarkDirty(false) to add, replace, and remove
Push changes
To push changes to a MobileFirst adapter, call the findAllDirtyDocuments to get a list of documents with modifications and then use WLClient.invokeProcedure. After the data is sent and a successful response is received make sure you call markDocumentsClean.
Inclusive terminology note: The Mobile First Platform team is making changes to support
the IBM® initiative to replace racially biased and other discriminatory language in our code and content
with more inclusive language. While IBM values the use of inclusive language, terms that are outside of
IBM's direct influence are sometimes required for the sake of maintaining user understanding. As other
industry leaders join IBM in embracing the use of inclusive language, IBM will continue to update the
documentation to reflect those changes.