query.js 5.7 KB

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