Home Reference Source

js/JSONStoreQuery.js

/* 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 { _checkParamClassType, _checkParamType, _mandatoryParam } from './Utils';

import {
    NativeModules
} from 'react-native';
var jsonStoreQuery = NativeModules.RNJSONStoreQuery;
import { queryPartMap, JSONStoreQueryPart } from './JSONStoreQueryPart';

import uuidv4 from 'uuid/v4';
/**
 * @ignore
 */
const queryMap = new Map();

/**
 * This class represents a group of query part objects that are joined with an OR statement. 
 */
class JSONStoreQuery {

    /**
     * @ignore
     */
    constructor() {
        queryMap.set(this, uuidv4());
        jsonStoreQuery.init(queryMap.get(this));
    }

    /**
     * Add a JSONStoreQueryPart object to the list of queries.
     * @param {JSONStoreQueryPart} queryPart - A query part that should be included in the query. All query parts are ORed together. For example, when adding query part A and query part B to the query content, the final query will be A OR B.
     */
    addQueryPart(queryPart = _mandatoryParam('queryPart')) {
        _checkParamClassType(queryPart, JSONStoreQueryPart);
        jsonStoreQuery.addQueryPart(queryMap.get(this), queryPartMap.get(queryPart));
    }

    /**
     * Retrieve the current list of query part objects in this query group.
     * @return {Promise<Array>} - A list of all query parts in the query content.
     * @example 
     * [
     *  {
     *      value: "[21, 50]", 
     *      operation: "BETWEEN", 
     *      isKeySpecial: false, 
     *      key: "age"
     *  },
     *  {
     *      value: "[20000, 40000]",
     *      operation: "BETWEEN", 
     *      isKeySpecial: false, 
     *      key: "salary"
     *  }
     * ]
     */
    async getAllQueryParts() {
        return jsonStoreQuery.getAllQueryParts(queryMap.get(this));
    }
}

export { JSONStoreQuery, queryMap }