operators.js 1.9 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384
  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. },
  45. OperatorsAliasMap: {},
  46. setOperatorsAliases(aliases) {
  47. if (!aliases || _.isEmpty(aliases)) {
  48. this.OperatorsAliasMap = false;
  49. } else {
  50. this.OperatorsAliasMap = Object.assign({}, aliases);
  51. }
  52. },
  53. _replaceAliases(orig) {
  54. const obj = {};
  55. if (!this.OperatorsAliasMap) {
  56. return orig;
  57. }
  58. Utils.getOperators(orig).forEach(op => {
  59. const item = orig[op];
  60. if (_.isPlainObject(item)) {
  61. obj[op] = this._replaceAliases(item);
  62. } else {
  63. obj[op] = item;
  64. }
  65. });
  66. _.forOwn(orig, (item, prop) => {
  67. prop = this.OperatorsAliasMap[prop] || prop;
  68. if (_.isPlainObject(item)) {
  69. item = this._replaceAliases(item);
  70. }
  71. obj[prop] = item;
  72. });
  73. return obj;
  74. }
  75. };
  76. module.exports = OperatorHelpers;