No Description
You can not select more than 25 topics Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.

encodeFirestoreValue.js 1.8KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859
  1. "use strict";
  2. Object.defineProperty(exports, "__esModule", { value: true });
  3. exports.encodeFirestoreValue = void 0;
  4. const _ = require("lodash");
  5. const error_1 = require("../error");
  6. function isPlainObject(input) {
  7. return (typeof input === "object" &&
  8. input !== null &&
  9. _.isEqual(Object.getPrototypeOf(input), Object.prototype));
  10. }
  11. function encodeHelper(val) {
  12. if (typeof val === "string") {
  13. return { stringValue: val };
  14. }
  15. if (val === !!val) {
  16. return { booleanValue: val };
  17. }
  18. if (Number.isInteger(val)) {
  19. return { integerValue: val };
  20. }
  21. if (typeof val === "number") {
  22. return { doubleValue: val };
  23. }
  24. if (val instanceof Date && !Number.isNaN(val)) {
  25. return { timestampValue: val.toISOString() };
  26. }
  27. if (Array.isArray(val)) {
  28. const encodedElements = [];
  29. for (const v of val) {
  30. const enc = encodeHelper(v);
  31. if (enc) {
  32. encodedElements.push(enc);
  33. }
  34. }
  35. return {
  36. arrayValue: { values: encodedElements },
  37. };
  38. }
  39. if (val === null) {
  40. return { nullValue: "NULL_VALUE" };
  41. }
  42. if (val instanceof Buffer || val instanceof Uint8Array) {
  43. return { bytesValue: val };
  44. }
  45. if (isPlainObject(val)) {
  46. return {
  47. mapValue: { fields: encodeFirestoreValue(val) },
  48. };
  49. }
  50. throw new error_1.FirebaseError(`Cannot encode ${val} to a Firestore Value. ` +
  51. "The emulator does not yet support Firestore document reference values or geo points.");
  52. }
  53. function encodeFirestoreValue(data) {
  54. return Object.entries(data).reduce((acc, [key, val]) => {
  55. acc[key] = encodeHelper(val);
  56. return acc;
  57. }, {});
  58. }
  59. exports.encodeFirestoreValue = encodeFirestoreValue;