UNPKG

backendless

Version:

Backendless JavaScript SDK for Node.js and the browser

352 lines (351 loc) 11.3 kB
"use strict"; var _interopRequireDefault = require("@babel/runtime/helpers/interopRequireDefault"); Object.defineProperty(exports, "__esModule", { value: true }); exports["default"] = exports.PAGING_DEFAULTS = void 0; var _defineProperty2 = _interopRequireDefault(require("@babel/runtime/helpers/defineProperty")); var _classCallCheck2 = _interopRequireDefault(require("@babel/runtime/helpers/classCallCheck")); var _createClass2 = _interopRequireDefault(require("@babel/runtime/helpers/createClass")); var _utils = _interopRequireDefault(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; } var PAGING_DEFAULTS = { pageSize: 10, offset: 0 }; exports.PAGING_DEFAULTS = PAGING_DEFAULTS; var DataQueryBuilder = /*#__PURE__*/function () { function DataQueryBuilder() { (0, _classCallCheck2["default"])(this, DataQueryBuilder); this.offset = PAGING_DEFAULTS.offset; this.pageSize = PAGING_DEFAULTS.pageSize; this.sortBy = null; this.groupBy = null; this.properties = null; this.excludeProps = null; this.whereClause = null; this.havingClause = null; this.relations = null; this.relationsDepth = null; this.relationsPageSize = null; this.distinct = false; } (0, _createClass2["default"])(DataQueryBuilder, [{ key: "setPageSize", value: function setPageSize(pageSize) { if (pageSize <= 0) { throw new Error('Page size must be a positive value.'); } this.pageSize = pageSize; return this; } }, { key: "getPageSize", value: function getPageSize() { return this.pageSize; } }, { key: "setOffset", value: function setOffset(offset) { if (offset < 0) { throw new Error('Offset cannot have a negative value.'); } this.offset = offset; return this; } }, { key: "getOffset", value: function getOffset() { return this.offset; } }, { key: "prepareNextPage", value: function prepareNextPage() { this.setOffset(this.offset + this.pageSize); return this; } }, { key: "preparePreviousPage", value: function preparePreviousPage() { this.setOffset(Math.max(this.offset - this.pageSize, 0)); return this; } }, { key: "getProperties", value: function getProperties() { return this.properties; } }, { key: "setProperties", value: function setProperties(properties) { this.properties = _utils["default"].castArray(properties); return this; } }, { key: "addProperty", value: function addProperty(prop) { this.properties = this.properties || []; this.properties.push(prop); return this; } }, { key: "addProperties", value: function addProperties() { var _this = this; for (var _len = arguments.length, properties = new Array(_len), _key = 0; _key < _len; _key++) { properties[_key] = arguments[_key]; } properties.forEach(function (p) { _utils["default"].castArray(p).forEach(function (property) { return _this.addProperty(property); }); }); return this; } }, { key: "addAllProperties", value: function addAllProperties() { this.addProperty('*'); return this; } }, { key: "excludeProperty", value: function excludeProperty(property) { this.excludeProps = this.excludeProps || []; this.excludeProps.push(property); return this; } }, { key: "excludeProperties", value: function excludeProperties() { var _this2 = this; for (var _len2 = arguments.length, properties = new Array(_len2), _key2 = 0; _key2 < _len2; _key2++) { properties[_key2] = arguments[_key2]; } properties.forEach(function (p) { _utils["default"].castArray(p).forEach(function (property) { return _this2.excludeProperty(property); }); }); return this; } }, { key: "getExcludeProperties", value: function getExcludeProperties() { return this.excludeProps; } }, { key: "getWhereClause", value: function getWhereClause() { return this.whereClause; } }, { key: "setWhereClause", value: function setWhereClause(whereClause) { this.whereClause = whereClause; return this; } }, { key: "getHavingClause", value: function getHavingClause() { return this.havingClause; } }, { key: "setHavingClause", value: function setHavingClause(havingClause) { this.havingClause = havingClause; return this; } }, { key: "getSortBy", value: function getSortBy() { return this.sortBy; } }, { key: "setSortBy", value: function setSortBy(sortBy) { this.sortBy = _utils["default"].castArray(sortBy); return this; } }, { key: "getGroupBy", value: function getGroupBy() { return this.groupBy; } }, { key: "setGroupBy", value: function setGroupBy(groupBy) { this.groupBy = _utils["default"].castArray(groupBy); return this; } }, { key: "getRelated", value: function getRelated() { return this.relations; } }, { key: "setRelated", value: function setRelated(relations) { this.relations = _utils["default"].castArray(relations); return this; } }, { key: "addRelated", value: function addRelated(relations) { this.relations = (this.relations || []).concat(relations); return this; } }, { key: "setRelationsDepth", value: function setRelationsDepth(relationsDepth) { this.relationsDepth = relationsDepth; return this; } }, { key: "getRelationsDepth", value: function getRelationsDepth() { return this.relationsDepth; } }, { key: "setRelationsPageSize", value: function setRelationsPageSize(relationsPageSize) { this.relationsPageSize = relationsPageSize; return this; } }, { key: "getRelationsPageSize", value: function getRelationsPageSize() { return this.relationsPageSize; } }, { key: "setDistinct", value: function setDistinct(distinct) { this.distinct = !!distinct; return this; } }, { key: "getDistinct", value: function getDistinct() { return this.distinct; } }, { key: "setFileReferencePrefix", value: function setFileReferencePrefix(fileReferencePrefix) { this.fileReferencePrefix = fileReferencePrefix; return this; } }, { key: "getFileReferencePrefix", value: function getFileReferencePrefix() { return this.fileReferencePrefix; } }, { key: "toJSON", value: function toJSON() { return { pageSize: this.pageSize, offset: this.offset, properties: this.properties, excludeProps: this.excludeProps, where: this.whereClause, having: this.havingClause, sortBy: this.sortBy, groupBy: this.groupBy, relations: this.relations, relationsDepth: this.relationsDepth, relationsPageSize: this.relationsPageSize, distinct: this.distinct, fileReferencePrefix: this.fileReferencePrefix }; } }], [{ key: "create", value: function create() { return new this(); } }, { key: "toRequestBody", value: function toRequestBody(queryBuilder) { var query = queryBuilder instanceof DataQueryBuilder ? queryBuilder.toJSON() : queryBuilder ? _objectSpread({}, queryBuilder) : {}; Object.keys(query).forEach(function (param) { if (Array.isArray(query[param])) { if (param !== 'groupPath') { query[param] = query[param].join(','); } } else if (query[param] == null) { delete query[param]; } }); if (query.properties) { query.props = query.properties; delete query.properties; } if (query.relations) { query.loadRelations = query.relations; delete query.relations; } return query; } }, { key: "toQueryString", value: function toQueryString(query) { if (!query) { return; } if (query instanceof DataQueryBuilder) { query = query.toJSON(); } var queryTokens = []; if (query.pageSize > 0) { queryTokens.push("pageSize=".concat(query.pageSize)); } if (query.offset > 0) { queryTokens.push("offset=".concat(query.offset)); } if (Array.isArray(query.properties) && query.properties.length) { query.properties.map(function (property) { queryTokens.push("property=".concat(encodeURIComponent(property))); }); } if (Array.isArray(query.excludeProps) && query.excludeProps.length) { queryTokens.push("excludeProps=".concat(encodeArrayToUriComponent(query.excludeProps))); } if (query.where) { queryTokens.push("where=".concat(encodeURIComponent(query.where))); } if (query.having) { queryTokens.push("having=".concat(encodeURIComponent(query.having))); } if (query.sortBy) { queryTokens.push(Array.isArray(query.sortBy) ? "sortBy=".concat(encodeArrayToUriComponent(query.sortBy)) : "sortBy=".concat(encodeURIComponent(query.sortBy))); } if (query.groupBy) { queryTokens.push(Array.isArray(query.groupBy) ? "groupBy=".concat(encodeArrayToUriComponent(query.groupBy)) : "groupBy=".concat(encodeURIComponent(query.groupBy))); } if (Array.isArray(query.relations)) { queryTokens.push(query.relations.length ? "loadRelations=".concat(encodeArrayToUriComponent(query.relations)) : 'loadRelations=*'); } if (query.relationsDepth > 0) { queryTokens.push("relationsDepth=".concat(query.relationsDepth)); } if (query.relationsPageSize > 0) { queryTokens.push("relationsPageSize=".concat(query.relationsPageSize)); } if (query.distinct) { queryTokens.push("distinct=".concat(query.distinct)); } if (query.fileReferencePrefix) { queryTokens.push("fileReferencePrefix=".concat(encodeURIComponent(query.fileReferencePrefix))); } return queryTokens.join('&'); } }]); return DataQueryBuilder; }(); exports["default"] = DataQueryBuilder; function encodeArrayToUriComponent(items) { return items.map(function (item) { return encodeURIComponent(item); }).join(','); }