@cloudbase/js-sdk
Version:
cloudbase javascript sdk
155 lines (154 loc) • 7.01 kB
JavaScript
var __extends = (this && this.__extends) || (function () {
var extendStatics = function (d, b) {
extendStatics = Object.setPrototypeOf ||
({ __proto__: [] } instanceof Array && function (d, b) { d.__proto__ = b; }) ||
function (d, b) { for (var p in b) if (Object.prototype.hasOwnProperty.call(b, p)) d[p] = b[p]; };
return extendStatics(d, b);
};
return function (d, b) {
if (typeof b !== "function" && b !== null)
throw new TypeError("Class extends value " + String(b) + " is not a constructor or null");
extendStatics(d, b);
function __() { this.constructor = d; }
d.prototype = b === null ? Object.create(b) : (__.prototype = b.prototype, new __());
};
})();
import { LogicCommand } from './logic';
import { SYMBOL_QUERY_COMMAND } from '../helper/symbol';
import { Point, LineString, Polygon, MultiPoint, MultiLineString, MultiPolygon } from '../geo/index';
import { isNumber } from '../utils/type';
export var EQ = 'eq';
export var NEQ = 'neq';
export var GT = 'gt';
export var GTE = 'gte';
export var LT = 'lt';
export var LTE = 'lte';
export var IN = 'in';
export var NIN = 'nin';
export var ALL = 'all';
export var ELEM_MATCH = 'elemMatch';
export var EXISTS = 'exists';
export var SIZE = 'size';
export var MOD = 'mod';
export var QUERY_COMMANDS_LITERAL;
(function (QUERY_COMMANDS_LITERAL) {
QUERY_COMMANDS_LITERAL["EQ"] = "eq";
QUERY_COMMANDS_LITERAL["NEQ"] = "neq";
QUERY_COMMANDS_LITERAL["GT"] = "gt";
QUERY_COMMANDS_LITERAL["GTE"] = "gte";
QUERY_COMMANDS_LITERAL["LT"] = "lt";
QUERY_COMMANDS_LITERAL["LTE"] = "lte";
QUERY_COMMANDS_LITERAL["IN"] = "in";
QUERY_COMMANDS_LITERAL["NIN"] = "nin";
QUERY_COMMANDS_LITERAL["ALL"] = "all";
QUERY_COMMANDS_LITERAL["ELEM_MATCH"] = "elemMatch";
QUERY_COMMANDS_LITERAL["EXISTS"] = "exists";
QUERY_COMMANDS_LITERAL["SIZE"] = "size";
QUERY_COMMANDS_LITERAL["MOD"] = "mod";
QUERY_COMMANDS_LITERAL["GEO_NEAR"] = "geoNear";
QUERY_COMMANDS_LITERAL["GEO_WITHIN"] = "geoWithin";
QUERY_COMMANDS_LITERAL["GEO_INTERSECTS"] = "geoIntersects";
})(QUERY_COMMANDS_LITERAL || (QUERY_COMMANDS_LITERAL = {}));
var QueryCommand = (function (_super) {
__extends(QueryCommand, _super);
function QueryCommand(operator, operands, fieldName) {
var _this = _super.call(this, operator, operands, fieldName) || this;
_this.operator = operator;
_this._internalType = SYMBOL_QUERY_COMMAND;
return _this;
}
QueryCommand.prototype.toJSON = function () {
var _a, _b;
switch (this.operator) {
case QUERY_COMMANDS_LITERAL.IN:
case QUERY_COMMANDS_LITERAL.NIN:
return _a = {},
_a['$' + this.operator] = this.operands,
_a;
default:
return _b = {},
_b['$' + this.operator] = this.operands[0],
_b;
}
};
QueryCommand.prototype._setFieldName = function (fieldName) {
var command = new QueryCommand(this.operator, this.operands, fieldName);
return command;
};
QueryCommand.prototype.eq = function (val) {
var command = new QueryCommand(QUERY_COMMANDS_LITERAL.EQ, [val], this.fieldName);
return this.and(command);
};
QueryCommand.prototype.neq = function (val) {
var command = new QueryCommand(QUERY_COMMANDS_LITERAL.NEQ, [val], this.fieldName);
return this.and(command);
};
QueryCommand.prototype.gt = function (val) {
var command = new QueryCommand(QUERY_COMMANDS_LITERAL.GT, [val], this.fieldName);
return this.and(command);
};
QueryCommand.prototype.gte = function (val) {
var command = new QueryCommand(QUERY_COMMANDS_LITERAL.GTE, [val], this.fieldName);
return this.and(command);
};
QueryCommand.prototype.lt = function (val) {
var command = new QueryCommand(QUERY_COMMANDS_LITERAL.LT, [val], this.fieldName);
return this.and(command);
};
QueryCommand.prototype.lte = function (val) {
var command = new QueryCommand(QUERY_COMMANDS_LITERAL.LTE, [val], this.fieldName);
return this.and(command);
};
QueryCommand.prototype.in = function (list) {
var command = new QueryCommand(QUERY_COMMANDS_LITERAL.IN, list, this.fieldName);
return this.and(command);
};
QueryCommand.prototype.nin = function (list) {
var command = new QueryCommand(QUERY_COMMANDS_LITERAL.NIN, list, this.fieldName);
return this.and(command);
};
QueryCommand.prototype.geoNear = function (val) {
if (!(val.geometry instanceof Point)) {
throw new TypeError("\"geometry\" must be of type Point. Received type ".concat(typeof val.geometry));
}
if (val.maxDistance !== undefined && !isNumber(val.maxDistance)) {
throw new TypeError("\"maxDistance\" must be of type Number. Received type ".concat(typeof val.maxDistance));
}
if (val.minDistance !== undefined && !isNumber(val.minDistance)) {
throw new TypeError("\"minDistance\" must be of type Number. Received type ".concat(typeof val.minDistance));
}
var command = new QueryCommand(QUERY_COMMANDS_LITERAL.GEO_NEAR, [val], this.fieldName);
return this.and(command);
};
QueryCommand.prototype.geoWithin = function (val) {
if (!(val.geometry instanceof MultiPolygon) && !(val.geometry instanceof Polygon)) {
throw new TypeError("\"geometry\" must be of type Polygon or MultiPolygon. Received type ".concat(typeof val.geometry));
}
var command = new QueryCommand(QUERY_COMMANDS_LITERAL.GEO_WITHIN, [val], this.fieldName);
return this.and(command);
};
QueryCommand.prototype.geoIntersects = function (val) {
if (!(val.geometry instanceof Point) &&
!(val.geometry instanceof LineString) &&
!(val.geometry instanceof Polygon) &&
!(val.geometry instanceof MultiPoint) &&
!(val.geometry instanceof MultiLineString) &&
!(val.geometry instanceof MultiPolygon)) {
throw new TypeError("\"geometry\" must be of type Point, LineString, Polygon, MultiPoint, MultiLineString or MultiPolygon. Received type ".concat(typeof val.geometry));
}
var command = new QueryCommand(QUERY_COMMANDS_LITERAL.GEO_INTERSECTS, [val], this.fieldName);
return this.and(command);
};
return QueryCommand;
}(LogicCommand));
export { QueryCommand };
export function isQueryCommand(object) {
return object && object instanceof QueryCommand && object._internalType === SYMBOL_QUERY_COMMAND;
}
export function isKnownQueryCommand(object) {
return isQueryCommand(object) && object.operator.toUpperCase() in QUERY_COMMANDS_LITERAL;
}
export function isComparisonCommand(object) {
return isQueryCommand(object);
}
export default QueryCommand;