OfflineQuery.js 14 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470
  1. var _interopRequireDefault = require("@babel/runtime/helpers/interopRequireDefault");
  2. var _slicedToArray2 = _interopRequireDefault(require("@babel/runtime/helpers/slicedToArray"));
  3. var equalObjects = require('./equals').default;
  4. var decode = require('./decode').default;
  5. var ParseError = require('./ParseError').default;
  6. var ParsePolygon = require('./ParsePolygon').default;
  7. var ParseGeoPoint = require('./ParseGeoPoint').default;
  8. function contains(haystack, needle) {
  9. if (needle && needle.__type && (needle.__type === 'Pointer' || needle.__type === 'Object')) {
  10. for (var i in haystack) {
  11. var ptr = haystack[i];
  12. if (typeof ptr === 'string' && ptr === needle.objectId) {
  13. return true;
  14. }
  15. if (ptr.className === needle.className && ptr.objectId === needle.objectId) {
  16. return true;
  17. }
  18. }
  19. return false;
  20. }
  21. if (Array.isArray(needle)) {
  22. for (var need of needle) {
  23. if (contains(haystack, need)) {
  24. return true;
  25. }
  26. }
  27. }
  28. return haystack.indexOf(needle) > -1;
  29. }
  30. function transformObject(object) {
  31. if (object._toFullJSON) {
  32. return object._toFullJSON();
  33. }
  34. return object;
  35. }
  36. function matchesQuery(className, object, objects, query) {
  37. if (object.className !== className) {
  38. return false;
  39. }
  40. var obj = object;
  41. var q = query;
  42. if (object.toJSON) {
  43. obj = object.toJSON();
  44. }
  45. if (query.toJSON) {
  46. q = query.toJSON().where;
  47. }
  48. obj.className = className;
  49. for (var field in q) {
  50. if (!matchesKeyConstraints(className, obj, objects, field, q[field])) {
  51. return false;
  52. }
  53. }
  54. return true;
  55. }
  56. function equalObjectsGeneric(obj, compareTo, eqlFn) {
  57. if (Array.isArray(obj)) {
  58. for (var i = 0; i < obj.length; i++) {
  59. if (eqlFn(obj[i], compareTo)) {
  60. return true;
  61. }
  62. }
  63. return false;
  64. }
  65. return eqlFn(obj, compareTo);
  66. }
  67. function relativeTimeToDate(text) {
  68. var now = arguments.length > 1 && arguments[1] !== undefined ? arguments[1] : new Date();
  69. text = text.toLowerCase();
  70. var parts = text.split(' ');
  71. parts = parts.filter(function (part) {
  72. return part !== '';
  73. });
  74. var future = parts[0] === 'in';
  75. var past = parts[parts.length - 1] === 'ago';
  76. if (!future && !past && text !== 'now') {
  77. return {
  78. status: 'error',
  79. info: "Time should either start with 'in' or end with 'ago'"
  80. };
  81. }
  82. if (future && past) {
  83. return {
  84. status: 'error',
  85. info: "Time cannot have both 'in' and 'ago'"
  86. };
  87. }
  88. if (future) {
  89. parts = parts.slice(1);
  90. } else {
  91. parts = parts.slice(0, parts.length - 1);
  92. }
  93. if (parts.length % 2 !== 0 && text !== 'now') {
  94. return {
  95. status: 'error',
  96. info: 'Invalid time string. Dangling unit or number.'
  97. };
  98. }
  99. var pairs = [];
  100. while (parts.length) {
  101. pairs.push([parts.shift(), parts.shift()]);
  102. }
  103. var seconds = 0;
  104. for (var _ref of pairs) {
  105. var _ref2 = (0, _slicedToArray2.default)(_ref, 2);
  106. var num = _ref2[0];
  107. var interval = _ref2[1];
  108. var val = Number(num);
  109. if (!Number.isInteger(val)) {
  110. return {
  111. status: 'error',
  112. info: `'${num}' is not an integer.`
  113. };
  114. }
  115. switch (interval) {
  116. case 'yr':
  117. case 'yrs':
  118. case 'year':
  119. case 'years':
  120. seconds += val * 31536000;
  121. break;
  122. case 'wk':
  123. case 'wks':
  124. case 'week':
  125. case 'weeks':
  126. seconds += val * 604800;
  127. break;
  128. case 'd':
  129. case 'day':
  130. case 'days':
  131. seconds += val * 86400;
  132. break;
  133. case 'hr':
  134. case 'hrs':
  135. case 'hour':
  136. case 'hours':
  137. seconds += val * 3600;
  138. break;
  139. case 'min':
  140. case 'mins':
  141. case 'minute':
  142. case 'minutes':
  143. seconds += val * 60;
  144. break;
  145. case 'sec':
  146. case 'secs':
  147. case 'second':
  148. case 'seconds':
  149. seconds += val;
  150. break;
  151. default:
  152. return {
  153. status: 'error',
  154. info: `Invalid interval: '${interval}'`
  155. };
  156. }
  157. }
  158. var milliseconds = seconds * 1000;
  159. if (future) {
  160. return {
  161. status: 'success',
  162. info: 'future',
  163. result: new Date(now.valueOf() + milliseconds)
  164. };
  165. } else if (past) {
  166. return {
  167. status: 'success',
  168. info: 'past',
  169. result: new Date(now.valueOf() - milliseconds)
  170. };
  171. } else {
  172. return {
  173. status: 'success',
  174. info: 'present',
  175. result: new Date(now.valueOf())
  176. };
  177. }
  178. }
  179. function matchesKeyConstraints(className, object, objects, key, constraints) {
  180. if (constraints === null) {
  181. return false;
  182. }
  183. if (key.indexOf('.') >= 0) {
  184. var keyComponents = key.split('.');
  185. var subObjectKey = keyComponents[0];
  186. var keyRemainder = keyComponents.slice(1).join('.');
  187. return matchesKeyConstraints(className, object[subObjectKey] || {}, objects, keyRemainder, constraints);
  188. }
  189. var i;
  190. if (key === '$or') {
  191. for (i = 0; i < constraints.length; i++) {
  192. if (matchesQuery(className, object, objects, constraints[i])) {
  193. return true;
  194. }
  195. }
  196. return false;
  197. }
  198. if (key === '$and') {
  199. for (i = 0; i < constraints.length; i++) {
  200. if (!matchesQuery(className, object, objects, constraints[i])) {
  201. return false;
  202. }
  203. }
  204. return true;
  205. }
  206. if (key === '$nor') {
  207. for (i = 0; i < constraints.length; i++) {
  208. if (matchesQuery(className, object, objects, constraints[i])) {
  209. return false;
  210. }
  211. }
  212. return true;
  213. }
  214. if (key === '$relatedTo') {
  215. return false;
  216. }
  217. if (!/^[A-Za-z][0-9A-Za-z_]*$/.test(key)) {
  218. throw new ParseError(ParseError.INVALID_KEY_NAME, `Invalid Key: ${key}`);
  219. }
  220. if (typeof constraints !== 'object') {
  221. if (Array.isArray(object[key])) {
  222. return object[key].indexOf(constraints) > -1;
  223. }
  224. return object[key] === constraints;
  225. }
  226. var compareTo;
  227. if (constraints.__type) {
  228. if (constraints.__type === 'Pointer') {
  229. return equalObjectsGeneric(object[key], constraints, function (obj, ptr) {
  230. return typeof obj !== 'undefined' && ptr.className === obj.className && ptr.objectId === obj.objectId;
  231. });
  232. }
  233. return equalObjectsGeneric(decode(object[key]), decode(constraints), equalObjects);
  234. }
  235. for (var condition in constraints) {
  236. compareTo = constraints[condition];
  237. if (compareTo.__type) {
  238. compareTo = decode(compareTo);
  239. }
  240. if (compareTo['$relativeTime']) {
  241. var parserResult = relativeTimeToDate(compareTo['$relativeTime']);
  242. if (parserResult.status !== 'success') {
  243. throw new ParseError(ParseError.INVALID_JSON, `bad $relativeTime (${key}) value. ${parserResult.info}`);
  244. }
  245. compareTo = parserResult.result;
  246. }
  247. if (toString.call(compareTo) === '[object Date]' || typeof compareTo === 'string' && new Date(compareTo) !== 'Invalid Date' && !isNaN(new Date(compareTo))) {
  248. object[key] = new Date(object[key].iso ? object[key].iso : object[key]);
  249. }
  250. switch (condition) {
  251. case '$lt':
  252. if (object[key] >= compareTo) {
  253. return false;
  254. }
  255. break;
  256. case '$lte':
  257. if (object[key] > compareTo) {
  258. return false;
  259. }
  260. break;
  261. case '$gt':
  262. if (object[key] <= compareTo) {
  263. return false;
  264. }
  265. break;
  266. case '$gte':
  267. if (object[key] < compareTo) {
  268. return false;
  269. }
  270. break;
  271. case '$ne':
  272. if (equalObjects(object[key], compareTo)) {
  273. return false;
  274. }
  275. break;
  276. case '$in':
  277. if (!contains(compareTo, object[key])) {
  278. return false;
  279. }
  280. break;
  281. case '$nin':
  282. if (contains(compareTo, object[key])) {
  283. return false;
  284. }
  285. break;
  286. case '$all':
  287. for (i = 0; i < compareTo.length; i++) {
  288. if (object[key].indexOf(compareTo[i]) < 0) {
  289. return false;
  290. }
  291. }
  292. break;
  293. case '$exists':
  294. {
  295. var propertyExists = typeof object[key] !== 'undefined';
  296. var existenceIsRequired = constraints['$exists'];
  297. if (typeof constraints['$exists'] !== 'boolean') {
  298. break;
  299. }
  300. if (!propertyExists && existenceIsRequired || propertyExists && !existenceIsRequired) {
  301. return false;
  302. }
  303. break;
  304. }
  305. case '$regex':
  306. {
  307. if (typeof compareTo === 'object') {
  308. return compareTo.test(object[key]);
  309. }
  310. var expString = '';
  311. var escapeEnd = -2;
  312. var escapeStart = compareTo.indexOf('\\Q');
  313. while (escapeStart > -1) {
  314. expString += compareTo.substring(escapeEnd + 2, escapeStart);
  315. escapeEnd = compareTo.indexOf('\\E', escapeStart);
  316. if (escapeEnd > -1) {
  317. expString += compareTo.substring(escapeStart + 2, escapeEnd).replace(/\\\\\\\\E/g, '\\E').replace(/\W/g, '\\$&');
  318. }
  319. escapeStart = compareTo.indexOf('\\Q', escapeEnd);
  320. }
  321. expString += compareTo.substring(Math.max(escapeStart, escapeEnd + 2));
  322. var modifiers = constraints.$options || '';
  323. modifiers = modifiers.replace('x', '').replace('s', '');
  324. var exp = new RegExp(expString, modifiers);
  325. if (!exp.test(object[key])) {
  326. return false;
  327. }
  328. break;
  329. }
  330. case '$nearSphere':
  331. {
  332. if (!compareTo || !object[key]) {
  333. return false;
  334. }
  335. var distance = compareTo.radiansTo(object[key]);
  336. var max = constraints.$maxDistance || Infinity;
  337. return distance <= max;
  338. }
  339. case '$within':
  340. {
  341. if (!compareTo || !object[key]) {
  342. return false;
  343. }
  344. var southWest = compareTo.$box[0];
  345. var northEast = compareTo.$box[1];
  346. if (southWest.latitude > northEast.latitude || southWest.longitude > northEast.longitude) {
  347. return false;
  348. }
  349. return object[key].latitude > southWest.latitude && object[key].latitude < northEast.latitude && object[key].longitude > southWest.longitude && object[key].longitude < northEast.longitude;
  350. }
  351. case '$options':
  352. break;
  353. case '$maxDistance':
  354. break;
  355. case '$select':
  356. {
  357. var subQueryObjects = objects.filter(function (obj, index, arr) {
  358. return matchesQuery(compareTo.query.className, obj, arr, compareTo.query.where);
  359. });
  360. for (var _i = 0; _i < subQueryObjects.length; _i += 1) {
  361. var subObject = transformObject(subQueryObjects[_i]);
  362. return equalObjects(object[key], subObject[compareTo.key]);
  363. }
  364. return false;
  365. }
  366. case '$dontSelect':
  367. {
  368. var _subQueryObjects = objects.filter(function (obj, index, arr) {
  369. return matchesQuery(compareTo.query.className, obj, arr, compareTo.query.where);
  370. });
  371. for (var _i2 = 0; _i2 < _subQueryObjects.length; _i2 += 1) {
  372. var _subObject = transformObject(_subQueryObjects[_i2]);
  373. return !equalObjects(object[key], _subObject[compareTo.key]);
  374. }
  375. return false;
  376. }
  377. case '$inQuery':
  378. {
  379. var _subQueryObjects2 = objects.filter(function (obj, index, arr) {
  380. return matchesQuery(compareTo.className, obj, arr, compareTo.where);
  381. });
  382. for (var _i3 = 0; _i3 < _subQueryObjects2.length; _i3 += 1) {
  383. var _subObject2 = transformObject(_subQueryObjects2[_i3]);
  384. if (object[key].className === _subObject2.className && object[key].objectId === _subObject2.objectId) {
  385. return true;
  386. }
  387. }
  388. return false;
  389. }
  390. case '$notInQuery':
  391. {
  392. var _subQueryObjects3 = objects.filter(function (obj, index, arr) {
  393. return matchesQuery(compareTo.className, obj, arr, compareTo.where);
  394. });
  395. for (var _i4 = 0; _i4 < _subQueryObjects3.length; _i4 += 1) {
  396. var _subObject3 = transformObject(_subQueryObjects3[_i4]);
  397. if (object[key].className === _subObject3.className && object[key].objectId === _subObject3.objectId) {
  398. return false;
  399. }
  400. }
  401. return true;
  402. }
  403. case '$containedBy':
  404. {
  405. for (var value of object[key]) {
  406. if (!contains(compareTo, value)) {
  407. return false;
  408. }
  409. }
  410. return true;
  411. }
  412. case '$geoWithin':
  413. {
  414. if (compareTo.$polygon) {
  415. var points = compareTo.$polygon.map(function (geoPoint) {
  416. return [geoPoint.latitude, geoPoint.longitude];
  417. });
  418. var polygon = new ParsePolygon(points);
  419. return polygon.containsPoint(object[key]);
  420. }
  421. if (compareTo.$centerSphere) {
  422. var _compareTo$$centerSph = (0, _slicedToArray2.default)(compareTo.$centerSphere, 2),
  423. WGS84Point = _compareTo$$centerSph[0],
  424. maxDistance = _compareTo$$centerSph[1];
  425. var centerPoint = new ParseGeoPoint({
  426. latitude: WGS84Point[1],
  427. longitude: WGS84Point[0]
  428. });
  429. var point = new ParseGeoPoint(object[key]);
  430. var _distance = point.radiansTo(centerPoint);
  431. return _distance <= maxDistance;
  432. }
  433. break;
  434. }
  435. case '$geoIntersects':
  436. {
  437. var _polygon = new ParsePolygon(object[key].coordinates);
  438. var _point = new ParseGeoPoint(compareTo.$point);
  439. return _polygon.containsPoint(_point);
  440. }
  441. default:
  442. return false;
  443. }
  444. }
  445. return true;
  446. }
  447. function validateQuery(query) {
  448. var q = query;
  449. if (query.toJSON) {
  450. q = query.toJSON().where;
  451. }
  452. var specialQuerykeys = ['$and', '$or', '$nor', '_rperm', '_wperm', '_perishable_token', '_email_verify_token', '_email_verify_token_expires_at', '_account_lockout_expires_at', '_failed_login_count'];
  453. Object.keys(q).forEach(function (key) {
  454. if (q && q[key] && q[key].$regex) {
  455. if (typeof q[key].$options === 'string') {
  456. if (!q[key].$options.match(/^[imxs]+$/)) {
  457. throw new ParseError(ParseError.INVALID_QUERY, `Bad $options value for query: ${q[key].$options}`);
  458. }
  459. }
  460. }
  461. if (specialQuerykeys.indexOf(key) < 0 && !key.match(/^[a-zA-Z][a-zA-Z0-9_\.]*$/)) {
  462. throw new ParseError(ParseError.INVALID_KEY_NAME, `Invalid key name: ${key}`);
  463. }
  464. });
  465. }
  466. var OfflineQuery = {
  467. matchesQuery: matchesQuery,
  468. validateQuery: validateQuery
  469. };
  470. module.exports = OfflineQuery;