update.js 4.8 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139
  1. import { UpdateCommand, isUpdateCommand, UPDATE_COMMANDS_LITERAL } from '../commands/update';
  2. import { SYMBOL_UNSET_FIELD_NAME } from '../helper/symbol';
  3. import { getType, isArray } from '../utils/type';
  4. import { operatorToString } from '../operator-map';
  5. import { flattenQueryObject, encodeInternalDataType, mergeConditionAfterEncode } from './common';
  6. import { stringifyByEJSON } from '../utils/utils';
  7. export class UpdateSerializer {
  8. constructor() { }
  9. static encode(query) {
  10. const stringifier = new UpdateSerializer();
  11. return stringifier.encodeUpdate(query);
  12. }
  13. static encodeEJSON(query) {
  14. const stringifier = new UpdateSerializer();
  15. return stringifyByEJSON(stringifier.encodeUpdate(query));
  16. }
  17. encodeUpdate(query) {
  18. if (isUpdateCommand(query)) {
  19. return this.encodeUpdateCommand(query);
  20. }
  21. else if (getType(query) === 'object') {
  22. return this.encodeUpdateObject(query);
  23. }
  24. else {
  25. return query;
  26. }
  27. }
  28. encodeUpdateCommand(query) {
  29. if (query.fieldName === SYMBOL_UNSET_FIELD_NAME) {
  30. throw new Error('Cannot encode a comparison command with unset field name');
  31. }
  32. switch (query.operator) {
  33. case UPDATE_COMMANDS_LITERAL.PUSH:
  34. case UPDATE_COMMANDS_LITERAL.PULL:
  35. case UPDATE_COMMANDS_LITERAL.PULL_ALL:
  36. case UPDATE_COMMANDS_LITERAL.POP:
  37. case UPDATE_COMMANDS_LITERAL.SHIFT:
  38. case UPDATE_COMMANDS_LITERAL.UNSHIFT:
  39. case UPDATE_COMMANDS_LITERAL.ADD_TO_SET: {
  40. return this.encodeArrayUpdateCommand(query);
  41. }
  42. default: {
  43. return this.encodeFieldUpdateCommand(query);
  44. }
  45. }
  46. }
  47. encodeFieldUpdateCommand(query) {
  48. const $op = operatorToString(query.operator);
  49. switch (query.operator) {
  50. case UPDATE_COMMANDS_LITERAL.REMOVE: {
  51. return {
  52. [$op]: {
  53. [query.fieldName]: ''
  54. }
  55. };
  56. }
  57. default: {
  58. return {
  59. [$op]: {
  60. [query.fieldName]: query.operands[0]
  61. }
  62. };
  63. }
  64. }
  65. }
  66. encodeArrayUpdateCommand(query) {
  67. const $op = operatorToString(query.operator);
  68. switch (query.operator) {
  69. case UPDATE_COMMANDS_LITERAL.PUSH: {
  70. let modifiers;
  71. if (isArray(query.operands)) {
  72. modifiers = {
  73. $each: query.operands.map(encodeInternalDataType)
  74. };
  75. }
  76. else {
  77. modifiers = query.operands;
  78. }
  79. return {
  80. [$op]: {
  81. [query.fieldName]: modifiers
  82. }
  83. };
  84. }
  85. case UPDATE_COMMANDS_LITERAL.UNSHIFT: {
  86. const modifiers = {
  87. $each: query.operands.map(encodeInternalDataType),
  88. $position: 0
  89. };
  90. return {
  91. [$op]: {
  92. [query.fieldName]: modifiers
  93. }
  94. };
  95. }
  96. case UPDATE_COMMANDS_LITERAL.POP: {
  97. return {
  98. [$op]: {
  99. [query.fieldName]: 1
  100. }
  101. };
  102. }
  103. case UPDATE_COMMANDS_LITERAL.SHIFT: {
  104. return {
  105. [$op]: {
  106. [query.fieldName]: -1
  107. }
  108. };
  109. }
  110. default: {
  111. return {
  112. [$op]: {
  113. [query.fieldName]: encodeInternalDataType(query.operands)
  114. }
  115. };
  116. }
  117. }
  118. }
  119. encodeUpdateObject(query) {
  120. const flattened = flattenQueryObject(query);
  121. for (const key in flattened) {
  122. if (/^\$/.test(key))
  123. continue;
  124. let val = flattened[key];
  125. if (isUpdateCommand(val)) {
  126. flattened[key] = val._setFieldName(key);
  127. const condition = this.encodeUpdateCommand(flattened[key]);
  128. mergeConditionAfterEncode(flattened, condition, key);
  129. }
  130. else {
  131. flattened[key] = val = encodeInternalDataType(val);
  132. const $setCommand = new UpdateCommand(UPDATE_COMMANDS_LITERAL.SET, [val], key);
  133. const condition = this.encodeUpdateCommand($setCommand);
  134. mergeConditionAfterEncode(flattened, condition, key);
  135. }
  136. }
  137. return flattened;
  138. }
  139. }