Reference Source

# <!--- Licensed Materials - Property of IBM

(C) Copyright 2018 IBM Corp.

Unless required by applicable law or agreed to in writing, software distributed under the License is distributed on an "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. See the License for the specific language governing permissions and limitations under the License. -->

React Native SDK for IBM MobileFirst JSONStore

Description

The IBM Mobile Foundation JSONStore is an optional client-side API providing a lightweight, document-oriented storage system. JSONStore enables persistent storage of JSON documents. Documents in an application are available in JSONStore even when the device is offline. This persistent, always-available storage can be useful to give users access to documents when,for example, there is no network connection available in the device.

Key features

Read more about JSONStore here.

Changelog

For details of the changes in this latest release, visit the IBM Mobile Foundation Blog.

Installation

The IBM MobileFirst™ JSONStore SDK for React Native apps is dependent on the IBM MobileFirst Foundation SDK. If you have already added the IBM MobileFirst Foundation SDK to your app, continue with the instructions below.

Navigate to the folder of your React Native app and run the following command to install the JSONStore SDK for React Native apps.

npm install react-native-ibm-mobilefirst-jsonstore --save

Getting Started

Pre-requisites

  1. Make sure you have all the pre-requisites for a React Native app covered. See the React Native Getting Started page if you're new to this whole stuff.

  2. A [Mobile Foundation instance on IBM Cloud] (https://console.bluemix.net/catalog/services/Mobile%20Foundation) OR an on-premise installation of the IBM Mobile Foundation server.

Create a React Native Project

react-native init JSONStoreApp

cd JSONStoreApp

npm install react-native-ibm-mobilefirst-jsonstore —-save

react-native link

Creating a JSONStore Collection

The first step in using the JSONStore API is to create a JSONStore Collection.

Import the JSONStore classes into your application. Open App.js and add the following line among the other import statements

import {WLJSONStore, JSONStoreCollection} from 'react-native-ibm-mobilefirst-jsonstore'; 

Next, create and open a collection. You can open multiple collections at once. Pass a JSONStoreInitOptions parameter for advanced options such as protecting your collection with a password, setting up sync policies etc.

var dogs = new JSONStoreCollection('dogs'); 
WLJSONStore.openCollections(['dogs']);  // Provide the name of the collection as a string. 

Adding data to a collection

Add JSON data to your collection.

    var hachi = { "name" : "Hachiko" , "breed" : "Akita" , "country" : "Japan"}; 
    dogs.addData(hachi).then( ()=> { 
      // Data was added to the collection successfully.
    }).catch ( (err) => { 
      // Error adding data. See the err object for more information on the error 
    })

Querying data from a collection

Use any of the JSONStoreCollection.find* APIs to query a collection. Use JSONStoreQueryPart and JSONStoreQuery APIs for advanced filtering and querying of JSON data.

    dogs.findAllDocuments().then( (result) => { 
       // result will have all the documents in the collection 
       // E.g. [ {"json": {"name":"Hachiko","breed":"Akita","country":"Japan"},"_id":2}]       
     }).catch((error) => {
        console.error ("Error finding docs " +  JSON.stringify(error));
     });

Closing, clearing and destroying your collection

Closing your collection will close your JSONStore collection for further access until it is opened again using the openCollections API

WLJSONStore.closeAll()

Clearing a collection will remove all documents from a collection but does not destroy it.

    dogs.clearCollection().then (()=>{
        // All documents cleared successfully
     }).catch( (err) => { 
       // An error occurred while clearing the collection. 
     })

Destroying a collection will permanently delete all data, all accessors and security artifacts. A collection once destroyed cannot be restored. All collections of the app will be destroyed.

    WLJSONStore.destroy()

Supported platforms

Documentation

Copyright 2018 IBM Corp.

IBM - IBM MobileFirst Platform Foundation v8.0 license agreement

Terms of Use