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