@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,32 @@
|
|
|
1
|
+
declare module 'mongoose' {
|
|
2
|
+
import mongodb = require('mongodb');
|
|
3
|
+
|
|
4
|
+
/**
|
|
5
|
+
* Mongoose uses this function to get the current time when setting
|
|
6
|
+
* [timestamps](/docs/guide.html#timestamps). You may stub out this function
|
|
7
|
+
* using a tool like [Sinon](https://www.npmjs.com/package/sinon) for testing.
|
|
8
|
+
*/
|
|
9
|
+
function now(): NativeDate;
|
|
10
|
+
|
|
11
|
+
/**
|
|
12
|
+
* Tells `sanitizeFilter()` to skip the given object when filtering out potential query selector injection attacks.
|
|
13
|
+
* Use this method when you have a known query selector that you want to use.
|
|
14
|
+
*/
|
|
15
|
+
function trusted<T>(obj: T): T;
|
|
16
|
+
|
|
17
|
+
/**
|
|
18
|
+
* Returns true if the given value is a Mongoose ObjectId (using `instanceof`) or if the
|
|
19
|
+
* given value is a 24 character hex string, which is the most commonly used string representation
|
|
20
|
+
* of an ObjectId.
|
|
21
|
+
*/
|
|
22
|
+
function isObjectIdOrHexString(v: mongodb.ObjectId): true;
|
|
23
|
+
function isObjectIdOrHexString(v: mongodb.ObjectId | string): boolean;
|
|
24
|
+
function isObjectIdOrHexString(v: any): false;
|
|
25
|
+
|
|
26
|
+
/**
|
|
27
|
+
* Returns true if Mongoose can cast the given value to an ObjectId, or
|
|
28
|
+
* false otherwise.
|
|
29
|
+
*/
|
|
30
|
+
function isValidObjectId(v: mongodb.ObjectId | Types.ObjectId): true;
|
|
31
|
+
function isValidObjectId(v: any): boolean;
|
|
32
|
+
}
|
package/types/index.d.ts
ADDED
|
@@ -0,0 +1,627 @@
|
|
|
1
|
+
/// <reference path="./aggregate.d.ts" />
|
|
2
|
+
/// <reference path="./callback.d.ts" />
|
|
3
|
+
/// <reference path="./collection.d.ts" />
|
|
4
|
+
/// <reference path="./connection.d.ts" />
|
|
5
|
+
/// <reference path="./cursor.d.ts" />
|
|
6
|
+
/// <reference path="./document.d.ts" />
|
|
7
|
+
/// <reference path="./error.d.ts" />
|
|
8
|
+
/// <reference path="./expressions.d.ts" />
|
|
9
|
+
/// <reference path="./helpers.d.ts" />
|
|
10
|
+
/// <reference path="./middlewares.d.ts" />
|
|
11
|
+
/// <reference path="./indexes.d.ts" />
|
|
12
|
+
/// <reference path="./models.d.ts" />
|
|
13
|
+
/// <reference path="./mongooseoptions.d.ts" />
|
|
14
|
+
/// <reference path="./pipelinestage.d.ts" />
|
|
15
|
+
/// <reference path="./populate.d.ts" />
|
|
16
|
+
/// <reference path="./query.d.ts" />
|
|
17
|
+
/// <reference path="./schemaoptions.d.ts" />
|
|
18
|
+
/// <reference path="./schematypes.d.ts" />
|
|
19
|
+
/// <reference path="./session.d.ts" />
|
|
20
|
+
/// <reference path="./types.d.ts" />
|
|
21
|
+
/// <reference path="./utility.d.ts" />
|
|
22
|
+
/// <reference path="./validation.d.ts" />
|
|
23
|
+
/// <reference path="./inferschematype.d.ts" />
|
|
24
|
+
/// <reference path="./virtuals.d.ts" />
|
|
25
|
+
/// <reference path="./augmentations.d.ts" />
|
|
26
|
+
|
|
27
|
+
declare class NativeDate extends global.Date { }
|
|
28
|
+
|
|
29
|
+
declare module 'mongoose' {
|
|
30
|
+
import events = require('events');
|
|
31
|
+
import mongodb = require('mongodb');
|
|
32
|
+
import mongoose = require('mongoose');
|
|
33
|
+
|
|
34
|
+
export type Mongoose = typeof mongoose;
|
|
35
|
+
|
|
36
|
+
/**
|
|
37
|
+
* Mongoose constructor. The exports object of the `mongoose` module is an instance of this
|
|
38
|
+
* class. Most apps will only use this one instance.
|
|
39
|
+
*/
|
|
40
|
+
export const Mongoose: new (options?: MongooseOptions | null) => Mongoose;
|
|
41
|
+
|
|
42
|
+
export let Promise: any;
|
|
43
|
+
export const PromiseProvider: any;
|
|
44
|
+
|
|
45
|
+
/**
|
|
46
|
+
* Can be extended to explicitly type specific models.
|
|
47
|
+
*/
|
|
48
|
+
export interface Models {
|
|
49
|
+
[modelName: string]: Model<any>
|
|
50
|
+
}
|
|
51
|
+
|
|
52
|
+
/** An array containing all models associated with this Mongoose instance. */
|
|
53
|
+
export const models: Models;
|
|
54
|
+
|
|
55
|
+
/**
|
|
56
|
+
* Removes the model named `name` from the default connection, if it exists.
|
|
57
|
+
* You can use this function to clean up any models you created in your tests to
|
|
58
|
+
* prevent OverwriteModelErrors.
|
|
59
|
+
*/
|
|
60
|
+
export function deleteModel(name: string | RegExp): Mongoose;
|
|
61
|
+
|
|
62
|
+
/**
|
|
63
|
+
* Sanitizes query filters against query selector injection attacks by wrapping
|
|
64
|
+
* any nested objects that have a property whose name starts with `$` in a `$eq`.
|
|
65
|
+
*/
|
|
66
|
+
export function sanitizeFilter<T>(filter: FilterQuery<T>): FilterQuery<T>;
|
|
67
|
+
|
|
68
|
+
/** Gets mongoose options */
|
|
69
|
+
export function get<K extends keyof MongooseOptions>(key: K): MongooseOptions[K];
|
|
70
|
+
|
|
71
|
+
/* ! ignore */
|
|
72
|
+
export type CompileModelOptions = {
|
|
73
|
+
overwriteModels?: boolean,
|
|
74
|
+
connection?: Connection
|
|
75
|
+
};
|
|
76
|
+
|
|
77
|
+
export function model<TSchema extends Schema = any>(
|
|
78
|
+
name: string,
|
|
79
|
+
schema?: TSchema,
|
|
80
|
+
collection?: string,
|
|
81
|
+
options?: CompileModelOptions
|
|
82
|
+
): Model<
|
|
83
|
+
InferSchemaType<TSchema>,
|
|
84
|
+
ObtainSchemaGeneric<TSchema, 'TQueryHelpers'>,
|
|
85
|
+
ObtainSchemaGeneric<TSchema, 'TInstanceMethods'>,
|
|
86
|
+
ObtainSchemaGeneric<TSchema, 'TVirtuals'>,
|
|
87
|
+
TSchema
|
|
88
|
+
> & ObtainSchemaGeneric<TSchema, 'TStaticMethods'>;
|
|
89
|
+
|
|
90
|
+
export function model<T>(name: string, schema?: Schema<T, any, any> | Schema<T & Document, any, any>, collection?: string, options?: CompileModelOptions): Model<T>;
|
|
91
|
+
|
|
92
|
+
export function model<T, U, TQueryHelpers = {}>(
|
|
93
|
+
name: string,
|
|
94
|
+
schema?: Schema<T, any, any, TQueryHelpers, any, any>,
|
|
95
|
+
collection?: string,
|
|
96
|
+
options?: CompileModelOptions
|
|
97
|
+
): U;
|
|
98
|
+
|
|
99
|
+
/** Returns an array of model names created on this instance of Mongoose. */
|
|
100
|
+
export function modelNames(): Array<string>;
|
|
101
|
+
|
|
102
|
+
/**
|
|
103
|
+
* Overwrites the current driver used by this Mongoose instance. A driver is a
|
|
104
|
+
* Mongoose-specific interface that defines functions like `find()`.
|
|
105
|
+
*/
|
|
106
|
+
export function setDriver(driver: any): Mongoose;
|
|
107
|
+
|
|
108
|
+
/** The node-mongodb-native driver Mongoose uses. */
|
|
109
|
+
export const mongo: typeof mongodb;
|
|
110
|
+
|
|
111
|
+
/** Declares a global plugin executed on all Schemas. */
|
|
112
|
+
export function plugin(fn: (schema: Schema, opts?: any) => void, opts?: any): Mongoose;
|
|
113
|
+
|
|
114
|
+
/** Getter/setter around function for pluralizing collection names. */
|
|
115
|
+
export function pluralize(fn?: ((str: string) => string) | null): ((str: string) => string) | null;
|
|
116
|
+
|
|
117
|
+
/** Sets mongoose options */
|
|
118
|
+
export function set<K extends keyof MongooseOptions>(key: K, value: MongooseOptions[K]): Mongoose;
|
|
119
|
+
export function set(options: { [K in keyof MongooseOptions]: MongooseOptions[K] }): Mongoose;
|
|
120
|
+
|
|
121
|
+
/** The Mongoose version */
|
|
122
|
+
export const version: string;
|
|
123
|
+
|
|
124
|
+
export type AnyKeys<T> = { [P in keyof T]?: T[P] | any };
|
|
125
|
+
export interface AnyObject {
|
|
126
|
+
[k: string]: any
|
|
127
|
+
}
|
|
128
|
+
|
|
129
|
+
export type Require_id<T> = T extends { _id?: infer U }
|
|
130
|
+
? IfAny<U, T & { _id: Types.ObjectId }, T & Required<{ _id: U }>>
|
|
131
|
+
: T & { _id: Types.ObjectId };
|
|
132
|
+
|
|
133
|
+
export type HydratedDocument<DocType, TMethodsAndOverrides = {}, TVirtuals = {}> = DocType extends Document ?
|
|
134
|
+
Require_id<DocType> :
|
|
135
|
+
Document<unknown, any, DocType> & MergeType<Require_id<DocType>, TVirtuals & TMethodsAndOverrides>;
|
|
136
|
+
|
|
137
|
+
export type HydratedDocumentFromSchema<TSchema extends Schema> = HydratedDocument<
|
|
138
|
+
InferSchemaType<TSchema>,
|
|
139
|
+
ObtainSchemaGeneric<TSchema, 'TInstanceMethods'>,
|
|
140
|
+
ObtainSchemaGeneric<TSchema, 'TVirtuals'>
|
|
141
|
+
>;
|
|
142
|
+
|
|
143
|
+
export interface TagSet {
|
|
144
|
+
[k: string]: string;
|
|
145
|
+
}
|
|
146
|
+
|
|
147
|
+
export interface ToObjectOptions {
|
|
148
|
+
/** apply all getters (path and virtual getters) */
|
|
149
|
+
getters?: boolean;
|
|
150
|
+
/** apply virtual getters (can override getters option) */
|
|
151
|
+
virtuals?: boolean | string[];
|
|
152
|
+
/** if `options.virtuals = true`, you can set `options.aliases = false` to skip applying aliases. This option is a no-op if `options.virtuals = false`. */
|
|
153
|
+
aliases?: boolean;
|
|
154
|
+
/** remove empty objects (defaults to true) */
|
|
155
|
+
minimize?: boolean;
|
|
156
|
+
/** if set, mongoose will call this function to allow you to transform the returned object */
|
|
157
|
+
transform?: boolean | ((doc: any, ret: any, options: any) => any);
|
|
158
|
+
/** if true, replace any conventionally populated paths with the original id in the output. Has no affect on virtual populated paths. */
|
|
159
|
+
depopulate?: boolean;
|
|
160
|
+
/** if false, exclude the version key (`__v` by default) from the output */
|
|
161
|
+
versionKey?: boolean;
|
|
162
|
+
/** if true, convert Maps to POJOs. Useful if you want to `JSON.stringify()` the result of `toObject()`. */
|
|
163
|
+
flattenMaps?: boolean;
|
|
164
|
+
/** 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. */
|
|
165
|
+
useProjection?: boolean;
|
|
166
|
+
}
|
|
167
|
+
|
|
168
|
+
export type DiscriminatorModel<M, T> = T extends Model<infer T1, infer T2, infer T3, infer T4>
|
|
169
|
+
?
|
|
170
|
+
M extends Model<infer M1, infer M2, infer M3, infer M4>
|
|
171
|
+
? Model<Omit<M1, keyof T1> & T1, M2 | T2, M3 | T3, M4 | T4>
|
|
172
|
+
: M
|
|
173
|
+
: M;
|
|
174
|
+
|
|
175
|
+
export type DiscriminatorSchema<DocType, M, TInstanceMethods, TQueryHelpers, TVirtuals, TStaticMethods, DisSchema> =
|
|
176
|
+
DisSchema extends Schema<infer DisSchemaEDocType, infer DisSchemaM, infer DisSchemaInstanceMethods, infer DisSchemaQueryhelpers, infer DisSchemaVirtuals, infer DisSchemaStatics>
|
|
177
|
+
? Schema<Omit<DocType, keyof DisSchemaEDocType> & DisSchemaEDocType, DiscriminatorModel<DisSchemaM, M>, DisSchemaInstanceMethods | TInstanceMethods, DisSchemaQueryhelpers | TQueryHelpers, DisSchemaVirtuals | TVirtuals, DisSchemaStatics & TStaticMethods>
|
|
178
|
+
: Schema<DocType, M, TInstanceMethods, TQueryHelpers, TVirtuals, TStaticMethods>;
|
|
179
|
+
|
|
180
|
+
type QueryResultType<T> = T extends Query<infer ResultType, any> ? ResultType : never;
|
|
181
|
+
|
|
182
|
+
type PluginFunction<
|
|
183
|
+
DocType,
|
|
184
|
+
M,
|
|
185
|
+
TInstanceMethods,
|
|
186
|
+
TQueryHelpers,
|
|
187
|
+
TVirtuals,
|
|
188
|
+
TStaticMethods> = (schema: Schema<DocType, M, TInstanceMethods, TQueryHelpers, TVirtuals, TStaticMethods>, opts?: any) => void;
|
|
189
|
+
|
|
190
|
+
export class Schema<
|
|
191
|
+
EnforcedDocType = any,
|
|
192
|
+
M = Model<EnforcedDocType, any, any, any>,
|
|
193
|
+
TInstanceMethods = {},
|
|
194
|
+
TQueryHelpers = {},
|
|
195
|
+
TVirtuals = {},
|
|
196
|
+
TStaticMethods = {},
|
|
197
|
+
TSchemaOptions extends ResolveSchemaOptions<TSchemaOptions> = DefaultSchemaOptions,
|
|
198
|
+
DocType extends ApplySchemaOptions<ObtainDocumentType<DocType, EnforcedDocType, TSchemaOptions>, TSchemaOptions> = ApplySchemaOptions<ObtainDocumentType<any, EnforcedDocType, TSchemaOptions>, TSchemaOptions>,
|
|
199
|
+
>
|
|
200
|
+
extends events.EventEmitter {
|
|
201
|
+
/**
|
|
202
|
+
* Create a new schema
|
|
203
|
+
*/
|
|
204
|
+
constructor(definition?: SchemaDefinition<SchemaDefinitionType<EnforcedDocType>> | DocType, options?: SchemaOptions<FlatRecord<DocType>, TInstanceMethods, TQueryHelpers, TStaticMethods, TVirtuals> | TSchemaOptions);
|
|
205
|
+
|
|
206
|
+
/** Adds key path / schema type pairs to this schema. */
|
|
207
|
+
add(obj: SchemaDefinition<SchemaDefinitionType<EnforcedDocType>> | Schema, prefix?: string): this;
|
|
208
|
+
|
|
209
|
+
/**
|
|
210
|
+
* Add an alias for `path`. This means getting or setting the `alias`
|
|
211
|
+
* is equivalent to getting or setting the `path`.
|
|
212
|
+
*/
|
|
213
|
+
alias(path: string, alias: string | string[]): this;
|
|
214
|
+
|
|
215
|
+
/**
|
|
216
|
+
* Array of child schemas (from document arrays and single nested subdocs)
|
|
217
|
+
* and their corresponding compiled models. Each element of the array is
|
|
218
|
+
* an object with 2 properties: `schema` and `model`.
|
|
219
|
+
*/
|
|
220
|
+
childSchemas: { schema: Schema, model: any }[];
|
|
221
|
+
|
|
222
|
+
/** Removes all indexes on this schema */
|
|
223
|
+
clearIndexes(): this;
|
|
224
|
+
|
|
225
|
+
/** Returns a copy of this schema */
|
|
226
|
+
clone<T = this>(): T;
|
|
227
|
+
|
|
228
|
+
discriminator<DisSchema = Schema>(name: string, schema: DisSchema): this;
|
|
229
|
+
|
|
230
|
+
/** Returns a new schema that has the picked `paths` from this schema. */
|
|
231
|
+
pick<T = this>(paths: string[], options?: SchemaOptions): T;
|
|
232
|
+
|
|
233
|
+
/** Object containing discriminators defined on this schema */
|
|
234
|
+
discriminators?: { [name: string]: Schema };
|
|
235
|
+
|
|
236
|
+
/** Iterates the schemas paths similar to Array#forEach. */
|
|
237
|
+
eachPath(fn: (path: string, type: SchemaType) => void): this;
|
|
238
|
+
|
|
239
|
+
/** Defines an index (most likely compound) for this schema. */
|
|
240
|
+
index(fields: IndexDefinition, options?: IndexOptions): this;
|
|
241
|
+
|
|
242
|
+
/**
|
|
243
|
+
* Returns a list of indexes that this schema declares, via `schema.index()`
|
|
244
|
+
* or by `index: true` in a path's options.
|
|
245
|
+
*/
|
|
246
|
+
indexes(): Array<IndexDefinition>;
|
|
247
|
+
|
|
248
|
+
/** Gets a schema option. */
|
|
249
|
+
get<K extends keyof SchemaOptions>(key: K): SchemaOptions[K];
|
|
250
|
+
|
|
251
|
+
/**
|
|
252
|
+
* Loads an ES6 class into a schema. Maps [setters](https://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/Functions/set) + [getters](https://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/Functions/get), [static methods](https://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/Classes/static),
|
|
253
|
+
* and [instance methods](https://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/Classes#Class_body_and_method_definitions)
|
|
254
|
+
* to schema [virtuals](http://mongoosejs.com/docs/guide.html#virtuals),
|
|
255
|
+
* [statics](http://mongoosejs.com/docs/guide.html#statics), and
|
|
256
|
+
* [methods](http://mongoosejs.com/docs/guide.html#methods).
|
|
257
|
+
*/
|
|
258
|
+
loadClass(model: Function, onlyVirtuals?: boolean): this;
|
|
259
|
+
|
|
260
|
+
/** Adds an instance method to documents constructed from Models compiled from this schema. */
|
|
261
|
+
method<Context = any>(name: string, fn: (this: Context, ...args: any[]) => any, opts?: any): this;
|
|
262
|
+
method(obj: Partial<TInstanceMethods>): this;
|
|
263
|
+
|
|
264
|
+
/** Object of currently defined methods on this schema. */
|
|
265
|
+
methods: { [F in keyof TInstanceMethods]: TInstanceMethods[F] } & AnyObject;
|
|
266
|
+
|
|
267
|
+
/** The original object passed to the schema constructor */
|
|
268
|
+
obj: SchemaDefinition<SchemaDefinitionType<EnforcedDocType>>;
|
|
269
|
+
|
|
270
|
+
/** Gets/sets schema paths. */
|
|
271
|
+
path<ResultType extends SchemaType = SchemaType<any, HydratedDocument<DocType, TInstanceMethods>>>(path: string): ResultType;
|
|
272
|
+
path<pathGeneric extends keyof EnforcedDocType>(path: pathGeneric): SchemaType<EnforcedDocType[pathGeneric]>;
|
|
273
|
+
path(path: string, constructor: any): this;
|
|
274
|
+
|
|
275
|
+
/** Lists all paths and their type in the schema. */
|
|
276
|
+
paths: {
|
|
277
|
+
[key: string]: SchemaType;
|
|
278
|
+
};
|
|
279
|
+
|
|
280
|
+
/** Returns the pathType of `path` for this schema. */
|
|
281
|
+
pathType(path: string): string;
|
|
282
|
+
|
|
283
|
+
/** Registers a plugin for this schema. */
|
|
284
|
+
plugin<PFunc extends PluginFunction<DocType, M, any, any, any, any>, POptions extends Parameters<PFunc>[1] = Parameters<PFunc>[1]>(fn: PFunc, opts?: POptions): this;
|
|
285
|
+
|
|
286
|
+
/** Defines a post hook for the model. */
|
|
287
|
+
post<T = Query<any, any>>(method: MongooseQueryMiddleware | MongooseQueryMiddleware[] | RegExp, options: SchemaPostOptions & { errorHandler: true }, fn: ErrorHandlingMiddlewareWithOption<T>): this;
|
|
288
|
+
post<T = HydratedDocument<DocType, TInstanceMethods>>(method: MongooseDocumentMiddleware | MongooseDocumentMiddleware[] | RegExp, options: SchemaPostOptions & { errorHandler: true }, fn: ErrorHandlingMiddlewareWithOption<T>): this;
|
|
289
|
+
post<T extends Aggregate<any>>(method: 'aggregate' | RegExp, options: SchemaPostOptions & { errorHandler: true }, fn: ErrorHandlingMiddlewareWithOption<T, Array<any>>): this;
|
|
290
|
+
post<T = M>(method: 'insertMany' | RegExp, options: SchemaPostOptions & { errorHandler: true }, fn: ErrorHandlingMiddlewareWithOption<T>): this;
|
|
291
|
+
|
|
292
|
+
post<T = Query<any, any>>(method: MongooseQueryMiddleware | MongooseQueryMiddleware[] | RegExp, fn: PostMiddlewareFunction<T, QueryResultType<T>>): this;
|
|
293
|
+
post<T = Query<any, any>>(method: MongooseQueryMiddleware | MongooseQueryMiddleware[] | RegExp, options: SchemaPostOptions, fn: PostMiddlewareFunction<T, QueryResultType<T>>): this;
|
|
294
|
+
post<T = HydratedDocument<DocType, TInstanceMethods>>(method: MongooseDocumentMiddleware | MongooseDocumentMiddleware[] | RegExp, fn: PostMiddlewareFunction<T, T>): this;
|
|
295
|
+
post<T = HydratedDocument<DocType, TInstanceMethods>>(method: MongooseDocumentMiddleware | MongooseDocumentMiddleware[] | RegExp, options: SchemaPostOptions, fn: PostMiddlewareFunction<T, T>): this;
|
|
296
|
+
post<T extends Aggregate<any>>(method: 'aggregate' | RegExp, fn: PostMiddlewareFunction<T, Array<AggregateExtract<T>>>): this;
|
|
297
|
+
post<T extends Aggregate<any>>(method: 'aggregate' | RegExp, options: SchemaPostOptions, fn: PostMiddlewareFunction<T, Array<AggregateExtract<T>>>): this;
|
|
298
|
+
post<T = M>(method: 'insertMany' | RegExp, fn: PostMiddlewareFunction<T, T>): this;
|
|
299
|
+
post<T = M>(method: 'insertMany' | RegExp, options: SchemaPostOptions, fn: PostMiddlewareFunction<T, T>): this;
|
|
300
|
+
|
|
301
|
+
post<T = Query<any, any>>(method: MongooseQueryMiddleware | MongooseQueryMiddleware[] | RegExp, fn: ErrorHandlingMiddlewareFunction<T>): this;
|
|
302
|
+
post<T = Query<any, any>>(method: MongooseQueryMiddleware | MongooseQueryMiddleware[] | RegExp, options: SchemaPostOptions, fn: ErrorHandlingMiddlewareFunction<T>): this;
|
|
303
|
+
post<T = HydratedDocument<DocType, TInstanceMethods>>(method: MongooseDocumentMiddleware | MongooseDocumentMiddleware[] | RegExp, fn: ErrorHandlingMiddlewareFunction<T>): this;
|
|
304
|
+
post<T = HydratedDocument<DocType, TInstanceMethods>>(method: MongooseDocumentMiddleware | MongooseDocumentMiddleware[] | RegExp, options: SchemaPostOptions, fn: ErrorHandlingMiddlewareFunction<T>): this;
|
|
305
|
+
post<T extends Aggregate<any>>(method: 'aggregate' | RegExp, fn: ErrorHandlingMiddlewareFunction<T, Array<any>>): this;
|
|
306
|
+
post<T extends Aggregate<any>>(method: 'aggregate' | RegExp, options: SchemaPostOptions, fn: ErrorHandlingMiddlewareFunction<T, Array<any>>): this;
|
|
307
|
+
post<T = M>(method: 'insertMany' | RegExp, fn: ErrorHandlingMiddlewareFunction<T>): this;
|
|
308
|
+
post<T = M>(method: 'insertMany' | RegExp, options: SchemaPostOptions, fn: ErrorHandlingMiddlewareFunction<T>): this;
|
|
309
|
+
|
|
310
|
+
/** Defines a pre hook for the model. */
|
|
311
|
+
pre<T = HydratedDocument<DocType, TInstanceMethods>>(
|
|
312
|
+
method: DocumentOrQueryMiddleware | DocumentOrQueryMiddleware[],
|
|
313
|
+
options: SchemaPreOptions & { document: true; query: false; },
|
|
314
|
+
fn: PreMiddlewareFunction<T>
|
|
315
|
+
): this;
|
|
316
|
+
pre<T = Query<any, any>>(
|
|
317
|
+
method: DocumentOrQueryMiddleware | DocumentOrQueryMiddleware[],
|
|
318
|
+
options: SchemaPreOptions & { document: false; query: true; },
|
|
319
|
+
fn: PreMiddlewareFunction<T>
|
|
320
|
+
): this;
|
|
321
|
+
pre<T = HydratedDocument<DocType, TInstanceMethods>>(method: 'save', fn: PreSaveMiddlewareFunction<T>): this;
|
|
322
|
+
pre<T = HydratedDocument<DocType, TInstanceMethods>>(method: 'save', options: SchemaPreOptions, fn: PreSaveMiddlewareFunction<T>): this;
|
|
323
|
+
pre<T = Query<any, any>>(method: MongooseQueryMiddleware | MongooseQueryMiddleware[] | RegExp, fn: PreMiddlewareFunction<T>): this;
|
|
324
|
+
pre<T = Query<any, any>>(method: MongooseQueryMiddleware | MongooseQueryMiddleware[] | RegExp, options: SchemaPreOptions, fn: PreMiddlewareFunction<T>): this;
|
|
325
|
+
pre<T = HydratedDocument<DocType, TInstanceMethods>>(method: MongooseDocumentMiddleware | MongooseDocumentMiddleware[] | RegExp, fn: PreMiddlewareFunction<T>): this;
|
|
326
|
+
pre<T = HydratedDocument<DocType, TInstanceMethods>>(method: MongooseDocumentMiddleware | MongooseDocumentMiddleware[] | RegExp, options: SchemaPreOptions, fn: PreMiddlewareFunction<T>): this;
|
|
327
|
+
pre<T extends Aggregate<any>>(method: 'aggregate' | RegExp, fn: PreMiddlewareFunction<T>): this;
|
|
328
|
+
pre<T extends Aggregate<any>>(method: 'aggregate' | RegExp, options: SchemaPreOptions, fn: PreMiddlewareFunction<T>): this;
|
|
329
|
+
pre<T = M>(method: 'insertMany' | RegExp, fn: (this: T, next: (err?: CallbackError) => void, docs: any | Array<any>) => void | Promise<void>): this;
|
|
330
|
+
pre<T = M>(method: 'insertMany' | RegExp, options: SchemaPreOptions, fn: (this: T, next: (err?: CallbackError) => void, docs: any | Array<any>) => void | Promise<void>): this;
|
|
331
|
+
|
|
332
|
+
/** Object of currently defined query helpers on this schema. */
|
|
333
|
+
query: TQueryHelpers;
|
|
334
|
+
|
|
335
|
+
/** Adds a method call to the queue. */
|
|
336
|
+
queue(name: string, args: any[]): this;
|
|
337
|
+
|
|
338
|
+
/** Removes the given `path` (or [`paths`]). */
|
|
339
|
+
remove(paths: string | Array<string>): this;
|
|
340
|
+
|
|
341
|
+
/** Removes index by name or index spec */
|
|
342
|
+
remove(index: string | AnyObject): this;
|
|
343
|
+
|
|
344
|
+
/** Returns an Array of path strings that are required by this schema. */
|
|
345
|
+
requiredPaths(invalidate?: boolean): string[];
|
|
346
|
+
|
|
347
|
+
/** Sets a schema option. */
|
|
348
|
+
set<K extends keyof SchemaOptions>(key: K, value: SchemaOptions[K], _tags?: any): this;
|
|
349
|
+
|
|
350
|
+
/** Adds static "class" methods to Models compiled from this schema. */
|
|
351
|
+
static<K extends keyof TStaticMethods>(name: K, fn: TStaticMethods[K]): this;
|
|
352
|
+
static(obj: { [F in keyof TStaticMethods]: TStaticMethods[F] } & { [name: string]: (this: M, ...args: any[]) => any }): this;
|
|
353
|
+
static(name: string, fn: (this: M, ...args: any[]) => any): this;
|
|
354
|
+
|
|
355
|
+
/** Object of currently defined statics on this schema. */
|
|
356
|
+
statics: { [F in keyof TStaticMethods]: TStaticMethods[F] } & { [name: string]: (this: M, ...args: any[]) => any };
|
|
357
|
+
|
|
358
|
+
/** Creates a virtual type with the given name. */
|
|
359
|
+
virtual<T = HydratedDocument<DocType, TInstanceMethods, TVirtuals>>(
|
|
360
|
+
name: keyof TVirtuals | string,
|
|
361
|
+
options?: VirtualTypeOptions<T, DocType>
|
|
362
|
+
): VirtualType<T>;
|
|
363
|
+
|
|
364
|
+
/** Object of currently defined virtuals on this schema */
|
|
365
|
+
virtuals: TVirtuals;
|
|
366
|
+
|
|
367
|
+
/** Returns the virtual type with the given `name`. */
|
|
368
|
+
virtualpath<T = HydratedDocument<DocType, TInstanceMethods>>(name: string): VirtualType<T> | null;
|
|
369
|
+
}
|
|
370
|
+
|
|
371
|
+
export type NumberSchemaDefinition = typeof Number | 'number' | 'Number' | typeof Schema.Types.Number;
|
|
372
|
+
export type StringSchemaDefinition = typeof String | 'string' | 'String' | typeof Schema.Types.String;
|
|
373
|
+
export type BooleanSchemaDefinition = typeof Boolean | 'boolean' | 'Boolean' | typeof Schema.Types.Boolean;
|
|
374
|
+
export type DateSchemaDefinition = typeof NativeDate | 'date' | 'Date' | typeof Schema.Types.Date;
|
|
375
|
+
export type ObjectIdSchemaDefinition = 'ObjectId' | 'ObjectID' | typeof Schema.Types.ObjectId;
|
|
376
|
+
|
|
377
|
+
export type SchemaDefinitionWithBuiltInClass<T> = T extends number
|
|
378
|
+
? NumberSchemaDefinition
|
|
379
|
+
: T extends string
|
|
380
|
+
? StringSchemaDefinition
|
|
381
|
+
: T extends boolean
|
|
382
|
+
? BooleanSchemaDefinition
|
|
383
|
+
: T extends NativeDate
|
|
384
|
+
? DateSchemaDefinition
|
|
385
|
+
: (Function | string);
|
|
386
|
+
|
|
387
|
+
export type SchemaDefinitionProperty<T = undefined> = SchemaDefinitionWithBuiltInClass<T> |
|
|
388
|
+
SchemaTypeOptions<T extends undefined ? any : T> |
|
|
389
|
+
typeof SchemaType |
|
|
390
|
+
Schema<any, any, any> |
|
|
391
|
+
Schema<any, any, any>[] |
|
|
392
|
+
SchemaTypeOptions<T extends undefined ? any : Unpacked<T>>[] |
|
|
393
|
+
Function[] |
|
|
394
|
+
SchemaDefinition<T> |
|
|
395
|
+
SchemaDefinition<Unpacked<T>>[] |
|
|
396
|
+
typeof Schema.Types.Mixed |
|
|
397
|
+
MixedSchemaTypeOptions;
|
|
398
|
+
|
|
399
|
+
export type SchemaDefinition<T = undefined> = T extends undefined
|
|
400
|
+
? { [path: string]: SchemaDefinitionProperty; }
|
|
401
|
+
: { [path in keyof T]?: SchemaDefinitionProperty<T[path]>; };
|
|
402
|
+
|
|
403
|
+
export type AnyArray<T> = T[] | ReadonlyArray<T>;
|
|
404
|
+
export type ExtractMongooseArray<T> = T extends Types.Array<any> ? AnyArray<Unpacked<T>> : T;
|
|
405
|
+
|
|
406
|
+
export interface MixedSchemaTypeOptions extends SchemaTypeOptions<Schema.Types.Mixed> {
|
|
407
|
+
type: typeof Schema.Types.Mixed;
|
|
408
|
+
}
|
|
409
|
+
|
|
410
|
+
export type RefType =
|
|
411
|
+
| number
|
|
412
|
+
| string
|
|
413
|
+
| Buffer
|
|
414
|
+
| undefined
|
|
415
|
+
| Types.ObjectId
|
|
416
|
+
| Types.Buffer
|
|
417
|
+
| typeof Schema.Types.Number
|
|
418
|
+
| typeof Schema.Types.String
|
|
419
|
+
| typeof Schema.Types.Buffer
|
|
420
|
+
| typeof Schema.Types.ObjectId;
|
|
421
|
+
|
|
422
|
+
|
|
423
|
+
export type InferId<T> = T extends { _id?: any } ? T['_id'] : Types.ObjectId;
|
|
424
|
+
|
|
425
|
+
export interface VirtualTypeOptions<HydratedDocType = Document, DocType = unknown> {
|
|
426
|
+
/** If `ref` is not nullish, this becomes a populated virtual. */
|
|
427
|
+
ref?: string | Function;
|
|
428
|
+
|
|
429
|
+
/** The local field to populate on if this is a populated virtual. */
|
|
430
|
+
localField?: string | ((this: HydratedDocType, doc: HydratedDocType) => string);
|
|
431
|
+
|
|
432
|
+
/** The foreign field to populate on if this is a populated virtual. */
|
|
433
|
+
foreignField?: string | ((this: HydratedDocType, doc: HydratedDocType) => string);
|
|
434
|
+
|
|
435
|
+
/**
|
|
436
|
+
* By default, a populated virtual is an array. If you set `justOne`,
|
|
437
|
+
* the populated virtual will be a single doc or `null`.
|
|
438
|
+
*/
|
|
439
|
+
justOne?: boolean;
|
|
440
|
+
|
|
441
|
+
/** If you set this to `true`, Mongoose will call any custom getters you defined on this virtual. */
|
|
442
|
+
getters?: boolean;
|
|
443
|
+
|
|
444
|
+
/**
|
|
445
|
+
* If you set this to `true`, `populate()` will set this virtual to the number of populated
|
|
446
|
+
* documents, as opposed to the documents themselves, using `Query#countDocuments()`.
|
|
447
|
+
*/
|
|
448
|
+
count?: boolean;
|
|
449
|
+
|
|
450
|
+
/** Add an extra match condition to `populate()`. */
|
|
451
|
+
match?: FilterQuery<any> | Function;
|
|
452
|
+
|
|
453
|
+
/** Add a default `limit` to the `populate()` query. */
|
|
454
|
+
limit?: number;
|
|
455
|
+
|
|
456
|
+
/** Add a default `skip` to the `populate()` query. */
|
|
457
|
+
skip?: number;
|
|
458
|
+
|
|
459
|
+
/**
|
|
460
|
+
* For legacy reasons, `limit` with `populate()` may give incorrect results because it only
|
|
461
|
+
* executes a single query for every document being populated. If you set `perDocumentLimit`,
|
|
462
|
+
* Mongoose will ensure correct `limit` per document by executing a separate query for each
|
|
463
|
+
* document to `populate()`. For example, `.find().populate({ path: 'test', perDocumentLimit: 2 })`
|
|
464
|
+
* will execute 2 additional queries if `.find()` returns 2 documents.
|
|
465
|
+
*/
|
|
466
|
+
perDocumentLimit?: number;
|
|
467
|
+
|
|
468
|
+
/** Additional options like `limit` and `lean`. */
|
|
469
|
+
options?: QueryOptions<DocType> & { match?: AnyObject };
|
|
470
|
+
|
|
471
|
+
/** Additional options for plugins */
|
|
472
|
+
[extra: string]: any;
|
|
473
|
+
}
|
|
474
|
+
|
|
475
|
+
export class VirtualType<HydratedDocType> {
|
|
476
|
+
/** Applies getters to `value`. */
|
|
477
|
+
applyGetters(value: any, doc: Document): any;
|
|
478
|
+
|
|
479
|
+
/** Applies setters to `value`. */
|
|
480
|
+
applySetters(value: any, doc: Document): any;
|
|
481
|
+
|
|
482
|
+
/** Adds a custom getter to this virtual. */
|
|
483
|
+
get<T = HydratedDocType>(fn: (this: T, value: any, virtualType: VirtualType<T>, doc: T) => any): this;
|
|
484
|
+
|
|
485
|
+
/** Adds a custom setter to this virtual. */
|
|
486
|
+
set<T = HydratedDocType>(fn: (this: T, value: any, virtualType: VirtualType<T>, doc: T) => void): this;
|
|
487
|
+
}
|
|
488
|
+
|
|
489
|
+
export type ReturnsNewDoc = { new: true } | { returnOriginal: false } | { returnDocument: 'after' };
|
|
490
|
+
|
|
491
|
+
export type ProjectionElementType = number | string;
|
|
492
|
+
export type ProjectionType<T> = { [P in keyof T]?: ProjectionElementType } | AnyObject | string;
|
|
493
|
+
|
|
494
|
+
export type SortValues = SortOrder;
|
|
495
|
+
|
|
496
|
+
export type SortOrder = -1 | 1 | 'asc' | 'ascending' | 'desc' | 'descending';
|
|
497
|
+
|
|
498
|
+
type _UpdateQuery<TSchema> = {
|
|
499
|
+
/** @see https://www.mongodb.com/docs/manual/reference/operator/update-field/ */
|
|
500
|
+
$currentDate?: AnyKeys<TSchema> & AnyObject;
|
|
501
|
+
$inc?: AnyKeys<TSchema> & AnyObject;
|
|
502
|
+
$min?: AnyKeys<TSchema> & AnyObject;
|
|
503
|
+
$max?: AnyKeys<TSchema> & AnyObject;
|
|
504
|
+
$mul?: AnyKeys<TSchema> & AnyObject;
|
|
505
|
+
$rename?: Record<string, string>;
|
|
506
|
+
$set?: AnyKeys<TSchema> & AnyObject;
|
|
507
|
+
$setOnInsert?: AnyKeys<TSchema> & AnyObject;
|
|
508
|
+
$unset?: AnyKeys<TSchema> & AnyObject;
|
|
509
|
+
|
|
510
|
+
/** @see https://www.mongodb.com/docs/manual/reference/operator/update-array/ */
|
|
511
|
+
$addToSet?: AnyKeys<TSchema> & AnyObject;
|
|
512
|
+
$pop?: AnyKeys<TSchema> & AnyObject;
|
|
513
|
+
$pull?: AnyKeys<TSchema> & AnyObject;
|
|
514
|
+
$push?: AnyKeys<TSchema> & AnyObject;
|
|
515
|
+
$pullAll?: AnyKeys<TSchema> & AnyObject;
|
|
516
|
+
|
|
517
|
+
/** @see https://www.mongodb.com/docs/manual/reference/operator/update-bitwise/ */
|
|
518
|
+
// Needs to be `AnyKeys` for now, because anything stricter makes us incompatible
|
|
519
|
+
// with the MongoDB Node driver's `UpdateFilter` interface (see gh-12595, gh-11911)
|
|
520
|
+
// and using the Node driver's `$bit` definition breaks because their `OnlyFieldsOfType`
|
|
521
|
+
// interface breaks on Mongoose Document class due to circular references.
|
|
522
|
+
// Re-evaluate this when we drop `extends Document` support in document interfaces.
|
|
523
|
+
$bit?: AnyKeys<TSchema>;
|
|
524
|
+
};
|
|
525
|
+
|
|
526
|
+
export type UpdateWithAggregationPipeline = UpdateAggregationStage[];
|
|
527
|
+
export type UpdateAggregationStage = { $addFields: any } |
|
|
528
|
+
{ $set: any } |
|
|
529
|
+
{ $project: any } |
|
|
530
|
+
{ $unset: any } |
|
|
531
|
+
{ $replaceRoot: any } |
|
|
532
|
+
{ $replaceWith: any };
|
|
533
|
+
|
|
534
|
+
/**
|
|
535
|
+
* Update query command to perform on the document
|
|
536
|
+
* @example
|
|
537
|
+
* ```js
|
|
538
|
+
* { age: 30 }
|
|
539
|
+
* ```
|
|
540
|
+
*/
|
|
541
|
+
export type UpdateQuery<T> = _UpdateQuery<T> & AnyObject;
|
|
542
|
+
|
|
543
|
+
export type DocumentDefinition<T> = {
|
|
544
|
+
[K in keyof Omit<T, Exclude<keyof Document, '_id' | 'id' | '__v'>>]:
|
|
545
|
+
[Extract<T[K], mongodb.ObjectId>] extends [never]
|
|
546
|
+
? T[K] extends TreatAsPrimitives
|
|
547
|
+
? T[K]
|
|
548
|
+
: LeanDocumentElement<T[K]>
|
|
549
|
+
: T[K] | string;
|
|
550
|
+
};
|
|
551
|
+
|
|
552
|
+
export type FlattenMaps<T> = {
|
|
553
|
+
[K in keyof T]: T[K] extends Map<any, any>
|
|
554
|
+
? AnyObject : T[K] extends TreatAsPrimitives
|
|
555
|
+
? T[K] : FlattenMaps<T[K]>;
|
|
556
|
+
};
|
|
557
|
+
|
|
558
|
+
export type actualPrimitives = string | boolean | number | bigint | symbol | null | undefined;
|
|
559
|
+
export type TreatAsPrimitives = actualPrimitives | NativeDate | RegExp | symbol | Error | BigInt | Types.ObjectId;
|
|
560
|
+
|
|
561
|
+
export type LeanType<T> =
|
|
562
|
+
0 extends (1 & T) ? T : // any
|
|
563
|
+
T extends TreatAsPrimitives ? T : // primitives
|
|
564
|
+
T extends Types.ArraySubdocument ? Omit<LeanDocument<T>, 'parentArray' | 'ownerDocument' | 'parent'> :
|
|
565
|
+
T extends Types.Subdocument ? Omit<LeanDocument<T>, '$isSingleNested' | 'ownerDocument' | 'parent'> :
|
|
566
|
+
LeanDocument<T>; // Documents and everything else
|
|
567
|
+
|
|
568
|
+
export type LeanArray<T extends unknown[]> = T extends unknown[][] ? LeanArray<T[number]>[] : LeanType<T[number]>[];
|
|
569
|
+
|
|
570
|
+
export type _LeanDocument<T> = {
|
|
571
|
+
[K in keyof T]: LeanDocumentElement<T[K]>;
|
|
572
|
+
};
|
|
573
|
+
|
|
574
|
+
// Keep this a separate type, to ensure that T is a naked type.
|
|
575
|
+
// This way, the conditional type is distributive over union types.
|
|
576
|
+
// This is required for PopulatedDoc.
|
|
577
|
+
export type LeanDocumentElement<T> =
|
|
578
|
+
T extends unknown[] ? LeanArray<T> : // Array
|
|
579
|
+
T extends Document ? LeanDocument<T> : // Subdocument
|
|
580
|
+
T;
|
|
581
|
+
|
|
582
|
+
export type SchemaDefinitionType<T> = T extends Document ? Omit<T, Exclude<keyof Document, '_id' | 'id' | '__v'>> : T;
|
|
583
|
+
|
|
584
|
+
// tests for these two types are located in test/types/lean.test.ts
|
|
585
|
+
export type DocTypeFromUnion<T> = T extends (Document<infer T1, infer T2, infer T3> & infer U) ?
|
|
586
|
+
[U] extends [Document<T1, T2, T3> & infer U] ? IfUnknown<IfAny<U, false>, false> : false : false;
|
|
587
|
+
|
|
588
|
+
export type DocTypeFromGeneric<T> = T extends Document<infer IdType, infer TQueryHelpers, infer DocType> ?
|
|
589
|
+
IfUnknown<IfAny<DocType, false>, false> : false;
|
|
590
|
+
|
|
591
|
+
/**
|
|
592
|
+
* Helper to choose the best option between two type helpers
|
|
593
|
+
*/
|
|
594
|
+
export type _pickObject<T1, T2, Fallback> = T1 extends false ? T2 extends false ? Fallback : T2 : T1;
|
|
595
|
+
|
|
596
|
+
/**
|
|
597
|
+
* There may be a better way to do this, but the goal is to return the DocType if it can be infered
|
|
598
|
+
* and if not to return a type which is easily identified as "not valid" so we fall back to
|
|
599
|
+
* "strip out known things added by extending Document"
|
|
600
|
+
* There are three basic ways to mix in Document -- "Document & T", "Document<ObjId, mixins, T>",
|
|
601
|
+
* and "T extends Document". In the last case there is no type without Document mixins, so we can only
|
|
602
|
+
* strip things out. In the other two cases we can infer the type, so we should
|
|
603
|
+
*/
|
|
604
|
+
export type BaseDocumentType<T> = _pickObject<DocTypeFromUnion<T>, DocTypeFromGeneric<T>, false>;
|
|
605
|
+
|
|
606
|
+
/**
|
|
607
|
+
* Documents returned from queries with the lean option enabled.
|
|
608
|
+
* Plain old JavaScript object documents (POJO).
|
|
609
|
+
* @see https://mongoosejs.com/docs/tutorials/lean.html
|
|
610
|
+
*/
|
|
611
|
+
export type LeanDocument<T> = Omit<_LeanDocument<T>, Exclude<keyof Document, '_id' | 'id' | '__v'> | '$isSingleNested'>;
|
|
612
|
+
|
|
613
|
+
export type LeanDocumentOrArray<T> = 0 extends (1 & T) ? T :
|
|
614
|
+
T extends unknown[] ? LeanDocument<T[number]>[] :
|
|
615
|
+
T extends Document ? LeanDocument<T> :
|
|
616
|
+
T;
|
|
617
|
+
|
|
618
|
+
export type LeanDocumentOrArrayWithRawType<T, RawDocType> = 0 extends (1 & T) ? T :
|
|
619
|
+
T extends unknown[] ? LeanDocument<RawDocType>[] :
|
|
620
|
+
T extends Document ? LeanDocument<RawDocType> :
|
|
621
|
+
T;
|
|
622
|
+
|
|
623
|
+
/* for ts-mongoose */
|
|
624
|
+
export class mquery { }
|
|
625
|
+
|
|
626
|
+
export default mongoose;
|
|
627
|
+
}
|