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 handleBitwiseOperator = require('./operators/bitwise');
  6var utils = require('../utils');
  7
  8var MongooseBuffer = require('../types').Buffer;
  9var SchemaType = require('../schematype');
 10
 11var Binary = MongooseBuffer.Binary;
 12var CastError = SchemaType.CastError;
 13var Document;
 14
 15/**
 16 * Buffer SchemaType constructor
 17 *
 18 * @param {String} key
 19 * @param {Object} options
 20 * @inherits SchemaType
 21 * @api public
 22 */
 23
 24function SchemaBuffer(key, options) {
 25  SchemaType.call(this, key, options, 'Buffer');
 26}
 27
 28/**
 29 * This schema type's name, to defend against minifiers that mangle
 30 * function names.
 31 *
 32 * @api public
 33 */
 34SchemaBuffer.schemaName = 'Buffer';
 35
 36/*!
 37 * Inherits from SchemaType.
 38 */
 39SchemaBuffer.prototype = Object.create(SchemaType.prototype);
 40SchemaBuffer.prototype.constructor = SchemaBuffer;
 41
 42/**
 43 * Check if the given value satisfies a required validator. To satisfy a
 44 * required validator, a buffer must not be null or undefined and have
 45 * non-zero length.
 46 *
 47 * @param {Any} value
 48 * @param {Document} doc
 49 * @return {Boolean}
 50 * @api public
 51 */
 52
 53SchemaBuffer.prototype.checkRequired = function(value, doc) {
 54  if (SchemaType._isRef(this, value, doc, true)) {
 55    return !!value;
 56  }
 57  return !!(value && value.length);
 58};
 59
 60/**
 61 * Casts contents
 62 *
 63 * @param {Object} value
 64 * @param {Document} doc document that triggers the casting
 65 * @param {Boolean} init
 66 * @api private
 67 */
 68
 69SchemaBuffer.prototype.cast = function(value, doc, init) {
 70  var ret;
 71  if (SchemaType._isRef(this, value, doc, init)) {
 72    // wait! we may need to cast this to a document
 73
 74    if (value === null || value === undefined) {
 75      return value;
 76    }
 77
 78    // lazy load
 79    Document || (Document = require('./../document'));
 80
 81    if (value instanceof Document) {
 82      value.$__.wasPopulated = true;
 83      return value;
 84    }
 85
 86    // setting a populated path
 87    if (Buffer.isBuffer(value)) {
 88      return value;
 89    } else if (!utils.isObject(value)) {
 90      throw new CastError('buffer', value, this.path);
 91    }
 92
 93    // Handle the case where user directly sets a populated
 94    // path to a plain object; cast to the Model used in
 95    // the population query.
 96    var path = doc.$__fullPath(this.path);
 97    var owner = doc.ownerDocument ? doc.ownerDocument() : doc;
 98    var pop = owner.populated(path, true);
 99    ret = new pop.options.model(value);
100    ret.$__.wasPopulated = true;
101    return ret;
102  }
103
104  // documents
105  if (value && value._id) {
106    value = value._id;
107  }
108
109  if (value && value.isMongooseBuffer) {
110    return value;
111  }
112
113  if (Buffer.isBuffer(value)) {
114    if (!value || !value.isMongooseBuffer) {
115      value = new MongooseBuffer(value, [this.path, doc]);
116    }
117
118    return value;
119  } else if (value instanceof Binary) {
120    ret = new MongooseBuffer(value.value(true), [this.path, doc]);
121    if (typeof value.sub_type !== 'number') {
122      throw new CastError('buffer', value, this.path);
123    }
124    ret._subtype = value.sub_type;
125    return ret;
126  }
127
128  if (value === null) {
129    return value;
130  }
131
132  var type = typeof value;
133  if (type === 'string' || type === 'number' || Array.isArray(value)) {
134    if (type === 'number') {
135      value = [value];
136    }
137    ret = new MongooseBuffer(value, [this.path, doc]);
138    return ret;
139  }
140
141  throw new CastError('buffer', value, this.path);
142};
143
144/*!
145 * ignore
146 */
147function handleSingle(val) {
148  return this.castForQuery(val);
149}
150
151SchemaBuffer.prototype.$conditionalHandlers =
152    utils.options(SchemaType.prototype.$conditionalHandlers, {
153      $bitsAllClear: handleBitwiseOperator,
154      $bitsAnyClear: handleBitwiseOperator,
155      $bitsAllSet: handleBitwiseOperator,
156      $bitsAnySet: handleBitwiseOperator,
157      $gt: handleSingle,
158      $gte: handleSingle,
159      $lt: handleSingle,
160      $lte: handleSingle
161    });
162
163/**
164 * Casts contents for queries.
165 *
166 * @param {String} $conditional
167 * @param {any} [value]
168 * @api private
169 */
170
171SchemaBuffer.prototype.castForQuery = function($conditional, val) {
172  var handler;
173  if (arguments.length === 2) {
174    handler = this.$conditionalHandlers[$conditional];
175    if (!handler) {
176      throw new Error('Can\'t use ' + $conditional + ' with Buffer.');
177    }
178    return handler.call(this, val);
179  }
180  val = $conditional;
181  return this.cast(val).toObject();
182};
183
184/*!
185 * Module exports.
186 */
187
188module.exports = SchemaBuffer;