@depup/mongoose 9.1.3-depup.0
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/LICENSE.md +22 -0
- package/README.md +397 -0
- package/SECURITY.md +1 -0
- package/eslint.config.mjs +198 -0
- package/index.js +64 -0
- package/lib/aggregate.js +1189 -0
- package/lib/cast/bigint.js +46 -0
- package/lib/cast/boolean.js +32 -0
- package/lib/cast/date.js +41 -0
- package/lib/cast/decimal128.js +39 -0
- package/lib/cast/double.js +50 -0
- package/lib/cast/int32.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/uuid.js +35 -0
- package/lib/cast.js +436 -0
- package/lib/collection.js +321 -0
- package/lib/connection.js +1855 -0
- package/lib/connectionState.js +26 -0
- package/lib/constants.js +73 -0
- package/lib/cursor/aggregationCursor.js +466 -0
- package/lib/cursor/changeStream.js +198 -0
- package/lib/cursor/queryCursor.js +622 -0
- package/lib/document.js +5521 -0
- package/lib/driver.js +15 -0
- package/lib/drivers/SPEC.md +4 -0
- package/lib/drivers/node-mongodb-native/bulkWriteResult.js +5 -0
- package/lib/drivers/node-mongodb-native/collection.js +393 -0
- package/lib/drivers/node-mongodb-native/connection.js +506 -0
- package/lib/drivers/node-mongodb-native/index.js +10 -0
- package/lib/error/browserMissingSchema.js +29 -0
- package/lib/error/bulkSaveIncompleteError.js +44 -0
- package/lib/error/bulkWriteError.js +41 -0
- package/lib/error/cast.js +158 -0
- package/lib/error/createCollectionsError.js +26 -0
- package/lib/error/divergentArray.js +40 -0
- package/lib/error/eachAsyncMultiError.js +41 -0
- package/lib/error/index.js +237 -0
- package/lib/error/invalidSchemaOption.js +32 -0
- package/lib/error/messages.js +47 -0
- package/lib/error/missingSchema.js +33 -0
- package/lib/error/mongooseError.js +13 -0
- package/lib/error/notFound.js +47 -0
- package/lib/error/objectExpected.js +31 -0
- package/lib/error/objectParameter.js +31 -0
- package/lib/error/overwriteModel.js +31 -0
- package/lib/error/parallelSave.js +33 -0
- package/lib/error/parallelValidate.js +33 -0
- package/lib/error/serverSelection.js +62 -0
- package/lib/error/setOptionError.js +103 -0
- package/lib/error/strict.js +35 -0
- package/lib/error/strictPopulate.js +31 -0
- package/lib/error/syncIndexes.js +30 -0
- package/lib/error/validation.js +97 -0
- package/lib/error/validator.js +100 -0
- package/lib/error/version.js +38 -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 +204 -0
- package/lib/helpers/common.js +127 -0
- package/lib/helpers/createJSONSchemaTypeDefinition.js +24 -0
- package/lib/helpers/cursor/eachAsync.js +225 -0
- package/lib/helpers/discriminator/applyEmbeddedDiscriminators.js +36 -0
- package/lib/helpers/discriminator/areDiscriminatorValuesEqual.js +16 -0
- package/lib/helpers/discriminator/checkEmbeddedDiscriminatorKeyProjection.js +12 -0
- package/lib/helpers/discriminator/getConstructor.js +29 -0
- package/lib/helpers/discriminator/getDiscriminatorByValue.js +28 -0
- package/lib/helpers/discriminator/getSchemaDiscriminatorByValue.js +27 -0
- package/lib/helpers/discriminator/mergeDiscriminatorSchema.js +91 -0
- package/lib/helpers/document/applyDefaults.js +132 -0
- package/lib/helpers/document/applyTimestamps.js +106 -0
- package/lib/helpers/document/applyVirtuals.js +147 -0
- package/lib/helpers/document/cleanModifiedSubpaths.js +45 -0
- package/lib/helpers/document/compile.js +238 -0
- package/lib/helpers/document/getDeepestSubdocumentForPath.js +38 -0
- package/lib/helpers/document/getEmbeddedDiscriminatorPath.js +53 -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 +18 -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 +63 -0
- package/lib/helpers/indexes/isDefaultIdIndex.js +18 -0
- package/lib/helpers/indexes/isIndexEqual.js +95 -0
- package/lib/helpers/indexes/isIndexSpecEqual.js +32 -0
- package/lib/helpers/indexes/isTextIndex.js +16 -0
- package/lib/helpers/indexes/isTimeseriesIndex.js +16 -0
- package/lib/helpers/isAsyncFunction.js +9 -0
- package/lib/helpers/isBsonType.js +15 -0
- package/lib/helpers/isMongooseObject.js +22 -0
- package/lib/helpers/isObject.js +16 -0
- package/lib/helpers/isPOJO.js +12 -0
- package/lib/helpers/isPromise.js +6 -0
- package/lib/helpers/isSimpleValidator.js +22 -0
- package/lib/helpers/minimize.js +41 -0
- package/lib/helpers/model/applyDefaultsToPOJO.js +52 -0
- package/lib/helpers/model/applyHooks.js +140 -0
- package/lib/helpers/model/applyMethods.js +70 -0
- package/lib/helpers/model/applyStaticHooks.js +33 -0
- package/lib/helpers/model/applyStatics.js +13 -0
- package/lib/helpers/model/castBulkWrite.js +316 -0
- package/lib/helpers/model/decorateBulkWriteResult.js +8 -0
- package/lib/helpers/model/discriminator.js +265 -0
- package/lib/helpers/model/pushNestedArrayPaths.js +15 -0
- package/lib/helpers/omitUndefined.js +20 -0
- package/lib/helpers/once.js +12 -0
- package/lib/helpers/parallelLimit.js +37 -0
- package/lib/helpers/path/parentPaths.js +18 -0
- package/lib/helpers/path/setDottedPath.js +33 -0
- package/lib/helpers/pluralize.js +95 -0
- package/lib/helpers/populate/assignRawDocsToIdStructure.js +129 -0
- package/lib/helpers/populate/assignVals.js +360 -0
- package/lib/helpers/populate/createPopulateQueryFilter.js +97 -0
- package/lib/helpers/populate/getModelsMapForPopulate.js +776 -0
- package/lib/helpers/populate/getSchemaTypes.js +228 -0
- package/lib/helpers/populate/getVirtual.js +103 -0
- package/lib/helpers/populate/leanPopulateMap.js +7 -0
- package/lib/helpers/populate/lookupLocalFields.js +40 -0
- package/lib/helpers/populate/markArraySubdocsPopulated.js +49 -0
- package/lib/helpers/populate/modelNamesFromRefPath.js +66 -0
- package/lib/helpers/populate/removeDeselectedForeignField.js +31 -0
- package/lib/helpers/populate/setPopulatedVirtualValue.js +33 -0
- package/lib/helpers/populate/skipPopulateValue.js +10 -0
- package/lib/helpers/populate/validateRef.js +19 -0
- package/lib/helpers/printJestWarning.js +21 -0
- package/lib/helpers/processConnectionOptions.js +65 -0
- package/lib/helpers/projection/applyProjection.js +83 -0
- package/lib/helpers/projection/hasIncludedChildren.js +41 -0
- package/lib/helpers/projection/isDefiningProjection.js +18 -0
- package/lib/helpers/projection/isExclusive.js +37 -0
- package/lib/helpers/projection/isInclusive.js +39 -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/query/applyGlobalOption.js +29 -0
- package/lib/helpers/query/cast$expr.js +287 -0
- package/lib/helpers/query/castFilterPath.js +54 -0
- package/lib/helpers/query/castUpdate.js +643 -0
- package/lib/helpers/query/getEmbeddedDiscriminatorPath.js +103 -0
- package/lib/helpers/query/handleImmutable.js +44 -0
- package/lib/helpers/query/handleReadPreferenceAliases.js +23 -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 +62 -0
- package/lib/helpers/query/trusted.js +13 -0
- package/lib/helpers/query/validOps.js +3 -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/applyReadConcern.js +20 -0
- package/lib/helpers/schema/applyWriteConcern.js +39 -0
- package/lib/helpers/schema/cleanPositionalOperators.js +12 -0
- package/lib/helpers/schema/getIndexes.js +171 -0
- package/lib/helpers/schema/getKeysInSchemaOrder.js +28 -0
- package/lib/helpers/schema/getPath.js +43 -0
- package/lib/helpers/schema/getSubdocumentStrictValue.js +32 -0
- package/lib/helpers/schema/handleIdOption.js +20 -0
- package/lib/helpers/schema/handleTimestampOption.js +24 -0
- package/lib/helpers/schema/idGetter.js +34 -0
- package/lib/helpers/schema/merge.js +36 -0
- package/lib/helpers/schematype/handleImmutable.js +50 -0
- package/lib/helpers/setDefaultsOnInsert.js +158 -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 +116 -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 +131 -0
- package/lib/helpers/update/castArrayFilters.js +113 -0
- package/lib/helpers/update/decorateUpdateWithVersionKey.js +35 -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 +193 -0
- package/lib/index.js +17 -0
- package/lib/internal.js +46 -0
- package/lib/model.js +5010 -0
- package/lib/modifiedPathsSnapshot.js +9 -0
- package/lib/mongoose.js +1411 -0
- package/lib/options/populateOptions.js +36 -0
- package/lib/options/propertyOptions.js +8 -0
- package/lib/options/saveOptions.js +16 -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 +66 -0
- package/lib/options/schemaTypeOptions.js +244 -0
- package/lib/options/schemaUnionOptions.js +32 -0
- package/lib/options/virtualOptions.js +164 -0
- package/lib/options.js +17 -0
- package/lib/plugins/index.js +6 -0
- package/lib/plugins/saveSubdocs.js +76 -0
- package/lib/plugins/sharding.js +84 -0
- package/lib/plugins/trackTransaction.js +84 -0
- package/lib/plugins/validateBeforeSave.js +41 -0
- package/lib/query.js +5673 -0
- package/lib/queryHelpers.js +387 -0
- package/lib/schema/array.js +699 -0
- package/lib/schema/bigint.js +282 -0
- package/lib/schema/boolean.js +332 -0
- package/lib/schema/buffer.js +343 -0
- package/lib/schema/date.js +467 -0
- package/lib/schema/decimal128.js +263 -0
- package/lib/schema/documentArray.js +656 -0
- package/lib/schema/documentArrayElement.js +137 -0
- package/lib/schema/double.js +246 -0
- package/lib/schema/index.js +32 -0
- package/lib/schema/int32.js +289 -0
- package/lib/schema/map.js +201 -0
- package/lib/schema/mixed.js +146 -0
- package/lib/schema/number.js +510 -0
- package/lib/schema/objectId.js +333 -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 +733 -0
- package/lib/schema/subdocument.js +436 -0
- package/lib/schema/symbols.js +5 -0
- package/lib/schema/union.js +113 -0
- package/lib/schema/uuid.js +305 -0
- package/lib/schema.js +3226 -0
- package/lib/schemaType.js +1835 -0
- package/lib/stateMachine.js +232 -0
- package/lib/types/array/index.js +119 -0
- package/lib/types/array/isMongooseArray.js +5 -0
- package/lib/types/array/methods/index.js +1095 -0
- package/lib/types/arraySubdocument.js +207 -0
- package/lib/types/buffer.js +294 -0
- package/lib/types/decimal128.js +13 -0
- package/lib/types/documentArray/index.js +113 -0
- package/lib/types/documentArray/isMongooseDocumentArray.js +5 -0
- package/lib/types/documentArray/methods/index.js +415 -0
- package/lib/types/double.js +13 -0
- package/lib/types/index.js +23 -0
- package/lib/types/map.js +419 -0
- package/lib/types/objectid.js +41 -0
- package/lib/types/subdocument.js +464 -0
- package/lib/types/uuid.js +13 -0
- package/lib/utils.js +1054 -0
- package/lib/validOptions.js +42 -0
- package/lib/virtualType.js +204 -0
- package/package.json +148 -0
- package/types/aggregate.d.ts +180 -0
- package/types/augmentations.d.ts +9 -0
- package/types/callback.d.ts +8 -0
- package/types/collection.d.ts +49 -0
- package/types/connection.d.ts +297 -0
- package/types/cursor.d.ts +67 -0
- package/types/document.d.ts +374 -0
- package/types/error.d.ts +143 -0
- package/types/expressions.d.ts +3053 -0
- package/types/helpers.d.ts +32 -0
- package/types/index.d.ts +1056 -0
- package/types/indexes.d.ts +97 -0
- package/types/inferhydrateddoctype.d.ts +115 -0
- package/types/inferrawdoctype.d.ts +135 -0
- package/types/inferschematype.d.ts +337 -0
- package/types/middlewares.d.ts +59 -0
- package/types/models.d.ts +1306 -0
- package/types/mongooseoptions.d.ts +228 -0
- package/types/pipelinestage.d.ts +333 -0
- package/types/populate.d.ts +53 -0
- package/types/query.d.ts +934 -0
- package/types/schemaoptions.d.ts +282 -0
- package/types/schematypes.d.ts +654 -0
- package/types/session.d.ts +32 -0
- package/types/types.d.ts +109 -0
- package/types/utility.d.ts +175 -0
- package/types/validation.d.ts +39 -0
- package/types/virtuals.d.ts +14 -0
package/types/index.d.ts
ADDED
|
@@ -0,0 +1,1056 @@
|
|
|
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="./inferhydrateddoctype.d.ts" />
|
|
24
|
+
/// <reference path="./inferrawdoctype.d.ts" />
|
|
25
|
+
/// <reference path="./inferschematype.d.ts" />
|
|
26
|
+
/// <reference path="./virtuals.d.ts" />
|
|
27
|
+
/// <reference path="./augmentations.d.ts" />
|
|
28
|
+
|
|
29
|
+
declare class NativeDate extends globalThis.Date { }
|
|
30
|
+
|
|
31
|
+
declare module 'mongoose' {
|
|
32
|
+
import Kareem = require('kareem');
|
|
33
|
+
import events = require('events');
|
|
34
|
+
import mongodb = require('mongodb');
|
|
35
|
+
import mongoose = require('mongoose');
|
|
36
|
+
|
|
37
|
+
export type Mongoose = typeof mongoose;
|
|
38
|
+
|
|
39
|
+
/**
|
|
40
|
+
* Mongoose constructor. The exports object of the `mongoose` module is an instance of this
|
|
41
|
+
* class. Most apps will only use this one instance.
|
|
42
|
+
*/
|
|
43
|
+
export const Mongoose: new (options?: MongooseOptions | null) => Mongoose;
|
|
44
|
+
|
|
45
|
+
export let Promise: any;
|
|
46
|
+
|
|
47
|
+
/**
|
|
48
|
+
* Can be extended to explicitly type specific models.
|
|
49
|
+
*/
|
|
50
|
+
export interface Models {
|
|
51
|
+
[modelName: string]: Model<any>
|
|
52
|
+
}
|
|
53
|
+
|
|
54
|
+
/** An array containing all models associated with this Mongoose instance. */
|
|
55
|
+
export const models: Models;
|
|
56
|
+
|
|
57
|
+
/**
|
|
58
|
+
* Removes the model named `name` from the default connection, if it exists.
|
|
59
|
+
* You can use this function to clean up any models you created in your tests to
|
|
60
|
+
* prevent OverwriteModelErrors.
|
|
61
|
+
*/
|
|
62
|
+
export function deleteModel(name: string | RegExp): Mongoose;
|
|
63
|
+
|
|
64
|
+
/**
|
|
65
|
+
* Sanitizes query filters against query selector injection attacks by wrapping
|
|
66
|
+
* any nested objects that have a property whose name starts with `$` in a `$eq`.
|
|
67
|
+
*/
|
|
68
|
+
export function sanitizeFilter<T>(filter: QueryFilter<T>): QueryFilter<T>;
|
|
69
|
+
|
|
70
|
+
/** Gets mongoose options */
|
|
71
|
+
export function get<K extends keyof MongooseOptions>(key: K): MongooseOptions[K];
|
|
72
|
+
|
|
73
|
+
export function omitUndefined<T extends Record<string, any>>(val: T): T;
|
|
74
|
+
|
|
75
|
+
export type HydratedDocFromModel<M extends Model<any>> = ReturnType<M['hydrate']>;
|
|
76
|
+
|
|
77
|
+
/* ! ignore */
|
|
78
|
+
export type CompileModelOptions = {
|
|
79
|
+
overwriteModels?: boolean,
|
|
80
|
+
connection?: Connection
|
|
81
|
+
};
|
|
82
|
+
|
|
83
|
+
export function model<TSchema extends Schema = any>(
|
|
84
|
+
name: string,
|
|
85
|
+
schema?: TSchema,
|
|
86
|
+
collection?: string,
|
|
87
|
+
options?: CompileModelOptions
|
|
88
|
+
): Model<
|
|
89
|
+
InferSchemaType<TSchema>,
|
|
90
|
+
ObtainSchemaGeneric<TSchema, 'TQueryHelpers'>,
|
|
91
|
+
ObtainSchemaGeneric<TSchema, 'TInstanceMethods'>,
|
|
92
|
+
ObtainSchemaGeneric<TSchema, 'TVirtuals'>,
|
|
93
|
+
// If first schema generic param is set, that means we have an explicit raw doc type,
|
|
94
|
+
// so user should also specify a hydrated doc type if the auto inferred one isn't correct.
|
|
95
|
+
IsItRecordAndNotAny<ObtainSchemaGeneric<TSchema, 'EnforcedDocType'>> extends true
|
|
96
|
+
? ObtainSchemaGeneric<TSchema, 'THydratedDocumentType'>
|
|
97
|
+
: HydratedDocument<
|
|
98
|
+
InferSchemaType<TSchema>,
|
|
99
|
+
ObtainSchemaGeneric<TSchema, 'TVirtuals'> & ObtainSchemaGeneric<TSchema, 'TInstanceMethods'>,
|
|
100
|
+
ObtainSchemaGeneric<TSchema, 'TQueryHelpers'>,
|
|
101
|
+
ObtainSchemaGeneric<TSchema, 'TVirtuals'>,
|
|
102
|
+
InferSchemaType<TSchema>,
|
|
103
|
+
ObtainSchemaGeneric<TSchema, 'TSchemaOptions'>
|
|
104
|
+
>,
|
|
105
|
+
TSchema,
|
|
106
|
+
ObtainSchemaGeneric<TSchema, 'TLeanResultType'>
|
|
107
|
+
> & ObtainSchemaGeneric<TSchema, 'TStaticMethods'>;
|
|
108
|
+
|
|
109
|
+
export function model<T>(name: string, schema?: Schema<T, any, any> | Schema<T & Document, any, any>, collection?: string, options?: CompileModelOptions): Model<T>;
|
|
110
|
+
|
|
111
|
+
export function model<T, U, TQueryHelpers = {}>(
|
|
112
|
+
name: string,
|
|
113
|
+
schema?: Schema<T, any, any, TQueryHelpers, any, any, any>,
|
|
114
|
+
collection?: string,
|
|
115
|
+
options?: CompileModelOptions
|
|
116
|
+
): U;
|
|
117
|
+
|
|
118
|
+
/** Returns an array of model names created on this instance of Mongoose. */
|
|
119
|
+
export function modelNames(): Array<string>;
|
|
120
|
+
|
|
121
|
+
/**
|
|
122
|
+
* Overwrites the current driver used by this Mongoose instance. A driver is a
|
|
123
|
+
* Mongoose-specific interface that defines functions like `find()`.
|
|
124
|
+
*/
|
|
125
|
+
export function setDriver(driver: any): Mongoose;
|
|
126
|
+
|
|
127
|
+
/** The node-mongodb-native driver Mongoose uses. */
|
|
128
|
+
export { mongodb as mongo };
|
|
129
|
+
|
|
130
|
+
/** Declares a global plugin executed on all Schemas. */
|
|
131
|
+
export function plugin(fn: (schema: Schema, opts?: any) => void, opts?: any): Mongoose;
|
|
132
|
+
|
|
133
|
+
/** Getter/setter around function for pluralizing collection names. */
|
|
134
|
+
export function pluralize(fn?: ((str: string) => string) | null): ((str: string) => string) | null;
|
|
135
|
+
|
|
136
|
+
/** Sets mongoose options */
|
|
137
|
+
export function set<K extends keyof MongooseOptions>(key: K, value: MongooseOptions[K]): Mongoose;
|
|
138
|
+
export function set(options: { [K in keyof MongooseOptions]: MongooseOptions[K] }): Mongoose;
|
|
139
|
+
|
|
140
|
+
/** The Mongoose version */
|
|
141
|
+
export const version: string;
|
|
142
|
+
|
|
143
|
+
export type AnyKeys<T> = { [P in keyof T]?: T[P] | any };
|
|
144
|
+
export interface AnyObject {
|
|
145
|
+
[k: string]: any
|
|
146
|
+
}
|
|
147
|
+
|
|
148
|
+
export type Require_id<T> = T extends { _id?: infer U }
|
|
149
|
+
? IfAny<U, T & { _id: Types.ObjectId }, T & Required<{ _id: U }>>
|
|
150
|
+
: T & { _id: Types.ObjectId };
|
|
151
|
+
|
|
152
|
+
export type Default__v<T, TSchemaOptions = {}> = TSchemaOptions extends { versionKey: false }
|
|
153
|
+
? T
|
|
154
|
+
: TSchemaOptions extends { versionKey: infer VK }
|
|
155
|
+
? (
|
|
156
|
+
// If VK is a *literal* string, add that property
|
|
157
|
+
T & {
|
|
158
|
+
[K in VK as K extends string
|
|
159
|
+
? (string extends K ? never : K) // drop if wide string
|
|
160
|
+
: never
|
|
161
|
+
]: number
|
|
162
|
+
}
|
|
163
|
+
)
|
|
164
|
+
: T extends { __v?: infer U }
|
|
165
|
+
? T
|
|
166
|
+
: T & { __v: number };
|
|
167
|
+
|
|
168
|
+
/** Helper type for getting the hydrated document type from the raw document type. The hydrated document type is what `new MyModel()` returns. */
|
|
169
|
+
export type HydratedDocument<
|
|
170
|
+
HydratedDocPathsType,
|
|
171
|
+
TOverrides = {},
|
|
172
|
+
TQueryHelpers = {},
|
|
173
|
+
TVirtuals = {},
|
|
174
|
+
RawDocType = HydratedDocPathsType,
|
|
175
|
+
TSchemaOptions = DefaultSchemaOptions
|
|
176
|
+
> = IfAny<
|
|
177
|
+
HydratedDocPathsType,
|
|
178
|
+
any,
|
|
179
|
+
TOverrides extends Record<string, never> ?
|
|
180
|
+
Document<unknown, TQueryHelpers, RawDocType, TVirtuals, TSchemaOptions> & Default__v<Require_id<HydratedDocPathsType>, TSchemaOptions> & AddDefaultId<HydratedDocPathsType, {}, TSchemaOptions> :
|
|
181
|
+
IfAny<
|
|
182
|
+
TOverrides,
|
|
183
|
+
Document<unknown, TQueryHelpers, RawDocType, TVirtuals, TSchemaOptions> & Default__v<Require_id<HydratedDocPathsType>, TSchemaOptions>,
|
|
184
|
+
Document<unknown, TQueryHelpers, RawDocType, TVirtuals, TSchemaOptions> & MergeType<
|
|
185
|
+
Default__v<Require_id<HydratedDocPathsType>, TSchemaOptions>,
|
|
186
|
+
TOverrides
|
|
187
|
+
>
|
|
188
|
+
>
|
|
189
|
+
>;
|
|
190
|
+
|
|
191
|
+
export type HydratedSingleSubdocument<
|
|
192
|
+
DocType,
|
|
193
|
+
TOverrides = {}
|
|
194
|
+
> = IfAny<
|
|
195
|
+
DocType,
|
|
196
|
+
any,
|
|
197
|
+
TOverrides extends Record<string, never> ?
|
|
198
|
+
Types.Subdocument<unknown, Record<string, never>, DocType> & Require_id<DocType> :
|
|
199
|
+
IfAny<
|
|
200
|
+
TOverrides,
|
|
201
|
+
Types.Subdocument<unknown, Record<string, never>, DocType> & Require_id<DocType>,
|
|
202
|
+
Types.Subdocument<unknown, Record<string, never>, DocType> & MergeType<
|
|
203
|
+
Require_id<DocType>,
|
|
204
|
+
TOverrides
|
|
205
|
+
>
|
|
206
|
+
>
|
|
207
|
+
>;
|
|
208
|
+
export type HydratedArraySubdocument<DocType, TOverrides = {}> = IfAny<
|
|
209
|
+
DocType,
|
|
210
|
+
any,
|
|
211
|
+
TOverrides extends Record<string, never> ?
|
|
212
|
+
Types.ArraySubdocument<unknown, Record<string, never>, DocType> & Require_id<DocType> :
|
|
213
|
+
IfAny<
|
|
214
|
+
TOverrides,
|
|
215
|
+
Types.ArraySubdocument<unknown, Record<string, never>, DocType> & Require_id<DocType>,
|
|
216
|
+
Types.ArraySubdocument<unknown, Record<string, never>, DocType> & MergeType<
|
|
217
|
+
Require_id<DocType>,
|
|
218
|
+
TOverrides
|
|
219
|
+
>
|
|
220
|
+
>
|
|
221
|
+
>;
|
|
222
|
+
|
|
223
|
+
export type HydratedDocumentFromSchema<TSchema extends Schema> = HydratedDocument<
|
|
224
|
+
InferSchemaType<TSchema>,
|
|
225
|
+
ObtainSchemaGeneric<TSchema, 'TInstanceMethods'> & ObtainSchemaGeneric<TSchema, 'TVirtuals'>,
|
|
226
|
+
ObtainSchemaGeneric<TSchema, 'TQueryHelpers'>,
|
|
227
|
+
ObtainSchemaGeneric<TSchema, 'TVirtuals'>,
|
|
228
|
+
InferSchemaType<TSchema>,
|
|
229
|
+
ObtainSchemaGeneric<TSchema, 'TSchemaOptions'>
|
|
230
|
+
>;
|
|
231
|
+
|
|
232
|
+
export interface TagSet {
|
|
233
|
+
[k: string]: string;
|
|
234
|
+
}
|
|
235
|
+
|
|
236
|
+
export interface ToObjectOptions<RawDocType = unknown, THydratedDocumentType = HydratedDocument<RawDocType>> {
|
|
237
|
+
/** if `options.virtuals = true`, you can set `options.aliases = false` to skip applying aliases. This option is a no-op if `options.virtuals = false`. */
|
|
238
|
+
aliases?: boolean;
|
|
239
|
+
/** if true, replace any conventionally populated paths with the original id in the output. Has no affect on virtual populated paths. */
|
|
240
|
+
depopulate?: boolean;
|
|
241
|
+
/** if true, convert Maps to POJOs. Useful if you want to `JSON.stringify()` the result of `toObject()`. */
|
|
242
|
+
flattenMaps?: boolean;
|
|
243
|
+
/** if true, convert any ObjectIds in the result to 24 character hex strings. */
|
|
244
|
+
flattenObjectIds?: boolean;
|
|
245
|
+
/** apply all getters (path and virtual getters) */
|
|
246
|
+
getters?: boolean;
|
|
247
|
+
/** remove empty objects (defaults to true) */
|
|
248
|
+
minimize?: boolean;
|
|
249
|
+
/** If true, the resulting object will only have fields that are defined in the document's schema. By default, `toJSON()` & `toObject()` returns all fields in the underlying document from MongoDB, including ones that are not listed in the schema. */
|
|
250
|
+
schemaFieldsOnly?: boolean;
|
|
251
|
+
/** if set, mongoose will call this function to allow you to transform the returned object */
|
|
252
|
+
transform?: boolean | ((
|
|
253
|
+
doc: THydratedDocumentType,
|
|
254
|
+
ret: Default__v<Require_id<RawDocType>>,
|
|
255
|
+
options: ToObjectOptions<THydratedDocumentType>
|
|
256
|
+
) => any);
|
|
257
|
+
/** 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. */
|
|
258
|
+
useProjection?: boolean;
|
|
259
|
+
/** if false, exclude the version key (`__v` by default) from the output */
|
|
260
|
+
versionKey?: boolean;
|
|
261
|
+
/** apply virtual getters (can override getters option) */
|
|
262
|
+
virtuals?: boolean | string[];
|
|
263
|
+
}
|
|
264
|
+
|
|
265
|
+
export type DiscriminatorModel<M, T> = T extends Model<infer T, infer TQueryHelpers, infer TInstanceMethods, infer TVirtuals>
|
|
266
|
+
?
|
|
267
|
+
M extends Model<infer M, infer MQueryHelpers, infer MInstanceMethods, infer MVirtuals>
|
|
268
|
+
? Model<Omit<M, keyof T> & T, MQueryHelpers | TQueryHelpers, MInstanceMethods | TInstanceMethods, MVirtuals | TVirtuals>
|
|
269
|
+
: M
|
|
270
|
+
: M;
|
|
271
|
+
|
|
272
|
+
export type DiscriminatorSchema<DocType, M, TInstanceMethods, TQueryHelpers, TVirtuals, TStaticMethods, DisSchema> =
|
|
273
|
+
DisSchema extends Schema<infer DisSchemaEDocType, infer DisSchemaM, infer DisSchemaInstanceMethods, infer DisSchemaQueryhelpers, infer DisSchemaVirtuals, infer DisSchemaStatics>
|
|
274
|
+
? Schema<MergeType<DocType, DisSchemaEDocType>, DiscriminatorModel<DisSchemaM, M>, DisSchemaInstanceMethods | TInstanceMethods, DisSchemaQueryhelpers | TQueryHelpers, DisSchemaVirtuals | TVirtuals, DisSchemaStatics & TStaticMethods>
|
|
275
|
+
: Schema<DocType, M, TInstanceMethods, TQueryHelpers, TVirtuals, TStaticMethods>;
|
|
276
|
+
|
|
277
|
+
type QueryResultType<T> = T extends Query<infer ResultType, any> ? ResultType : never;
|
|
278
|
+
|
|
279
|
+
type PluginFunction<
|
|
280
|
+
DocType,
|
|
281
|
+
M,
|
|
282
|
+
TInstanceMethods,
|
|
283
|
+
TQueryHelpers,
|
|
284
|
+
TVirtuals,
|
|
285
|
+
TStaticMethods> = (schema: Schema<DocType, M, TInstanceMethods, TQueryHelpers, TVirtuals, TStaticMethods>, opts?: any) => void;
|
|
286
|
+
|
|
287
|
+
export class Schema<
|
|
288
|
+
RawDocType = any,
|
|
289
|
+
TModelType = Model<RawDocType, any, any, any>,
|
|
290
|
+
TInstanceMethods = {},
|
|
291
|
+
TQueryHelpers = {},
|
|
292
|
+
TVirtuals = {},
|
|
293
|
+
TStaticMethods = {},
|
|
294
|
+
TSchemaOptions = DefaultSchemaOptions,
|
|
295
|
+
DocType extends ApplySchemaOptions<
|
|
296
|
+
ObtainDocumentType<DocType, RawDocType, ResolveSchemaOptions<TSchemaOptions>>,
|
|
297
|
+
ResolveSchemaOptions<TSchemaOptions>
|
|
298
|
+
> = ApplySchemaOptions<
|
|
299
|
+
ObtainDocumentType<any, RawDocType, ResolveSchemaOptions<TSchemaOptions>>,
|
|
300
|
+
ResolveSchemaOptions<TSchemaOptions>
|
|
301
|
+
>,
|
|
302
|
+
THydratedDocumentType = HydratedDocument<
|
|
303
|
+
DocType,
|
|
304
|
+
AddDefaultId<DocType, TVirtuals, TSchemaOptions> & TInstanceMethods,
|
|
305
|
+
TQueryHelpers,
|
|
306
|
+
AddDefaultId<DocType, TVirtuals, TSchemaOptions>,
|
|
307
|
+
IsItRecordAndNotAny<RawDocType> extends true ? RawDocType : DocType,
|
|
308
|
+
ResolveSchemaOptions<TSchemaOptions>
|
|
309
|
+
>,
|
|
310
|
+
TSchemaDefinition = SchemaDefinition<SchemaDefinitionType<RawDocType>, RawDocType, THydratedDocumentType>,
|
|
311
|
+
LeanResultType = IsItRecordAndNotAny<RawDocType> extends true ? RawDocType : Default__v<Require_id<BufferToBinary<FlattenMaps<DocType>>>>
|
|
312
|
+
>
|
|
313
|
+
extends events.EventEmitter {
|
|
314
|
+
/**
|
|
315
|
+
* Create a new schema
|
|
316
|
+
*/
|
|
317
|
+
constructor(
|
|
318
|
+
definition?: SchemaDefinition<SchemaDefinitionType<RawDocType>, RawDocType, THydratedDocumentType> | DocType,
|
|
319
|
+
options?: SchemaOptions<
|
|
320
|
+
FlatRecord<DocType>,
|
|
321
|
+
TInstanceMethods,
|
|
322
|
+
TQueryHelpers,
|
|
323
|
+
TStaticMethods,
|
|
324
|
+
TVirtuals,
|
|
325
|
+
THydratedDocumentType,
|
|
326
|
+
IfEquals<
|
|
327
|
+
TModelType,
|
|
328
|
+
Model<any, any, any, any>,
|
|
329
|
+
Model<DocType, TQueryHelpers, TInstanceMethods, TVirtuals, THydratedDocumentType>,
|
|
330
|
+
TModelType
|
|
331
|
+
>
|
|
332
|
+
> | ResolveSchemaOptions<TSchemaOptions>
|
|
333
|
+
);
|
|
334
|
+
|
|
335
|
+
/* Creates a new schema with the given definition and options. Equivalent to `new Schema(definition, options)`, but with better automatic type inference. */
|
|
336
|
+
static create<
|
|
337
|
+
TSchemaDefinition extends SchemaDefinition<undefined, RawDocType, THydratedDocumentType>,
|
|
338
|
+
TSchemaOptions extends DefaultSchemaOptions,
|
|
339
|
+
RawDocType extends ApplySchemaOptions<
|
|
340
|
+
InferRawDocType<TSchemaDefinition, ResolveSchemaOptions<TSchemaOptions>>,
|
|
341
|
+
ResolveSchemaOptions<TSchemaOptions>
|
|
342
|
+
>,
|
|
343
|
+
THydratedDocumentType extends AnyObject = HydratedDocument<
|
|
344
|
+
InferHydratedDocType<TSchemaDefinition, ResolveSchemaOptions<TSchemaOptions>>,
|
|
345
|
+
TSchemaOptions extends { methods: infer M } ? M : {},
|
|
346
|
+
TSchemaOptions extends { query: any } ? TSchemaOptions['query'] : {},
|
|
347
|
+
TSchemaOptions extends { virtuals: any } ? TSchemaOptions['virtuals'] : {},
|
|
348
|
+
RawDocType,
|
|
349
|
+
ResolveSchemaOptions<TSchemaOptions>
|
|
350
|
+
>
|
|
351
|
+
>(def: TSchemaDefinition): Schema<
|
|
352
|
+
RawDocType,
|
|
353
|
+
Model<RawDocType, any, any, any>,
|
|
354
|
+
TSchemaOptions extends { methods: infer M } ? M : {},
|
|
355
|
+
TSchemaOptions extends { query: any } ? TSchemaOptions['query'] : {},
|
|
356
|
+
TSchemaOptions extends { virtuals: any } ? TSchemaOptions['virtuals'] : {},
|
|
357
|
+
TSchemaOptions extends { statics: any } ? TSchemaOptions['statics'] : {},
|
|
358
|
+
TSchemaOptions,
|
|
359
|
+
ApplySchemaOptions<
|
|
360
|
+
ObtainDocumentType<any, RawDocType, ResolveSchemaOptions<TSchemaOptions>>,
|
|
361
|
+
ResolveSchemaOptions<TSchemaOptions>
|
|
362
|
+
>,
|
|
363
|
+
THydratedDocumentType,
|
|
364
|
+
TSchemaDefinition,
|
|
365
|
+
ApplySchemaOptions<
|
|
366
|
+
InferRawDocType<TSchemaDefinition, ResolveSchemaOptions<TSchemaOptions>, { bufferToBinary: true }>,
|
|
367
|
+
ResolveSchemaOptions<TSchemaOptions>
|
|
368
|
+
>
|
|
369
|
+
>;
|
|
370
|
+
|
|
371
|
+
static create<
|
|
372
|
+
TSchemaDefinition extends SchemaDefinition<undefined, RawDocType, THydratedDocumentType>,
|
|
373
|
+
TSchemaOptions extends SchemaOptions<InferRawDocType<TSchemaDefinition>>,
|
|
374
|
+
RawDocType extends ApplySchemaOptions<
|
|
375
|
+
InferRawDocType<TSchemaDefinition, ResolveSchemaOptions<TSchemaOptions>>,
|
|
376
|
+
ResolveSchemaOptions<TSchemaOptions>
|
|
377
|
+
>,
|
|
378
|
+
THydratedDocumentType extends AnyObject = HydratedDocument<
|
|
379
|
+
InferHydratedDocType<TSchemaDefinition, ResolveSchemaOptions<TSchemaOptions>>,
|
|
380
|
+
TSchemaOptions extends { methods: infer M } ? M : {},
|
|
381
|
+
TSchemaOptions extends { query: any } ? TSchemaOptions['query'] : {},
|
|
382
|
+
TSchemaOptions extends { virtuals: any } ? TSchemaOptions['virtuals'] : {},
|
|
383
|
+
RawDocType,
|
|
384
|
+
ResolveSchemaOptions<TSchemaOptions>
|
|
385
|
+
>
|
|
386
|
+
>(def: TSchemaDefinition, options: TSchemaOptions): Schema<
|
|
387
|
+
RawDocType,
|
|
388
|
+
Model<RawDocType, any, any, any>,
|
|
389
|
+
TSchemaOptions extends { methods: infer M } ? M : {},
|
|
390
|
+
TSchemaOptions extends { query: any } ? TSchemaOptions['query'] : {},
|
|
391
|
+
TSchemaOptions extends { virtuals: any } ? TSchemaOptions['virtuals'] : {},
|
|
392
|
+
TSchemaOptions extends { statics: any } ? TSchemaOptions['statics'] : {},
|
|
393
|
+
TSchemaOptions,
|
|
394
|
+
ApplySchemaOptions<
|
|
395
|
+
ObtainDocumentType<any, RawDocType, ResolveSchemaOptions<TSchemaOptions>>,
|
|
396
|
+
ResolveSchemaOptions<TSchemaOptions>
|
|
397
|
+
>,
|
|
398
|
+
THydratedDocumentType,
|
|
399
|
+
TSchemaDefinition,
|
|
400
|
+
ApplySchemaOptions<
|
|
401
|
+
InferRawDocType<TSchemaDefinition, ResolveSchemaOptions<TSchemaOptions>, { bufferToBinary: true }>,
|
|
402
|
+
ResolveSchemaOptions<TSchemaOptions>
|
|
403
|
+
>
|
|
404
|
+
>;
|
|
405
|
+
|
|
406
|
+
/** Adds key path / schema type pairs to this schema. */
|
|
407
|
+
add(obj: SchemaDefinition<SchemaDefinitionType<RawDocType>, RawDocType> | Schema, prefix?: string): this;
|
|
408
|
+
|
|
409
|
+
/**
|
|
410
|
+
* Add an alias for `path`. This means getting or setting the `alias`
|
|
411
|
+
* is equivalent to getting or setting the `path`.
|
|
412
|
+
*/
|
|
413
|
+
alias(path: string, alias: string | string[]): this;
|
|
414
|
+
|
|
415
|
+
/**
|
|
416
|
+
* Array of child schemas (from document arrays and single nested subdocs)
|
|
417
|
+
* and their corresponding compiled models. Each element of the array is
|
|
418
|
+
* an object with 2 properties: `schema` and `model`.
|
|
419
|
+
*/
|
|
420
|
+
childSchemas: { schema: Schema, model: any }[];
|
|
421
|
+
|
|
422
|
+
/** Removes all indexes on this schema */
|
|
423
|
+
clearIndexes(): this;
|
|
424
|
+
|
|
425
|
+
/** Returns a copy of this schema */
|
|
426
|
+
clone(): this;
|
|
427
|
+
|
|
428
|
+
discriminator<DisSchema = Schema>(name: string | number, schema: DisSchema, options?: DiscriminatorOptions): this;
|
|
429
|
+
|
|
430
|
+
/** Returns a new schema that has the picked `paths` from this schema. */
|
|
431
|
+
pick<T = this>(paths: string[], options?: SchemaOptions): T;
|
|
432
|
+
|
|
433
|
+
/** Object containing discriminators defined on this schema */
|
|
434
|
+
discriminators?: { [name: string]: Schema };
|
|
435
|
+
|
|
436
|
+
/** Iterates the schemas paths similar to Array#forEach. */
|
|
437
|
+
eachPath(fn: (path: string, type: SchemaType) => void): this;
|
|
438
|
+
|
|
439
|
+
/** Defines an index (most likely compound) for this schema. */
|
|
440
|
+
index(fields: IndexDefinition, options?: Omit<IndexOptions, 'unique'> & { unique?: boolean | [true, string] }): this;
|
|
441
|
+
|
|
442
|
+
/**
|
|
443
|
+
* Define a search index for this schema.
|
|
444
|
+
*
|
|
445
|
+
* @remarks Search indexes are only supported when used against a 7.0+ Mongo Atlas cluster.
|
|
446
|
+
*/
|
|
447
|
+
searchIndex(description: SearchIndexDescription): this;
|
|
448
|
+
|
|
449
|
+
/**
|
|
450
|
+
* Returns a list of indexes that this schema declares, via `schema.index()`
|
|
451
|
+
* or by `index: true` in a path's options.
|
|
452
|
+
*/
|
|
453
|
+
indexes(): Array<[IndexDefinition, IndexOptions]>;
|
|
454
|
+
|
|
455
|
+
/** Gets a schema option. */
|
|
456
|
+
get<K extends keyof SchemaOptions>(key: K): SchemaOptions[K];
|
|
457
|
+
|
|
458
|
+
/**
|
|
459
|
+
* 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),
|
|
460
|
+
* and [instance methods](https://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/Classes#Class_body_and_method_definitions)
|
|
461
|
+
* to schema [virtuals](http://mongoosejs.com/docs/guide.html#virtuals),
|
|
462
|
+
* [statics](http://mongoosejs.com/docs/guide.html#statics), and
|
|
463
|
+
* [methods](http://mongoosejs.com/docs/guide.html#methods).
|
|
464
|
+
*/
|
|
465
|
+
loadClass(model: Function, onlyVirtuals?: boolean): this;
|
|
466
|
+
|
|
467
|
+
/** Adds an instance method to documents constructed from Models compiled from this schema. */
|
|
468
|
+
method<Context = THydratedDocumentType>(name: string, fn: (this: Context, ...args: any[]) => any, opts?: any): this;
|
|
469
|
+
method(obj: Partial<TInstanceMethods>): this;
|
|
470
|
+
|
|
471
|
+
/** Object of currently defined methods on this schema. */
|
|
472
|
+
methods: AddThisParameter<TInstanceMethods, THydratedDocumentType> & AnyObject;
|
|
473
|
+
|
|
474
|
+
/** The original object passed to the schema constructor */
|
|
475
|
+
obj: SchemaDefinition<SchemaDefinitionType<RawDocType>, RawDocType>;
|
|
476
|
+
|
|
477
|
+
/** Returns a new schema that has the `paths` from the original schema, minus the omitted ones. */
|
|
478
|
+
omit<T = this>(paths: string[], options?: SchemaOptions): T;
|
|
479
|
+
|
|
480
|
+
options: SchemaOptions;
|
|
481
|
+
|
|
482
|
+
/** Gets/sets schema paths. */
|
|
483
|
+
path<ResultType extends SchemaType = SchemaType<any, THydratedDocumentType>>(path: string): ResultType;
|
|
484
|
+
path<pathGeneric extends keyof RawDocType>(path: pathGeneric): SchemaType<RawDocType[pathGeneric]>;
|
|
485
|
+
path(path: string, constructor: any): this;
|
|
486
|
+
|
|
487
|
+
/** Lists all paths and their type in the schema. */
|
|
488
|
+
paths: {
|
|
489
|
+
[key: string]: SchemaType;
|
|
490
|
+
};
|
|
491
|
+
|
|
492
|
+
/** Returns the pathType of `path` for this schema. */
|
|
493
|
+
pathType(path: string): string;
|
|
494
|
+
|
|
495
|
+
/** Registers a plugin for this schema. */
|
|
496
|
+
plugin<PFunc extends PluginFunction<DocType, TModelType, any, any, any, any>, POptions extends Parameters<PFunc>[1] = Parameters<PFunc>[1]>(fn: PFunc, opts?: POptions): this;
|
|
497
|
+
|
|
498
|
+
/** Defines a post hook for the model. */
|
|
499
|
+
|
|
500
|
+
// PostMiddlewareFunction
|
|
501
|
+
// with errorHandler set to true
|
|
502
|
+
post<T = Query<any, any>>(method: MongooseQueryMiddleware | MongooseQueryMiddleware[] | RegExp, options: SchemaPostOptions & { errorHandler: true }, fn: ErrorHandlingMiddlewareWithOption<T>): this;
|
|
503
|
+
post<T = THydratedDocumentType>(method: MongooseDocumentMiddleware | MongooseDocumentMiddleware[] | RegExp, options: SchemaPostOptions & { errorHandler: true }, fn: ErrorHandlingMiddlewareWithOption<T>): this;
|
|
504
|
+
post<T extends Aggregate<any>>(method: 'aggregate' | RegExp, options: SchemaPostOptions & { errorHandler: true }, fn: ErrorHandlingMiddlewareWithOption<T, Array<any>>): this;
|
|
505
|
+
post(method: 'insertMany' | RegExp, options: SchemaPostOptions & { errorHandler: true }, fn: ErrorHandlingMiddlewareWithOption<TModelType>): this;
|
|
506
|
+
|
|
507
|
+
// this = never since it never happens
|
|
508
|
+
post<T = never>(method: MongooseQueryOrDocumentMiddleware | MongooseQueryOrDocumentMiddleware[] | RegExp, options: SchemaPostOptions & { document: false, query: false }, fn: PostMiddlewareFunction<never, never>): this;
|
|
509
|
+
post<T = never>(method: MongooseDistinctQueryMiddleware|MongooseDistinctQueryMiddleware[], options: SchemaPostOptions & { document: boolean, query: false }, fn: PostMiddlewareFunction<T, QueryResultType<T>>): this;
|
|
510
|
+
post<T = never>(method: MongooseDistinctDocumentMiddleware | MongooseDistinctDocumentMiddleware[] | RegExp, options: SchemaPostOptions & { document: false, query: true }, fn: PostMiddlewareFunction<T, T>): this;
|
|
511
|
+
// this = Document
|
|
512
|
+
post<T = THydratedDocumentType>(method: MongooseDistinctDocumentMiddleware|MongooseDistinctDocumentMiddleware[], fn: PostMiddlewareFunction<T, T>): this;
|
|
513
|
+
post<T = THydratedDocumentType>(method: MongooseDistinctDocumentMiddleware|MongooseDistinctDocumentMiddleware[], options: SchemaPostOptions & SchemaPostOptions, fn: PostMiddlewareFunction<T, T>): this;
|
|
514
|
+
post<T = THydratedDocumentType>(method: MongooseQueryOrDocumentMiddleware | MongooseQueryOrDocumentMiddleware[] | RegExp, options: SchemaPostOptions & { document: true, query: false }, fn: PostMiddlewareFunction<T, T>): this;
|
|
515
|
+
post<T = THydratedDocumentType>(method: 'init', fn: PostMiddlewareFunction<T, T>): this;
|
|
516
|
+
|
|
517
|
+
// this = Query
|
|
518
|
+
post<T = Query<any, any>>(method: MongooseRawResultQueryMiddleware|MongooseRawResultQueryMiddleware[], fn: PostMiddlewareFunction<T, null | QueryResultType<T> | ModifyResult<QueryResultType<T>>>): this;
|
|
519
|
+
post<T = Query<any, any>>(method: MongooseDefaultQueryMiddleware|MongooseDefaultQueryMiddleware[], fn: PostMiddlewareFunction<T, QueryResultType<T>>): this;
|
|
520
|
+
post<T = Query<any, any>>(method: MongooseDistinctQueryMiddleware|MongooseDistinctQueryMiddleware[], options: SchemaPostOptions, fn: PostMiddlewareFunction<T, QueryResultType<T>>): this;
|
|
521
|
+
post<T = Query<any, any>>(method: MongooseQueryOrDocumentMiddleware | MongooseQueryOrDocumentMiddleware[] | RegExp, options: SchemaPostOptions & { document: false, query: true }, fn: PostMiddlewareFunction<T, QueryResultType<T>>): this;
|
|
522
|
+
// this = Union of Document and Query, could be called with any of them
|
|
523
|
+
post<T = THydratedDocumentType|Query<any, any>>(method: MongooseQueryOrDocumentMiddleware | MongooseQueryOrDocumentMiddleware[] | RegExp, options: SchemaPostOptions & { document: true, query: true }, fn: PostMiddlewareFunction<T, T|QueryResultType<T>>): this;
|
|
524
|
+
post<T = THydratedDocumentType|Query<any, any>>(method: MongooseQueryOrDocumentMiddleware | MongooseQueryOrDocumentMiddleware[] | RegExp, fn: PostMiddlewareFunction<T, T|QueryResultType<T>>): this;
|
|
525
|
+
|
|
526
|
+
// ErrorHandlingMiddlewareFunction
|
|
527
|
+
// this = never since it never happens
|
|
528
|
+
post<T = never>(method: MongooseDistinctQueryMiddleware|MongooseDistinctQueryMiddleware[], options: SchemaPostOptions & { document: boolean, query: false }, fn: ErrorHandlingMiddlewareFunction<T>): this;
|
|
529
|
+
post<T = never>(method: MongooseDistinctDocumentMiddleware|MongooseDistinctDocumentMiddleware[], options: SchemaPostOptions & { document: false, query: boolean }, fn: ErrorHandlingMiddlewareFunction<T>): this;
|
|
530
|
+
post<T = never>(method: MongooseQueryOrDocumentMiddleware | MongooseQueryOrDocumentMiddleware[] | RegExp, options: SchemaPostOptions & { document: false, query: false }, fn: ErrorHandlingMiddlewareFunction<T>): this;
|
|
531
|
+
// this = Document
|
|
532
|
+
post<T = THydratedDocumentType>(method: MongooseDistinctDocumentMiddleware|MongooseDistinctDocumentMiddleware[], fn: ErrorHandlingMiddlewareFunction<T>): this;
|
|
533
|
+
post<T = THydratedDocumentType>(method: MongooseDistinctDocumentMiddleware|MongooseDistinctDocumentMiddleware[], options: SchemaPostOptions, fn: ErrorHandlingMiddlewareFunction<T>): this;
|
|
534
|
+
post<T = THydratedDocumentType>(method: MongooseQueryOrDocumentMiddleware | MongooseQueryOrDocumentMiddleware[] | RegExp, options: SchemaPostOptions & { document: true, query: false }, fn: ErrorHandlingMiddlewareFunction<T>): this;
|
|
535
|
+
// this = Query
|
|
536
|
+
post<T = Query<any, any>>(method: MongooseDefaultQueryMiddleware|MongooseDefaultQueryMiddleware[], fn: ErrorHandlingMiddlewareFunction<T>): this;
|
|
537
|
+
post<T = Query<any, any>>(method: MongooseDistinctQueryMiddleware|MongooseDistinctQueryMiddleware[], options: SchemaPostOptions, fn: ErrorHandlingMiddlewareFunction<T>): this;
|
|
538
|
+
post<T = Query<any, any>>(method: MongooseQueryOrDocumentMiddleware | MongooseQueryOrDocumentMiddleware[] | RegExp, options: SchemaPostOptions & { document: false, query: true }, fn: ErrorHandlingMiddlewareFunction<T>): this;
|
|
539
|
+
// this = Union of Document and Query, could be called with any of them
|
|
540
|
+
post<T = THydratedDocumentType|Query<any, any>>(method: MongooseQueryOrDocumentMiddleware | MongooseQueryOrDocumentMiddleware[] | RegExp, options: SchemaPostOptions & { document: true, query: true }, fn: ErrorHandlingMiddlewareFunction<T>): this;
|
|
541
|
+
post<T = THydratedDocumentType|Query<any, any>>(method: MongooseQueryOrDocumentMiddleware | MongooseQueryOrDocumentMiddleware[] | RegExp, fn: ErrorHandlingMiddlewareFunction<T>): this;
|
|
542
|
+
|
|
543
|
+
// method aggregate and insertMany with PostMiddlewareFunction
|
|
544
|
+
post<T extends Aggregate<any>>(method: 'aggregate' | RegExp, fn: PostMiddlewareFunction<T, Array<AggregateExtract<T>>>): this;
|
|
545
|
+
post<T extends Aggregate<any>>(method: 'aggregate' | RegExp, options: SchemaPostOptions, fn: PostMiddlewareFunction<T, Array<AggregateExtract<T>>>): this;
|
|
546
|
+
post(method: 'insertMany' | RegExp, fn: PostMiddlewareFunction<TModelType, TModelType>): this;
|
|
547
|
+
post(method: 'insertMany' | RegExp, options: SchemaPostOptions, fn: PostMiddlewareFunction<TModelType, TModelType>): this;
|
|
548
|
+
|
|
549
|
+
// method aggregate and insertMany with ErrorHandlingMiddlewareFunction
|
|
550
|
+
post<T extends Aggregate<any>>(method: 'aggregate' | RegExp, fn: ErrorHandlingMiddlewareFunction<T, Array<any>>): this;
|
|
551
|
+
post<T extends Aggregate<any>>(method: 'aggregate' | RegExp, options: SchemaPostOptions, fn: ErrorHandlingMiddlewareFunction<T, Array<any>>): this;
|
|
552
|
+
post(method: 'bulkWrite' | 'createCollection' | 'insertMany' | RegExp, fn: ErrorHandlingMiddlewareFunction<TModelType>): this;
|
|
553
|
+
post(method: 'bulkWrite' | 'createCollection' | 'insertMany' | RegExp, options: SchemaPostOptions, fn: ErrorHandlingMiddlewareFunction<TModelType>): this;
|
|
554
|
+
|
|
555
|
+
/** Defines a pre hook for the model. */
|
|
556
|
+
// this = never since it never happens
|
|
557
|
+
pre<T = never>(method: 'save', options: SchemaPreOptions & { document: false, query: boolean }, fn: PreSaveMiddlewareFunction<T>): this;
|
|
558
|
+
pre<T = never>(method: MongooseQueryOrDocumentMiddleware | MongooseQueryOrDocumentMiddleware[] | RegExp, options: SchemaPreOptions & { document: false, query: false }, fn: PreMiddlewareFunction<T>): this;
|
|
559
|
+
pre<T = never>(method: MongooseDistinctQueryMiddleware|MongooseDistinctQueryMiddleware[], options: SchemaPreOptions & { document: boolean, query: false }, fn: PreMiddlewareFunction<T>): this;
|
|
560
|
+
pre<T = never>(method: MongooseDistinctDocumentMiddleware | MongooseDistinctDocumentMiddleware[] | RegExp, options: SchemaPreOptions & { document: false, query: boolean }, fn: PreMiddlewareFunction<T>): this;
|
|
561
|
+
// this = Union of Document and Query, could be called with any of them
|
|
562
|
+
pre<T = THydratedDocumentType | Query<any, any>>(
|
|
563
|
+
method: 'updateOne' | RegExp,
|
|
564
|
+
options: SchemaPreOptions & { document: true, query: true },
|
|
565
|
+
fn: PreUpdateOneMiddlewareFunction<T>
|
|
566
|
+
): this;
|
|
567
|
+
pre<T = THydratedDocumentType | Query<any, any>>(
|
|
568
|
+
method: 'deleteOne' | RegExp,
|
|
569
|
+
options: SchemaPreOptions & { document: true, query: true },
|
|
570
|
+
fn: PreDeleteOneMiddlewareFunction<T>
|
|
571
|
+
): this;
|
|
572
|
+
// this = Document
|
|
573
|
+
pre<T = THydratedDocumentType>(method: 'save', fn: PreSaveMiddlewareFunction<T>): this;
|
|
574
|
+
pre<T = THydratedDocumentType, U = RawDocType>(method: 'init', fn: (this: T, doc: U) => void): this;
|
|
575
|
+
pre<T = THydratedDocumentType>(method: MongooseDistinctDocumentMiddleware|MongooseDistinctDocumentMiddleware[], fn: PreMiddlewareFunction<T>): this;
|
|
576
|
+
pre<T = THydratedDocumentType>(method: MongooseDistinctDocumentMiddleware|MongooseDistinctDocumentMiddleware[], options: SchemaPreOptions, fn: PreMiddlewareFunction<T>): this;
|
|
577
|
+
pre<T = THydratedDocumentType>(
|
|
578
|
+
method: 'updateOne' | RegExp,
|
|
579
|
+
options: SchemaPreOptions & { document: true },
|
|
580
|
+
fn: PreUpdateOneMiddlewareFunction<T>
|
|
581
|
+
): this;
|
|
582
|
+
pre<T = THydratedDocumentType>(
|
|
583
|
+
method: 'deleteOne' | RegExp,
|
|
584
|
+
options: SchemaPreOptions & { document: true },
|
|
585
|
+
fn: PreDeleteOneMiddlewareFunction<T>
|
|
586
|
+
): this;
|
|
587
|
+
pre<T = THydratedDocumentType>(method: MongooseQueryOrDocumentMiddleware | MongooseQueryOrDocumentMiddleware[] | RegExp, options: SchemaPreOptions & { document: true, query: false }, fn: PreMiddlewareFunction<T>): this;
|
|
588
|
+
// this = Query
|
|
589
|
+
pre<T = Query<any, any>>(method: MongooseDefaultQueryMiddleware|MongooseDefaultQueryMiddleware[], fn: PreMiddlewareFunction<T>): this;
|
|
590
|
+
pre<T = Query<any, any>>(method: MongooseDistinctQueryMiddleware|MongooseDistinctQueryMiddleware[], options: SchemaPreOptions, fn: PreMiddlewareFunction<T>): this;
|
|
591
|
+
pre<T = Query<any, any>>(method: MongooseQueryOrDocumentMiddleware | MongooseQueryOrDocumentMiddleware[] | RegExp, options: SchemaPreOptions & { document: false, query: true }, fn: PreMiddlewareFunction<T>): this;
|
|
592
|
+
// this = Union of Document and Query, could be called with any of them
|
|
593
|
+
pre<T = THydratedDocumentType|Query<any, any>>(method: MongooseQueryOrDocumentMiddleware | MongooseQueryOrDocumentMiddleware[] | RegExp, options: SchemaPreOptions & { document: true, query: true }, fn: PreMiddlewareFunction<T>): this;
|
|
594
|
+
pre<T = THydratedDocumentType|Query<any, any>>(method: MongooseQueryOrDocumentMiddleware | MongooseQueryOrDocumentMiddleware[] | RegExp, fn: PreMiddlewareFunction<T>): this;
|
|
595
|
+
// method aggregate
|
|
596
|
+
pre<T extends Aggregate<any>>(method: 'aggregate' | RegExp, fn: PreMiddlewareFunction<T>): this;
|
|
597
|
+
/* method insertMany */
|
|
598
|
+
pre(
|
|
599
|
+
method: 'insertMany' | RegExp,
|
|
600
|
+
fn: (
|
|
601
|
+
this: TModelType,
|
|
602
|
+
docs: any | Array<any>,
|
|
603
|
+
options?: InsertManyOptions & { lean?: boolean }
|
|
604
|
+
) => void | Promise<void>
|
|
605
|
+
): this;
|
|
606
|
+
/* method bulkWrite */
|
|
607
|
+
pre(
|
|
608
|
+
method: 'bulkWrite' | RegExp,
|
|
609
|
+
fn: (
|
|
610
|
+
this: TModelType,
|
|
611
|
+
ops: Array<AnyBulkWriteOperation<any>>,
|
|
612
|
+
options?: mongodb.BulkWriteOptions & MongooseBulkWriteOptions
|
|
613
|
+
) => void | Promise<void>
|
|
614
|
+
): this;
|
|
615
|
+
/* method createCollection */
|
|
616
|
+
pre(
|
|
617
|
+
method: 'createCollection' | RegExp,
|
|
618
|
+
fn: (
|
|
619
|
+
this: TModelType,
|
|
620
|
+
options?: mongodb.CreateCollectionOptions & Pick<SchemaOptions, 'expires'>
|
|
621
|
+
) => void | Promise<void>
|
|
622
|
+
): this;
|
|
623
|
+
|
|
624
|
+
/** Object of currently defined query helpers on this schema. */
|
|
625
|
+
query: TQueryHelpers;
|
|
626
|
+
|
|
627
|
+
/** Adds a method call to the queue. */
|
|
628
|
+
queue(name: string, args: any[]): this;
|
|
629
|
+
|
|
630
|
+
/** Removes the given `path` (or [`paths`]). */
|
|
631
|
+
remove(paths: string | Array<string>): this;
|
|
632
|
+
|
|
633
|
+
/** Removes index by name or index spec */
|
|
634
|
+
removeIndex(index: string | AnyObject): this;
|
|
635
|
+
|
|
636
|
+
/** Returns an Array of path strings that are required by this schema. */
|
|
637
|
+
requiredPaths(invalidate?: boolean): string[];
|
|
638
|
+
|
|
639
|
+
/** Sets a schema option. */
|
|
640
|
+
set<K extends keyof SchemaOptions>(key: K, value: SchemaOptions<DocType>[K], _tags?: any): this;
|
|
641
|
+
|
|
642
|
+
/** Adds static "class" methods to Models compiled from this schema. */
|
|
643
|
+
static<K extends keyof TStaticMethods>(name: K, fn: TStaticMethods[K]): this;
|
|
644
|
+
static(obj: Partial<TStaticMethods> & { [name: string]: (this: TModelType, ...args: any[]) => any }): this;
|
|
645
|
+
static(name: string, fn: (this: TModelType, ...args: any[]) => any): this;
|
|
646
|
+
|
|
647
|
+
/** Object of currently defined statics on this schema. */
|
|
648
|
+
statics: { [F in keyof TStaticMethods]: TStaticMethods[F] } &
|
|
649
|
+
{ [name: string]: (this: TModelType, ...args: any[]) => unknown };
|
|
650
|
+
|
|
651
|
+
toJSONSchema(options?: { useBsonType?: boolean }): Record<string, any>;
|
|
652
|
+
|
|
653
|
+
/** Creates a virtual type with the given name. */
|
|
654
|
+
virtual<T = THydratedDocumentType>(
|
|
655
|
+
name: keyof TVirtuals | string,
|
|
656
|
+
options?: VirtualTypeOptions<T, DocType>
|
|
657
|
+
): VirtualType<T>;
|
|
658
|
+
|
|
659
|
+
/** Object of currently defined virtuals on this schema */
|
|
660
|
+
virtuals: TVirtuals;
|
|
661
|
+
|
|
662
|
+
/** Returns the virtual type with the given `name`. */
|
|
663
|
+
virtualpath(name: string): VirtualType<THydratedDocumentType> | null;
|
|
664
|
+
|
|
665
|
+
static ObjectId: typeof Schema.Types.ObjectId;
|
|
666
|
+
}
|
|
667
|
+
|
|
668
|
+
export type NumberSchemaDefinition = typeof Number | 'number' | 'Number' | typeof Schema.Types.Number | Schema.Types.Number;
|
|
669
|
+
export type StringSchemaDefinition = typeof String | 'string' | 'String' | typeof Schema.Types.String | Schema.Types.String;
|
|
670
|
+
export type BooleanSchemaDefinition = typeof Boolean | 'boolean' | 'Boolean' | typeof Schema.Types.Boolean | Schema.Types.Boolean;
|
|
671
|
+
export type DateSchemaDefinition = DateConstructor | 'date' | 'Date' | typeof Schema.Types.Date | Schema.Types.Date;
|
|
672
|
+
export type ObjectIdSchemaDefinition = 'ObjectId' | 'ObjectID' | typeof Schema.Types.ObjectId | Schema.Types.ObjectId | Types.ObjectId | typeof Types.ObjectId;
|
|
673
|
+
export type BufferSchemaDefinition = typeof Buffer | 'buffer' | 'Buffer' | typeof Schema.Types.Buffer;
|
|
674
|
+
export type Decimal128SchemaDefinition = 'decimal128' | 'Decimal128' | typeof Schema.Types.Decimal128 | Schema.Types.Decimal128 | Types.Decimal128;
|
|
675
|
+
export type BigintSchemaDefinition = 'bigint' | 'BigInt' | typeof Schema.Types.BigInt | Schema.Types.BigInt | typeof BigInt | BigInt;
|
|
676
|
+
export type UuidSchemaDefinition = 'uuid' | 'UUID' | typeof Schema.Types.UUID | Schema.Types.UUID;
|
|
677
|
+
export type MapSchemaDefinition = MapConstructor | 'Map' | typeof Schema.Types.Map;
|
|
678
|
+
export type UnionSchemaDefinition = 'Union' | 'union' | typeof Schema.Types.Union | Schema.Types.Union;
|
|
679
|
+
export type DoubleSchemaDefinition = 'double' | 'Double' | typeof Schema.Types.Double | Schema.Types.Double;
|
|
680
|
+
|
|
681
|
+
export type SchemaDefinitionWithBuiltInClass<T> = T extends number
|
|
682
|
+
? NumberSchemaDefinition
|
|
683
|
+
: T extends string
|
|
684
|
+
? StringSchemaDefinition
|
|
685
|
+
: T extends boolean
|
|
686
|
+
? BooleanSchemaDefinition
|
|
687
|
+
: T extends NativeDate
|
|
688
|
+
? DateSchemaDefinition
|
|
689
|
+
: (Function | string);
|
|
690
|
+
|
|
691
|
+
export type SchemaDefinitionProperty<T = undefined, EnforcedDocType = any, THydratedDocumentType = HydratedDocument<EnforcedDocType>> =
|
|
692
|
+
// ThisType intersection here avoids corrupting ThisType for SchemaTypeOptions (see test gh11828)
|
|
693
|
+
| SchemaDefinitionWithBuiltInClass<T> & ThisType<THydratedDocumentType>
|
|
694
|
+
| SchemaTypeOptions<T extends undefined ? any : T, EnforcedDocType, THydratedDocumentType>
|
|
695
|
+
| typeof SchemaType
|
|
696
|
+
| Schema<any, any, any>
|
|
697
|
+
| Schema<any, any, any>[]
|
|
698
|
+
| SchemaTypeOptions<T extends undefined ? any : Unpacked<T>, EnforcedDocType, THydratedDocumentType>[]
|
|
699
|
+
| Function[]
|
|
700
|
+
| SchemaDefinition<T, EnforcedDocType, THydratedDocumentType>
|
|
701
|
+
| SchemaDefinition<Unpacked<T>, EnforcedDocType, THydratedDocumentType>[]
|
|
702
|
+
| typeof Schema.Types.Mixed
|
|
703
|
+
| MixedSchemaTypeOptions<EnforcedDocType, THydratedDocumentType>;
|
|
704
|
+
|
|
705
|
+
export type SchemaDefinition<T = undefined, EnforcedDocType = any, THydratedDocumentType = HydratedDocument<EnforcedDocType>> = T extends undefined
|
|
706
|
+
? { [path: string]: SchemaDefinitionProperty; }
|
|
707
|
+
: { [path in keyof T]?: SchemaDefinitionProperty<T[path], EnforcedDocType, THydratedDocumentType>; };
|
|
708
|
+
|
|
709
|
+
export type AnyArray<T> = T[] | ReadonlyArray<T>;
|
|
710
|
+
export type ExtractMongooseArray<T> = T extends Types.Array<any> ? AnyArray<Unpacked<T>> : T;
|
|
711
|
+
|
|
712
|
+
export interface MixedSchemaTypeOptions<EnforcedDocType, THydratedDocumentType> extends SchemaTypeOptions<Schema.Types.Mixed, EnforcedDocType, THydratedDocumentType> {
|
|
713
|
+
type: typeof Schema.Types.Mixed;
|
|
714
|
+
}
|
|
715
|
+
|
|
716
|
+
export type RefType =
|
|
717
|
+
| number
|
|
718
|
+
| string
|
|
719
|
+
| Buffer
|
|
720
|
+
| undefined
|
|
721
|
+
| Types.ObjectId
|
|
722
|
+
| Types.Buffer
|
|
723
|
+
| typeof Schema.Types.Number
|
|
724
|
+
| typeof Schema.Types.String
|
|
725
|
+
| typeof Schema.Types.Buffer
|
|
726
|
+
| typeof Schema.Types.ObjectId
|
|
727
|
+
| typeof Schema.Types.UUID;
|
|
728
|
+
|
|
729
|
+
|
|
730
|
+
export type InferId<T> = mongodb.InferIdType<T>;
|
|
731
|
+
|
|
732
|
+
export interface VirtualTypeOptions<HydratedDocType = Document, DocType = unknown> {
|
|
733
|
+
/** If `ref` is not nullish, this becomes a populated virtual. */
|
|
734
|
+
ref?: string | Function;
|
|
735
|
+
|
|
736
|
+
/** The local field to populate on if this is a populated virtual. */
|
|
737
|
+
localField?: string | ((this: HydratedDocType, doc: HydratedDocType) => string);
|
|
738
|
+
|
|
739
|
+
/** The foreign field to populate on if this is a populated virtual. */
|
|
740
|
+
foreignField?: string | ((this: HydratedDocType, doc: HydratedDocType) => string);
|
|
741
|
+
|
|
742
|
+
/**
|
|
743
|
+
* By default, a populated virtual is an array. If you set `justOne`,
|
|
744
|
+
* the populated virtual will be a single doc or `null`.
|
|
745
|
+
*/
|
|
746
|
+
justOne?: boolean;
|
|
747
|
+
|
|
748
|
+
/** If you set this to `true`, Mongoose will call any custom getters you defined on this virtual. */
|
|
749
|
+
getters?: boolean;
|
|
750
|
+
|
|
751
|
+
/**
|
|
752
|
+
* If you set this to `true`, `populate()` will set this virtual to the number of populated
|
|
753
|
+
* documents, as opposed to the documents themselves, using `Query#countDocuments()`.
|
|
754
|
+
*/
|
|
755
|
+
count?: boolean;
|
|
756
|
+
|
|
757
|
+
/** Add an extra match condition to `populate()`. */
|
|
758
|
+
match?: QueryFilter<any> | ((doc: Record<string, any>, virtual?: this) => Record<string, any> | null);
|
|
759
|
+
|
|
760
|
+
/** Add a default `limit` to the `populate()` query. */
|
|
761
|
+
limit?: number;
|
|
762
|
+
|
|
763
|
+
/** Add a default `skip` to the `populate()` query. */
|
|
764
|
+
skip?: number;
|
|
765
|
+
|
|
766
|
+
/**
|
|
767
|
+
* For legacy reasons, `limit` with `populate()` may give incorrect results because it only
|
|
768
|
+
* executes a single query for every document being populated. If you set `perDocumentLimit`,
|
|
769
|
+
* Mongoose will ensure correct `limit` per document by executing a separate query for each
|
|
770
|
+
* document to `populate()`. For example, `.find().populate({ path: 'test', perDocumentLimit: 2 })`
|
|
771
|
+
* will execute 2 additional queries if `.find()` returns 2 documents.
|
|
772
|
+
*/
|
|
773
|
+
perDocumentLimit?: number;
|
|
774
|
+
|
|
775
|
+
/** Additional options like `limit` and `lean`. */
|
|
776
|
+
options?: QueryOptions<DocType> & { match?: AnyObject };
|
|
777
|
+
|
|
778
|
+
/** If true and the given `name` is a direct child of an array, apply the virtual to the array rather than the elements. */
|
|
779
|
+
applyToArray?: boolean;
|
|
780
|
+
|
|
781
|
+
/** Additional options for plugins */
|
|
782
|
+
[extra: string]: any;
|
|
783
|
+
}
|
|
784
|
+
|
|
785
|
+
export class VirtualType<HydratedDocType> {
|
|
786
|
+
/** Applies getters to `value`. */
|
|
787
|
+
applyGetters(value: any, doc: Document): any;
|
|
788
|
+
|
|
789
|
+
/** Applies setters to `value`. */
|
|
790
|
+
applySetters(value: any, doc: Document): any;
|
|
791
|
+
|
|
792
|
+
/** Adds a custom getter to this virtual. */
|
|
793
|
+
get<T = HydratedDocType>(fn: (this: T, value: any, virtualType: VirtualType<T>, doc: T) => any): this;
|
|
794
|
+
|
|
795
|
+
/** Adds a custom setter to this virtual. */
|
|
796
|
+
set<T = HydratedDocType>(fn: (this: T, value: any, virtualType: VirtualType<T>, doc: T) => void): this;
|
|
797
|
+
}
|
|
798
|
+
|
|
799
|
+
export type ReturnsNewDoc = { new: true } | { returnOriginal: false } | { returnDocument: 'after' };
|
|
800
|
+
|
|
801
|
+
type ArrayOperators = { $slice: number | [number, number]; $elemMatch?: never } | { $elemMatch: Record<string, any>; $slice?: never };
|
|
802
|
+
/**
|
|
803
|
+
* This Type Assigns `Element | undefined` recursively to the `T` type.
|
|
804
|
+
* if it is an array it will do this to the element of the array, if it is an object it will do this for the properties of the object.
|
|
805
|
+
* `Element` is the truthy or falsy values that are going to be used as the value of the projection.(1 | true or 0 | false)
|
|
806
|
+
* For the elements of the array we will use: `Element | `undefined` | `ArrayOperators`
|
|
807
|
+
* @example
|
|
808
|
+
* type CalculatedType = Projector<{ a: string, b: number, c: { d: string }, d: string[] }, true>
|
|
809
|
+
* type CalculatedType = {
|
|
810
|
+
a?: true | undefined;
|
|
811
|
+
b?: true | undefined;
|
|
812
|
+
c?: true | {
|
|
813
|
+
d?: true | undefined;
|
|
814
|
+
} | undefined;
|
|
815
|
+
d?: true | ArrayOperators | undefined;
|
|
816
|
+
}
|
|
817
|
+
*/
|
|
818
|
+
type Projector<T, Element> = T extends Array<infer U>
|
|
819
|
+
? Projector<U, Element> | ArrayOperators
|
|
820
|
+
: T extends TreatAsPrimitives
|
|
821
|
+
? Element
|
|
822
|
+
: T extends Record<string, any>
|
|
823
|
+
? {
|
|
824
|
+
[K in keyof T]?: T[K] extends Record<string, any> ? Projector<T[K], Element> | Element : Element;
|
|
825
|
+
}
|
|
826
|
+
: Element;
|
|
827
|
+
type _IDType = { _id?: boolean | number };
|
|
828
|
+
export type InclusionProjection<T> = IsItRecordAndNotAny<T> extends true
|
|
829
|
+
? Omit<Projector<WithLevel1NestedPaths<T>, boolean | number>, '_id'> & _IDType
|
|
830
|
+
: AnyObject;
|
|
831
|
+
export type ExclusionProjection<T> = IsItRecordAndNotAny<T> extends true
|
|
832
|
+
? Omit<Projector<WithLevel1NestedPaths<T>, false | 0>, '_id'> & _IDType
|
|
833
|
+
: AnyObject;
|
|
834
|
+
|
|
835
|
+
export type ProjectionType<T> = (InclusionProjection<T> & AnyObject)
|
|
836
|
+
| (ExclusionProjection<T> & AnyObject)
|
|
837
|
+
| string;
|
|
838
|
+
export type SortValues = SortOrder;
|
|
839
|
+
|
|
840
|
+
export type SortOrder = -1 | 1 | 'asc' | 'ascending' | 'desc' | 'descending';
|
|
841
|
+
|
|
842
|
+
type _UpdateQuery<TSchema, AdditionalProperties = AnyObject> = {
|
|
843
|
+
/** @see https://www.mongodb.com/docs/manual/reference/operator/update-field/ */
|
|
844
|
+
$currentDate?: AnyKeys<TSchema> & AdditionalProperties;
|
|
845
|
+
$inc?: AnyKeys<TSchema> & AdditionalProperties;
|
|
846
|
+
$min?: AnyKeys<TSchema> & AdditionalProperties;
|
|
847
|
+
$max?: AnyKeys<TSchema> & AdditionalProperties;
|
|
848
|
+
$mul?: AnyKeys<TSchema> & AdditionalProperties;
|
|
849
|
+
$rename?: Record<string, string>;
|
|
850
|
+
$set?: AnyKeys<TSchema> & AdditionalProperties;
|
|
851
|
+
$setOnInsert?: AnyKeys<TSchema> & AdditionalProperties;
|
|
852
|
+
$unset?: AnyKeys<TSchema> & AdditionalProperties;
|
|
853
|
+
|
|
854
|
+
/** @see https://www.mongodb.com/docs/manual/reference/operator/update-array/ */
|
|
855
|
+
$addToSet?: AnyKeys<TSchema> & AdditionalProperties;
|
|
856
|
+
$pop?: AnyKeys<TSchema> & AdditionalProperties;
|
|
857
|
+
$pull?: AnyKeys<TSchema> & AdditionalProperties;
|
|
858
|
+
$push?: AnyKeys<TSchema> & AdditionalProperties;
|
|
859
|
+
$pullAll?: AnyKeys<TSchema> & AdditionalProperties;
|
|
860
|
+
|
|
861
|
+
/** @see https://www.mongodb.com/docs/manual/reference/operator/update-bitwise/ */
|
|
862
|
+
$bit?: AnyKeys<TSchema>;
|
|
863
|
+
};
|
|
864
|
+
|
|
865
|
+
export type UpdateWithAggregationPipeline = UpdateAggregationStage[];
|
|
866
|
+
export type UpdateAggregationStage = { $addFields: any } |
|
|
867
|
+
{ $set: any } |
|
|
868
|
+
{ $project: any } |
|
|
869
|
+
{ $unset: any } |
|
|
870
|
+
{ $replaceRoot: any } |
|
|
871
|
+
{ $replaceWith: any };
|
|
872
|
+
|
|
873
|
+
/**
|
|
874
|
+
* Update query command to perform on the document
|
|
875
|
+
* @example
|
|
876
|
+
* ```js
|
|
877
|
+
* { age: 30 }
|
|
878
|
+
* ```
|
|
879
|
+
*/
|
|
880
|
+
export type UpdateQuery<T> = AnyKeys<T> & _UpdateQuery<T> & AnyObject;
|
|
881
|
+
|
|
882
|
+
/**
|
|
883
|
+
* A more strict form of UpdateQuery that enforces updating only
|
|
884
|
+
* known top-level properties.
|
|
885
|
+
* @example
|
|
886
|
+
* ```ts
|
|
887
|
+
* function updateUser(_id: mongoose.Types.ObjectId, update: UpdateQueryKnownOnly<IUser>) {
|
|
888
|
+
* return User.updateOne({ _id }, update);
|
|
889
|
+
* }
|
|
890
|
+
* ```
|
|
891
|
+
*/
|
|
892
|
+
export type UpdateQueryKnownOnly<T> = _UpdateQuery<T, {}>;
|
|
893
|
+
|
|
894
|
+
export type FlattenMaps<T> = {
|
|
895
|
+
[K in keyof T]: FlattenProperty<T[K]>;
|
|
896
|
+
};
|
|
897
|
+
|
|
898
|
+
export type BufferToBinaryProperty<T> = unknown extends Buffer
|
|
899
|
+
? T
|
|
900
|
+
: T extends Buffer
|
|
901
|
+
? mongodb.Binary
|
|
902
|
+
: T extends Types.DocumentArray<infer ItemType>
|
|
903
|
+
? Types.DocumentArray<BufferToBinary<ItemType>>
|
|
904
|
+
: T extends Types.Subdocument<unknown, unknown, infer SubdocType>
|
|
905
|
+
? HydratedSingleSubdocument<BufferToBinary<SubdocType>>
|
|
906
|
+
: BufferToBinary<T>;
|
|
907
|
+
|
|
908
|
+
/**
|
|
909
|
+
* Converts any Buffer properties into mongodb.Binary instances, which is what `lean()` returns
|
|
910
|
+
*/
|
|
911
|
+
export type BufferToBinary<T> = unknown extends Buffer
|
|
912
|
+
? T
|
|
913
|
+
: T extends Buffer
|
|
914
|
+
? mongodb.Binary
|
|
915
|
+
: T extends Document
|
|
916
|
+
? T
|
|
917
|
+
: T extends TreatAsPrimitives
|
|
918
|
+
? T
|
|
919
|
+
: T extends Record<string, any>
|
|
920
|
+
? {
|
|
921
|
+
[K in keyof T]: BufferToBinaryProperty<T[K]>
|
|
922
|
+
}
|
|
923
|
+
: T;
|
|
924
|
+
|
|
925
|
+
/**
|
|
926
|
+
* Converts any Buffer properties into { type: 'buffer', data: [1, 2, 3] } format for JSON serialization
|
|
927
|
+
*/
|
|
928
|
+
export type BufferToJSON<T> = unknown extends Buffer
|
|
929
|
+
? T
|
|
930
|
+
: T extends Buffer
|
|
931
|
+
? { type: 'buffer', data: number[] }
|
|
932
|
+
: T extends Document
|
|
933
|
+
? T
|
|
934
|
+
: T extends TreatAsPrimitives
|
|
935
|
+
? T
|
|
936
|
+
: T extends Record<string, any> ? {
|
|
937
|
+
[K in keyof T]: T[K] extends Buffer
|
|
938
|
+
? { type: 'buffer', data: number[] }
|
|
939
|
+
: T[K] extends Types.DocumentArray<infer ItemType>
|
|
940
|
+
? Types.DocumentArray<BufferToBinary<ItemType>>
|
|
941
|
+
: T[K] extends Types.Subdocument<unknown, unknown, infer SubdocType>
|
|
942
|
+
? HydratedSingleSubdocument<SubdocType>
|
|
943
|
+
: BufferToBinary<T[K]>;
|
|
944
|
+
} : T;
|
|
945
|
+
|
|
946
|
+
/**
|
|
947
|
+
* Converts any Buffer properties into "{ type: 'buffer', data: [1, 2, 3] }" format for JSON serialization
|
|
948
|
+
*/
|
|
949
|
+
export type UUIDToJSON<T> = T extends mongodb.UUID
|
|
950
|
+
? string
|
|
951
|
+
: T extends Document
|
|
952
|
+
? T
|
|
953
|
+
: T extends TreatAsPrimitives
|
|
954
|
+
? T
|
|
955
|
+
: T extends Record<string, any> ? {
|
|
956
|
+
[K in keyof T]: T[K] extends mongodb.UUID
|
|
957
|
+
? string
|
|
958
|
+
: T[K] extends Types.DocumentArray<infer ItemType>
|
|
959
|
+
? Types.DocumentArray<UUIDToJSON<ItemType>>
|
|
960
|
+
: T[K] extends Types.Subdocument<unknown, unknown, infer SubdocType>
|
|
961
|
+
? HydratedSingleSubdocument<SubdocType>
|
|
962
|
+
: UUIDToJSON<T[K]>;
|
|
963
|
+
} : T;
|
|
964
|
+
|
|
965
|
+
/**
|
|
966
|
+
* Converts any ObjectId properties into strings for JSON serialization
|
|
967
|
+
*/
|
|
968
|
+
export type ObjectIdToString<T> = T extends mongodb.ObjectId
|
|
969
|
+
? string
|
|
970
|
+
: T extends Document
|
|
971
|
+
? T
|
|
972
|
+
: T extends TreatAsPrimitives
|
|
973
|
+
? T
|
|
974
|
+
: T extends Record<string, any> ? {
|
|
975
|
+
[K in keyof T]: T[K] extends mongodb.ObjectId
|
|
976
|
+
? string
|
|
977
|
+
: T[K] extends Types.DocumentArray<infer ItemType>
|
|
978
|
+
? Types.DocumentArray<ObjectIdToString<ItemType>>
|
|
979
|
+
: T[K] extends Types.Subdocument<unknown, unknown, infer SubdocType>
|
|
980
|
+
? HydratedSingleSubdocument<ObjectIdToString<SubdocType>>
|
|
981
|
+
: ObjectIdToString<T[K]>;
|
|
982
|
+
} : T;
|
|
983
|
+
|
|
984
|
+
/**
|
|
985
|
+
* Converts any Date properties into strings for JSON serialization
|
|
986
|
+
*/
|
|
987
|
+
export type DateToString<T> = T extends NativeDate
|
|
988
|
+
? string
|
|
989
|
+
: T extends Document
|
|
990
|
+
? T
|
|
991
|
+
: T extends TreatAsPrimitives
|
|
992
|
+
? T
|
|
993
|
+
: T extends Record<string, any> ? {
|
|
994
|
+
[K in keyof T]: T[K] extends NativeDate
|
|
995
|
+
? string
|
|
996
|
+
: T[K] extends (NativeDate | null | undefined)
|
|
997
|
+
? string | null | undefined
|
|
998
|
+
: T[K] extends Types.DocumentArray<infer ItemType>
|
|
999
|
+
? Types.DocumentArray<DateToString<ItemType>>
|
|
1000
|
+
: T[K] extends Types.Subdocument<unknown, unknown, infer SubdocType>
|
|
1001
|
+
? HydratedSingleSubdocument<DateToString<SubdocType>>
|
|
1002
|
+
: DateToString<T[K]>;
|
|
1003
|
+
} : T;
|
|
1004
|
+
|
|
1005
|
+
/**
|
|
1006
|
+
* Converts any Mongoose subdocuments (single nested or doc arrays) into POJO equivalents
|
|
1007
|
+
*/
|
|
1008
|
+
export type SubdocsToPOJOs<T> = T extends Document
|
|
1009
|
+
? T
|
|
1010
|
+
: T extends TreatAsPrimitives
|
|
1011
|
+
? T
|
|
1012
|
+
: T extends Record<string, any> ? {
|
|
1013
|
+
[K in keyof T]: T[K] extends Types.DocumentArray<infer ItemType>
|
|
1014
|
+
? ItemType[]
|
|
1015
|
+
: T[K] extends Types.Subdocument<unknown, unknown, infer SubdocType>
|
|
1016
|
+
? SubdocType
|
|
1017
|
+
: SubdocsToPOJOs<T[K]>;
|
|
1018
|
+
} : T;
|
|
1019
|
+
|
|
1020
|
+
export type JSONSerialized<T> = SubdocsToPOJOs<
|
|
1021
|
+
FlattenMaps<
|
|
1022
|
+
BufferToJSON<
|
|
1023
|
+
ObjectIdToString<
|
|
1024
|
+
UUIDToJSON<
|
|
1025
|
+
DateToString<T>
|
|
1026
|
+
>
|
|
1027
|
+
>
|
|
1028
|
+
>
|
|
1029
|
+
>
|
|
1030
|
+
>;
|
|
1031
|
+
|
|
1032
|
+
/**
|
|
1033
|
+
* Separate type is needed for properties of union type (for example, Types.DocumentArray | undefined) to apply conditional check to each member of it
|
|
1034
|
+
* https://www.typescriptlang.org/docs/handbook/2/conditional-types.html#distributive-conditional-types
|
|
1035
|
+
*/
|
|
1036
|
+
type FlattenProperty<T> = T extends Map<any, infer V>
|
|
1037
|
+
? Record<string, V> : T extends TreatAsPrimitives
|
|
1038
|
+
? T : T extends Document ? T : T extends Types.DocumentArray<infer ItemType>
|
|
1039
|
+
? Types.DocumentArray<FlattenMaps<ItemType>> : FlattenMaps<T>;
|
|
1040
|
+
|
|
1041
|
+
export type actualPrimitives = string | boolean | number | bigint | symbol | null | undefined;
|
|
1042
|
+
export type TreatAsPrimitives = actualPrimitives | NativeDate | RegExp | symbol | Error | BigInt | Types.ObjectId | Buffer | Function | mongodb.Binary | mongodb.ClientSession;
|
|
1043
|
+
|
|
1044
|
+
export type SchemaDefinitionType<T> = T extends Document ? Omit<T, Exclude<keyof Document, '_id' | 'id' | '__v'>> : T;
|
|
1045
|
+
|
|
1046
|
+
/* for ts-mongoose */
|
|
1047
|
+
export class mquery { }
|
|
1048
|
+
|
|
1049
|
+
export function overwriteMiddlewareResult(val: any): Kareem.OverwriteMiddlewareResult;
|
|
1050
|
+
|
|
1051
|
+
export function skipMiddlewareFunction(val: any): Kareem.SkipWrappedFunction;
|
|
1052
|
+
|
|
1053
|
+
export function overwriteMiddlewareArguments(val: any): Kareem.OverwriteArguments;
|
|
1054
|
+
|
|
1055
|
+
export default mongoose;
|
|
1056
|
+
}
|