backendless
Version:
Backendless JavaScript SDK for Node.js and the browser
379 lines (378 loc) • 16.8 kB
JavaScript
"use strict";
var _interopRequireDefault = require("@babel/runtime/helpers/interopRequireDefault");
Object.defineProperty(exports, "__esModule", {
value: true
});
exports.SortedSetStore = void 0;
var _toConsumableArray2 = _interopRequireDefault(require("@babel/runtime/helpers/toConsumableArray"));
var _classCallCheck2 = _interopRequireDefault(require("@babel/runtime/helpers/classCallCheck"));
var _createClass2 = _interopRequireDefault(require("@babel/runtime/helpers/createClass"));
var _inherits2 = _interopRequireDefault(require("@babel/runtime/helpers/inherits"));
var _possibleConstructorReturn2 = _interopRequireDefault(require("@babel/runtime/helpers/possibleConstructorReturn"));
var _getPrototypeOf2 = _interopRequireDefault(require("@babel/runtime/helpers/getPrototypeOf"));
var _defineProperty2 = _interopRequireDefault(require("@babel/runtime/helpers/defineProperty"));
var _baseStore = require("./base-store");
var _constants = require("../constants");
var _utils = _interopRequireDefault(require("../../utils"));
var _set = require("./set");
var _utils2 = require("../utils");
function ownKeys(object, enumerableOnly) { var keys = Object.keys(object); if (Object.getOwnPropertySymbols) { var symbols = Object.getOwnPropertySymbols(object); enumerableOnly && (symbols = symbols.filter(function (sym) { return Object.getOwnPropertyDescriptor(object, sym).enumerable; })), keys.push.apply(keys, symbols); } return keys; }
function _objectSpread(target) { for (var i = 1; i < arguments.length; i++) { var source = null != arguments[i] ? arguments[i] : {}; i % 2 ? ownKeys(Object(source), !0).forEach(function (key) { (0, _defineProperty2["default"])(target, key, source[key]); }) : Object.getOwnPropertyDescriptors ? Object.defineProperties(target, Object.getOwnPropertyDescriptors(source)) : ownKeys(Object(source)).forEach(function (key) { Object.defineProperty(target, key, Object.getOwnPropertyDescriptor(source, key)); }); } return target; }
function _createSuper(Derived) { var hasNativeReflectConstruct = _isNativeReflectConstruct(); return function _createSuperInternal() { var Super = (0, _getPrototypeOf2["default"])(Derived), result; if (hasNativeReflectConstruct) { var NewTarget = (0, _getPrototypeOf2["default"])(this).constructor; result = Reflect.construct(Super, arguments, NewTarget); } else { result = Super.apply(this, arguments); } return (0, _possibleConstructorReturn2["default"])(this, result); }; }
function _isNativeReflectConstruct() { if (typeof Reflect === "undefined" || !Reflect.construct) return false; if (Reflect.construct.sham) return false; if (typeof Proxy === "function") return true; try { Boolean.prototype.valueOf.call(Reflect.construct(Boolean, [], function () {})); return true; } catch (e) { return false; } }
var SortedSetStore = /*#__PURE__*/function (_HiveStore) {
(0, _inherits2["default"])(SortedSetStore, _HiveStore);
var _super = _createSuper(SortedSetStore);
function SortedSetStore() {
(0, _classCallCheck2["default"])(this, SortedSetStore);
return _super.apply(this, arguments);
}
(0, _createClass2["default"])(SortedSetStore, [{
key: "add",
value: function add(items, options) {
if (!items || !Array.isArray(items)) {
throw new Error('Items must be provided and must be an array.');
}
if (options !== undefined) {
if (!_utils["default"].isObject(options)) {
throw new Error('Options must be an object.');
}
var duplicateBehaviour = options.duplicateBehaviour,
scoreUpdateMode = options.scoreUpdateMode,
resultType = options.resultType;
if (duplicateBehaviour !== undefined && !['OnlyUpdate', 'AlwaysAdd'].includes(duplicateBehaviour)) {
throw new Error('Duplicate Behaviour argument must be one of this values: OnlyUpdate, AlwaysAdd.');
}
if (scoreUpdateMode !== undefined && !['Greater', 'Less'].includes(scoreUpdateMode)) {
throw new Error('Score Update Mode argument must be one of this values: Greater, Less.');
}
if (resultType !== undefined && !['NewAdded', 'TotalChanged'].includes(resultType)) {
throw new Error('Result Type must be one of this values: NewAdded, TotalChanged.');
}
}
return this.app.request.put({
url: "".concat(this.getBaseURL(), "/add"),
data: _objectSpread({
items: items
}, options)
});
}
}, {
key: "incrementScore",
value: function incrementScore(value, scoreValue) {
if (!(0, _utils2.isHiveValueValid)(value)) {
throw new Error('Value must be provided and must be one of types: string, number, boolean, object, array.');
}
if (isNaN(scoreValue) || typeof scoreValue !== 'number') {
throw new Error('ScoreValue must be provided and must be a number.');
}
return this.app.request.put({
url: "".concat(this.getBaseURL(), "/increment"),
data: {
scoreValue: scoreValue,
value: value
}
});
}
}, {
key: "decrementScore",
value: function decrementScore(value, scoreValue) {
if (!(0, _utils2.isHiveValueValid)(value)) {
throw new Error('Value must be provided and must be one of types: string, number, boolean, object, array.');
}
if (isNaN(scoreValue) || typeof scoreValue !== 'number') {
throw new Error('ScoreValue must be provided and must be a number.');
}
return this.app.request.put({
url: "".concat(this.getBaseURL(), "/decrement"),
data: {
scoreValue: scoreValue,
value: value
}
});
}
}, {
key: "getAndDeleteMaxScore",
value: function getAndDeleteMaxScore(count) {
if (count !== undefined && (isNaN(count) || typeof count !== 'number')) {
throw new Error('Count must be a number.');
}
return this.app.request.put({
url: "".concat(this.getBaseURL(), "/get-with-max-score-and-delete"),
query: {
count: count
}
});
}
}, {
key: "getAndDeleteMinScore",
value: function getAndDeleteMinScore(count) {
if (count !== undefined && (isNaN(count) || typeof count !== 'number')) {
throw new Error('Count must be a number.');
}
return this.app.request.put({
url: "".concat(this.getBaseURL(), "/get-with-min-score-and-delete"),
query: {
count: count
}
});
}
}, {
key: "getRandom",
value: function getRandom(options) {
if (options !== undefined) {
if (!_utils["default"].isObject(options)) {
throw new Error('Options must be an object.');
}
var count = options.count,
withScores = options.withScores;
if (count !== undefined && (isNaN(count) || typeof count !== 'number')) {
throw new Error('Count must be a number.');
}
if (withScores !== undefined && typeof withScores !== 'boolean') {
throw new Error('With Scores argument must be a boolean.');
}
}
return this.app.request.get({
url: "".concat(this.getBaseURL(), "/get-random"),
query: _objectSpread({}, options)
});
}
}, {
key: "getScore",
value: function getScore(value) {
if (!(0, _utils2.isHiveValueValid)(value)) {
throw new Error('Value must be provided and must be one of types: string, number, boolean, object, array.');
}
return this.app.request.post({
url: "".concat(this.getBaseURL(), "/get-score"),
data: {
value: value
}
});
}
}, {
key: "getRank",
value: function getRank(value, reverse) {
if (!(0, _utils2.isHiveValueValid)(value)) {
throw new Error('Value must be provided and must be one of types: string, number, boolean, object, array.');
}
if (reverse !== undefined && typeof reverse !== 'boolean') {
throw new Error('Reverse argument must be a boolean.');
}
return this.app.request.post({
url: "".concat(this.getBaseURL(), "/get-rank"),
data: {
value: value,
reverse: reverse
}
});
}
}, {
key: "getRangeByRank",
value: function getRangeByRank(startRank, stopRank, options) {
if (isNaN(startRank) || typeof startRank !== 'number') {
throw new Error('Start Rank must be provided and must be a number.');
}
if (isNaN(stopRank) || typeof stopRank !== 'number') {
throw new Error('Stop Rank must be provided and must be a number.');
}
if (options !== undefined) {
if (!_utils["default"].isObject(options)) {
throw new Error('Options must be an object.');
}
var withScores = options.withScores,
reverse = options.reverse;
if (withScores !== undefined && typeof withScores !== 'boolean') {
throw new Error('With Scores argument must be a boolean.');
}
if (reverse !== undefined && typeof reverse !== 'boolean') {
throw new Error('Reverse argument must be a boolean.');
}
}
return this.app.request.get({
url: "".concat(this.getBaseURL(), "/get-range-by-rank"),
query: _objectSpread({
startRank: startRank,
stopRank: stopRank
}, options)
});
}
}, {
key: "getRangeByScore",
value: function getRangeByScore(options) {
if (options !== undefined) {
if (!_utils["default"].isObject(options)) {
throw new Error('Options must be an object.');
}
var minScore = options.minScore,
maxScore = options.maxScore,
minBound = options.minBound,
maxBound = options.maxBound,
offset = options.offset,
pageSize = options.pageSize,
withScores = options.withScores,
reverse = options.reverse;
if (minScore !== undefined && (isNaN(minScore) || typeof minScore !== 'number')) {
throw new Error('Minimal Score must be a number.');
}
if (maxScore !== undefined && (isNaN(maxScore) || typeof maxScore !== 'number')) {
throw new Error('Maximal Score must be a number.');
}
if (minBound !== undefined && !['Include', 'Exclude', 'Infinity'].includes(minBound)) {
throw new Error('Minimal bound must be one of this values: Include, Exclude, Infinity.');
}
if (maxBound !== undefined && !['Include', 'Exclude', 'Infinity'].includes(maxBound)) {
throw new Error('Maximal bound must be one of this values: Include, Exclude, Infinity.');
}
if (offset !== undefined && (typeof offset !== 'number' || isNaN(offset))) {
throw new Error('Offset must be a number.');
}
if (pageSize !== undefined && (typeof pageSize !== 'number' || isNaN(pageSize))) {
throw new Error('Page Size must be a number.');
}
if (withScores !== undefined && typeof withScores !== 'boolean') {
throw new Error('With Scores argument must be a boolean.');
}
if (reverse !== undefined && typeof reverse !== 'boolean') {
throw new Error('Reverse argument must be a boolean.');
}
}
return this.app.request.get({
url: "".concat(this.getBaseURL(), "/get-range-by-score"),
query: _objectSpread({}, options)
});
}
}, {
key: "deleteValue",
value: function deleteValue(value) {
if (!(0, _utils2.isHiveValueValid)(value)) {
throw new Error('Value must be provided and must be one of types: string, number, boolean, object, array.');
}
return this.app.request["delete"]({
url: "".concat(this.getBaseURL(), "/values"),
data: [value]
});
}
}, {
key: "deleteValues",
value: function deleteValues(values) {
if (!values || !Array.isArray(values) || !values.length || !(0, _utils2.isHiveValueValid)(values)) {
throw new Error('Value must be provided and must be a list of JSON items.');
}
return this.app.request["delete"]({
url: "".concat(this.getBaseURL(), "/values"),
data: values
});
}
}, {
key: "deleteValuesByRank",
value: function deleteValuesByRank(startRank, stopRank) {
if (isNaN(startRank) || typeof startRank !== 'number') {
throw new Error('Start Rank must be provided and must be a number.');
}
if (isNaN(stopRank) || typeof stopRank !== 'number') {
throw new Error('Stop Rank must be provided and must be a number.');
}
return this.app.request["delete"]({
url: "".concat(this.getBaseURL(), "/delete-by-rank"),
query: {
startRank: startRank,
stopRank: stopRank
}
});
}
}, {
key: "deleteValuesByScore",
value: function deleteValuesByScore(options) {
if (options !== undefined) {
if (!_utils["default"].isObject(options)) {
throw new Error('Options must be an object.');
}
var minScore = options.minScore,
maxScore = options.maxScore,
minBound = options.minBound,
maxBound = options.maxBound;
if (minScore !== undefined && (isNaN(minScore) || typeof minScore !== 'number')) {
throw new Error('Minimal Score must be a number.');
}
if (maxScore !== undefined && (isNaN(maxScore) || typeof maxScore !== 'number')) {
throw new Error('Maximal Score must be a number.');
}
if (minBound !== undefined && !['Include', 'Exclude', 'Infinity'].includes(minBound)) {
throw new Error('Minimal bound must be one of this values: Include, Exclude, Infinity.');
}
if (maxBound !== undefined && !['Include', 'Exclude', 'Infinity'].includes(maxBound)) {
throw new Error('Maximal bound must be one of this values: Include, Exclude, Infinity.');
}
}
return this.app.request["delete"]({
url: "".concat(this.getBaseURL(), "/delete-by-score"),
query: _objectSpread({}, options)
});
}
}, {
key: "length",
value: function length() {
return this.app.request.get({
url: "".concat(this.getBaseURL(), "/length")
});
}
}, {
key: "countBetweenScores",
value: function countBetweenScores(options) {
if (options !== undefined) {
if (!_utils["default"].isObject(options)) {
throw new Error('Options must be an object.');
}
var minScore = options.minScore,
maxScore = options.maxScore,
minBound = options.minBound,
maxBound = options.maxBound;
if (minScore !== undefined && (isNaN(minScore) || typeof minScore !== 'number')) {
throw new Error('Minimal Score must be a number.');
}
if (maxScore !== undefined && (isNaN(maxScore) || typeof maxScore !== 'number')) {
throw new Error('Maximal Score must be a number.');
}
if (minBound !== undefined && !['Include', 'Exclude', 'Infinity'].includes(minBound)) {
throw new Error('Minimal bound must be one of this values: Include, Exclude, Infinity.');
}
if (maxBound !== undefined && !['Include', 'Exclude', 'Infinity'].includes(maxBound)) {
throw new Error('Maximal bound must be one of this values: Include, Exclude, Infinity.');
}
}
return this.app.request.get({
url: "".concat(this.getBaseURL(), "/count"),
query: _objectSpread({}, options)
});
}
}], [{
key: "intersection",
value: function intersection(keyNames) {
if (!Array.isArray(keyNames)) {
throw new Error('Store keys must be provided and must be an array.');
}
return this.app.request.post({
url: "".concat(this.app.urls.hiveStore(this.hiveName, this.TYPE), "/action/intersection"),
data: keyNames
});
}
}, {
key: "union",
value: function union(keyNames) {
if (!Array.isArray(keyNames)) {
throw new Error('Store keys must be provided and must be an array.');
}
return this.app.request.post({
url: "".concat(this.app.urls.hiveStore(this.hiveName, this.TYPE), "/action/union"),
data: keyNames
});
}
}]);
return SortedSetStore;
}(_baseStore.HiveStore);
exports.SortedSetStore = SortedSetStore;
(0, _defineProperty2["default"])(SortedSetStore, "TYPE", _constants.HiveTypes.SORTED_SET);
(0, _defineProperty2["default"])(SortedSetStore, "STATIC_METHODS", [].concat((0, _toConsumableArray2["default"])(_baseStore.HiveStore.STATIC_METHODS), ['difference', 'intersection', 'union']));
(0, _defineProperty2["default"])(SortedSetStore, "difference", _set.SetStore.difference);