@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,36 @@
|
|
|
1
|
+
|
|
2
|
+
/*!
|
|
3
|
+
* Valid mongoose options
|
|
4
|
+
*/
|
|
5
|
+
|
|
6
|
+
'use strict';
|
|
7
|
+
|
|
8
|
+
const VALID_OPTIONS = Object.freeze([
|
|
9
|
+
'allowDiskUse',
|
|
10
|
+
'applyPluginsToChildSchemas',
|
|
11
|
+
'applyPluginsToDiscriminators',
|
|
12
|
+
'autoCreate',
|
|
13
|
+
'autoIndex',
|
|
14
|
+
'bufferCommands',
|
|
15
|
+
'bufferTimeoutMS',
|
|
16
|
+
'cloneSchemas',
|
|
17
|
+
'debug',
|
|
18
|
+
'id',
|
|
19
|
+
'timestamps.createdAt.immutable',
|
|
20
|
+
'maxTimeMS',
|
|
21
|
+
'objectIdGetter',
|
|
22
|
+
'overwriteModels',
|
|
23
|
+
'returnOriginal',
|
|
24
|
+
'runValidators',
|
|
25
|
+
'sanitizeFilter',
|
|
26
|
+
'sanitizeProjection',
|
|
27
|
+
'selectPopulatedPaths',
|
|
28
|
+
'setDefaultsOnInsert',
|
|
29
|
+
'strict',
|
|
30
|
+
'strictPopulate',
|
|
31
|
+
'strictQuery',
|
|
32
|
+
'toJSON',
|
|
33
|
+
'toObject'
|
|
34
|
+
]);
|
|
35
|
+
|
|
36
|
+
module.exports = VALID_OPTIONS;
|
|
@@ -0,0 +1,175 @@
|
|
|
1
|
+
'use strict';
|
|
2
|
+
|
|
3
|
+
const utils = require('./utils');
|
|
4
|
+
|
|
5
|
+
/**
|
|
6
|
+
* VirtualType constructor
|
|
7
|
+
*
|
|
8
|
+
* This is what mongoose uses to define virtual attributes via `Schema.prototype.virtual`.
|
|
9
|
+
*
|
|
10
|
+
* #### Example:
|
|
11
|
+
*
|
|
12
|
+
* const fullname = schema.virtual('fullname');
|
|
13
|
+
* fullname instanceof mongoose.VirtualType // true
|
|
14
|
+
*
|
|
15
|
+
* @param {Object} options
|
|
16
|
+
* @param {String|Function} [options.ref] if `ref` is not nullish, this becomes a [populated virtual](/docs/populate.html#populate-virtuals)
|
|
17
|
+
* @param {String|Function} [options.localField] the local field to populate on if this is a populated virtual.
|
|
18
|
+
* @param {String|Function} [options.foreignField] the foreign field to populate on if this is a populated virtual.
|
|
19
|
+
* @param {Boolean} [options.justOne=false] by default, a populated virtual is an array. If you set `justOne`, the populated virtual will be a single doc or `null`.
|
|
20
|
+
* @param {Boolean} [options.getters=false] if you set this to `true`, Mongoose will call any custom getters you defined on this virtual
|
|
21
|
+
* @param {Boolean} [options.count=false] if you set this to `true`, `populate()` will set this virtual to the number of populated documents, as opposed to the documents themselves, using [`Query#countDocuments()`](/docs/api/query.html#query_Query-countDocuments)
|
|
22
|
+
* @param {Object|Function} [options.match=null] add an extra match condition to `populate()`
|
|
23
|
+
* @param {Number} [options.limit=null] add a default `limit` to the `populate()` query
|
|
24
|
+
* @param {Number} [options.skip=null] add a default `skip` to the `populate()` query
|
|
25
|
+
* @param {Number} [options.perDocumentLimit=null] For legacy reasons, `limit` with `populate()` may give incorrect results because it only executes a single query for every document being populated. If you set `perDocumentLimit`, Mongoose will ensure correct `limit` per document by executing a separate query for each document to `populate()`. For example, `.find().populate({ path: 'test', perDocumentLimit: 2 })` will execute 2 additional queries if `.find()` returns 2 documents.
|
|
26
|
+
* @param {Object} [options.options=null] Additional options like `limit` and `lean`.
|
|
27
|
+
* @param {String} name
|
|
28
|
+
* @api public
|
|
29
|
+
*/
|
|
30
|
+
|
|
31
|
+
function VirtualType(options, name) {
|
|
32
|
+
this.path = name;
|
|
33
|
+
this.getters = [];
|
|
34
|
+
this.setters = [];
|
|
35
|
+
this.options = Object.assign({}, options);
|
|
36
|
+
}
|
|
37
|
+
|
|
38
|
+
/**
|
|
39
|
+
* If no getters/setters, add a default
|
|
40
|
+
*
|
|
41
|
+
* @api private
|
|
42
|
+
*/
|
|
43
|
+
|
|
44
|
+
VirtualType.prototype._applyDefaultGetters = function() {
|
|
45
|
+
if (this.getters.length > 0 || this.setters.length > 0) {
|
|
46
|
+
return;
|
|
47
|
+
}
|
|
48
|
+
|
|
49
|
+
const path = this.path;
|
|
50
|
+
const internalProperty = '$' + path;
|
|
51
|
+
this.getters.push(function() {
|
|
52
|
+
return this.$locals[internalProperty];
|
|
53
|
+
});
|
|
54
|
+
this.setters.push(function(v) {
|
|
55
|
+
this.$locals[internalProperty] = v;
|
|
56
|
+
});
|
|
57
|
+
};
|
|
58
|
+
|
|
59
|
+
/*!
|
|
60
|
+
* ignore
|
|
61
|
+
*/
|
|
62
|
+
|
|
63
|
+
VirtualType.prototype.clone = function() {
|
|
64
|
+
const clone = new VirtualType(this.options, this.path);
|
|
65
|
+
clone.getters = [].concat(this.getters);
|
|
66
|
+
clone.setters = [].concat(this.setters);
|
|
67
|
+
return clone;
|
|
68
|
+
};
|
|
69
|
+
|
|
70
|
+
/**
|
|
71
|
+
* Adds a custom getter to this virtual.
|
|
72
|
+
*
|
|
73
|
+
* Mongoose calls the getter function with the below 3 parameters.
|
|
74
|
+
*
|
|
75
|
+
* - `value`: the value returned by the previous getter. If there is only one getter, `value` will be `undefined`.
|
|
76
|
+
* - `virtual`: the virtual object you called `.get()` on.
|
|
77
|
+
* - `doc`: the document this virtual is attached to. Equivalent to `this`.
|
|
78
|
+
*
|
|
79
|
+
* #### Example:
|
|
80
|
+
*
|
|
81
|
+
* const virtual = schema.virtual('fullname');
|
|
82
|
+
* virtual.get(function(value, virtual, doc) {
|
|
83
|
+
* return this.name.first + ' ' + this.name.last;
|
|
84
|
+
* });
|
|
85
|
+
*
|
|
86
|
+
* @param {Function} fn
|
|
87
|
+
* @return {VirtualType} this
|
|
88
|
+
* @api public
|
|
89
|
+
*/
|
|
90
|
+
|
|
91
|
+
VirtualType.prototype.get = function(fn) {
|
|
92
|
+
this.getters.push(fn);
|
|
93
|
+
return this;
|
|
94
|
+
};
|
|
95
|
+
|
|
96
|
+
/**
|
|
97
|
+
* Adds a custom setter to this virtual.
|
|
98
|
+
*
|
|
99
|
+
* Mongoose calls the setter function with the below 3 parameters.
|
|
100
|
+
*
|
|
101
|
+
* - `value`: the value being set.
|
|
102
|
+
* - `virtual`: the virtual object you're calling `.set()` on.
|
|
103
|
+
* - `doc`: the document this virtual is attached to. Equivalent to `this`.
|
|
104
|
+
*
|
|
105
|
+
* #### Example:
|
|
106
|
+
*
|
|
107
|
+
* const virtual = schema.virtual('fullname');
|
|
108
|
+
* virtual.set(function(value, virtual, doc) {
|
|
109
|
+
* const parts = value.split(' ');
|
|
110
|
+
* this.name.first = parts[0];
|
|
111
|
+
* this.name.last = parts[1];
|
|
112
|
+
* });
|
|
113
|
+
*
|
|
114
|
+
* const Model = mongoose.model('Test', schema);
|
|
115
|
+
* const doc = new Model();
|
|
116
|
+
* // Calls the setter with `value = 'Jean-Luc Picard'`
|
|
117
|
+
* doc.fullname = 'Jean-Luc Picard';
|
|
118
|
+
* doc.name.first; // 'Jean-Luc'
|
|
119
|
+
* doc.name.last; // 'Picard'
|
|
120
|
+
*
|
|
121
|
+
* @param {Function} fn
|
|
122
|
+
* @return {VirtualType} this
|
|
123
|
+
* @api public
|
|
124
|
+
*/
|
|
125
|
+
|
|
126
|
+
VirtualType.prototype.set = function(fn) {
|
|
127
|
+
this.setters.push(fn);
|
|
128
|
+
return this;
|
|
129
|
+
};
|
|
130
|
+
|
|
131
|
+
/**
|
|
132
|
+
* Applies getters to `value`.
|
|
133
|
+
*
|
|
134
|
+
* @param {Object} value
|
|
135
|
+
* @param {Document} doc The document this virtual is attached to
|
|
136
|
+
* @return {Any} the value after applying all getters
|
|
137
|
+
* @api public
|
|
138
|
+
*/
|
|
139
|
+
|
|
140
|
+
VirtualType.prototype.applyGetters = function(value, doc) {
|
|
141
|
+
if (utils.hasUserDefinedProperty(this.options, ['ref', 'refPath']) &&
|
|
142
|
+
doc.$$populatedVirtuals &&
|
|
143
|
+
doc.$$populatedVirtuals.hasOwnProperty(this.path)) {
|
|
144
|
+
value = doc.$$populatedVirtuals[this.path];
|
|
145
|
+
}
|
|
146
|
+
|
|
147
|
+
let v = value;
|
|
148
|
+
for (const getter of this.getters) {
|
|
149
|
+
v = getter.call(doc, v, this, doc);
|
|
150
|
+
}
|
|
151
|
+
return v;
|
|
152
|
+
};
|
|
153
|
+
|
|
154
|
+
/**
|
|
155
|
+
* Applies setters to `value`.
|
|
156
|
+
*
|
|
157
|
+
* @param {Object} value
|
|
158
|
+
* @param {Document} doc
|
|
159
|
+
* @return {Any} the value after applying all setters
|
|
160
|
+
* @api public
|
|
161
|
+
*/
|
|
162
|
+
|
|
163
|
+
VirtualType.prototype.applySetters = function(value, doc) {
|
|
164
|
+
let v = value;
|
|
165
|
+
for (const setter of this.setters) {
|
|
166
|
+
v = setter.call(doc, v, this, doc);
|
|
167
|
+
}
|
|
168
|
+
return v;
|
|
169
|
+
};
|
|
170
|
+
|
|
171
|
+
/*!
|
|
172
|
+
* exports
|
|
173
|
+
*/
|
|
174
|
+
|
|
175
|
+
module.exports = VirtualType;
|
package/package.json
ADDED
|
@@ -0,0 +1,164 @@
|
|
|
1
|
+
{
|
|
2
|
+
"name": "@datanimbus/mongoose",
|
|
3
|
+
"description": "Mongoose MongoDB ODM",
|
|
4
|
+
"version": "6.13.10",
|
|
5
|
+
"author": "Guillermo Rauch <guillermo@learnboost.com>",
|
|
6
|
+
"keywords": [
|
|
7
|
+
"mongodb",
|
|
8
|
+
"document",
|
|
9
|
+
"model",
|
|
10
|
+
"schema",
|
|
11
|
+
"database",
|
|
12
|
+
"odm",
|
|
13
|
+
"data",
|
|
14
|
+
"datastore",
|
|
15
|
+
"query",
|
|
16
|
+
"nosql",
|
|
17
|
+
"orm",
|
|
18
|
+
"db"
|
|
19
|
+
],
|
|
20
|
+
"license": "MIT",
|
|
21
|
+
"dependencies": {
|
|
22
|
+
"bson": "^4.7.2",
|
|
23
|
+
"kareem": "2.5.1",
|
|
24
|
+
"mongodb": "4.17.2",
|
|
25
|
+
"mpath": "0.9.0",
|
|
26
|
+
"mquery": "4.0.3",
|
|
27
|
+
"ms": "2.1.3",
|
|
28
|
+
"sift": "16.0.1"
|
|
29
|
+
},
|
|
30
|
+
"devDependencies": {
|
|
31
|
+
"@babel/core": "7.20.12",
|
|
32
|
+
"@babel/preset-env": "7.20.2",
|
|
33
|
+
"@sinonjs/fake-timers": "11.2.2",
|
|
34
|
+
"@typescript-eslint/eslint-plugin": "5.50.0",
|
|
35
|
+
"@typescript-eslint/parser": "5.50.0",
|
|
36
|
+
"acquit": "1.3.0",
|
|
37
|
+
"acquit-ignore": "0.2.1",
|
|
38
|
+
"acquit-require": "0.1.1",
|
|
39
|
+
"assert-browserify": "2.0.0",
|
|
40
|
+
"axios": "1.1.3",
|
|
41
|
+
"babel-loader": "8.2.5",
|
|
42
|
+
"benchmark": "2.1.4",
|
|
43
|
+
"bluebird": "3.7.2",
|
|
44
|
+
"broken-link-checker": "^0.7.8",
|
|
45
|
+
"buffer": "^5.6.0",
|
|
46
|
+
"cheerio": "1.0.0-rc.12",
|
|
47
|
+
"crypto-browserify": "3.12.0",
|
|
48
|
+
"dotenv": "16.0.3",
|
|
49
|
+
"dox": "1.0.0",
|
|
50
|
+
"eslint": "8.33.0",
|
|
51
|
+
"eslint-plugin-markdown": "^3.0.0",
|
|
52
|
+
"eslint-plugin-mocha-no-only": "1.1.1",
|
|
53
|
+
"express": "^4.18.1",
|
|
54
|
+
"fs-extra": "~11.1.1",
|
|
55
|
+
"highlight.js": "11.7.0",
|
|
56
|
+
"lodash.isequal": "4.5.0",
|
|
57
|
+
"lodash.isequalwith": "4.4.0",
|
|
58
|
+
"marked": "4.2.12",
|
|
59
|
+
"mkdirp": "^2.1.3",
|
|
60
|
+
"mocha": "10.2.0",
|
|
61
|
+
"moment": "2.x",
|
|
62
|
+
"mongodb-memory-server": "8.11.4",
|
|
63
|
+
"ncp": "^2.0.0",
|
|
64
|
+
"nyc": "15.1.0",
|
|
65
|
+
"pug": "3.0.2",
|
|
66
|
+
"q": "1.5.1",
|
|
67
|
+
"sinon": "15.0.1",
|
|
68
|
+
"stream-browserify": "3.0.0",
|
|
69
|
+
"tmp": "0.2.1",
|
|
70
|
+
"tsd": "0.25.0",
|
|
71
|
+
"typescript": "4.9.5",
|
|
72
|
+
"uuid": "9.0.0",
|
|
73
|
+
"webpack": "5.75.0"
|
|
74
|
+
},
|
|
75
|
+
"directories": {
|
|
76
|
+
"lib": "./lib/mongoose"
|
|
77
|
+
},
|
|
78
|
+
"scripts": {
|
|
79
|
+
"docs:clean": "npm run docs:clean:stable",
|
|
80
|
+
"docs:clean:stable": "rimraf index.html && rimraf -rf ./docs/*.html && rimraf -rf ./docs/api && rimraf -rf ./docs/tutorials/*.html && rimraf -rf ./docs/typescript/*.html && rimraf -rf ./docs/*.html && rimraf -rf ./docs/source/_docs && rimraf -rf ./tmp",
|
|
81
|
+
"docs:clean:5x": "rimraf index.html && rimraf -rf ./docs/5.x && rimraf -rf ./docs/source/_docs && rimraf -rf ./tmp",
|
|
82
|
+
"docs:clean:6x": "rimraf index.html && rimraf -rf ./docs/6.x && rimraf -rf ./docs/source/_docs && rimraf -rf ./tmp",
|
|
83
|
+
"docs:copy:tmp": "mkdirp ./tmp/docs/css && mkdirp ./tmp/docs/js && mkdirp ./tmp/docs/images && mkdirp ./tmp/docs/tutorials && mkdirp ./tmp/docs/typescript && mkdirp ./tmp/docs/api && ncp ./docs/css ./tmp/docs/css --filter=.css$ && ncp ./docs/js ./tmp/docs/js --filter=.js$ && ncp ./docs/images ./tmp/docs/images && ncp ./docs/tutorials ./tmp/docs/tutorials && ncp ./docs/typescript ./tmp/docs/typescript && ncp ./docs/api ./tmp/docs/api && cp index.html ./tmp && cp docs/*.html ./tmp/docs/",
|
|
84
|
+
"docs:copy:tmp:5x": "rimraf ./docs/5.x && ncp ./tmp ./docs/5.x",
|
|
85
|
+
"docs:move:6x:tmp": "mv ./docs/6.x ./tmp",
|
|
86
|
+
"docs:copy:tmp:6x": "rimraf ./docs/6.x && ncp ./tmp ./docs/6.x",
|
|
87
|
+
"docs:checkout:gh-pages": "git checkout gh-pages",
|
|
88
|
+
"docs:checkout:5x": "git checkout 5.x",
|
|
89
|
+
"docs:checkout:6x": "git checkout 6.x",
|
|
90
|
+
"docs:generate": "node ./scripts/website.js",
|
|
91
|
+
"docs:generate:search": "node ./scripts/generateSearch.js",
|
|
92
|
+
"docs:generate:sponsorData": "node ./scripts/loadSponsorData.js",
|
|
93
|
+
"docs:merge:stable": "git merge master",
|
|
94
|
+
"docs:merge:5x": "git merge 5.x",
|
|
95
|
+
"docs:merge:6x": "git merge 6.x",
|
|
96
|
+
"docs:test": "npm run docs:generate && npm run docs:generate:search",
|
|
97
|
+
"docs:view": "node ./scripts/static.js",
|
|
98
|
+
"docs:prepare:publish:stable": "npm run docs:checkout:gh-pages && npm run docs:merge:stable && npm run docs:clean:stable && npm run docs:generate && npm run docs:generate:search",
|
|
99
|
+
"docs:prepare:publish:5x": "npm run docs:checkout:5x && npm run docs:merge:5x && npm run docs:clean:stable && npm run docs:generate && npm run docs:copy:tmp && npm run docs:checkout:gh-pages && npm run docs:copy:tmp:5x",
|
|
100
|
+
"docs:prepare:publish:6x": "npm run docs:checkout:6x && npm run docs:merge:6x && npm run docs:clean:stable && env DOCS_DEPLOY=true npm run docs:generate && npm run docs:move:6x:tmp && npm run docs:checkout:gh-pages && npm run docs:copy:tmp:6x",
|
|
101
|
+
"docs:check-links": "blc http://127.0.0.1:8089 -ro",
|
|
102
|
+
"lint": "eslint .",
|
|
103
|
+
"lint-js": "eslint . --ext .js",
|
|
104
|
+
"lint-ts": "eslint . --ext .ts",
|
|
105
|
+
"lint-md": "eslint . --ext .md",
|
|
106
|
+
"build-browser": "(rm ./dist/* || true) && node ./scripts/build-browser.js",
|
|
107
|
+
"prepublishOnly": "npm run build-browser",
|
|
108
|
+
"release": "git pull && git push origin master --tags && npm publish",
|
|
109
|
+
"release-5x": "git pull origin 5.x && git push origin 5.x && git push origin 5.x --tags && npm publish --tag 5x",
|
|
110
|
+
"release-6x": "git pull origin 6.x && git push origin 6.x && git push origin 6.x --tags && npm publish --tag 6x",
|
|
111
|
+
"mongo": "node ./tools/repl.js",
|
|
112
|
+
"test": "mocha --exit ./test/*.test.js",
|
|
113
|
+
"test-deno": "deno run --allow-env --allow-read --allow-net --allow-run --allow-sys --allow-write ./test/deno.js",
|
|
114
|
+
"test-rs": "START_REPLICA_SET=1 mocha --timeout 30000 --exit ./test/*.test.js",
|
|
115
|
+
"test-tsd": "node ./test/types/check-types-filename && tsd",
|
|
116
|
+
"tdd": "mocha ./test/*.test.js --inspect --watch --recursive --watch-files ./**/*.{js,ts}",
|
|
117
|
+
"test-coverage": "nyc --reporter=html --reporter=text npm test",
|
|
118
|
+
"ts-benchmark": "cd ./benchmarks/typescript/simple && npm install && npm run benchmark | node ../../../scripts/tsc-diagnostics-check"
|
|
119
|
+
},
|
|
120
|
+
"main": "./index.js",
|
|
121
|
+
"types": "./types/index.d.ts",
|
|
122
|
+
"engines": {
|
|
123
|
+
"node": ">=12.0.0"
|
|
124
|
+
},
|
|
125
|
+
"bugs": {
|
|
126
|
+
"url": "https://github.com/Automattic/mongoose/issues/new"
|
|
127
|
+
},
|
|
128
|
+
"repository": {
|
|
129
|
+
"type": "git",
|
|
130
|
+
"url": "git://github.com/Automattic/mongoose.git"
|
|
131
|
+
},
|
|
132
|
+
"homepage": "https://mongoosejs.com",
|
|
133
|
+
"browser": "./dist/browser.umd.js",
|
|
134
|
+
"mocha": {
|
|
135
|
+
"extension": [
|
|
136
|
+
"test.js"
|
|
137
|
+
],
|
|
138
|
+
"watch-files": [
|
|
139
|
+
"test/**/*.js"
|
|
140
|
+
]
|
|
141
|
+
},
|
|
142
|
+
"config": {
|
|
143
|
+
"mongodbMemoryServer": {
|
|
144
|
+
"disablePostinstall": true
|
|
145
|
+
}
|
|
146
|
+
},
|
|
147
|
+
"funding": {
|
|
148
|
+
"type": "opencollective",
|
|
149
|
+
"url": "https://opencollective.com/mongoose"
|
|
150
|
+
},
|
|
151
|
+
"tsd": {
|
|
152
|
+
"directory": "test/types",
|
|
153
|
+
"compilerOptions": {
|
|
154
|
+
"esModuleInterop": false,
|
|
155
|
+
"strict": true,
|
|
156
|
+
"allowSyntheticDefaultImports": true,
|
|
157
|
+
"strictPropertyInitialization": false,
|
|
158
|
+
"noImplicitAny": false,
|
|
159
|
+
"strictNullChecks": true,
|
|
160
|
+
"module": "commonjs",
|
|
161
|
+
"target": "ES2017"
|
|
162
|
+
}
|
|
163
|
+
}
|
|
164
|
+
}
|
|
@@ -0,0 +1,18 @@
|
|
|
1
|
+
'use strict';
|
|
2
|
+
|
|
3
|
+
const config = require('../webpack.config.js');
|
|
4
|
+
const webpack = require('webpack');
|
|
5
|
+
|
|
6
|
+
const compiler = webpack(config);
|
|
7
|
+
|
|
8
|
+
console.log('Starting browser build...');
|
|
9
|
+
compiler.run((err, stats) => {
|
|
10
|
+
if (err) {
|
|
11
|
+
console.err(stats.toString());
|
|
12
|
+
console.err('Browser build unsuccessful.');
|
|
13
|
+
process.exit(1);
|
|
14
|
+
}
|
|
15
|
+
console.log(stats.toString());
|
|
16
|
+
console.log('Browser build successful.');
|
|
17
|
+
process.exit(0);
|
|
18
|
+
});
|
|
@@ -0,0 +1,154 @@
|
|
|
1
|
+
'use strict';
|
|
2
|
+
|
|
3
|
+
const config = require('../.config');
|
|
4
|
+
const cheerio = require('cheerio');
|
|
5
|
+
const docsFilemap = require('../docs/source');
|
|
6
|
+
const fs = require('fs');
|
|
7
|
+
const pug = require('pug');
|
|
8
|
+
const mongoose = require('../');
|
|
9
|
+
let { version } = require('../package.json');
|
|
10
|
+
|
|
11
|
+
const { marked: markdown } = require('marked');
|
|
12
|
+
const highlight = require('highlight.js');
|
|
13
|
+
markdown.setOptions({
|
|
14
|
+
highlight: function(code) {
|
|
15
|
+
return highlight.highlight(code, { language: 'JavaScript' }).value;
|
|
16
|
+
}
|
|
17
|
+
});
|
|
18
|
+
|
|
19
|
+
// 5.13.5 -> 5.x, 6.8.2 -> 6.x, etc.
|
|
20
|
+
version = version.slice(0, version.indexOf('.')) + '.x';
|
|
21
|
+
|
|
22
|
+
const contentSchema = new mongoose.Schema({
|
|
23
|
+
title: { type: String, required: true },
|
|
24
|
+
body: { type: String, required: true },
|
|
25
|
+
url: { type: String, required: true },
|
|
26
|
+
version: { type: String, required: true, default: version },
|
|
27
|
+
versionNumber: { type: Number, required: true, default: version.replace(/\.x$/, '') }
|
|
28
|
+
});
|
|
29
|
+
contentSchema.index({ title: 'text', body: 'text' });
|
|
30
|
+
const Content = mongoose.model('Content', contentSchema, 'Content');
|
|
31
|
+
|
|
32
|
+
const contents = [];
|
|
33
|
+
|
|
34
|
+
for (const [filename, file] of Object.entries(docsFilemap.fileMap)) {
|
|
35
|
+
if (file.api) {
|
|
36
|
+
for (const prop of file.props) {
|
|
37
|
+
const content = new Content({
|
|
38
|
+
title: `API: ${prop.name}`,
|
|
39
|
+
body: prop.description,
|
|
40
|
+
url: `${filename.replace(/^docs/, '')}#${prop.anchorId}`
|
|
41
|
+
});
|
|
42
|
+
const err = content.validateSync();
|
|
43
|
+
if (err != null) {
|
|
44
|
+
console.error(content);
|
|
45
|
+
throw err;
|
|
46
|
+
}
|
|
47
|
+
contents.push(content);
|
|
48
|
+
}
|
|
49
|
+
} else if (file.markdown) {
|
|
50
|
+
let text = fs.readFileSync(filename, 'utf8');
|
|
51
|
+
text = markdown.parse(text);
|
|
52
|
+
|
|
53
|
+
const content = new Content({
|
|
54
|
+
title: file.title,
|
|
55
|
+
body: text,
|
|
56
|
+
url: filename.replace('.md', '.html').replace(/^docs/, '')
|
|
57
|
+
});
|
|
58
|
+
|
|
59
|
+
content.validateSync();
|
|
60
|
+
|
|
61
|
+
const $ = cheerio.load(text);
|
|
62
|
+
|
|
63
|
+
contents.push(content);
|
|
64
|
+
|
|
65
|
+
// Break up individual h3's into separate content for more fine grained search
|
|
66
|
+
$('h3').each((index, el) => {
|
|
67
|
+
el = $(el);
|
|
68
|
+
const title = el.text();
|
|
69
|
+
const html = el.nextUntil('h3').html();
|
|
70
|
+
const content = new Content({
|
|
71
|
+
title: `${file.title}: ${title}`,
|
|
72
|
+
body: html,
|
|
73
|
+
url: `${filename.replace('.md', '.html').replace(/^docs/, '')}#${el.prop('id')}`
|
|
74
|
+
});
|
|
75
|
+
|
|
76
|
+
content.validateSync();
|
|
77
|
+
contents.push(content);
|
|
78
|
+
});
|
|
79
|
+
} else if (file.guide) {
|
|
80
|
+
let text = fs.readFileSync(filename, 'utf8');
|
|
81
|
+
text = text.substr(text.indexOf('block content') + 'block content\n'.length);
|
|
82
|
+
text = pug.render(`div\n${text}`, { filters: { markdown }, filename });
|
|
83
|
+
|
|
84
|
+
const content = new Content({
|
|
85
|
+
title: file.title,
|
|
86
|
+
body: text,
|
|
87
|
+
url: filename.replace('.pug', '.html').replace(/^docs/, '')
|
|
88
|
+
});
|
|
89
|
+
|
|
90
|
+
content.validateSync();
|
|
91
|
+
|
|
92
|
+
const $ = cheerio.load(text);
|
|
93
|
+
|
|
94
|
+
contents.push(content);
|
|
95
|
+
|
|
96
|
+
// Break up individual h3's into separate content for more fine grained search
|
|
97
|
+
$('h3').each((index, el) => {
|
|
98
|
+
el = $(el);
|
|
99
|
+
const title = el.text();
|
|
100
|
+
const html = el.nextUntil('h3').html();
|
|
101
|
+
const content = new Content({
|
|
102
|
+
title: `${file.title}: ${title}`,
|
|
103
|
+
body: html,
|
|
104
|
+
url: `${filename.replace('.pug', '.html').replace(/^docs/, '')}#${el.prop('id')}`
|
|
105
|
+
});
|
|
106
|
+
|
|
107
|
+
content.validateSync();
|
|
108
|
+
contents.push(content);
|
|
109
|
+
});
|
|
110
|
+
}
|
|
111
|
+
}
|
|
112
|
+
|
|
113
|
+
run().catch(async error => {
|
|
114
|
+
console.error(error.stack);
|
|
115
|
+
|
|
116
|
+
// ensure the script exists in case of error
|
|
117
|
+
await mongoose.disconnect();
|
|
118
|
+
});
|
|
119
|
+
|
|
120
|
+
async function run() {
|
|
121
|
+
if (!config || !config.uri) {
|
|
122
|
+
console.error('No Config or config.URI given, please create a .config.js file with those values');
|
|
123
|
+
process.exit(-1);
|
|
124
|
+
}
|
|
125
|
+
|
|
126
|
+
await mongoose.connect(config.uri, { dbName: 'mongoose', serverSelectionTimeoutMS: 5000 });
|
|
127
|
+
|
|
128
|
+
// wait for the index to be created
|
|
129
|
+
await Content.init();
|
|
130
|
+
|
|
131
|
+
await Content.deleteMany({ version });
|
|
132
|
+
for (const content of contents) {
|
|
133
|
+
if (version === '7.x') {
|
|
134
|
+
let url = content.url.startsWith('/') ? content.url : `/${content.url}`;
|
|
135
|
+
if (!url.startsWith('/docs')) {
|
|
136
|
+
url = '/docs' + url;
|
|
137
|
+
}
|
|
138
|
+
content.url = url;
|
|
139
|
+
} else {
|
|
140
|
+
const url = content.url.startsWith('/') ? content.url : `/${content.url}`;
|
|
141
|
+
content.url = `/docs/${version}/docs${url}`;
|
|
142
|
+
}
|
|
143
|
+
await content.save();
|
|
144
|
+
}
|
|
145
|
+
|
|
146
|
+
const results = await Content.
|
|
147
|
+
find({ $text: { $search: 'validate' }, version }, { score: { $meta: 'textScore' } }).
|
|
148
|
+
sort({ score: { $meta: 'textScore' } }).
|
|
149
|
+
limit(10);
|
|
150
|
+
|
|
151
|
+
console.log(results.map(res => res.url));
|
|
152
|
+
|
|
153
|
+
process.exit(0);
|
|
154
|
+
}
|
|
@@ -0,0 +1,15 @@
|
|
|
1
|
+
'use strict';
|
|
2
|
+
|
|
3
|
+
const fs = require('fs');
|
|
4
|
+
|
|
5
|
+
const stdin = fs.readFileSync(0).toString('utf8');
|
|
6
|
+
const maxInstantiations = isNaN(process.argv[2]) ? 100000 : parseInt(process.argv[2], 10);
|
|
7
|
+
|
|
8
|
+
console.log(stdin);
|
|
9
|
+
|
|
10
|
+
const numInstantiations = parseInt(stdin.match(/Instantiations:\s+(\d+)/)[1], 10);
|
|
11
|
+
if (numInstantiations > maxInstantiations) {
|
|
12
|
+
throw new Error(`Instantiations ${numInstantiations} > max ${maxInstantiations}`);
|
|
13
|
+
}
|
|
14
|
+
|
|
15
|
+
process.exit(0);
|
package/tools/auth.js
ADDED
|
@@ -0,0 +1,31 @@
|
|
|
1
|
+
'use strict';
|
|
2
|
+
|
|
3
|
+
const Server = require('mongodb-topology-manager').Server;
|
|
4
|
+
const mongodb = require('mongodb');
|
|
5
|
+
|
|
6
|
+
run().catch(error => {
|
|
7
|
+
console.error(error);
|
|
8
|
+
process.exit(-1);
|
|
9
|
+
});
|
|
10
|
+
|
|
11
|
+
async function run() {
|
|
12
|
+
// Create new instance
|
|
13
|
+
const server = new Server('mongod', {
|
|
14
|
+
auth: null,
|
|
15
|
+
dbpath: '/data/db/27017'
|
|
16
|
+
});
|
|
17
|
+
|
|
18
|
+
// Purge the directory
|
|
19
|
+
await server.purge();
|
|
20
|
+
|
|
21
|
+
// Start process
|
|
22
|
+
await server.start();
|
|
23
|
+
|
|
24
|
+
const db = await mongodb.MongoClient.connect('mongodb://127.0.0.1:27017/admin');
|
|
25
|
+
|
|
26
|
+
await db.addUser('passwordIsTaco', 'taco', {
|
|
27
|
+
roles: ['dbOwner']
|
|
28
|
+
});
|
|
29
|
+
|
|
30
|
+
console.log('done');
|
|
31
|
+
}
|
package/tools/repl.js
ADDED
|
@@ -0,0 +1,35 @@
|
|
|
1
|
+
'use strict';
|
|
2
|
+
|
|
3
|
+
run().catch(error => {
|
|
4
|
+
console.error(error);
|
|
5
|
+
process.exit(-1);
|
|
6
|
+
});
|
|
7
|
+
|
|
8
|
+
async function run() {
|
|
9
|
+
const ReplSet = require('mongodb-memory-server').MongoMemoryReplSet;
|
|
10
|
+
|
|
11
|
+
// Create new instance
|
|
12
|
+
const replSet = new ReplSet({
|
|
13
|
+
binary: {
|
|
14
|
+
version: process.argv[2]
|
|
15
|
+
},
|
|
16
|
+
instanceOpts: [
|
|
17
|
+
// Set the expiry job in MongoDB to run every second
|
|
18
|
+
{
|
|
19
|
+
port: 27017,
|
|
20
|
+
args: ['--setParameter', 'ttlMonitorSleepSecs=1']
|
|
21
|
+
}
|
|
22
|
+
],
|
|
23
|
+
dbName: 'mongoose_test',
|
|
24
|
+
replSet: {
|
|
25
|
+
name: 'rs0',
|
|
26
|
+
count: 2,
|
|
27
|
+
storageEngine: 'wiredTiger'
|
|
28
|
+
}
|
|
29
|
+
});
|
|
30
|
+
|
|
31
|
+
await replSet.start();
|
|
32
|
+
await replSet.waitUntilRunning();
|
|
33
|
+
console.log('MongoDB-ReplicaSet is now running.');
|
|
34
|
+
console.log(replSet.getUri('mongoose_test'));
|
|
35
|
+
}
|
package/tools/sharded.js
ADDED
|
@@ -0,0 +1,46 @@
|
|
|
1
|
+
'use strict';
|
|
2
|
+
|
|
3
|
+
run().catch(error => {
|
|
4
|
+
console.error(error);
|
|
5
|
+
process.exit(-1);
|
|
6
|
+
});
|
|
7
|
+
|
|
8
|
+
|
|
9
|
+
async function run() {
|
|
10
|
+
const Sharded = require('mongodb-topology-manager').Sharded;
|
|
11
|
+
|
|
12
|
+
// Create new instance
|
|
13
|
+
const topology = new Sharded({
|
|
14
|
+
mongod: 'mongod',
|
|
15
|
+
mongos: 'mongos'
|
|
16
|
+
});
|
|
17
|
+
|
|
18
|
+
await topology.addShard([{
|
|
19
|
+
options: {
|
|
20
|
+
bind_ip: '127.0.0.1', port: 31000, dbpath: '/data/db/31000', shardsvr: null
|
|
21
|
+
}
|
|
22
|
+
}], { replSet: 'rs1' });
|
|
23
|
+
|
|
24
|
+
await topology.addConfigurationServers([{
|
|
25
|
+
options: {
|
|
26
|
+
bind_ip: '127.0.0.1', port: 35000, dbpath: '/data/db/35000'
|
|
27
|
+
}
|
|
28
|
+
}], { replSet: 'rs0' });
|
|
29
|
+
|
|
30
|
+
await topology.addProxies([{
|
|
31
|
+
bind_ip: '127.0.0.1', port: 51000, configdb: '127.0.0.1:35000'
|
|
32
|
+
}], {
|
|
33
|
+
binary: 'mongos'
|
|
34
|
+
});
|
|
35
|
+
|
|
36
|
+
console.log('Start...');
|
|
37
|
+
// Start up topology
|
|
38
|
+
await topology.start();
|
|
39
|
+
|
|
40
|
+
console.log('Started');
|
|
41
|
+
|
|
42
|
+
// Shard db
|
|
43
|
+
await topology.enableSharding('test');
|
|
44
|
+
|
|
45
|
+
console.log('done');
|
|
46
|
+
}
|