mongoose 8.9.4 → 8.9.6

This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
package/lib/aggregate.js CHANGED
@@ -392,11 +392,23 @@ Aggregate.prototype.project = function(arg) {
392
392
  * @memberOf Aggregate
393
393
  * @instance
394
394
  * @param {Object} arg
395
+ * @param {Object|Array<Number>} arg.near GeoJSON point or coordinates array
395
396
  * @return {Aggregate}
396
397
  * @api public
397
398
  */
398
399
 
399
400
  Aggregate.prototype.near = function(arg) {
401
+ if (arg == null) {
402
+ throw new MongooseError('Aggregate `near()` must be called with non-nullish argument');
403
+ }
404
+ if (arg.near == null) {
405
+ throw new MongooseError('Aggregate `near()` argument must have a `near` property');
406
+ }
407
+ const coordinates = Array.isArray(arg.near) ? arg.near : arg.near.coordinates;
408
+ if (typeof arg.near === 'object' && (!Array.isArray(coordinates) || coordinates.length < 2 || coordinates.find(c => typeof c !== 'number'))) {
409
+ throw new MongooseError(`Aggregate \`near()\` argument has invalid coordinates, got "${coordinates}"`);
410
+ }
411
+
400
412
  const op = {};
401
413
  op.$geoNear = arg;
402
414
  return this.append(op);
@@ -0,0 +1,78 @@
1
+ 'use strict';
2
+
3
+ const MongooseBuffer = require('../types/buffer');
4
+
5
+ const UUID_FORMAT = /[0-9a-f]{8}-[0-9a-f]{4}-[0-9][0-9a-f]{3}-[89ab][0-9a-f]{3}-[0-9a-f]{12}/i;
6
+ const Binary = MongooseBuffer.Binary;
7
+
8
+ module.exports = function castUUID(value) {
9
+ if (value == null) {
10
+ return value;
11
+ }
12
+
13
+ function newBuffer(initbuff) {
14
+ const buff = new MongooseBuffer(initbuff);
15
+ buff._subtype = 4;
16
+ return buff;
17
+ }
18
+
19
+ if (typeof value === 'string') {
20
+ if (UUID_FORMAT.test(value)) {
21
+ return stringToBinary(value);
22
+ } else {
23
+ throw new Error(`"${value}" is not a valid UUID string`);
24
+ }
25
+ }
26
+
27
+ if (Buffer.isBuffer(value)) {
28
+ return newBuffer(value);
29
+ }
30
+
31
+ if (value instanceof Binary) {
32
+ return newBuffer(value.value(true));
33
+ }
34
+
35
+ // Re: gh-647 and gh-3030, we're ok with casting using `toString()`
36
+ // **unless** its the default Object.toString, because "[object Object]"
37
+ // doesn't really qualify as useful data
38
+ if (value.toString && value.toString !== Object.prototype.toString) {
39
+ if (UUID_FORMAT.test(value.toString())) {
40
+ return stringToBinary(value.toString());
41
+ }
42
+ }
43
+
44
+ throw new Error(`"${value}" cannot be casted to a UUID`);
45
+ };
46
+
47
+ module.exports.UUID_FORMAT = UUID_FORMAT;
48
+
49
+ /**
50
+ * Helper function to convert the input hex-string to a buffer
51
+ * @param {String} hex The hex string to convert
52
+ * @returns {Buffer} The hex as buffer
53
+ * @api private
54
+ */
55
+
56
+ function hex2buffer(hex) {
57
+ // use buffer built-in function to convert from hex-string to buffer
58
+ const buff = hex != null && Buffer.from(hex, 'hex');
59
+ return buff;
60
+ }
61
+
62
+ /**
63
+ * Convert a String to Binary
64
+ * @param {String} uuidStr The value to process
65
+ * @returns {MongooseBuffer} The binary to store
66
+ * @api private
67
+ */
68
+
69
+ function stringToBinary(uuidStr) {
70
+ // Protect against undefined & throwing err
71
+ if (typeof uuidStr !== 'string') uuidStr = '';
72
+ const hex = uuidStr.replace(/[{}-]/g, ''); // remove extra characters
73
+ const bytes = hex2buffer(hex);
74
+ const buff = new MongooseBuffer(bytes);
75
+ buff._subtype = 4;
76
+
77
+ return buff;
78
+ }
package/lib/document.js CHANGED
@@ -1147,7 +1147,7 @@ Document.prototype.$set = function $set(path, val, type, options) {
1147
1147
  } else if (pathtype === 'nested' && valForKey == null) {
1148
1148
  this.$set(pathName, valForKey, constructing, options);
1149
1149
  }
1150
- } else if (valForKey !== void 0) {
1150
+ } else {
1151
1151
  this.$set(pathName, valForKey, constructing, options);
1152
1152
  }
1153
1153
  }
