1 |
|
2 |
|
3 |
|
4 |
|
5 | 'use strict';
|
6 |
|
7 | const MongooseBuffer = require('../types/buffer');
|
8 | const SchemaBufferOptions = require('../options/SchemaBufferOptions');
|
9 | const SchemaType = require('../schematype');
|
10 | const handleBitwiseOperator = require('./operators/bitwise');
|
11 | const utils = require('../utils');
|
12 |
|
13 | const populateModelSymbol = require('../helpers/symbols').populateModelSymbol;
|
14 |
|
15 | const Binary = MongooseBuffer.Binary;
|
16 | const CastError = SchemaType.CastError;
|
17 | let Document;
|
18 |
|
19 |
|
20 |
|
21 |
|
22 |
|
23 |
|
24 |
|
25 |
|
26 |
|
27 |
|
28 | function SchemaBuffer(key, options) {
|
29 | SchemaType.call(this, key, options, 'Buffer');
|
30 | }
|
31 |
|
32 |
|
33 |
|
34 |
|
35 |
|
36 |
|
37 |
|
38 | SchemaBuffer.schemaName = 'Buffer';
|
39 |
|
40 | SchemaBuffer.defaultOptions = {};
|
41 |
|
42 |
|
43 |
|
44 |
|
45 | SchemaBuffer.prototype = Object.create(SchemaType.prototype);
|
46 | SchemaBuffer.prototype.constructor = SchemaBuffer;
|
47 | SchemaBuffer.prototype.OptionsConstructor = SchemaBufferOptions;
|
48 |
|
49 |
|
50 |
|
51 |
|
52 |
|
53 | SchemaBuffer._checkRequired = v => !!(v && v.length);
|
54 |
|
55 |
|
56 |
|
57 |
|
58 |
|
59 |
|
60 |
|
61 |
|
62 |
|
63 |
|
64 |
|
65 |
|
66 |
|
67 |
|
68 |
|
69 |
|
70 |
|
71 |
|
72 |
|
73 |
|
74 | SchemaBuffer.set = SchemaType.set;
|
75 |
|
76 |
|
77 |
|
78 |
|
79 |
|
80 |
|
81 |
|
82 |
|
83 |
|
84 |
|
85 |
|
86 |
|
87 |
|
88 |
|
89 |
|
90 |
|
91 |
|
92 |
|
93 |
|
94 |
|
95 | SchemaBuffer.checkRequired = SchemaType.checkRequired;
|
96 |
|
97 |
|
98 |
|
99 |
|
100 |
|
101 |
|
102 |
|
103 |
|
104 |
|
105 |
|
106 |
|
107 |
|
108 | SchemaBuffer.prototype.checkRequired = function(value, doc) {
|
109 | if (SchemaType._isRef(this, value, doc, true)) {
|
110 | return !!value;
|
111 | }
|
112 | return this.constructor._checkRequired(value);
|
113 | };
|
114 |
|
115 |
|
116 |
|
117 |
|
118 |
|
119 |
|
120 |
|
121 |
|
122 |
|
123 |
|
124 | SchemaBuffer.prototype.cast = function(value, doc, init) {
|
125 | let ret;
|
126 | if (SchemaType._isRef(this, value, doc, init)) {
|
127 |
|
128 |
|
129 | if (value === null || value === undefined) {
|
130 | return value;
|
131 | }
|
132 |
|
133 |
|
134 | Document || (Document = require('./../document'));
|
135 |
|
136 | if (value instanceof Document) {
|
137 | value.$__.wasPopulated = true;
|
138 | return value;
|
139 | }
|
140 |
|
141 |
|
142 | if (Buffer.isBuffer(value)) {
|
143 | return value;
|
144 | } else if (!utils.isObject(value)) {
|
145 | throw new CastError('Buffer', value, this.path, null, this);
|
146 | }
|
147 |
|
148 |
|
149 |
|
150 |
|
151 | const path = doc.$__fullPath(this.path);
|
152 | const owner = doc.ownerDocument ? doc.ownerDocument() : doc;
|
153 | const pop = owner.populated(path, true);
|
154 | ret = new pop.options[populateModelSymbol](value);
|
155 | ret.$__.wasPopulated = true;
|
156 | return ret;
|
157 | }
|
158 |
|
159 |
|
160 | if (value && value._id) {
|
161 | value = value._id;
|
162 | }
|
163 |
|
164 | if (value && value.isMongooseBuffer) {
|
165 | return value;
|
166 | }
|
167 |
|
168 | if (Buffer.isBuffer(value)) {
|
169 | if (!value || !value.isMongooseBuffer) {
|
170 | value = new MongooseBuffer(value, [this.path, doc]);
|
171 | if (this.options.subtype != null) {
|
172 | value._subtype = this.options.subtype;
|
173 | }
|
174 | }
|
175 | return value;
|
176 | }
|
177 |
|
178 | if (value instanceof Binary) {
|
179 | ret = new MongooseBuffer(value.value(true), [this.path, doc]);
|
180 | if (typeof value.sub_type !== 'number') {
|
181 | throw new CastError('Buffer', value, this.path, null, this);
|
182 | }
|
183 | ret._subtype = value.sub_type;
|
184 | return ret;
|
185 | }
|
186 |
|
187 | if (value === null) {
|
188 | return value;
|
189 | }
|
190 |
|
191 |
|
192 | const type = typeof value;
|
193 | if (
|
194 | type === 'string' || type === 'number' || Array.isArray(value) ||
|
195 | (type === 'object' && value.type === 'Buffer' && Array.isArray(value.data))
|
196 | ) {
|
197 | if (type === 'number') {
|
198 | value = [value];
|
199 | }
|
200 | ret = new MongooseBuffer(value, [this.path, doc]);
|
201 | if (this.options.subtype != null) {
|
202 | ret._subtype = this.options.subtype;
|
203 | }
|
204 | return ret;
|
205 | }
|
206 |
|
207 | throw new CastError('Buffer', value, this.path, null, this);
|
208 | };
|
209 |
|
210 |
|
211 |
|
212 |
|
213 |
|
214 |
|
215 |
|
216 |
|
217 |
|
218 |
|
219 |
|
220 |
|
221 |
|
222 |
|
223 |
|
224 |
|
225 |
|
226 | SchemaBuffer.prototype.subtype = function(subtype) {
|
227 | this.options.subtype = subtype;
|
228 | return this;
|
229 | };
|
230 |
|
231 |
|
232 |
|
233 |
|
234 | function handleSingle(val) {
|
235 | return this.castForQuery(val);
|
236 | }
|
237 |
|
238 | SchemaBuffer.prototype.$conditionalHandlers =
|
239 | utils.options(SchemaType.prototype.$conditionalHandlers, {
|
240 | $bitsAllClear: handleBitwiseOperator,
|
241 | $bitsAnyClear: handleBitwiseOperator,
|
242 | $bitsAllSet: handleBitwiseOperator,
|
243 | $bitsAnySet: handleBitwiseOperator,
|
244 | $gt: handleSingle,
|
245 | $gte: handleSingle,
|
246 | $lt: handleSingle,
|
247 | $lte: handleSingle
|
248 | });
|
249 |
|
250 |
|
251 |
|
252 |
|
253 |
|
254 |
|
255 |
|
256 |
|
257 |
|
258 | SchemaBuffer.prototype.castForQuery = function($conditional, val) {
|
259 | let handler;
|
260 | if (arguments.length === 2) {
|
261 | handler = this.$conditionalHandlers[$conditional];
|
262 | if (!handler) {
|
263 | throw new Error('Can\'t use ' + $conditional + ' with Buffer.');
|
264 | }
|
265 | return handler.call(this, val);
|
266 | }
|
267 | val = $conditional;
|
268 | const casted = this._castForQuery(val);
|
269 | return casted ? casted.toObject({ transform: false, virtuals: false }) : casted;
|
270 | };
|
271 |
|
272 |
|
273 |
|
274 |
|
275 |
|
276 | module.exports = SchemaBuffer;
|