map.js 698 B

123456789101112131415161718192021222324252627282930313233343536373839
  1. 'use strict';
  2. /*!
  3. * ignore
  4. */
  5. const MongooseMap = require('../types/map');
  6. const SchemaType = require('../schematype');
  7. /*!
  8. * ignore
  9. */
  10. class Map extends SchemaType {
  11. constructor(key, options) {
  12. super(key, options, 'Map');
  13. this.$isSchemaMap = true;
  14. }
  15. cast(val, doc, init) {
  16. if (val instanceof MongooseMap) {
  17. return val;
  18. }
  19. if (init) {
  20. const map = new MongooseMap({}, this.path, doc, this.$__schemaType);
  21. for (const key of Object.keys(val)) {
  22. map.$init(key, this.$__schemaType.cast(val[key], doc, true));
  23. }
  24. return map;
  25. }
  26. return new MongooseMap(val, this.path, doc, this.$__schemaType);
  27. }
  28. }
  29. module.exports = Map;