@@ -191,15 +191,7 @@ module.exports = function getModelsMapForPopulate(model, docs, options) {
191
191
  if (hasMatchFunction) {
192
192
  match = match.call(doc, doc);
193
193
  }
194
- if (Array.isArray(match)) {
195
- for (const item of match) {
196
- if (item != null && item.$where) {
197
- throw new MongooseError('Cannot use $where filter with populate() match');
198
- }
199
- }
200
- } else if (match != null && match.$where != null) {
201
- throw new MongooseError('Cannot use $where filter with populate() match');
202
- }
194
+ throwOn$where(match);
203
195
  data.match = match;
204
196
  data.hasMatchFunction = hasMatchFunction;
205
197
  data.isRefPath = isRefPath;
@@ -463,15 +455,7 @@ function _virtualPopulate(model, docs, options, _virtualRes) {
463
455
  data.match = match;
464
456
  data.hasMatchFunction = hasMatchFunction;
465
457
 
466
- if (Array.isArray(match)) {
467
- for (const item of match) {
468
- if (item != null && item.$where) {
469
- throw new MongooseError('Cannot use $where filter with populate() match');
470
- }
471
- }
472
- } else if (match != null && match.$where != null) {
473
- throw new MongooseError('Cannot use $where filter with populate() match');
474
- }
458
+ throwOn$where(match);
475
459
 
476
460
  // Get local fields
477
461
  const ret = _getLocalFieldValues(doc, localField, model, options, virtual);
@@ -759,3 +743,24 @@ function _findRefPathForDiscriminators(doc, modelSchema, data, options, normaliz
759
743
 
760
744
  return modelNames;
761
745
  }
746
+
747
+ /**
748
+ * Throw an error if there are any $where keys
749
+ */
750
+
751
+ function throwOn$where(match) {
752
+ if (match == null) {
753
+ return;
754
+ }
755
+ if (typeof match !== 'object') {
756
+ return;
757
+ }
758
+ for (const key of Object.keys(match)) {
759
+ if (key === '$where') {
760
+ throw new MongooseError('Cannot use $where filter with populate() match');
761
+ }
762
+ if (match[key] != null && typeof match[key] === 'object') {
763
+ throwOn$where(match[key]);
764
+ }
765
+ }
766
+ }
@@ -209,7 +209,7 @@ SchemaBigInt.prototype.castForQuery = function($conditional, val, context) {
209
209
  return handler.call(this, val);
210
210
  }
211
211
 
212
- return this.applySetters(null, val, context);
212
+ return this.applySetters(val, context);
213
213
  }
214
214
 
215
215
  try {
@@ -253,7 +253,7 @@ SchemaBoolean.prototype.castForQuery = function($conditional, val, context) {
253
253
  return handler.call(this, val);
254
254
  }
255
255
 
256
- return this.applySetters(null, val, context);
256
+ return this.applySetters(val, context);
257
257
  }
258
258
 
259
259
  try {
@@ -7,6 +7,7 @@
7
7
  const CastError = require('../error/cast');
8
8
  const SchemaType = require('../schemaType');
9
9
  const castInt32 = require('../cast/int32');
10
+ const handleBitwiseOperator = require('./operators/bitwise');
10
11
 
11
12
  /**
12
13
  * Int32 SchemaType constructor.
@@ -200,7 +201,11 @@ SchemaInt32.$conditionalHandlers = {
200
201
  $gt: handleSingle,
201
202
  $gte: handleSingle,
202
203
  $lt: handleSingle,
203
- $lte: handleSingle
204
+ $lte: handleSingle,
205
+ $bitsAllClear: handleBitwiseOperator,
206
+ $bitsAnyClear: handleBitwiseOperator,
207
+ $bitsAllSet: handleBitwiseOperator,
208
+ $bitsAnySet: handleBitwiseOperator
204
209
  };
205
210
 
206
211
  /*!
@@ -228,7 +233,7 @@ SchemaInt32.prototype.castForQuery = function($conditional, val, context) {
228
233
  return handler.call(this, val);
229
234
  }
230
235
 
231
- return this.applySetters(null, val, context);
236
+ return this.applySetters(val, context);
232
237
  }
233
238
 
234
239
  try {
@@ -7,43 +7,13 @@
7
7
  const MongooseBuffer = require('../types/buffer');
8
8
  const SchemaType = require('../schemaType');
9
9
  const CastError = SchemaType.CastError;
10
+ const castUUID = require('../cast/uuid');
10
11
  const utils = require('../utils');
11
12
  const handleBitwiseOperator = require('./operators/bitwise');
12
13
 
13
- const UUID_FORMAT = /[0-9a-f]{8}-[0-9a-f]{4}-[0-9][0-9a-f]{3}-[89ab][0-9a-f]{3}-[0-9a-f]{12}/i;
14
+ const UUID_FORMAT = castUUID.UUID_FORMAT;
14
15
  const Binary = MongooseBuffer.Binary;
15
16
 
16
- /**
17
- * Helper function to convert the input hex-string to a buffer
18
- * @param {String} hex The hex string to convert
19
- * @returns {Buffer} The hex as buffer
20
- * @api private
21
- */
22
-
23
- function hex2buffer(hex) {
24
- // use buffer built-in function to convert from hex-string to buffer
25
- const buff = hex != null && Buffer.from(hex, 'hex');
26
- return buff;
27
- }
28
-
29
- /**
30
- * Convert a String to Binary
31
- * @param {String} uuidStr The value to process
32
- * @returns {MongooseBuffer} The binary to store
33
- * @api private
34
- */
35
-
36
- function stringToBinary(uuidStr) {
37
- // Protect against undefined & throwing err
38
- if (typeof uuidStr !== 'string') uuidStr = '';
39
- const hex = uuidStr.replace(/[{}-]/g, ''); // remove extra characters
40
- const bytes = hex2buffer(hex);
41
- const buff = new MongooseBuffer(bytes);
42
- buff._subtype = 4;
43
-
44
- return buff;
45
- }
46
-
47
17
  /**
48
18
  * Convert binary to a uuid string
49
19
  * @param {Buffer|Binary|String} uuidBin The value to process
@@ -109,44 +79,7 @@ SchemaUUID.prototype.constructor = SchemaUUID;
109
79
  * ignore
110
80
  */
111
81
 
112
- SchemaUUID._cast = function(value) {
113
- if (value == null) {
114
- return value;
115
- }
116
-
117
- function newBuffer(initbuff) {
118
- const buff = new MongooseBuffer(initbuff);
119
- buff._subtype = 4;
120
- return buff;
121
- }
122
-
123
- if (typeof value === 'string') {
124
- if (UUID_FORMAT.test(value)) {
125
- return stringToBinary(value);
126
- } else {
127
- throw new CastError(SchemaUUID.schemaName, value, this.path);
128
- }
129
- }
130
-
131
- if (Buffer.isBuffer(value)) {
132
- return newBuffer(value);
133
- }
134
-
135
- if (value instanceof Binary) {
136
- return newBuffer(value.value(true));
137
- }
138
-
139
- // Re: gh-647 and gh-3030, we're ok with casting using `toString()`
140
- // **unless** its the default Object.toString, because "[object Object]"
141
- // doesn't really qualify as useful data
142
- if (value.toString && value.toString !== Object.prototype.toString) {
143
- if (UUID_FORMAT.test(value.toString())) {
144
- return stringToBinary(value.toString());
145
- }
146
- }
147
-
148
- throw new CastError(SchemaUUID.schemaName, value, this.path);
149
- };
82
+ SchemaUUID._cast = castUUID;
150
83
 
151
84
  /**
152
85
  * Attaches a getter for all UUID instances.
package/package.json CHANGED
@@ -1,7 +1,7 @@
1
1
  {
2
2
  "name": "mongoose",
3
3
  "description": "Mongoose MongoDB ODM",
4
- "version": "8.9.4",
4
+ "version": "8.9.6",
5
5
  "author": "Guillermo Rauch <guillermo@learnboost.com>",
6
6
  "keywords": [
7
7
  "mongodb",