@datanimbus/mongoose 6.13.10
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/.eslintrc.js +232 -0
- package/.mocharc.yml +4 -0
- package/LICENSE.md +22 -0
- package/README.md +387 -0
- package/SECURITY.md +1 -0
- package/browser.js +8 -0
- package/dist/browser.umd.js +2 -0
- package/index.js +63 -0
- package/lgtm.yml +12 -0
- package/lib/aggregate.js +1164 -0
- package/lib/browser.js +158 -0
- package/lib/browserDocument.js +101 -0
- package/lib/cast/boolean.js +32 -0
- package/lib/cast/date.js +41 -0
- package/lib/cast/decimal128.js +36 -0
- package/lib/cast/number.js +42 -0
- package/lib/cast/objectid.js +29 -0
- package/lib/cast/string.js +37 -0
- package/lib/cast.js +418 -0
- package/lib/collection.js +311 -0
- package/lib/connection.js +1574 -0
- package/lib/connectionstate.js +26 -0
- package/lib/cursor/AggregationCursor.js +380 -0
- package/lib/cursor/ChangeStream.js +154 -0
- package/lib/cursor/QueryCursor.js +538 -0
- package/lib/document.js +4771 -0
- package/lib/document_provider.js +30 -0
- package/lib/driver.js +15 -0
- package/lib/drivers/SPEC.md +4 -0
- package/lib/drivers/browser/ReadPreference.js +7 -0
- package/lib/drivers/browser/binary.js +14 -0
- package/lib/drivers/browser/decimal128.js +7 -0
- package/lib/drivers/browser/index.js +16 -0
- package/lib/drivers/browser/objectid.js +29 -0
- package/lib/drivers/node-mongodb-native/ReadPreference.js +47 -0
- package/lib/drivers/node-mongodb-native/binary.js +10 -0
- package/lib/drivers/node-mongodb-native/collection.js +460 -0
- package/lib/drivers/node-mongodb-native/connection.js +166 -0
- package/lib/drivers/node-mongodb-native/decimal128.js +7 -0
- package/lib/drivers/node-mongodb-native/index.js +12 -0
- package/lib/drivers/node-mongodb-native/objectid.js +16 -0
- package/lib/error/browserMissingSchema.js +28 -0
- package/lib/error/bulkWriteError.js +41 -0
- package/lib/error/cast.js +158 -0
- package/lib/error/disconnected.js +33 -0
- package/lib/error/divergentArray.js +38 -0
- package/lib/error/eachAsyncMultiError.js +41 -0
- package/lib/error/index.js +228 -0
- package/lib/error/messages.js +47 -0
- package/lib/error/missingSchema.js +31 -0
- package/lib/error/mongooseError.js +13 -0
- package/lib/error/notFound.js +45 -0
- package/lib/error/objectExpected.js +30 -0
- package/lib/error/objectParameter.js +30 -0
- package/lib/error/overwriteModel.js +30 -0
- package/lib/error/parallelSave.js +30 -0
- package/lib/error/parallelValidate.js +31 -0
- package/lib/error/serverSelection.js +61 -0
- package/lib/error/setOptionError.js +101 -0
- package/lib/error/strict.js +33 -0
- package/lib/error/strictPopulate.js +29 -0
- package/lib/error/syncIndexes.js +30 -0
- package/lib/error/validation.js +103 -0
- package/lib/error/validator.js +99 -0
- package/lib/error/version.js +36 -0
- package/lib/helpers/aggregate/prepareDiscriminatorPipeline.js +39 -0
- package/lib/helpers/aggregate/stringifyFunctionOperators.js +50 -0
- package/lib/helpers/arrayDepth.js +33 -0
- package/lib/helpers/clone.js +177 -0
- package/lib/helpers/common.js +127 -0
- package/lib/helpers/cursor/eachAsync.js +222 -0
- package/lib/helpers/discriminator/areDiscriminatorValuesEqual.js +16 -0
- package/lib/helpers/discriminator/checkEmbeddedDiscriminatorKeyProjection.js +12 -0
- package/lib/helpers/discriminator/getConstructor.js +26 -0
- package/lib/helpers/discriminator/getDiscriminatorByValue.js +28 -0
- package/lib/helpers/discriminator/getSchemaDiscriminatorByValue.js +27 -0
- package/lib/helpers/discriminator/mergeDiscriminatorSchema.js +64 -0
- package/lib/helpers/document/applyDefaults.js +128 -0
- package/lib/helpers/document/cleanModifiedSubpaths.js +35 -0
- package/lib/helpers/document/compile.js +237 -0
- package/lib/helpers/document/getEmbeddedDiscriminatorPath.js +50 -0
- package/lib/helpers/document/handleSpreadDoc.js +35 -0
- package/lib/helpers/each.js +25 -0
- package/lib/helpers/error/combinePathErrors.js +22 -0
- package/lib/helpers/firstKey.js +8 -0
- package/lib/helpers/get.js +65 -0
- package/lib/helpers/getConstructorName.js +16 -0
- package/lib/helpers/getDefaultBulkwriteResult.js +27 -0
- package/lib/helpers/getFunctionName.js +10 -0
- package/lib/helpers/immediate.js +16 -0
- package/lib/helpers/indexes/applySchemaCollation.js +13 -0
- package/lib/helpers/indexes/decorateDiscriminatorIndexOptions.js +14 -0
- package/lib/helpers/indexes/getRelatedIndexes.js +59 -0
- package/lib/helpers/indexes/isDefaultIdIndex.js +18 -0
- package/lib/helpers/indexes/isIndexEqual.js +96 -0
- package/lib/helpers/indexes/isTextIndex.js +16 -0
- package/lib/helpers/isAsyncFunction.js +9 -0
- package/lib/helpers/isBsonType.js +16 -0
- package/lib/helpers/isMongooseObject.js +22 -0
- package/lib/helpers/isObject.js +16 -0
- package/lib/helpers/isPromise.js +6 -0
- package/lib/helpers/isSimpleValidator.js +22 -0
- package/lib/helpers/model/applyDefaultsToPOJO.js +52 -0
- package/lib/helpers/model/applyHooks.js +149 -0
- package/lib/helpers/model/applyMethods.js +70 -0
- package/lib/helpers/model/applyStaticHooks.js +71 -0
- package/lib/helpers/model/applyStatics.js +13 -0
- package/lib/helpers/model/castBulkWrite.js +240 -0
- package/lib/helpers/model/discriminator.js +218 -0
- package/lib/helpers/model/pushNestedArrayPaths.js +15 -0
- package/lib/helpers/once.js +12 -0
- package/lib/helpers/parallelLimit.js +55 -0
- package/lib/helpers/path/parentPaths.js +18 -0
- package/lib/helpers/path/setDottedPath.js +33 -0
- package/lib/helpers/pluralize.js +94 -0
- package/lib/helpers/populate/SkipPopulateValue.js +10 -0
- package/lib/helpers/populate/assignRawDocsToIdStructure.js +124 -0
- package/lib/helpers/populate/assignVals.js +337 -0
- package/lib/helpers/populate/createPopulateQueryFilter.js +97 -0
- package/lib/helpers/populate/getModelsMapForPopulate.js +756 -0
- package/lib/helpers/populate/getSchemaTypes.js +233 -0
- package/lib/helpers/populate/getVirtual.js +72 -0
- package/lib/helpers/populate/leanPopulateMap.js +7 -0
- package/lib/helpers/populate/lookupLocalFields.js +40 -0
- package/lib/helpers/populate/markArraySubdocsPopulated.js +47 -0
- package/lib/helpers/populate/modelNamesFromRefPath.js +68 -0
- package/lib/helpers/populate/removeDeselectedForeignField.js +31 -0
- package/lib/helpers/populate/validateRef.js +19 -0
- package/lib/helpers/printJestWarning.js +21 -0
- package/lib/helpers/printStrictQueryWarning.js +11 -0
- package/lib/helpers/processConnectionOptions.js +65 -0
- package/lib/helpers/projection/applyProjection.js +77 -0
- package/lib/helpers/projection/hasIncludedChildren.js +41 -0
- package/lib/helpers/projection/isDefiningProjection.js +18 -0
- package/lib/helpers/projection/isExclusive.js +35 -0
- package/lib/helpers/projection/isInclusive.js +38 -0
- package/lib/helpers/projection/isNestedProjection.js +8 -0
- package/lib/helpers/projection/isPathExcluded.js +40 -0
- package/lib/helpers/projection/isPathSelectedInclusive.js +28 -0
- package/lib/helpers/projection/isSubpath.js +14 -0
- package/lib/helpers/projection/parseProjection.js +33 -0
- package/lib/helpers/promiseOrCallback.js +55 -0
- package/lib/helpers/query/applyGlobalOption.js +29 -0
- package/lib/helpers/query/applyQueryMiddleware.js +79 -0
- package/lib/helpers/query/cast$expr.js +282 -0
- package/lib/helpers/query/castFilterPath.js +54 -0
- package/lib/helpers/query/castUpdate.js +560 -0
- package/lib/helpers/query/completeMany.js +52 -0
- package/lib/helpers/query/getEmbeddedDiscriminatorPath.js +90 -0
- package/lib/helpers/query/handleImmutable.js +28 -0
- package/lib/helpers/query/hasDollarKeys.js +23 -0
- package/lib/helpers/query/isOperator.js +14 -0
- package/lib/helpers/query/sanitizeFilter.js +38 -0
- package/lib/helpers/query/sanitizeProjection.js +14 -0
- package/lib/helpers/query/selectPopulatedFields.js +49 -0
- package/lib/helpers/query/trusted.js +13 -0
- package/lib/helpers/query/validOps.js +24 -0
- package/lib/helpers/query/wrapThunk.js +31 -0
- package/lib/helpers/schema/addAutoId.js +7 -0
- package/lib/helpers/schema/applyBuiltinPlugins.js +12 -0
- package/lib/helpers/schema/applyPlugins.js +55 -0
- package/lib/helpers/schema/applyWriteConcern.js +30 -0
- package/lib/helpers/schema/cleanPositionalOperators.js +12 -0
- package/lib/helpers/schema/getIndexes.js +164 -0
- package/lib/helpers/schema/getKeysInSchemaOrder.js +28 -0
- package/lib/helpers/schema/getPath.js +37 -0
- package/lib/helpers/schema/handleIdOption.js +20 -0
- package/lib/helpers/schema/handleTimestampOption.js +24 -0
- package/lib/helpers/schema/idGetter.js +32 -0
- package/lib/helpers/schema/merge.js +28 -0
- package/lib/helpers/schematype/handleImmutable.js +50 -0
- package/lib/helpers/setDefaultsOnInsert.js +132 -0
- package/lib/helpers/specialProperties.js +3 -0
- package/lib/helpers/symbols.js +20 -0
- package/lib/helpers/timers.js +3 -0
- package/lib/helpers/timestamps/setDocumentTimestamps.js +26 -0
- package/lib/helpers/timestamps/setupTimestamps.js +101 -0
- package/lib/helpers/topology/allServersUnknown.js +12 -0
- package/lib/helpers/topology/isAtlas.js +31 -0
- package/lib/helpers/topology/isSSLError.js +16 -0
- package/lib/helpers/update/applyTimestampsToChildren.js +193 -0
- package/lib/helpers/update/applyTimestampsToUpdate.js +117 -0
- package/lib/helpers/update/castArrayFilters.js +109 -0
- package/lib/helpers/update/modifiedPaths.js +33 -0
- package/lib/helpers/update/moveImmutableProperties.js +53 -0
- package/lib/helpers/update/removeUnusedArrayFilters.js +32 -0
- package/lib/helpers/update/updatedPathsByArrayFilter.js +27 -0
- package/lib/helpers/updateValidators.js +249 -0
- package/lib/index.js +1338 -0
- package/lib/internal.js +46 -0
- package/lib/model.js +5474 -0
- package/lib/options/PopulateOptions.js +36 -0
- package/lib/options/SchemaArrayOptions.js +78 -0
- package/lib/options/SchemaBufferOptions.js +38 -0
- package/lib/options/SchemaDateOptions.js +71 -0
- package/lib/options/SchemaDocumentArrayOptions.js +68 -0
- package/lib/options/SchemaMapOptions.js +43 -0
- package/lib/options/SchemaNumberOptions.js +101 -0
- package/lib/options/SchemaObjectIdOptions.js +64 -0
- package/lib/options/SchemaStringOptions.js +138 -0
- package/lib/options/SchemaSubdocumentOptions.js +42 -0
- package/lib/options/SchemaTypeOptions.js +244 -0
- package/lib/options/VirtualOptions.js +164 -0
- package/lib/options/propertyOptions.js +8 -0
- package/lib/options/removeOptions.js +14 -0
- package/lib/options/saveOptions.js +14 -0
- package/lib/options.js +15 -0
- package/lib/plugins/index.js +7 -0
- package/lib/plugins/removeSubdocs.js +31 -0
- package/lib/plugins/saveSubdocs.js +66 -0
- package/lib/plugins/sharding.js +83 -0
- package/lib/plugins/trackTransaction.js +92 -0
- package/lib/plugins/validateBeforeSave.js +45 -0
- package/lib/promise_provider.js +49 -0
- package/lib/query.js +5982 -0
- package/lib/queryhelpers.js +353 -0
- package/lib/schema/DocumentArrayElement.js +100 -0
- package/lib/schema/SubdocumentPath.js +369 -0
- package/lib/schema/array.js +663 -0
- package/lib/schema/boolean.js +267 -0
- package/lib/schema/buffer.js +269 -0
- package/lib/schema/date.js +404 -0
- package/lib/schema/decimal128.js +210 -0
- package/lib/schema/documentarray.js +598 -0
- package/lib/schema/index.js +39 -0
- package/lib/schema/map.js +84 -0
- package/lib/schema/mixed.js +132 -0
- package/lib/schema/number.js +438 -0
- package/lib/schema/objectid.js +295 -0
- package/lib/schema/operators/bitwise.js +38 -0
- package/lib/schema/operators/exists.js +12 -0
- package/lib/schema/operators/geospatial.js +107 -0
- package/lib/schema/operators/helpers.js +32 -0
- package/lib/schema/operators/text.js +39 -0
- package/lib/schema/operators/type.js +20 -0
- package/lib/schema/string.js +694 -0
- package/lib/schema/symbols.js +5 -0
- package/lib/schema/uuid.js +334 -0
- package/lib/schema.js +2665 -0
- package/lib/schematype.js +1727 -0
- package/lib/statemachine.js +207 -0
- package/lib/types/ArraySubdocument.js +189 -0
- package/lib/types/DocumentArray/index.js +113 -0
- package/lib/types/DocumentArray/isMongooseDocumentArray.js +5 -0
- package/lib/types/DocumentArray/methods/index.js +383 -0
- package/lib/types/array/index.js +116 -0
- package/lib/types/array/isMongooseArray.js +5 -0
- package/lib/types/array/methods/index.js +1014 -0
- package/lib/types/buffer.js +277 -0
- package/lib/types/decimal128.js +13 -0
- package/lib/types/index.js +20 -0
- package/lib/types/map.js +354 -0
- package/lib/types/objectid.js +41 -0
- package/lib/types/subdocument.js +432 -0
- package/lib/utils.js +1011 -0
- package/lib/validoptions.js +36 -0
- package/lib/virtualtype.js +175 -0
- package/package.json +164 -0
- package/scripts/build-browser.js +18 -0
- package/scripts/create-tarball.js +7 -0
- package/scripts/generateSearch.js +154 -0
- package/scripts/tsc-diagnostics-check.js +15 -0
- package/tools/auth.js +31 -0
- package/tools/repl.js +35 -0
- package/tools/sharded.js +46 -0
- package/tsconfig.json +9 -0
- package/types/aggregate.d.ts +174 -0
- package/types/augmentations.d.ts +9 -0
- package/types/callback.d.ts +8 -0
- package/types/collection.d.ts +44 -0
- package/types/connection.d.ts +242 -0
- package/types/cursor.d.ts +62 -0
- package/types/document.d.ts +266 -0
- package/types/error.d.ts +138 -0
- package/types/expressions.d.ts +2936 -0
- package/types/helpers.d.ts +32 -0
- package/types/index.d.ts +627 -0
- package/types/indexes.d.ts +98 -0
- package/types/inferschematype.d.ts +228 -0
- package/types/middlewares.d.ts +32 -0
- package/types/models.d.ts +488 -0
- package/types/mongooseoptions.d.ts +206 -0
- package/types/pipelinestage.d.ts +297 -0
- package/types/populate.d.ts +45 -0
- package/types/query.d.ts +659 -0
- package/types/schemaoptions.d.ts +231 -0
- package/types/schematypes.d.ts +478 -0
- package/types/session.d.ts +36 -0
- package/types/types.d.ts +104 -0
- package/types/utility.d.ts +43 -0
- package/types/validation.d.ts +34 -0
- package/types/virtuals.d.ts +14 -0
|
@@ -0,0 +1,132 @@
|
|
|
1
|
+
/*!
|
|
2
|
+
* Module dependencies.
|
|
3
|
+
*/
|
|
4
|
+
|
|
5
|
+
'use strict';
|
|
6
|
+
|
|
7
|
+
const SchemaType = require('../schematype');
|
|
8
|
+
const symbols = require('./symbols');
|
|
9
|
+
const isObject = require('../helpers/isObject');
|
|
10
|
+
const utils = require('../utils');
|
|
11
|
+
|
|
12
|
+
/**
|
|
13
|
+
* Mixed SchemaType constructor.
|
|
14
|
+
*
|
|
15
|
+
* @param {String} path
|
|
16
|
+
* @param {Object} options
|
|
17
|
+
* @inherits SchemaType
|
|
18
|
+
* @api public
|
|
19
|
+
*/
|
|
20
|
+
|
|
21
|
+
function Mixed(path, options) {
|
|
22
|
+
if (options && options.default) {
|
|
23
|
+
const def = options.default;
|
|
24
|
+
if (Array.isArray(def) && def.length === 0) {
|
|
25
|
+
// make sure empty array defaults are handled
|
|
26
|
+
options.default = Array;
|
|
27
|
+
} else if (!options.shared && isObject(def) && Object.keys(def).length === 0) {
|
|
28
|
+
// prevent odd "shared" objects between documents
|
|
29
|
+
options.default = function() {
|
|
30
|
+
return {};
|
|
31
|
+
};
|
|
32
|
+
}
|
|
33
|
+
}
|
|
34
|
+
|
|
35
|
+
SchemaType.call(this, path, options, 'Mixed');
|
|
36
|
+
|
|
37
|
+
this[symbols.schemaMixedSymbol] = true;
|
|
38
|
+
}
|
|
39
|
+
|
|
40
|
+
/**
|
|
41
|
+
* This schema type's name, to defend against minifiers that mangle
|
|
42
|
+
* function names.
|
|
43
|
+
*
|
|
44
|
+
* @api public
|
|
45
|
+
*/
|
|
46
|
+
Mixed.schemaName = 'Mixed';
|
|
47
|
+
|
|
48
|
+
Mixed.defaultOptions = {};
|
|
49
|
+
|
|
50
|
+
/*!
|
|
51
|
+
* Inherits from SchemaType.
|
|
52
|
+
*/
|
|
53
|
+
Mixed.prototype = Object.create(SchemaType.prototype);
|
|
54
|
+
Mixed.prototype.constructor = Mixed;
|
|
55
|
+
|
|
56
|
+
/**
|
|
57
|
+
* Attaches a getter for all Mixed paths.
|
|
58
|
+
*
|
|
59
|
+
* #### Example:
|
|
60
|
+
*
|
|
61
|
+
* // Hide the 'hidden' path
|
|
62
|
+
* mongoose.Schema.Mixed.get(v => Object.assign({}, v, { hidden: null }));
|
|
63
|
+
*
|
|
64
|
+
* const Model = mongoose.model('Test', new Schema({ test: {} }));
|
|
65
|
+
* new Model({ test: { hidden: 'Secret!' } }).test.hidden; // null
|
|
66
|
+
*
|
|
67
|
+
* @param {Function} getter
|
|
68
|
+
* @return {this}
|
|
69
|
+
* @function get
|
|
70
|
+
* @static
|
|
71
|
+
* @api public
|
|
72
|
+
*/
|
|
73
|
+
|
|
74
|
+
Mixed.get = SchemaType.get;
|
|
75
|
+
|
|
76
|
+
/**
|
|
77
|
+
* Sets a default option for all Mixed instances.
|
|
78
|
+
*
|
|
79
|
+
* #### Example:
|
|
80
|
+
*
|
|
81
|
+
* // Make all mixed instances have `required` of true by default.
|
|
82
|
+
* mongoose.Schema.Mixed.set('required', true);
|
|
83
|
+
*
|
|
84
|
+
* const User = mongoose.model('User', new Schema({ test: mongoose.Mixed }));
|
|
85
|
+
* new User({ }).validateSync().errors.test.message; // Path `test` is required.
|
|
86
|
+
*
|
|
87
|
+
* @param {String} option The option you'd like to set the value for
|
|
88
|
+
* @param {Any} value value for option
|
|
89
|
+
* @return {undefined}
|
|
90
|
+
* @function set
|
|
91
|
+
* @static
|
|
92
|
+
* @api public
|
|
93
|
+
*/
|
|
94
|
+
|
|
95
|
+
Mixed.set = SchemaType.set;
|
|
96
|
+
|
|
97
|
+
/**
|
|
98
|
+
* Casts `val` for Mixed.
|
|
99
|
+
*
|
|
100
|
+
* _this is a no-op_
|
|
101
|
+
*
|
|
102
|
+
* @param {Object} value to cast
|
|
103
|
+
* @api private
|
|
104
|
+
*/
|
|
105
|
+
|
|
106
|
+
Mixed.prototype.cast = function(val) {
|
|
107
|
+
if (val instanceof Error) {
|
|
108
|
+
return utils.errorToPOJO(val);
|
|
109
|
+
}
|
|
110
|
+
return val;
|
|
111
|
+
};
|
|
112
|
+
|
|
113
|
+
/**
|
|
114
|
+
* Casts contents for queries.
|
|
115
|
+
*
|
|
116
|
+
* @param {String} $cond
|
|
117
|
+
* @param {any} [val]
|
|
118
|
+
* @api private
|
|
119
|
+
*/
|
|
120
|
+
|
|
121
|
+
Mixed.prototype.castForQuery = function($cond, val) {
|
|
122
|
+
if (arguments.length === 2) {
|
|
123
|
+
return val;
|
|
124
|
+
}
|
|
125
|
+
return $cond;
|
|
126
|
+
};
|
|
127
|
+
|
|
128
|
+
/*!
|
|
129
|
+
* Module exports.
|
|
130
|
+
*/
|
|
131
|
+
|
|
132
|
+
module.exports = Mixed;
|
|
@@ -0,0 +1,438 @@
|
|
|
1
|
+
'use strict';
|
|
2
|
+
|
|
3
|
+
/*!
|
|
4
|
+
* Module requirements.
|
|
5
|
+
*/
|
|
6
|
+
|
|
7
|
+
const MongooseError = require('../error/index');
|
|
8
|
+
const SchemaNumberOptions = require('../options/SchemaNumberOptions');
|
|
9
|
+
const SchemaType = require('../schematype');
|
|
10
|
+
const castNumber = require('../cast/number');
|
|
11
|
+
const handleBitwiseOperator = require('./operators/bitwise');
|
|
12
|
+
const utils = require('../utils');
|
|
13
|
+
|
|
14
|
+
const CastError = SchemaType.CastError;
|
|
15
|
+
|
|
16
|
+
/**
|
|
17
|
+
* Number SchemaType constructor.
|
|
18
|
+
*
|
|
19
|
+
* @param {String} key
|
|
20
|
+
* @param {Object} options
|
|
21
|
+
* @inherits SchemaType
|
|
22
|
+
* @api public
|
|
23
|
+
*/
|
|
24
|
+
|
|
25
|
+
function SchemaNumber(key, options) {
|
|
26
|
+
SchemaType.call(this, key, options, 'Number');
|
|
27
|
+
}
|
|
28
|
+
|
|
29
|
+
/**
|
|
30
|
+
* Attaches a getter for all Number instances.
|
|
31
|
+
*
|
|
32
|
+
* #### Example:
|
|
33
|
+
*
|
|
34
|
+
* // Make all numbers round down
|
|
35
|
+
* mongoose.Number.get(function(v) { return Math.floor(v); });
|
|
36
|
+
*
|
|
37
|
+
* const Model = mongoose.model('Test', new Schema({ test: Number }));
|
|
38
|
+
* new Model({ test: 3.14 }).test; // 3
|
|
39
|
+
*
|
|
40
|
+
* @param {Function} getter
|
|
41
|
+
* @return {this}
|
|
42
|
+
* @function get
|
|
43
|
+
* @static
|
|
44
|
+
* @api public
|
|
45
|
+
*/
|
|
46
|
+
|
|
47
|
+
SchemaNumber.get = SchemaType.get;
|
|
48
|
+
|
|
49
|
+
/**
|
|
50
|
+
* Sets a default option for all Number instances.
|
|
51
|
+
*
|
|
52
|
+
* #### Example:
|
|
53
|
+
*
|
|
54
|
+
* // Make all numbers have option `min` equal to 0.
|
|
55
|
+
* mongoose.Schema.Number.set('min', 0);
|
|
56
|
+
*
|
|
57
|
+
* const Order = mongoose.model('Order', new Schema({ amount: Number }));
|
|
58
|
+
* new Order({ amount: -10 }).validateSync().errors.amount.message; // Path `amount` must be larger than 0.
|
|
59
|
+
*
|
|
60
|
+
* @param {String} option The option you'd like to set the value for
|
|
61
|
+
* @param {Any} value value for option
|
|
62
|
+
* @return {undefined}
|
|
63
|
+
* @function set
|
|
64
|
+
* @static
|
|
65
|
+
* @api public
|
|
66
|
+
*/
|
|
67
|
+
|
|
68
|
+
SchemaNumber.set = SchemaType.set;
|
|
69
|
+
|
|
70
|
+
/*!
|
|
71
|
+
* ignore
|
|
72
|
+
*/
|
|
73
|
+
|
|
74
|
+
SchemaNumber._cast = castNumber;
|
|
75
|
+
|
|
76
|
+
/**
|
|
77
|
+
* Get/set the function used to cast arbitrary values to numbers.
|
|
78
|
+
*
|
|
79
|
+
* #### Example:
|
|
80
|
+
*
|
|
81
|
+
* // Make Mongoose cast empty strings '' to 0 for paths declared as numbers
|
|
82
|
+
* const original = mongoose.Number.cast();
|
|
83
|
+
* mongoose.Number.cast(v => {
|
|
84
|
+
* if (v === '') { return 0; }
|
|
85
|
+
* return original(v);
|
|
86
|
+
* });
|
|
87
|
+
*
|
|
88
|
+
* // Or disable casting entirely
|
|
89
|
+
* mongoose.Number.cast(false);
|
|
90
|
+
*
|
|
91
|
+
* @param {Function} caster
|
|
92
|
+
* @return {Function}
|
|
93
|
+
* @function get
|
|
94
|
+
* @static
|
|
95
|
+
* @api public
|
|
96
|
+
*/
|
|
97
|
+
|
|
98
|
+
SchemaNumber.cast = function cast(caster) {
|
|
99
|
+
if (arguments.length === 0) {
|
|
100
|
+
return this._cast;
|
|
101
|
+
}
|
|
102
|
+
if (caster === false) {
|
|
103
|
+
caster = this._defaultCaster;
|
|
104
|
+
}
|
|
105
|
+
this._cast = caster;
|
|
106
|
+
|
|
107
|
+
return this._cast;
|
|
108
|
+
};
|
|
109
|
+
|
|
110
|
+
/*!
|
|
111
|
+
* ignore
|
|
112
|
+
*/
|
|
113
|
+
|
|
114
|
+
SchemaNumber._defaultCaster = v => {
|
|
115
|
+
if (typeof v !== 'number') {
|
|
116
|
+
throw new Error();
|
|
117
|
+
}
|
|
118
|
+
return v;
|
|
119
|
+
};
|
|
120
|
+
|
|
121
|
+
/**
|
|
122
|
+
* This schema type's name, to defend against minifiers that mangle
|
|
123
|
+
* function names.
|
|
124
|
+
*
|
|
125
|
+
* @api public
|
|
126
|
+
*/
|
|
127
|
+
SchemaNumber.schemaName = 'Number';
|
|
128
|
+
|
|
129
|
+
SchemaNumber.defaultOptions = {};
|
|
130
|
+
|
|
131
|
+
/*!
|
|
132
|
+
* Inherits from SchemaType.
|
|
133
|
+
*/
|
|
134
|
+
SchemaNumber.prototype = Object.create(SchemaType.prototype);
|
|
135
|
+
SchemaNumber.prototype.constructor = SchemaNumber;
|
|
136
|
+
SchemaNumber.prototype.OptionsConstructor = SchemaNumberOptions;
|
|
137
|
+
|
|
138
|
+
/*!
|
|
139
|
+
* ignore
|
|
140
|
+
*/
|
|
141
|
+
|
|
142
|
+
SchemaNumber._checkRequired = v => typeof v === 'number' || v instanceof Number;
|
|
143
|
+
|
|
144
|
+
/**
|
|
145
|
+
* Override the function the required validator uses to check whether a string
|
|
146
|
+
* passes the `required` check.
|
|
147
|
+
*
|
|
148
|
+
* @param {Function} fn
|
|
149
|
+
* @return {Function}
|
|
150
|
+
* @function checkRequired
|
|
151
|
+
* @static
|
|
152
|
+
* @api public
|
|
153
|
+
*/
|
|
154
|
+
|
|
155
|
+
SchemaNumber.checkRequired = SchemaType.checkRequired;
|
|
156
|
+
|
|
157
|
+
/**
|
|
158
|
+
* Check if the given value satisfies a required validator.
|
|
159
|
+
*
|
|
160
|
+
* @param {Any} value
|
|
161
|
+
* @param {Document} doc
|
|
162
|
+
* @return {Boolean}
|
|
163
|
+
* @api public
|
|
164
|
+
*/
|
|
165
|
+
|
|
166
|
+
SchemaNumber.prototype.checkRequired = function checkRequired(value, doc) {
|
|
167
|
+
if (typeof value === 'object' && SchemaType._isRef(this, value, doc, true)) {
|
|
168
|
+
return value != null;
|
|
169
|
+
}
|
|
170
|
+
|
|
171
|
+
// `require('util').inherits()` does **not** copy static properties, and
|
|
172
|
+
// plugins like mongoose-float use `inherits()` for pre-ES6.
|
|
173
|
+
const _checkRequired = typeof this.constructor.checkRequired === 'function' ?
|
|
174
|
+
this.constructor.checkRequired() :
|
|
175
|
+
SchemaNumber.checkRequired();
|
|
176
|
+
|
|
177
|
+
return _checkRequired(value);
|
|
178
|
+
};
|
|
179
|
+
|
|
180
|
+
/**
|
|
181
|
+
* Sets a minimum number validator.
|
|
182
|
+
*
|
|
183
|
+
* #### Example:
|
|
184
|
+
*
|
|
185
|
+
* const s = new Schema({ n: { type: Number, min: 10 })
|
|
186
|
+
* const M = db.model('M', s)
|
|
187
|
+
* const m = new M({ n: 9 })
|
|
188
|
+
* m.save(function (err) {
|
|
189
|
+
* console.error(err) // validator error
|
|
190
|
+
* m.n = 10;
|
|
191
|
+
* m.save() // success
|
|
192
|
+
* })
|
|
193
|
+
*
|
|
194
|
+
* // custom error messages
|
|
195
|
+
* // We can also use the special {MIN} token which will be replaced with the invalid value
|
|
196
|
+
* const min = [10, 'The value of path `{PATH}` ({VALUE}) is beneath the limit ({MIN}).'];
|
|
197
|
+
* const schema = new Schema({ n: { type: Number, min: min })
|
|
198
|
+
* const M = mongoose.model('Measurement', schema);
|
|
199
|
+
* const s= new M({ n: 4 });
|
|
200
|
+
* s.validate(function (err) {
|
|
201
|
+
* console.log(String(err)) // ValidationError: The value of path `n` (4) is beneath the limit (10).
|
|
202
|
+
* })
|
|
203
|
+
*
|
|
204
|
+
* @param {Number} value minimum number
|
|
205
|
+
* @param {String} [message] optional custom error message
|
|
206
|
+
* @return {SchemaType} this
|
|
207
|
+
* @see Customized Error Messages #error_messages_MongooseError-messages
|
|
208
|
+
* @api public
|
|
209
|
+
*/
|
|
210
|
+
|
|
211
|
+
SchemaNumber.prototype.min = function(value, message) {
|
|
212
|
+
if (this.minValidator) {
|
|
213
|
+
this.validators = this.validators.filter(function(v) {
|
|
214
|
+
return v.validator !== this.minValidator;
|
|
215
|
+
}, this);
|
|
216
|
+
}
|
|
217
|
+
|
|
218
|
+
if (value !== null && value !== undefined) {
|
|
219
|
+
let msg = message || MongooseError.messages.Number.min;
|
|
220
|
+
msg = msg.replace(/{MIN}/, value);
|
|
221
|
+
this.validators.push({
|
|
222
|
+
validator: this.minValidator = function(v) {
|
|
223
|
+
return v == null || v >= value;
|
|
224
|
+
},
|
|
225
|
+
message: msg,
|
|
226
|
+
type: 'min',
|
|
227
|
+
min: value
|
|
228
|
+
});
|
|
229
|
+
}
|
|
230
|
+
|
|
231
|
+
return this;
|
|
232
|
+
};
|
|
233
|
+
|
|
234
|
+
/**
|
|
235
|
+
* Sets a maximum number validator.
|
|
236
|
+
*
|
|
237
|
+
* #### Example:
|
|
238
|
+
*
|
|
239
|
+
* const s = new Schema({ n: { type: Number, max: 10 })
|
|
240
|
+
* const M = db.model('M', s)
|
|
241
|
+
* const m = new M({ n: 11 })
|
|
242
|
+
* m.save(function (err) {
|
|
243
|
+
* console.error(err) // validator error
|
|
244
|
+
* m.n = 10;
|
|
245
|
+
* m.save() // success
|
|
246
|
+
* })
|
|
247
|
+
*
|
|
248
|
+
* // custom error messages
|
|
249
|
+
* // We can also use the special {MAX} token which will be replaced with the invalid value
|
|
250
|
+
* const max = [10, 'The value of path `{PATH}` ({VALUE}) exceeds the limit ({MAX}).'];
|
|
251
|
+
* const schema = new Schema({ n: { type: Number, max: max })
|
|
252
|
+
* const M = mongoose.model('Measurement', schema);
|
|
253
|
+
* const s= new M({ n: 4 });
|
|
254
|
+
* s.validate(function (err) {
|
|
255
|
+
* console.log(String(err)) // ValidationError: The value of path `n` (4) exceeds the limit (10).
|
|
256
|
+
* })
|
|
257
|
+
*
|
|
258
|
+
* @param {Number} maximum number
|
|
259
|
+
* @param {String} [message] optional custom error message
|
|
260
|
+
* @return {SchemaType} this
|
|
261
|
+
* @see Customized Error Messages #error_messages_MongooseError-messages
|
|
262
|
+
* @api public
|
|
263
|
+
*/
|
|
264
|
+
|
|
265
|
+
SchemaNumber.prototype.max = function(value, message) {
|
|
266
|
+
if (this.maxValidator) {
|
|
267
|
+
this.validators = this.validators.filter(function(v) {
|
|
268
|
+
return v.validator !== this.maxValidator;
|
|
269
|
+
}, this);
|
|
270
|
+
}
|
|
271
|
+
|
|
272
|
+
if (value !== null && value !== undefined) {
|
|
273
|
+
let msg = message || MongooseError.messages.Number.max;
|
|
274
|
+
msg = msg.replace(/{MAX}/, value);
|
|
275
|
+
this.validators.push({
|
|
276
|
+
validator: this.maxValidator = function(v) {
|
|
277
|
+
return v == null || v <= value;
|
|
278
|
+
},
|
|
279
|
+
message: msg,
|
|
280
|
+
type: 'max',
|
|
281
|
+
max: value
|
|
282
|
+
});
|
|
283
|
+
}
|
|
284
|
+
|
|
285
|
+
return this;
|
|
286
|
+
};
|
|
287
|
+
|
|
288
|
+
/**
|
|
289
|
+
* Sets a enum validator
|
|
290
|
+
*
|
|
291
|
+
* #### Example:
|
|
292
|
+
*
|
|
293
|
+
* const s = new Schema({ n: { type: Number, enum: [1, 2, 3] });
|
|
294
|
+
* const M = db.model('M', s);
|
|
295
|
+
*
|
|
296
|
+
* const m = new M({ n: 4 });
|
|
297
|
+
* await m.save(); // throws validation error
|
|
298
|
+
*
|
|
299
|
+
* m.n = 3;
|
|
300
|
+
* await m.save(); // succeeds
|
|
301
|
+
*
|
|
302
|
+
* @param {Array} values allowed values
|
|
303
|
+
* @param {String} [message] optional custom error message
|
|
304
|
+
* @return {SchemaType} this
|
|
305
|
+
* @see Customized Error Messages #error_messages_MongooseError-messages
|
|
306
|
+
* @api public
|
|
307
|
+
*/
|
|
308
|
+
|
|
309
|
+
SchemaNumber.prototype.enum = function(values, message) {
|
|
310
|
+
if (this.enumValidator) {
|
|
311
|
+
this.validators = this.validators.filter(function(v) {
|
|
312
|
+
return v.validator !== this.enumValidator;
|
|
313
|
+
}, this);
|
|
314
|
+
}
|
|
315
|
+
|
|
316
|
+
|
|
317
|
+
if (!Array.isArray(values)) {
|
|
318
|
+
const isObjectSyntax = utils.isPOJO(values) && values.values != null;
|
|
319
|
+
if (isObjectSyntax) {
|
|
320
|
+
message = values.message;
|
|
321
|
+
values = values.values;
|
|
322
|
+
} else if (typeof values === 'number') {
|
|
323
|
+
values = Array.prototype.slice.call(arguments);
|
|
324
|
+
message = null;
|
|
325
|
+
}
|
|
326
|
+
|
|
327
|
+
if (utils.isPOJO(values)) {
|
|
328
|
+
values = Object.values(values);
|
|
329
|
+
}
|
|
330
|
+
message = message || MongooseError.messages.Number.enum;
|
|
331
|
+
}
|
|
332
|
+
|
|
333
|
+
message = message == null ? MongooseError.messages.Number.enum : message;
|
|
334
|
+
|
|
335
|
+
this.enumValidator = v => v == null || values.indexOf(v) !== -1;
|
|
336
|
+
this.validators.push({
|
|
337
|
+
validator: this.enumValidator,
|
|
338
|
+
message: message,
|
|
339
|
+
type: 'enum',
|
|
340
|
+
enumValues: values
|
|
341
|
+
});
|
|
342
|
+
|
|
343
|
+
return this;
|
|
344
|
+
};
|
|
345
|
+
|
|
346
|
+
/**
|
|
347
|
+
* Casts to number
|
|
348
|
+
*
|
|
349
|
+
* @param {Object} value value to cast
|
|
350
|
+
* @param {Document} doc document that triggers the casting
|
|
351
|
+
* @param {Boolean} init
|
|
352
|
+
* @api private
|
|
353
|
+
*/
|
|
354
|
+
|
|
355
|
+
SchemaNumber.prototype.cast = function(value, doc, init) {
|
|
356
|
+
if (typeof value !== 'number' && SchemaType._isRef(this, value, doc, init)) {
|
|
357
|
+
if (value == null || utils.isNonBuiltinObject(value)) {
|
|
358
|
+
return this._castRef(value, doc, init);
|
|
359
|
+
}
|
|
360
|
+
}
|
|
361
|
+
|
|
362
|
+
const val = value && typeof value._id !== 'undefined' ?
|
|
363
|
+
value._id : // documents
|
|
364
|
+
value;
|
|
365
|
+
|
|
366
|
+
let castNumber;
|
|
367
|
+
if (typeof this._castFunction === 'function') {
|
|
368
|
+
castNumber = this._castFunction;
|
|
369
|
+
} else if (typeof this.constructor.cast === 'function') {
|
|
370
|
+
castNumber = this.constructor.cast();
|
|
371
|
+
} else {
|
|
372
|
+
castNumber = SchemaNumber.cast();
|
|
373
|
+
}
|
|
374
|
+
|
|
375
|
+
try {
|
|
376
|
+
return castNumber(val);
|
|
377
|
+
} catch (err) {
|
|
378
|
+
throw new CastError('Number', val, this.path, err, this);
|
|
379
|
+
}
|
|
380
|
+
};
|
|
381
|
+
|
|
382
|
+
/*!
|
|
383
|
+
* ignore
|
|
384
|
+
*/
|
|
385
|
+
|
|
386
|
+
function handleSingle(val) {
|
|
387
|
+
return this.cast(val);
|
|
388
|
+
}
|
|
389
|
+
|
|
390
|
+
function handleArray(val) {
|
|
391
|
+
const _this = this;
|
|
392
|
+
if (!Array.isArray(val)) {
|
|
393
|
+
return [this.cast(val)];
|
|
394
|
+
}
|
|
395
|
+
return val.map(function(m) {
|
|
396
|
+
return _this.cast(m);
|
|
397
|
+
});
|
|
398
|
+
}
|
|
399
|
+
|
|
400
|
+
SchemaNumber.prototype.$conditionalHandlers =
|
|
401
|
+
utils.options(SchemaType.prototype.$conditionalHandlers, {
|
|
402
|
+
$bitsAllClear: handleBitwiseOperator,
|
|
403
|
+
$bitsAnyClear: handleBitwiseOperator,
|
|
404
|
+
$bitsAllSet: handleBitwiseOperator,
|
|
405
|
+
$bitsAnySet: handleBitwiseOperator,
|
|
406
|
+
$gt: handleSingle,
|
|
407
|
+
$gte: handleSingle,
|
|
408
|
+
$lt: handleSingle,
|
|
409
|
+
$lte: handleSingle,
|
|
410
|
+
$mod: handleArray
|
|
411
|
+
});
|
|
412
|
+
|
|
413
|
+
/**
|
|
414
|
+
* Casts contents for queries.
|
|
415
|
+
*
|
|
416
|
+
* @param {String} $conditional
|
|
417
|
+
* @param {any} [value]
|
|
418
|
+
* @api private
|
|
419
|
+
*/
|
|
420
|
+
|
|
421
|
+
SchemaNumber.prototype.castForQuery = function($conditional, val) {
|
|
422
|
+
let handler;
|
|
423
|
+
if (arguments.length === 2) {
|
|
424
|
+
handler = this.$conditionalHandlers[$conditional];
|
|
425
|
+
if (!handler) {
|
|
426
|
+
throw new CastError('number', val, this.path, null, this);
|
|
427
|
+
}
|
|
428
|
+
return handler.call(this, val);
|
|
429
|
+
}
|
|
430
|
+
val = this._castForQuery($conditional);
|
|
431
|
+
return val;
|
|
432
|
+
};
|
|
433
|
+
|
|
434
|
+
/*!
|
|
435
|
+
* Module exports.
|
|
436
|
+
*/
|
|
437
|
+
|
|
438
|
+
module.exports = SchemaNumber;
|