operators.js 1.9 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273747576777879808182838485
  1. 'use strict';
  2. const _ = require('lodash');
  3. const Op = require('../../../operators');
  4. const Utils = require('../../../utils');
  5. const OperatorHelpers = {
  6. OperatorMap: {
  7. [Op.eq]: '=',
  8. [Op.ne]: '!=',
  9. [Op.gte]: '>=',
  10. [Op.gt]: '>',
  11. [Op.lte]: '<=',
  12. [Op.lt]: '<',
  13. [Op.not]: 'IS NOT',
  14. [Op.is]: 'IS',
  15. [Op.in]: 'IN',
  16. [Op.notIn]: 'NOT IN',
  17. [Op.like]: 'LIKE',
  18. [Op.notLike]: 'NOT LIKE',
  19. [Op.iLike]: 'ILIKE',
  20. [Op.notILike]: 'NOT ILIKE',
  21. [Op.startsWith]: 'LIKE',
  22. [Op.endsWith]: 'LIKE',
  23. [Op.substring]: 'LIKE',
  24. [Op.regexp]: '~',
  25. [Op.notRegexp]: '!~',
  26. [Op.iRegexp]: '~*',
  27. [Op.notIRegexp]: '!~*',
  28. [Op.between]: 'BETWEEN',
  29. [Op.notBetween]: 'NOT BETWEEN',
  30. [Op.overlap]: '&&',
  31. [Op.contains]: '@>',
  32. [Op.contained]: '<@',
  33. [Op.adjacent]: '-|-',
  34. [Op.strictLeft]: '<<',
  35. [Op.strictRight]: '>>',
  36. [Op.noExtendRight]: '&<',
  37. [Op.noExtendLeft]: '&>',
  38. [Op.any]: 'ANY',
  39. [Op.all]: 'ALL',
  40. [Op.and]: ' AND ',
  41. [Op.or]: ' OR ',
  42. [Op.col]: 'COL',
  43. [Op.placeholder]: '$$PLACEHOLDER$$',
  44. [Op.match]: '@@'
  45. },
  46. OperatorsAliasMap: {},
  47. setOperatorsAliases(aliases) {
  48. if (!aliases || _.isEmpty(aliases)) {
  49. this.OperatorsAliasMap = false;
  50. } else {
  51. this.OperatorsAliasMap = { ...aliases };
  52. }
  53. },
  54. _replaceAliases(orig) {
  55. const obj = {};
  56. if (!this.OperatorsAliasMap) {
  57. return orig;
  58. }
  59. Utils.getOperators(orig).forEach(op => {
  60. const item = orig[op];
  61. if (_.isPlainObject(item)) {
  62. obj[op] = this._replaceAliases(item);
  63. } else {
  64. obj[op] = item;
  65. }
  66. });
  67. _.forOwn(orig, (item, prop) => {
  68. prop = this.OperatorsAliasMap[prop] || prop;
  69. if (_.isPlainObject(item)) {
  70. item = this._replaceAliases(item);
  71. }
  72. obj[prop] = item;
  73. });
  74. return obj;
  75. }
  76. };
  77. module.exports = OperatorHelpers;