8.0.2022081612 • Published 2 years ago

react-native-ibm-mobilefirst-jsonstore v8.0.2022081612

Weekly downloads
66
License
SEE LICENSE IN LI...
Repository
-
Last release
2 years ago

React Native SDK for IBM MobileFirst JSONStore

Description

The IBM Mobile Foundation JSONStore is a 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

  • Automatic upstream or downstream synchronization of on-device data with a Cloudant database (or a database compliant with the Couch DB protocol).
  • Data indexing for efficient searching
  • Mechanism for tracking local-only changes to the stored data
  • Support for multiple users
  • AES 256 encryption of stored data provides security and confidentiality. You can segment protection by user with password-protection, in the case of more than one user on a single device.

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 OR an on-premise installation of the IBM Mobile Foundation server.

Create a React Native Project

  • The first step is to create a React Native project. Let's call our app as the JSONStoreApp. Use the React Native CLI to create a new project.

react-native init JSONStoreApp

cd JSONStoreApp

  • Install the MobileFirst Core SDK and complete the Getting Started Instructions from here.

  • Next, add the IBM MobileFirst JSONStore SDK to your app

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

  • Link your project so that all native dependencies are added to your React Native project. This step is not required for react-native version 0.60 and above.

react-native link

  • Install Mobilefirst specific cocopods dependencies to the project.

cd ios && pod install

This step is applicable only for iOS platform.

Configure your Android App:

Add the following lines to the android section of <React-Native-Project>/android/app/build.gradle:

packagingOptions{
	exclude 'META-INF/ASL2.0'
}

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

  • Android
  • iOS

Documentation

Copyright 2018 IBM Corp.

IBM - IBM MobileFirst Platform Foundation v8.0 license agreement

Terms of Use