You signed in with another tab or window. Reload to refresh your session.You signed out in another tab or window. Reload to refresh your session.You switched accounts on another tab or window. Reload to refresh your session.Dismiss alert
Copy file name to clipboardExpand all lines: lib/cast/bigint.js
+13-3Lines changed: 13 additions & 3 deletions
Original file line number
Diff line number
Diff line change
@@ -1,6 +1,5 @@
1
1
'use strict';
2
2
3
-
constassert=require('assert');
4
3
const{ Long }=require('bson');
5
4
6
5
/**
@@ -13,6 +12,10 @@ const { Long } = require('bson');
13
12
* @api private
14
13
*/
15
14
15
+
constMAX_BIGINT=9223372036854775807n;
16
+
constMIN_BIGINT=-9223372036854775808n;
17
+
constERROR_MESSAGE=`Mongoose only supports BigInts between ${MIN_BIGINT} and ${MAX_BIGINT} because MongoDB does not support arbitrary precision integers`;
18
+
16
19
module.exports=functioncastBigInt(val){
17
20
if(val==null){
18
21
returnval;
@@ -21,6 +24,9 @@ module.exports = function castBigInt(val) {
21
24
returnnull;
22
25
}
23
26
if(typeofval==='bigint'){
27
+
if(val>MAX_BIGINT||val<MIN_BIGINT){
28
+
thrownewError(ERROR_MESSAGE);
29
+
}
24
30
returnval;
25
31
}
26
32
@@ -29,8 +35,12 @@ module.exports = function castBigInt(val) {
29
35
}
30
36
31
37
if(typeofval==='string'||typeofval==='number'){
32
-
returnBigInt(val);
38
+
val=BigInt(val);
39
+
if(val>MAX_BIGINT||val<MIN_BIGINT){
40
+
thrownewError(ERROR_MESSAGE);
41
+
}
42
+
returnval;
33
43
}
34
44
35
-
assert.ok(false);
45
+
thrownewError(`Cannot convert value to BigInt: "${val}"`);
// Safe to do this even in the schemaFieldsOnly case because we assume there's no nested paths
3917
3943
constvalue=this._doc[key];
3918
3944
if(valueinstanceofDate){
3919
3945
ret[key]=newDate(value);
@@ -4066,6 +4092,7 @@ Document.prototype.$__toObjectShallow = function $__toObjectShallow() {
4066
4092
* @param {Boolean} [options.flattenMaps=false] if true, convert Maps to POJOs. Useful if you want to `JSON.stringify()` the result of `toObject()`.
4067
4093
* @param {Boolean} [options.flattenObjectIds=false] if true, convert any ObjectIds in the result to 24 character hex strings.
4068
4094
* @param {Boolean} [options.useProjection=false] - If true, omits fields that are excluded in this document's projection. Unless you specified a projection, this will omit any field that has `select: false` in the schema.
4095
+
* @param {Boolean} [options.schemaFieldsOnly=false] - If true, the resulting object will only have fields that are defined in the document's schema. By default, `toObject()` returns all fields in the underlying document from MongoDB, including ones that are not listed in the schema.
4069
4096
* @return {Object} document as a plain old JavaScript object (POJO). This object may contain ObjectIds, Maps, Dates, mongodb.Binary, Buffers, and other non-POJO values.
@@ -4336,6 +4363,7 @@ function omitDeselectedFields(self, json) {
4336
4363
* @param {Object} options
4337
4364
* @param {Boolean} [options.flattenMaps=true] if true, convert Maps to [POJOs](https://masteringjs.io/tutorials/fundamentals/pojo). Useful if you want to `JSON.stringify()` the result.
4338
4365
* @param {Boolean} [options.flattenObjectIds=false] if true, convert any ObjectIds in the result to 24 character hex strings.
4366
+
* @param {Boolean} [options.schemaFieldsOnly=false] - If true, the resulting object will only have fields that are defined in the document's schema. By default, `toJSON()` returns all fields in the underlying document from MongoDB, including ones that are not listed in the schema.
* @param {Object|Function} [options.match=null] Add an additional filter to the populate query. Can be a filter object containing [MongoDB query syntax](https://www.mongodb.com/docs/manual/tutorial/query-documents/), or a function that returns a filter object.
4507
4535
* @param {Function} [options.transform=null] Function that Mongoose will call on every populated document that allows you to transform the populated document.
4508
4536
* @param {Object} [options.options=null] Additional options like `limit` and `lean`.
4537
+
* @param {Boolean} [options.forceRepopulate=true] Set to `false` to prevent Mongoose from repopulating paths that are already populated
4538
+
* @param {Boolean} [options.ordered=false] Set to `true` to execute any populate queries one at a time, as opposed to in parallel. We recommend setting this option to `true` if using transactions, especially if also populating multiple paths or paths with multiple models. MongoDB server does **not** support multiple operations in parallel on a single transaction.
4509
4539
* @param {Function} [callback] Callback
4510
4540
* @see population https://mongoosejs.com/docs/populate.html
* @param {Object} [options.options=null] Additional options like `limit` and `lean`.
4387
4382
* @param {Function} [options.transform=null] Function that Mongoose will call on every populated document that allows you to transform the populated document.
4388
4383
* @param {Boolean} [options.forceRepopulate=true] Set to `false` to prevent Mongoose from repopulating paths that are already populated
4384
+
* @param {Boolean} [options.ordered=false] Set to `true` to execute any populate queries one at a time, as opposed to in parallel. Set this option to `true` if populating multiple paths or paths with multiple models in transactions.
0 commit comments