compile.js 3.9 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147
  1. 'use strict';
  2. const get = require('../../helpers/get');
  3. const getSymbol = require('../../helpers/symbols').getSymbol;
  4. const utils = require('../../utils');
  5. let Document;
  6. /*!
  7. * exports
  8. */
  9. exports.compile = compile;
  10. exports.defineKey = defineKey;
  11. /*!
  12. * Compiles schemas.
  13. */
  14. function compile(tree, proto, prefix, options) {
  15. Document = Document || require('../../document');
  16. const keys = Object.keys(tree);
  17. const len = keys.length;
  18. let limb;
  19. let key;
  20. for (let i = 0; i < len; ++i) {
  21. key = keys[i];
  22. limb = tree[key];
  23. const hasSubprops = utils.isPOJO(limb) && Object.keys(limb).length &&
  24. (!limb[options.typeKey] || (options.typeKey === 'type' && limb.type.type));
  25. const subprops = hasSubprops ? limb : null;
  26. defineKey(key, subprops, proto, prefix, keys, options);
  27. }
  28. }
  29. /*!
  30. * Defines the accessor named prop on the incoming prototype.
  31. */
  32. function defineKey(prop, subprops, prototype, prefix, keys, options) {
  33. Document = Document || require('../../document');
  34. const path = (prefix ? prefix + '.' : '') + prop;
  35. prefix = prefix || '';
  36. if (subprops) {
  37. Object.defineProperty(prototype, prop, {
  38. enumerable: true,
  39. configurable: true,
  40. get: function() {
  41. const _this = this;
  42. if (!this.$__.getters) {
  43. this.$__.getters = {};
  44. }
  45. if (!this.$__.getters[path]) {
  46. const nested = Object.create(Document.prototype, getOwnPropertyDescriptors(this));
  47. // save scope for nested getters/setters
  48. if (!prefix) {
  49. nested.$__.scope = this;
  50. }
  51. nested.$__.nestedPath = path;
  52. Object.defineProperty(nested, 'schema', {
  53. enumerable: false,
  54. configurable: true,
  55. writable: false,
  56. value: prototype.schema
  57. });
  58. Object.defineProperty(nested, 'toObject', {
  59. enumerable: false,
  60. configurable: true,
  61. writable: false,
  62. value: function() {
  63. return utils.clone(_this.get(path, null, {
  64. virtuals: get(this, 'schema.options.toObject.virtuals', null)
  65. }));
  66. }
  67. });
  68. Object.defineProperty(nested, 'toJSON', {
  69. enumerable: false,
  70. configurable: true,
  71. writable: false,
  72. value: function() {
  73. return _this.get(path, null, {
  74. virtuals: get(_this, 'schema.options.toJSON.virtuals', null)
  75. });
  76. }
  77. });
  78. Object.defineProperty(nested, '$__isNested', {
  79. enumerable: false,
  80. configurable: true,
  81. writable: false,
  82. value: true
  83. });
  84. compile(subprops, nested, path, options);
  85. this.$__.getters[path] = nested;
  86. }
  87. return this.$__.getters[path];
  88. },
  89. set: function(v) {
  90. if (v instanceof Document) {
  91. v = v.toObject({ transform: false });
  92. }
  93. const doc = this.$__.scope || this;
  94. return doc.$set(path, v);
  95. }
  96. });
  97. } else {
  98. Object.defineProperty(prototype, prop, {
  99. enumerable: true,
  100. configurable: true,
  101. get: function() {
  102. return this[getSymbol].call(this.$__.scope || this, path);
  103. },
  104. set: function(v) {
  105. return this.$set.call(this.$__.scope || this, path, v);
  106. }
  107. });
  108. }
  109. }
  110. // gets descriptors for all properties of `object`
  111. // makes all properties non-enumerable to match previous behavior to #2211
  112. function getOwnPropertyDescriptors(object) {
  113. const result = {};
  114. Object.getOwnPropertyNames(object).forEach(function(key) {
  115. result[key] = Object.getOwnPropertyDescriptor(object, key);
  116. // Assume these are schema paths, ignore them re: #5470
  117. if (result[key].get) {
  118. delete result[key];
  119. return;
  120. }
  121. result[key].enumerable = ['isNew', '$__', 'errors', '_doc'].indexOf(key) === -1;
  122. });
  123. return result;
  124. }