123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267 |
- 'use strict';
- const MongooseBuffer = require('../types/buffer');
- const SchemaBufferOptions = require('../options/SchemaBufferOptions');
- const SchemaType = require('../schematype');
- const handleBitwiseOperator = require('./operators/bitwise');
- const utils = require('../utils');
- const Binary = MongooseBuffer.Binary;
- const CastError = SchemaType.CastError;
- function SchemaBuffer(key, options) {
- SchemaType.call(this, key, options, 'Buffer');
- }
- SchemaBuffer.schemaName = 'Buffer';
- SchemaBuffer.defaultOptions = {};
- SchemaBuffer.prototype = Object.create(SchemaType.prototype);
- SchemaBuffer.prototype.constructor = SchemaBuffer;
- SchemaBuffer.prototype.OptionsConstructor = SchemaBufferOptions;
- SchemaBuffer._checkRequired = v => !!(v && v.length);
- SchemaBuffer.set = SchemaType.set;
- SchemaBuffer.checkRequired = SchemaType.checkRequired;
- SchemaBuffer.prototype.checkRequired = function(value, doc) {
- if (SchemaType._isRef(this, value, doc, true)) {
- return !!value;
- }
- return this.constructor._checkRequired(value);
- };
- SchemaBuffer.prototype.cast = function(value, doc, init) {
- let ret;
- if (SchemaType._isRef(this, value, doc, init)) {
- if (value && value.isMongooseBuffer) {
- return value;
- }
- if (Buffer.isBuffer(value)) {
- if (!value || !value.isMongooseBuffer) {
- value = new MongooseBuffer(value, [this.path, doc]);
- if (this.options.subtype != null) {
- value._subtype = this.options.subtype;
- }
- }
- return value;
- }
- if (value instanceof Binary) {
- ret = new MongooseBuffer(value.value(true), [this.path, doc]);
- if (typeof value.sub_type !== 'number') {
- throw new CastError('Buffer', value, this.path, null, this);
- }
- ret._subtype = value.sub_type;
- return ret;
- }
- return this._castRef(value, doc, init);
- }
-
- if (value && value._id) {
- value = value._id;
- }
- if (value && value.isMongooseBuffer) {
- return value;
- }
- if (Buffer.isBuffer(value)) {
- if (!value || !value.isMongooseBuffer) {
- value = new MongooseBuffer(value, [this.path, doc]);
- if (this.options.subtype != null) {
- value._subtype = this.options.subtype;
- }
- }
- return value;
- }
- if (value instanceof Binary) {
- ret = new MongooseBuffer(value.value(true), [this.path, doc]);
- if (typeof value.sub_type !== 'number') {
- throw new CastError('Buffer', value, this.path, null, this);
- }
- ret._subtype = value.sub_type;
- return ret;
- }
- if (value === null) {
- return value;
- }
- const type = typeof value;
- if (
- type === 'string' || type === 'number' || Array.isArray(value) ||
- (type === 'object' && value.type === 'Buffer' && Array.isArray(value.data))
- ) {
- if (type === 'number') {
- value = [value];
- }
- ret = new MongooseBuffer(value, [this.path, doc]);
- if (this.options.subtype != null) {
- ret._subtype = this.options.subtype;
- }
- return ret;
- }
- throw new CastError('Buffer', value, this.path, null, this);
- };
- SchemaBuffer.prototype.subtype = function(subtype) {
- this.options.subtype = subtype;
- return this;
- };
- function handleSingle(val) {
- return this.castForQuery(val);
- }
- SchemaBuffer.prototype.$conditionalHandlers =
- utils.options(SchemaType.prototype.$conditionalHandlers, {
- $bitsAllClear: handleBitwiseOperator,
- $bitsAnyClear: handleBitwiseOperator,
- $bitsAllSet: handleBitwiseOperator,
- $bitsAnySet: handleBitwiseOperator,
- $gt: handleSingle,
- $gte: handleSingle,
- $lt: handleSingle,
- $lte: handleSingle
- });
- SchemaBuffer.prototype.castForQuery = function($conditional, val) {
- let handler;
- if (arguments.length === 2) {
- handler = this.$conditionalHandlers[$conditional];
- if (!handler) {
- throw new Error('Can\'t use ' + $conditional + ' with Buffer.');
- }
- return handler.call(this, val);
- }
- val = $conditional;
- const casted = this._castForQuery(val);
- return casted ? casted.toObject({ transform: false, virtuals: false }) : casted;
- };
- module.exports = SchemaBuffer;
|