OfflineQuery.js 12 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472
  1. function _createForOfIteratorHelperLoose(o, allowArrayLike) {
  2. var it = typeof Symbol !== "undefined" && o[Symbol.iterator] || o["@@iterator"];
  3. if (it) return (it = it.call(o)).next.bind(it);
  4. if (Array.isArray(o) || (it = _unsupportedIterableToArray(o)) || allowArrayLike && o && typeof o.length === "number") {
  5. if (it) o = it;
  6. var i = 0;
  7. return function () {
  8. if (i >= o.length) return {
  9. done: true
  10. };
  11. return {
  12. done: false,
  13. value: o[i++]
  14. };
  15. };
  16. }
  17. throw new TypeError("Invalid attempt to iterate non-iterable instance.\nIn order to be iterable, non-array objects must have a [Symbol.iterator]() method.");
  18. }
  19. function _unsupportedIterableToArray(o, minLen) {
  20. if (!o) return;
  21. if (typeof o === "string") return _arrayLikeToArray(o, minLen);
  22. var n = Object.prototype.toString.call(o).slice(8, -1);
  23. if (n === "Object" && o.constructor) n = o.constructor.name;
  24. if (n === "Map" || n === "Set") return Array.from(o);
  25. if (n === "Arguments" || /^(?:Ui|I)nt(?:8|16|32)(?:Clamped)?Array$/.test(n)) return _arrayLikeToArray(o, minLen);
  26. }
  27. function _arrayLikeToArray(arr, len) {
  28. if (len == null || len > arr.length) len = arr.length;
  29. for (var i = 0, arr2 = new Array(len); i < len; i++) {
  30. arr2[i] = arr[i];
  31. }
  32. return arr2;
  33. }
  34. var equalObjects = require('./equals').default;
  35. var decode = require('./decode').default;
  36. var ParseError = require('./ParseError').default;
  37. var ParsePolygon = require('./ParsePolygon').default;
  38. var ParseGeoPoint = require('./ParseGeoPoint').default;
  39. function contains(haystack, needle) {
  40. if (needle && needle.__type && (needle.__type === 'Pointer' || needle.__type === 'Object')) {
  41. for (var i in haystack) {
  42. var ptr = haystack[i];
  43. if (typeof ptr === 'string' && ptr === needle.objectId) {
  44. return true;
  45. }
  46. if (ptr.className === needle.className && ptr.objectId === needle.objectId) {
  47. return true;
  48. }
  49. }
  50. return false;
  51. }
  52. return haystack.indexOf(needle) > -1;
  53. }
  54. function transformObject(object) {
  55. if (object._toFullJSON) {
  56. return object._toFullJSON();
  57. }
  58. return object;
  59. }
  60. function matchesQuery(className, object, objects, query) {
  61. if (object.className !== className) {
  62. return false;
  63. }
  64. var obj = object;
  65. var q = query;
  66. if (object.toJSON) {
  67. obj = object.toJSON();
  68. }
  69. if (query.toJSON) {
  70. q = query.toJSON().where;
  71. }
  72. obj.className = className;
  73. for (var field in q) {
  74. if (!matchesKeyConstraints(className, obj, objects, field, q[field])) {
  75. return false;
  76. }
  77. }
  78. return true;
  79. }
  80. function equalObjectsGeneric(obj, compareTo, eqlFn) {
  81. if (Array.isArray(obj)) {
  82. for (var i = 0; i < obj.length; i++) {
  83. if (eqlFn(obj[i], compareTo)) {
  84. return true;
  85. }
  86. }
  87. return false;
  88. }
  89. return eqlFn(obj, compareTo);
  90. }
  91. function matchesKeyConstraints(className, object, objects, key, constraints) {
  92. if (constraints === null) {
  93. return false;
  94. }
  95. if (key.indexOf('.') >= 0) {
  96. var keyComponents = key.split('.');
  97. var subObjectKey = keyComponents[0];
  98. var keyRemainder = keyComponents.slice(1).join('.');
  99. return matchesKeyConstraints(className, object[subObjectKey] || {}, objects, keyRemainder, constraints);
  100. }
  101. var i;
  102. if (key === '$or') {
  103. for (i = 0; i < constraints.length; i++) {
  104. if (matchesQuery(className, object, objects, constraints[i])) {
  105. return true;
  106. }
  107. }
  108. return false;
  109. }
  110. if (key === '$and') {
  111. for (i = 0; i < constraints.length; i++) {
  112. if (!matchesQuery(className, object, objects, constraints[i])) {
  113. return false;
  114. }
  115. }
  116. return true;
  117. }
  118. if (key === '$nor') {
  119. for (i = 0; i < constraints.length; i++) {
  120. if (matchesQuery(className, object, objects, constraints[i])) {
  121. return false;
  122. }
  123. }
  124. return true;
  125. }
  126. if (key === '$relatedTo') {
  127. return false;
  128. }
  129. if (!/^[A-Za-z][0-9A-Za-z_]*$/.test(key)) {
  130. throw new ParseError(ParseError.INVALID_KEY_NAME, "Invalid Key: " + key);
  131. }
  132. if (typeof constraints !== 'object') {
  133. if (Array.isArray(object[key])) {
  134. return object[key].indexOf(constraints) > -1;
  135. }
  136. return object[key] === constraints;
  137. }
  138. var compareTo;
  139. if (constraints.__type) {
  140. if (constraints.__type === 'Pointer') {
  141. return equalObjectsGeneric(object[key], constraints, function (obj, ptr) {
  142. return typeof obj !== 'undefined' && ptr.className === obj.className && ptr.objectId === obj.objectId;
  143. });
  144. }
  145. return equalObjectsGeneric(decode(object[key]), decode(constraints), equalObjects);
  146. }
  147. for (var condition in constraints) {
  148. compareTo = constraints[condition];
  149. if (compareTo.__type) {
  150. compareTo = decode(compareTo);
  151. }
  152. if (toString.call(compareTo) === '[object Date]' || typeof compareTo === 'string' && new Date(compareTo) !== 'Invalid Date' && !isNaN(new Date(compareTo))) {
  153. object[key] = new Date(object[key].iso ? object[key].iso : object[key]);
  154. }
  155. switch (condition) {
  156. case '$lt':
  157. if (object[key] >= compareTo) {
  158. return false;
  159. }
  160. break;
  161. case '$lte':
  162. if (object[key] > compareTo) {
  163. return false;
  164. }
  165. break;
  166. case '$gt':
  167. if (object[key] <= compareTo) {
  168. return false;
  169. }
  170. break;
  171. case '$gte':
  172. if (object[key] < compareTo) {
  173. return false;
  174. }
  175. break;
  176. case '$ne':
  177. if (equalObjects(object[key], compareTo)) {
  178. return false;
  179. }
  180. break;
  181. case '$in':
  182. if (!contains(compareTo, object[key])) {
  183. return false;
  184. }
  185. break;
  186. case '$nin':
  187. if (contains(compareTo, object[key])) {
  188. return false;
  189. }
  190. break;
  191. case '$all':
  192. for (i = 0; i < compareTo.length; i++) {
  193. if (object[key].indexOf(compareTo[i]) < 0) {
  194. return false;
  195. }
  196. }
  197. break;
  198. case '$exists':
  199. {
  200. var propertyExists = typeof object[key] !== 'undefined';
  201. var existenceIsRequired = constraints.$exists;
  202. if (typeof constraints.$exists !== 'boolean') {
  203. break;
  204. }
  205. if (!propertyExists && existenceIsRequired || propertyExists && !existenceIsRequired) {
  206. return false;
  207. }
  208. break;
  209. }
  210. case '$regex':
  211. {
  212. if (typeof compareTo === 'object') {
  213. return compareTo.test(object[key]);
  214. }
  215. var expString = '';
  216. var escapeEnd = -2;
  217. var escapeStart = compareTo.indexOf('\\Q');
  218. while (escapeStart > -1) {
  219. expString += compareTo.substring(escapeEnd + 2, escapeStart);
  220. escapeEnd = compareTo.indexOf('\\E', escapeStart);
  221. if (escapeEnd > -1) {
  222. expString += compareTo.substring(escapeStart + 2, escapeEnd).replace(/\\\\\\\\E/g, '\\E').replace(/\W/g, '\\$&');
  223. }
  224. escapeStart = compareTo.indexOf('\\Q', escapeEnd);
  225. }
  226. expString += compareTo.substring(Math.max(escapeStart, escapeEnd + 2));
  227. var modifiers = constraints.$options || '';
  228. modifiers = modifiers.replace('x', '').replace('s', '');
  229. var exp = new RegExp(expString, modifiers);
  230. if (!exp.test(object[key])) {
  231. return false;
  232. }
  233. break;
  234. }
  235. case '$nearSphere':
  236. {
  237. if (!compareTo || !object[key]) {
  238. return false;
  239. }
  240. var distance = compareTo.radiansTo(object[key]);
  241. var max = constraints.$maxDistance || Infinity;
  242. return distance <= max;
  243. }
  244. case '$within':
  245. {
  246. if (!compareTo || !object[key]) {
  247. return false;
  248. }
  249. var southWest = compareTo.$box[0];
  250. var northEast = compareTo.$box[1];
  251. if (southWest.latitude > northEast.latitude || southWest.longitude > northEast.longitude) {
  252. return false;
  253. }
  254. return object[key].latitude > southWest.latitude && object[key].latitude < northEast.latitude && object[key].longitude > southWest.longitude && object[key].longitude < northEast.longitude;
  255. }
  256. case '$options':
  257. break;
  258. case '$maxDistance':
  259. break;
  260. case '$select':
  261. {
  262. var subQueryObjects = objects.filter(function (obj, index, arr) {
  263. return matchesQuery(compareTo.query.className, obj, arr, compareTo.query.where);
  264. });
  265. for (var _i = 0; _i < subQueryObjects.length; _i += 1) {
  266. var subObject = transformObject(subQueryObjects[_i]);
  267. return equalObjects(object[key], subObject[compareTo.key]);
  268. }
  269. return false;
  270. }
  271. case '$dontSelect':
  272. {
  273. var _subQueryObjects = objects.filter(function (obj, index, arr) {
  274. return matchesQuery(compareTo.query.className, obj, arr, compareTo.query.where);
  275. });
  276. for (var _i2 = 0; _i2 < _subQueryObjects.length; _i2 += 1) {
  277. var _subObject = transformObject(_subQueryObjects[_i2]);
  278. return !equalObjects(object[key], _subObject[compareTo.key]);
  279. }
  280. return false;
  281. }
  282. case '$inQuery':
  283. {
  284. var _subQueryObjects2 = objects.filter(function (obj, index, arr) {
  285. return matchesQuery(compareTo.className, obj, arr, compareTo.where);
  286. });
  287. for (var _i3 = 0; _i3 < _subQueryObjects2.length; _i3 += 1) {
  288. var _subObject2 = transformObject(_subQueryObjects2[_i3]);
  289. if (object[key].className === _subObject2.className && object[key].objectId === _subObject2.objectId) {
  290. return true;
  291. }
  292. }
  293. return false;
  294. }
  295. case '$notInQuery':
  296. {
  297. var _subQueryObjects3 = objects.filter(function (obj, index, arr) {
  298. return matchesQuery(compareTo.className, obj, arr, compareTo.where);
  299. });
  300. for (var _i4 = 0; _i4 < _subQueryObjects3.length; _i4 += 1) {
  301. var _subObject3 = transformObject(_subQueryObjects3[_i4]);
  302. if (object[key].className === _subObject3.className && object[key].objectId === _subObject3.objectId) {
  303. return false;
  304. }
  305. }
  306. return true;
  307. }
  308. case '$containedBy':
  309. {
  310. for (var _iterator = _createForOfIteratorHelperLoose(object[key]), _step; !(_step = _iterator()).done;) {
  311. var value = _step.value;
  312. if (!contains(compareTo, value)) {
  313. return false;
  314. }
  315. }
  316. return true;
  317. }
  318. case '$geoWithin':
  319. {
  320. var points = compareTo.$polygon.map(function (geoPoint) {
  321. return [geoPoint.latitude, geoPoint.longitude];
  322. });
  323. var polygon = new ParsePolygon(points);
  324. return polygon.containsPoint(object[key]);
  325. }
  326. case '$geoIntersects':
  327. {
  328. var _polygon = new ParsePolygon(object[key].coordinates);
  329. var point = new ParseGeoPoint(compareTo.$point);
  330. return _polygon.containsPoint(point);
  331. }
  332. default:
  333. return false;
  334. }
  335. }
  336. return true;
  337. }
  338. function validateQuery(query) {
  339. var q = query;
  340. if (query.toJSON) {
  341. q = query.toJSON().where;
  342. }
  343. var specialQuerykeys = ['$and', '$or', '$nor', '_rperm', '_wperm', '_perishable_token', '_email_verify_token', '_email_verify_token_expires_at', '_account_lockout_expires_at', '_failed_login_count'];
  344. Object.keys(q).forEach(function (key) {
  345. if (q && q[key] && q[key].$regex) {
  346. if (typeof q[key].$options === 'string') {
  347. if (!q[key].$options.match(/^[imxs]+$/)) {
  348. throw new ParseError(ParseError.INVALID_QUERY, "Bad $options value for query: " + q[key].$options);
  349. }
  350. }
  351. }
  352. if (specialQuerykeys.indexOf(key) < 0 && !key.match(/^[a-zA-Z][a-zA-Z0-9_.]*$/)) {
  353. throw new ParseError(ParseError.INVALID_KEY_NAME, "Invalid key name: " + key);
  354. }
  355. });
  356. }
  357. var OfflineQuery = {
  358. matchesQuery: matchesQuery,
  359. validateQuery: validateQuery
  360. };
  361. module.exports = OfflineQuery;