buffer.js 3.9 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184
  1. /*!
  2. * Module dependencies.
  3. */
  4. var utils = require('../utils');
  5. var MongooseBuffer = require('../types').Buffer;
  6. var SchemaType = require('../schematype');
  7. var Binary = MongooseBuffer.Binary;
  8. var CastError = SchemaType.CastError;
  9. var Document;
  10. /**
  11. * Buffer SchemaType constructor
  12. *
  13. * @param {String} key
  14. * @param {SchemaType} cast
  15. * @inherits SchemaType
  16. * @api private
  17. */
  18. function SchemaBuffer (key, options) {
  19. SchemaType.call(this, key, options, 'Buffer');
  20. }
  21. /**
  22. * This schema type's name, to defend against minifiers that mangle
  23. * function names.
  24. *
  25. * @api private
  26. */
  27. SchemaBuffer.schemaName = 'Buffer';
  28. /*!
  29. * Inherits from SchemaType.
  30. */
  31. SchemaBuffer.prototype = Object.create( SchemaType.prototype );
  32. SchemaBuffer.prototype.constructor = SchemaBuffer;
  33. /**
  34. * Check required
  35. *
  36. * @api private
  37. */
  38. SchemaBuffer.prototype.checkRequired = function (value, doc) {
  39. if (SchemaType._isRef(this, value, doc, true)) {
  40. return null != value;
  41. } else {
  42. return !!(value && value.length);
  43. }
  44. };
  45. /**
  46. * Casts contents
  47. *
  48. * @param {Object} value
  49. * @param {Document} doc document that triggers the casting
  50. * @param {Boolean} init
  51. * @api private
  52. */
  53. SchemaBuffer.prototype.cast = function (value, doc, init) {
  54. if (SchemaType._isRef(this, value, doc, init)) {
  55. // wait! we may need to cast this to a document
  56. if (null == value) {
  57. return value;
  58. }
  59. // lazy load
  60. Document || (Document = require('./../document'));
  61. if (value instanceof Document) {
  62. value.$__.wasPopulated = true;
  63. return value;
  64. }
  65. // setting a populated path
  66. if (Buffer.isBuffer(value)) {
  67. return value;
  68. } else if (!utils.isObject(value)) {
  69. throw new CastError('buffer', value, this.path);
  70. }
  71. // Handle the case where user directly sets a populated
  72. // path to a plain object; cast to the Model used in
  73. // the population query.
  74. var path = doc.$__fullPath(this.path);
  75. var owner = doc.ownerDocument ? doc.ownerDocument() : doc;
  76. var pop = owner.populated(path, true);
  77. var ret = new pop.options.model(value);
  78. ret.$__.wasPopulated = true;
  79. return ret;
  80. }
  81. // documents
  82. if (value && value._id) {
  83. value = value._id;
  84. }
  85. if (value && value.isMongooseBuffer) {
  86. return value;
  87. }
  88. if (Buffer.isBuffer(value)) {
  89. if (!value || !value.isMongooseBuffer) {
  90. value = new MongooseBuffer(value, [this.path, doc]);
  91. }
  92. return value;
  93. } else if (value instanceof Binary) {
  94. var ret = new MongooseBuffer(value.value(true), [this.path, doc]);
  95. if (typeof value.sub_type !== 'number') {
  96. throw new CastError('buffer', value, this.path);
  97. }
  98. ret._subtype = value.sub_type;
  99. return ret;
  100. }
  101. if (null === value) return value;
  102. var type = typeof value;
  103. if ('string' == type || 'number' == type || Array.isArray(value)) {
  104. var ret = new MongooseBuffer(value, [this.path, doc]);
  105. return ret;
  106. }
  107. throw new CastError('buffer', value, this.path);
  108. };
  109. /*!
  110. * ignore
  111. */
  112. function handleSingle (val) {
  113. return this.castForQuery(val);
  114. }
  115. function handleArray (val) {
  116. var self = this;
  117. return val.map( function (m) {
  118. return self.castForQuery(m);
  119. });
  120. }
  121. SchemaBuffer.prototype.$conditionalHandlers =
  122. utils.options(SchemaType.prototype.$conditionalHandlers, {
  123. '$gt' : handleSingle,
  124. '$gte': handleSingle,
  125. '$in' : handleArray,
  126. '$lt' : handleSingle,
  127. '$lte': handleSingle,
  128. '$ne' : handleSingle,
  129. '$nin': handleArray
  130. });
  131. /**
  132. * Casts contents for queries.
  133. *
  134. * @param {String} $conditional
  135. * @param {any} [value]
  136. * @api private
  137. */
  138. SchemaBuffer.prototype.castForQuery = function ($conditional, val) {
  139. var handler;
  140. if (arguments.length === 2) {
  141. handler = this.$conditionalHandlers[$conditional];
  142. if (!handler)
  143. throw new Error("Can't use " + $conditional + " with Buffer.");
  144. return handler.call(this, val);
  145. } else {
  146. val = $conditional;
  147. return this.cast(val).toObject();
  148. }
  149. };
  150. /*!
  151. * Module exports.
  152. */
  153. module.exports = SchemaBuffer;