started mongo stuff, but it's a PITA. fuck mongo
This commit is contained in:
parent
4cd36ea3fc
commit
0f1bc5dff3
937 changed files with 205043 additions and 0 deletions
46
parts/3/phonebookBackend/node_modules/mongoose/lib/cast/bigint.js
generated
vendored
Normal file
46
parts/3/phonebookBackend/node_modules/mongoose/lib/cast/bigint.js
generated
vendored
Normal file
|
|
@ -0,0 +1,46 @@
|
|||
'use strict';
|
||||
|
||||
const { Long } = require('bson');
|
||||
|
||||
/**
|
||||
* Given a value, cast it to a BigInt, or throw an `Error` if the value
|
||||
* cannot be casted. `null` and `undefined` are considered valid.
|
||||
*
|
||||
* @param {Any} value
|
||||
* @return {Number}
|
||||
* @throws {Error} if `value` is not one of the allowed values
|
||||
* @api private
|
||||
*/
|
||||
|
||||
const MAX_BIGINT = 9223372036854775807n;
|
||||
const MIN_BIGINT = -9223372036854775808n;
|
||||
const ERROR_MESSAGE = `Mongoose only supports BigInts between ${MIN_BIGINT} and ${MAX_BIGINT} because MongoDB does not support arbitrary precision integers`;
|
||||
|
||||
module.exports = function castBigInt(val) {
|
||||
if (val == null) {
|
||||
return val;
|
||||
}
|
||||
if (val === '') {
|
||||
return null;
|
||||
}
|
||||
if (typeof val === 'bigint') {
|
||||
if (val > MAX_BIGINT || val < MIN_BIGINT) {
|
||||
throw new Error(ERROR_MESSAGE);
|
||||
}
|
||||
return val;
|
||||
}
|
||||
|
||||
if (val instanceof Long) {
|
||||
return val.toBigInt();
|
||||
}
|
||||
|
||||
if (typeof val === 'string' || typeof val === 'number') {
|
||||
val = BigInt(val);
|
||||
if (val > MAX_BIGINT || val < MIN_BIGINT) {
|
||||
throw new Error(ERROR_MESSAGE);
|
||||
}
|
||||
return val;
|
||||
}
|
||||
|
||||
throw new Error(`Cannot convert value to BigInt: "${val}"`);
|
||||
};
|
||||
Loading…
Add table
Add a link
Reference in a new issue