UNPKG

react-native-ibm-mobilefirst-jsonstore

Version:

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

274 lines (246 loc) 12.2 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 { _checkParamClassType, _checkParamType, _mandatoryParam, _isNativeType, _checkIfNativeType } from './Utils'; import { NativeModules, Platform } from 'react-native'; import 'react-native-get-random-values'; import { v4 as uuidv4 } from 'uuid'; var jsonStoreQueryPart = NativeModules.RNJSONStoreQueryPart; /** * @ignore */ const queryPartMap = new Map(); /** * This class is used to build query objects. It represents a group of operations that are joined with an AND. */ class JSONStoreQueryPart { /** * @ignore */ constructor() { queryPartMap.set(this, uuidv4()); jsonStoreQueryPart.init(queryPartMap.get(this)); } /** * Add BETWEEN criteria to the query part. * @param {string} searchField - The search field to compare start and end against with a BETWEEN statement. * @param {number} start - All documents that are returned must have the search field set to a number greater than or equal to this start. * @param {number} end - All documents that are returned must have the search field set to a number less than or equal to this end. */ addBetween(searchField = _mandatoryParam('searchField'), start = _mandatoryParam('start'), end = _mandatoryParam('end')) { _checkParamType(searchField, 'string'); _checkParamType(start, 'number'); _checkParamType(end, 'number'); jsonStoreQueryPart.addBetween(queryPartMap.get(this), searchField, start, end); return this; } /** * Add NOT BETWEEN criteria to the query part. * @param {string} searchField - The search field to compare start and end against with a NOT BETWEEN statement. * @param {number} start - All documents that are returned must have the search field set to a number greater than or equal to this start. * @param {number} end - All documents that are returned must have the search field set to a number less than or equal to this end. */ addNotBetween(searchField = _mandatoryParam('searchField'), start = _mandatoryParam('start'), end = _mandatoryParam('end')) { _checkParamType(searchField, 'string'); _checkParamType(start, 'number'); _checkParamType(end, 'number'); jsonStoreQueryPart.addNotBetween(queryPartMap.get(this), searchField, start, end); return this; } /** * Add GREATER THAN criteria to the query part. * @param {string} searchField - The search field to compare with a GREATER THAN statement. * @param {number} value - All documents returned must have the search field set to a number greater than this value. */ addGreaterThan(searchField = _mandatoryParam('searchField'), value = _mandatoryParam('value')) { _checkParamType(searchField, 'string'); _checkParamType(value, 'number'); jsonStoreQueryPart.addGreaterThan(queryPartMap.get(this), searchField, value); return this; } /** * Add GREATER THAN OR EQUAL criteria to the query part. * @param {string} searchField - The search field to compare with a GREATER THAN OR EQUAL statement. * @param {number} value - All documents returned must have the search field set to a number greater than or equal to this value. */ addGreaterThanOrEqual(searchField = _mandatoryParam('searchField'), value = _mandatoryParam('value')) { _checkParamType(searchField, 'string'); _checkParamType(value, 'number'); jsonStoreQueryPart.addGreaterThanOrEqual(queryPartMap.get(this), searchField, value); return this; } /** * Add LESS THAN criteria to the query part. * @param {string} searchField - The search field to compare with a LESS THAN statement. * @param {number} value - All documents returned must have the search field set to a number less than this value. */ addLessThan(searchField = _mandatoryParam('searchField'), value = _mandatoryParam('value')) { _checkParamType(searchField, 'string'); _checkParamType(value, 'number'); jsonStoreQueryPart.addLessThan(queryPartMap.get(this), searchField, value); return this; } /** * Add LESS THAN OR EQUAL criteria to the query part. * @param {string} searchField - The search field to compare with a LESS THAN OR EQUAL statement. * @param {number} value - All documents returned must have the search field set to a number less or equal than this value. */ addLessThanOrEqual(searchField = _mandatoryParam('searchField'), value = _mandatoryParam('value')) { _checkParamType(searchField, 'string'); _checkParamType(value, 'number'); jsonStoreQueryPart.addLessThanOrEqual(queryPartMap.get(this), searchField, value); return this; } /** * Add a INSIDE criteria to the query part. * @param {string} searchField - The search field to compare with an INSIDE statement. * @param {array} values - An non-empty list of numbers, booleans, and/or strings. All documents where the search field is one of the values in the list. */ addInside(searchField = _mandatoryParam('searchField'), values = _mandatoryParam('values')) { _checkParamType(searchField, 'string'); _checkParamClassType(values, Array); jsonStoreQueryPart.addInside(queryPartMap.get(this), searchField, values); return this; } /** * Add a NOT INSIDE criteria to the query part. * @param {string} searchField - The search field to compare with a NOT INSIDE statement. * @param {array} values - An non-empty list of numbers, booleans, and/or strings. All matching documents must not match the search field to one of the values in the list. */ addNotInside(searchField = _mandatoryParam('searchField'), values = _mandatoryParam('values')) { _checkParamType(searchField, 'string'); _checkParamClassType(values, Array); jsonStoreQueryPart.addNotInside(queryPartMap.get(this), searchField, values); return this; } /** * Add a EQUAL criteria to the query part. * @param {string} searchField - The search field to compare with an EQUAL statement. * @param {any} value - Search field's value for the EQUAL criteria. */ addEqual(searchField = _mandatoryParam('searchField'), value = _mandatoryParam('value')) { _checkParamType(searchField, 'string'); _checkIfNativeType(value); switch (typeof value) { case 'boolean': if (Platform.OS === 'ios'){ this.addInside(searchField, [value]); } else { jsonStoreQueryPart.addEqualBoolean(queryPartMap.get(this), searchField, value); } break; case 'string': jsonStoreQueryPart.addEqualString(queryPartMap.get(this), searchField, value); break; case 'number': if (Platform.OS === 'ios'){ this.addInside(searchField, [value]); } else { jsonStoreQueryPart.addEqualNumber(queryPartMap.get(this), searchField, value); } break; } return this; } /** * Add a NOT EQUAL criteria to the query part. * @param {string} searchField - The search field to compare with a NOT EQUAL statement. * @param {any} value - Search field's value for the NOT EQUAL criteria. */ addNotEqual(searchField = _mandatoryParam('searchField'), value = _mandatoryParam('value')) { _checkParamType(searchField, 'string'); _checkIfNativeType(value); switch (typeof value) { case 'boolean': if (Platform.OS === 'ios'){ this.addNotInside(searchField, [value]); } else { jsonStoreQueryPart.addNotEqualBoolean(queryPartMap.get(this), searchField, value); } break; case 'string': jsonStoreQueryPart.addNotEqualString(queryPartMap.get(this), searchField, value); break; case 'number': if (Platform.OS === 'ios'){ this.addNotInside(searchField, [value]); } jsonStoreQueryPart.addNotEqualNumber(queryPartMap.get(this), searchField, value); break; } return this; } /** * Add a LIKE criteria to the query part. * @param {string} searchField - The search field to compare with a LIKE statement. * @param {any} value - Search field's value for the LIKE criteria. */ addLike(searchField = _mandatoryParam('searchField'), value = _mandatoryParam('value')) { _checkParamType(searchField, 'string'); _checkParamType(value, 'string'); jsonStoreQueryPart.addLike(queryPartMap.get(this), searchField, value); return this; } /** * Add a LEFT LIKE criteria to the query part. * @param {string} searchField - The search field to compare start and end against with a LEFT LIKE statement. * @param {any} value - Search field's value for the LEFT LIKE criteria. */ addLeftLike(searchField = _mandatoryParam('searchField'), value = _mandatoryParam('value')) { _checkParamType(searchField, 'string'); _checkParamType(value, 'string'); jsonStoreQueryPart.addLeftLike(queryPartMap.get(this), searchField, value); return this; } /** * Add a RIGHT LIKE criteria to the query part. * @param {string} searchField - The search field to compare start and end against with a RIGHT LIKE statement. * @param {any} value - Search field's value for the RIGHT LIKE criteria. */ addRightLike(searchField = _mandatoryParam('searchField'), value = _mandatoryParam('value')) { _checkParamType(searchField, 'string'); _checkParamType(value, 'string'); jsonStoreQueryPart.addRightLike(queryPartMap.get(this), searchField, value); return this; } /** * Add a NOT LIKE criteria to the query part. * @param {string} searchField - The search field to compare start and end against with a NOT LIKE statement. * @param {any} value - Search field's value for the NOT LIKE criteria. */ addNotLike(searchField = _mandatoryParam('searchField'), value = _mandatoryParam('value')) { _checkParamType(searchField, 'string'); _checkParamType(value, 'string'); jsonStoreQueryPart.addNotLike(queryPartMap.get(this), searchField, value); return this; } /** * Add a NOT LEFT LIKE criteria to the query part. * @param {string} searchField - The search field to compare start and end against with a NOT LEFT LIKE statement. * @param {any} value - Search field's value for the NOT LEFT LIKE criteria. */ addNotLeftLike(searchField = _mandatoryParam('searchField'), value = _mandatoryParam('value')) { _checkParamType(searchField, 'string'); _checkParamType(value, 'string'); jsonStoreQueryPart.addNotLeftLike(queryPartMap.get(this), searchField, value); return this; } /** * Add a NOT RIGHT LIKE criteria to the query part. * @param {string} searchField - The search field to compare start and end against with a NOT RIGHT LIKE statement. * @param {any} value - Search field's value for the NOT RIGHT LIKE criteria. */ addNotRightLike(searchField = _mandatoryParam('searchField'), value = _mandatoryParam('value')) { _checkParamType(searchField, 'string'); _checkParamType(value, 'string'); jsonStoreQueryPart.addNotRightLike(queryPartMap.get(this), searchField, value); return this; } } export { JSONStoreQueryPart, queryPartMap }