query.js 5.9 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147
  1. "use strict";
  2. Object.defineProperty(exports, "__esModule", { value: true });
  3. const logic_1 = require("./logic");
  4. const symbol_1 = require("../helper/symbol");
  5. const index_1 = require("../geo/index");
  6. const type_1 = require("../utils/type");
  7. const validate_1 = require("../validate");
  8. exports.EQ = 'eq';
  9. exports.NEQ = 'neq';
  10. exports.GT = 'gt';
  11. exports.GTE = 'gte';
  12. exports.LT = 'lt';
  13. exports.LTE = 'lte';
  14. exports.IN = 'in';
  15. exports.NIN = 'nin';
  16. exports.ALL = 'all';
  17. exports.ELEM_MATCH = 'elemMatch';
  18. exports.EXISTS = 'exists';
  19. exports.SIZE = 'size';
  20. exports.MOD = 'mod';
  21. var QUERY_COMMANDS_LITERAL;
  22. (function (QUERY_COMMANDS_LITERAL) {
  23. QUERY_COMMANDS_LITERAL["EQ"] = "eq";
  24. QUERY_COMMANDS_LITERAL["NEQ"] = "neq";
  25. QUERY_COMMANDS_LITERAL["GT"] = "gt";
  26. QUERY_COMMANDS_LITERAL["GTE"] = "gte";
  27. QUERY_COMMANDS_LITERAL["LT"] = "lt";
  28. QUERY_COMMANDS_LITERAL["LTE"] = "lte";
  29. QUERY_COMMANDS_LITERAL["IN"] = "in";
  30. QUERY_COMMANDS_LITERAL["NIN"] = "nin";
  31. QUERY_COMMANDS_LITERAL["ALL"] = "all";
  32. QUERY_COMMANDS_LITERAL["ELEM_MATCH"] = "elemMatch";
  33. QUERY_COMMANDS_LITERAL["EXISTS"] = "exists";
  34. QUERY_COMMANDS_LITERAL["SIZE"] = "size";
  35. QUERY_COMMANDS_LITERAL["MOD"] = "mod";
  36. QUERY_COMMANDS_LITERAL["GEO_NEAR"] = "geoNear";
  37. QUERY_COMMANDS_LITERAL["GEO_WITHIN"] = "geoWithin";
  38. QUERY_COMMANDS_LITERAL["GEO_INTERSECTS"] = "geoIntersects";
  39. })(QUERY_COMMANDS_LITERAL = exports.QUERY_COMMANDS_LITERAL || (exports.QUERY_COMMANDS_LITERAL = {}));
  40. class QueryCommand extends logic_1.LogicCommand {
  41. constructor(operator, operands, fieldName) {
  42. super(operator, operands, fieldName);
  43. this.operator = operator;
  44. this._internalType = symbol_1.SYMBOL_QUERY_COMMAND;
  45. }
  46. toJSON() {
  47. switch (this.operator) {
  48. case QUERY_COMMANDS_LITERAL.IN:
  49. case QUERY_COMMANDS_LITERAL.NIN:
  50. return {
  51. ['$' + this.operator]: this.operands
  52. };
  53. case QUERY_COMMANDS_LITERAL.NEQ:
  54. return {
  55. ['$ne']: this.operands[0]
  56. };
  57. default:
  58. return {
  59. ['$' + this.operator]: this.operands[0]
  60. };
  61. }
  62. }
  63. _setFieldName(fieldName) {
  64. const command = new QueryCommand(this.operator, this.operands, fieldName);
  65. return command;
  66. }
  67. eq(val) {
  68. const command = new QueryCommand(QUERY_COMMANDS_LITERAL.EQ, [val], this.fieldName);
  69. return this.and(command);
  70. }
  71. neq(val) {
  72. const command = new QueryCommand(QUERY_COMMANDS_LITERAL.NEQ, [val], this.fieldName);
  73. return this.and(command);
  74. }
  75. gt(val) {
  76. const command = new QueryCommand(QUERY_COMMANDS_LITERAL.GT, [val], this.fieldName);
  77. return this.and(command);
  78. }
  79. gte(val) {
  80. const command = new QueryCommand(QUERY_COMMANDS_LITERAL.GTE, [val], this.fieldName);
  81. return this.and(command);
  82. }
  83. lt(val) {
  84. const command = new QueryCommand(QUERY_COMMANDS_LITERAL.LT, [val], this.fieldName);
  85. return this.and(command);
  86. }
  87. lte(val) {
  88. const command = new QueryCommand(QUERY_COMMANDS_LITERAL.LTE, [val], this.fieldName);
  89. return this.and(command);
  90. }
  91. in(list) {
  92. const command = new QueryCommand(QUERY_COMMANDS_LITERAL.IN, list, this.fieldName);
  93. return this.and(command);
  94. }
  95. nin(list) {
  96. const command = new QueryCommand(QUERY_COMMANDS_LITERAL.NIN, list, this.fieldName);
  97. return this.and(command);
  98. }
  99. geoNear(val) {
  100. if (!(val.geometry instanceof index_1.Point)) {
  101. throw new TypeError(`"geometry" must be of type Point. Received type ${typeof val.geometry}`);
  102. }
  103. if (val.maxDistance !== undefined && !type_1.isNumber(val.maxDistance)) {
  104. throw new TypeError(`"maxDistance" must be of type Number. Received type ${typeof val.maxDistance}`);
  105. }
  106. if (val.minDistance !== undefined && !type_1.isNumber(val.minDistance)) {
  107. throw new TypeError(`"minDistance" must be of type Number. Received type ${typeof val.minDistance}`);
  108. }
  109. const command = new QueryCommand(QUERY_COMMANDS_LITERAL.GEO_NEAR, [val], this.fieldName);
  110. return this.and(command);
  111. }
  112. geoWithin(val) {
  113. if (!(val.geometry instanceof index_1.MultiPolygon) &&
  114. !(val.geometry instanceof index_1.Polygon) &&
  115. !validate_1.Validate.isCentersPhere(val.centerSphere)) {
  116. throw new TypeError(`"geometry" must be of type Polygon or MultiPolygon. Received type ${typeof val.geometry}`);
  117. }
  118. const command = new QueryCommand(QUERY_COMMANDS_LITERAL.GEO_WITHIN, [val], this.fieldName);
  119. return this.and(command);
  120. }
  121. geoIntersects(val) {
  122. if (!(val.geometry instanceof index_1.Point) &&
  123. !(val.geometry instanceof index_1.LineString) &&
  124. !(val.geometry instanceof index_1.Polygon) &&
  125. !(val.geometry instanceof index_1.MultiPoint) &&
  126. !(val.geometry instanceof index_1.MultiLineString) &&
  127. !(val.geometry instanceof index_1.MultiPolygon)) {
  128. throw new TypeError(`"geometry" must be of type Point, LineString, Polygon, MultiPoint, MultiLineString or MultiPolygon. Received type ${typeof val.geometry}`);
  129. }
  130. const command = new QueryCommand(QUERY_COMMANDS_LITERAL.GEO_INTERSECTS, [val], this.fieldName);
  131. return this.and(command);
  132. }
  133. }
  134. exports.QueryCommand = QueryCommand;
  135. function isQueryCommand(object) {
  136. return object && object instanceof QueryCommand && object._internalType === symbol_1.SYMBOL_QUERY_COMMAND;
  137. }
  138. exports.isQueryCommand = isQueryCommand;
  139. function isKnownQueryCommand(object) {
  140. return isQueryCommand(object) && object.operator.toUpperCase() in QUERY_COMMANDS_LITERAL;
  141. }
  142. exports.isKnownQueryCommand = isKnownQueryCommand;
  143. function isComparisonCommand(object) {
  144. return isQueryCommand(object);
  145. }
  146. exports.isComparisonCommand = isComparisonCommand;
  147. exports.default = QueryCommand;