JSONStore in React Native applications
Prerequisites
- Read the JSONStore parent tutorial
 - Make sure the PMF React Native Core SDK was added to the project.
 
Jump to:
Adding JSONStore
To add JSONStore plug-in to your React Native application:
- Open a Command-line window and navigate to your React Native project folder.
 - Run the command:
    
npm install react-native-ibm-mobilefirst-jsonstore --save npm install <path of plugin.tgz> --save - 
    
For iOS only, install the Persistent Mobile Foundation Pod dependencies
cd ios && pod install 
Basic Usage
Creating a new JSONStore Collection
- We use 
JSONStoreCollectionclass to create instances of JSONStore. We can also set additional configuration to this newly created JSONStore Collection (eg. setting search fields). - To start interacting with an existing JSONStore collection (eg:  adding or removing data) we need to Open the collection. We use 
openCollections()API to do this.var collection = new JSONStoreCollection('people'); WLJSONStore.openCollections(['people']) .then(res => { // handle success }).catch(err => { // handle failure }); 
Add
Use addData() API to store JSON Data in a collection.
var data = { "name": "John", age: 28 };
var collection = new JSONStoreCollection('people');
collection.addData(data)
.then(res => {
  // handle success
}).catch(err => {
  // handle failure
});
You can add a single JSON object or an Array of JSON objects using this API.
Find
- Use 
findto locate a document inside a collection by using a query. - Use 
findAllDocuments()API for retrieving all the documents inside a collection. - Use 
findDocumentById()andfindDocumentsById()API to search using the document unique identifier. - Use 
findDocuments()API to query the collection. For querying, you can useJSONStoreQueryPartclass objects to filter the data. 
Pass an array of
JSONStoreQueryPartobjects as a parameter tofindDocumentsAPI.
var collection = new JSONStoreCollection('people');
var query = new JSONStoreQueryPart();
query.addEqual("name", "John");
collection.findDocuments([query])
.then(res => {
	// handle success
}).catch(err => {
	// handle failure
});
Remove
Use remove to delete a document from a collection.
var id = 1; // for example
var collection = new JSONStoreCollection('people');
collection.removeDocumentById(id)
.then(res => {
	// handle success
}).catch(err => {
	// handle failure     
});
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.
var collection = new JSONStoreCollection('people');
collection.removeCollection()
.then(res => {
	// handle success
}).catch(err => {
	// handle failure
});
Sample app for the Persistent Mobile Foundation JSONStore
Download the sample here.
Running the Sample
Within the sample’s root directory, run the following command, which installs all the project dependencies:
npm install
Note: Make sure your mfpclient.properties and mfpclient.plist are pointing to correct the Persistent Mobile Foundation Server.
- Register the app. Go to the 
androiddirectory and run the following command:pmfdev app register - Configuring the app.
 (For Android only)
    
- 
        
Open
android/app/src/main/AndroidManifest.xmlfile from React Native project root directory.
Add the following line to the<manifest>tag:
xmlns:tools="http://schemas.android.com/tools"
Add the following line to the<application>tag:
tools:replace="android:allowBackup"
This step is required by react-native-ibm-mobilefirst library. - 
        
Open
android/app/build.gradlefile from the React Native project root directory.
Add the following code inside the android {} :packagingOptions{ exclude 'META-INF/ASL2.0' }This step is required by the react-native-ibm-mobilefirst-jsonstore library.
 
 - 
        
 - Running the app. Return to the root directory and navigate to 
iOSdirectory and run the command:pmfdev app register 
We’re now ready to run the app. To run on android, execute the following command:
react-native run-android