UNPKG

react-native-ibm-mobilefirst-jsonstore

Version:

React Native SDK for IBM JSONStore. Works with IBM Mobile First

193 lines (176 loc) 7.93 kB
/* Licensed Materials - Property of IBM * 5725-I43 (C) Copyright IBM Corp. 2018. All Rights Reserved. * US Government Users Restricted Rights - Use, duplication or * disclosure restricted by GSA ADP Schedule Contract with IBM Corp. */ /* author - Yash Soni | yashsoni21@in.ibm.com */ import { Platform, NativeModules, NativeEventEmitter, DeviceEventEmitter } from 'react-native'; import JSONStoreInitOptions from './JSONStoreInitOptions'; import { _checkParamClassType, _checkParamType, _mandatoryParam, ParamTypes } from './Utils'; var wlJsonStore = NativeModules.RNWLJSONStore; var eventEmitter; if(Platform.OS === 'ios') { eventEmitter = new NativeEventEmitter(NativeModules.RNWLJSONStore); } else { eventEmitter = DeviceEventEmitter; } /** * This class is the public WLJSONStore API for React-Native applications. */ class WLJSONStore { /** * @ignore */ constructor() { eventEmitter.addListener('syncSuccessEvent', function(event) { console.log("Successfully synced collection: '" + event.collectionName + "'."); }); eventEmitter.addListener('syncFailureEvent', function(event) { console.log("Failed to sync collection: '" + event.collectionName + "'."); }); } /** * Change the password for the given user. * @param {string} username - The user name. * @param {string} oldPassword - The old password. * @param {string} newPassword - The new password. * @param {number} [pbkdf2Iterations] - The number of iterations used by the Password-Based Key Derivation Function 2 algorithm used to secure the password. * @return {Promise<string, JSON>} - This function returns an empty string in case of success and it returns a JSON in case of any error. * @example - If the promise is rejected, an error JSON in the following format is available with more information. * { * errorMsg: "Error adding data to collection: favourites", * reason: "Collection is not initialized." * } */ async changePassword(username = _mandatoryParam('username'), oldPassword = _mandatoryParam('oldPassword'), newPassword = _mandatoryParam('newPassword'), pbkdf2Iterations = 10000) { _checkParamType(username, ParamTypes.STRING); _checkParamType(oldPassword, ParamTypes.STRING); _checkParamType(newPassword, ParamTypes.STRING); _checkParamType(pbkdf2Iterations, ParamTypes.NUMBER); result = await wlJsonStore .changePassword(username, oldPassword, newPassword, pbkdf2Iterations); return result; } /** * Locks access to all the collections until the init method is called. * @return {Promise<string, JSON>} - This function returns an empty string in case of success and it returns a JSON in case of any error * @example - If the promise is rejected, an error JSON in the following format is available with more information. * { * errorMsg: "Error adding data to collection: favourites", * reason: "Collection is not initialized." * } */ async closeAll() { result = await wlJsonStore .closeAll(); return result; } /** * Commit a transaction. * @return {Promise<string, JSON>} - This function returns an empty string in case of success and it returns a JSON in case of any error * @example - If the promise is rejected, an error JSON in the following format is available with more information. * { * errorMsg: "Error adding data to collection: favourites", * reason: "Collection is not initialized." * } */ async commitTransaction() { result = await wlJsonStore .commitTransaction(); return result; } /** * Permanently deletes all data for all users, clears security artifacts, and removes accessors. * @param {string} [username] - The user name. * @return {Promise<string, JSON>} - This function returns an empty string in case of success and it returns a JSON in case of any error * @example - If the promise is rejected, an error JSON in the following format is available with more information. * { * errorMsg: "Error adding data to collection: favourites", * reason: "Collection is not initialized." * } */ async destroy(username = null) { result = await wlJsonStore .destroy(username); return result; } /** * Returns an array of objects with information about all the stores in the device. * @return {Promise<Array, JSON>} - This function returns an Array of JSON Objects in case of success, representing the details of the JSONStore info, and it returns a JSON in case of any error * @example - This is how a sample response will look like: * [ * { * encrypted:false, * fileSize:7168, * username:"com.worklight.jsonstore" * } * ] */ async getFileInfo() { result = await wlJsonStore.getFileInfo(); return result; } /** * Provides access to the collections that are inside the store, and creates them if they do not already exist. * @param {array} collections - An array of collection definitions (JSONStoreCollection objects) that are to be initialized (either reopened or created). * @param {JSONStoreInitOptions} [options] - An object of class JSONStoreInitOptions. This is a specific set of options to initialize the collection with (such as security credentials). * @return {Promise<string, JSON>} - This function returns an empty string in case of success and it returns a JSON in case of any error * @example - If the promise is rejected, an error JSON in the following format is available with more information. * { * errorMsg: "Error adding data to collection: favourites", * reason: "Collection is not initialized." * } */ async openCollections(collections = _mandatoryParam('collections'), options = new JSONStoreInitOptions()) { _checkParamClassType(collections, Array); _checkParamClassType(options, JSONStoreInitOptions); result = await wlJsonStore .openCollections(collections, options); return result; } /** * Roll back a transaction. * @return {Promise<boolean, JSON>} - This returns a boolean value on completion - true for success, false for failure. * @example - If the promise is rejected, an error JSON in the following format is available with more information. * { * errorMsg: "Error adding data to collection: favourites", * reason: "Collection is not initialized." * } */ async rollbackTransaction() { result = await wlJsonStore .rollbackTransaction(); return result; } /** * Enable or disable the collection of analytic data for JSONStore. * @param {boolean} isAnalyticsEnabled - ture: to collect Analytics data, false: otherwise */ setAnalyticsEnabled(isAnalyticsEnabled = _mandatoryParam('isAnalyticsEnabled')) { _checkParamType(isAnalyticsEnabled, ParamTypes.BOOLEAN); wlJsonStore.setAnalyticsEnabled(isAnalyticsEnabled); } /** * Start a transaction. * @return {Promise<string, JSON>} - This returns an empty string on success, and a JSON in case of any error. * @example - If the promise is rejected, an error JSON in the following format is available with more information. * { * errorMsg: "Error adding data to collection: favourites", * reason: "Collection is not initialized." * } */ async startTransaction() { result = await wlJsonStore .startTransaction(); return result; } } export default WLJSONStore