Ohm-Management - Projektarbeit B-ME
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.

discriminator.js 5.8KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188
  1. 'use strict';
  2. const defineKey = require('../document/compile').defineKey;
  3. const get = require('../get');
  4. const utils = require('../../utils');
  5. const CUSTOMIZABLE_DISCRIMINATOR_OPTIONS = {
  6. toJSON: true,
  7. toObject: true,
  8. _id: true,
  9. id: true
  10. };
  11. /*!
  12. * ignore
  13. */
  14. module.exports = function discriminator(model, name, schema, tiedValue, applyPlugins) {
  15. if (!(schema && schema.instanceOfSchema)) {
  16. throw new Error('You must pass a valid discriminator Schema');
  17. }
  18. if (model.schema.discriminatorMapping &&
  19. !model.schema.discriminatorMapping.isRoot) {
  20. throw new Error('Discriminator "' + name +
  21. '" can only be a discriminator of the root model');
  22. }
  23. if (applyPlugins) {
  24. const applyPluginsToDiscriminators = get(model.base,
  25. 'options.applyPluginsToDiscriminators', false);
  26. // Even if `applyPluginsToDiscriminators` isn't set, we should still apply
  27. // global plugins to schemas embedded in the discriminator schema (gh-7370)
  28. model.base._applyPlugins(schema, {
  29. skipTopLevel: !applyPluginsToDiscriminators
  30. });
  31. }
  32. const key = model.schema.options.discriminatorKey;
  33. const existingPath = model.schema.path(key);
  34. if (existingPath != null) {
  35. if (!utils.hasUserDefinedProperty(existingPath.options, 'select')) {
  36. existingPath.options.select = true;
  37. }
  38. existingPath.options.$skipDiscriminatorCheck = true;
  39. } else {
  40. const baseSchemaAddition = {};
  41. baseSchemaAddition[key] = {
  42. default: void 0,
  43. select: true,
  44. $skipDiscriminatorCheck: true
  45. };
  46. baseSchemaAddition[key][model.schema.options.typeKey] = String;
  47. model.schema.add(baseSchemaAddition);
  48. defineKey(key, null, model.prototype, null, [key], model.schema.options);
  49. }
  50. if (schema.path(key) && schema.path(key).options.$skipDiscriminatorCheck !== true) {
  51. throw new Error('Discriminator "' + name +
  52. '" cannot have field with name "' + key + '"');
  53. }
  54. let value = name;
  55. if (typeof tiedValue == 'string' && tiedValue.length) {
  56. value = tiedValue;
  57. }
  58. function merge(schema, baseSchema) {
  59. // Retain original schema before merging base schema
  60. schema._originalSchema = schema.clone();
  61. schema._baseSchema = baseSchema;
  62. if (baseSchema.paths._id &&
  63. baseSchema.paths._id.options &&
  64. !baseSchema.paths._id.options.auto) {
  65. const originalSchema = schema;
  66. utils.merge(schema, originalSchema);
  67. delete schema.paths._id;
  68. delete schema.tree._id;
  69. }
  70. // Find conflicting paths: if something is a path in the base schema
  71. // and a nested path in the child schema, overwrite the base schema path.
  72. // See gh-6076
  73. const baseSchemaPaths = Object.keys(baseSchema.paths);
  74. const conflictingPaths = [];
  75. for (let i = 0; i < baseSchemaPaths.length; ++i) {
  76. if (schema.nested[baseSchemaPaths[i]]) {
  77. conflictingPaths.push(baseSchemaPaths[i]);
  78. }
  79. }
  80. utils.merge(schema, baseSchema, {
  81. omit: { discriminators: true },
  82. omitNested: conflictingPaths.reduce((cur, path) => {
  83. cur['tree.' + path] = true;
  84. return cur;
  85. }, {})
  86. });
  87. // Clean up conflicting paths _after_ merging re: gh-6076
  88. for (let i = 0; i < conflictingPaths.length; ++i) {
  89. delete schema.paths[conflictingPaths[i]];
  90. }
  91. // Rebuild schema models because schemas may have been merged re: #7884
  92. schema.childSchemas.forEach(obj => {
  93. obj.model.prototype.$__setSchema(obj.schema);
  94. });
  95. const obj = {};
  96. obj[key] = {
  97. default: value,
  98. select: true,
  99. set: function(newName) {
  100. if (newName === value) {
  101. return value;
  102. }
  103. throw new Error('Can\'t set discriminator key "' + key + '"');
  104. },
  105. $skipDiscriminatorCheck: true
  106. };
  107. obj[key][schema.options.typeKey] = existingPath ?
  108. existingPath.instance :
  109. String;
  110. schema.add(obj);
  111. schema.discriminatorMapping = {key: key, value: value, isRoot: false};
  112. if (baseSchema.options.collection) {
  113. schema.options.collection = baseSchema.options.collection;
  114. }
  115. const toJSON = schema.options.toJSON;
  116. const toObject = schema.options.toObject;
  117. const _id = schema.options._id;
  118. const id = schema.options.id;
  119. const keys = Object.keys(schema.options);
  120. schema.options.discriminatorKey = baseSchema.options.discriminatorKey;
  121. for (let i = 0; i < keys.length; ++i) {
  122. const _key = keys[i];
  123. if (!CUSTOMIZABLE_DISCRIMINATOR_OPTIONS[_key]) {
  124. if (!utils.deepEqual(schema.options[_key], baseSchema.options[_key])) {
  125. throw new Error('Can\'t customize discriminator option ' + _key +
  126. ' (can only modify ' +
  127. Object.keys(CUSTOMIZABLE_DISCRIMINATOR_OPTIONS).join(', ') +
  128. ')');
  129. }
  130. }
  131. }
  132. schema.options = utils.clone(baseSchema.options);
  133. if (toJSON) schema.options.toJSON = toJSON;
  134. if (toObject) schema.options.toObject = toObject;
  135. if (typeof _id !== 'undefined') {
  136. schema.options._id = _id;
  137. }
  138. schema.options.id = id;
  139. schema.s.hooks = model.schema.s.hooks.merge(schema.s.hooks);
  140. schema.plugins = Array.prototype.slice(baseSchema.plugins);
  141. schema.callQueue = baseSchema.callQueue.concat(schema.callQueue);
  142. delete schema._requiredpaths; // reset just in case Schema#requiredPaths() was called on either schema
  143. }
  144. // merges base schema into new discriminator schema and sets new type field.
  145. merge(schema, model.schema);
  146. if (!model.discriminators) {
  147. model.discriminators = {};
  148. }
  149. if (!model.schema.discriminatorMapping) {
  150. model.schema.discriminatorMapping = {key: key, value: null, isRoot: true};
  151. }
  152. if (!model.schema.discriminators) {
  153. model.schema.discriminators = {};
  154. }
  155. model.schema.discriminators[name] = schema;
  156. if (model.discriminators[name]) {
  157. throw new Error('Discriminator with name "' + name + '" already exists');
  158. }
  159. return schema;
  160. };