mongoose 6.2.7 → 6.2.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.
Files changed (59) hide show
  1. package/.eslintrc.json +35 -26
  2. package/CHANGELOG.md +27 -0
  3. package/dist/browser.umd.js +71 -71
  4. package/lib/aggregate.js +36 -36
  5. package/lib/browser.js +4 -4
  6. package/lib/browserDocument.js +1 -0
  7. package/lib/connection.js +21 -21
  8. package/lib/cursor/AggregationCursor.js +2 -3
  9. package/lib/cursor/QueryCursor.js +3 -3
  10. package/lib/document.js +75 -47
  11. package/lib/error/index.js +2 -2
  12. package/lib/helpers/document/handleSpreadDoc.js +19 -1
  13. package/lib/helpers/populate/markArraySubdocsPopulated.js +1 -1
  14. package/lib/helpers/projection/hasIncludedChildren.js +1 -1
  15. package/lib/helpers/query/castFilterPath.js +0 -1
  16. package/lib/index.js +24 -26
  17. package/lib/model.js +141 -136
  18. package/lib/options/SchemaArrayOptions.js +2 -2
  19. package/lib/options/SchemaBufferOptions.js +1 -1
  20. package/lib/options/SchemaDateOptions.js +9 -2
  21. package/lib/options/SchemaDocumentArrayOptions.js +3 -3
  22. package/lib/options/SchemaMapOptions.js +2 -2
  23. package/lib/options/SchemaNumberOptions.js +3 -3
  24. package/lib/options/SchemaObjectIdOptions.js +2 -2
  25. package/lib/options/SchemaStringOptions.js +1 -1
  26. package/lib/options/SchemaSubdocumentOptions.js +2 -2
  27. package/lib/options/SchemaTypeOptions.js +3 -3
  28. package/lib/query.js +253 -225
  29. package/lib/schema/SubdocumentPath.js +6 -3
  30. package/lib/schema/array.js +3 -2
  31. package/lib/schema/boolean.js +4 -4
  32. package/lib/schema/buffer.js +3 -3
  33. package/lib/schema/date.js +7 -7
  34. package/lib/schema/decimal128.js +2 -2
  35. package/lib/schema/documentarray.js +17 -10
  36. package/lib/schema/mixed.js +2 -2
  37. package/lib/schema/number.js +6 -6
  38. package/lib/schema/objectid.js +4 -4
  39. package/lib/schema/string.js +14 -14
  40. package/lib/schema.js +28 -28
  41. package/lib/schematype.js +78 -68
  42. package/lib/types/ArraySubdocument.js +1 -1
  43. package/lib/types/DocumentArray/methods/index.js +2 -2
  44. package/lib/types/array/index.js +1 -1
  45. package/lib/types/array/methods/index.js +14 -12
  46. package/lib/types/buffer.js +1 -1
  47. package/lib/types/decimal128.js +1 -1
  48. package/lib/types/objectid.js +1 -1
  49. package/lib/types/subdocument.js +2 -2
  50. package/lib/virtualtype.js +4 -3
  51. package/package.json +18 -17
  52. package/tools/repl.js +8 -8
  53. package/tools/sharded.js +3 -3
  54. package/types/connection.d.ts +116 -116
  55. package/types/document.d.ts +3 -0
  56. package/types/error.d.ts +2 -2
  57. package/types/index.d.ts +75 -67
  58. package/types/pipelinestage.d.ts +194 -194
  59. package/types/schemaoptions.d.ts +2 -2
package/types/index.d.ts CHANGED
@@ -178,7 +178,7 @@ declare module 'mongoose' {
178
178
  // eslint-disable-next-line @typescript-eslint/no-empty-interface
179
179
  interface ClientSession extends mongodb.ClientSession { }
180
180
 
181
- /*
181
+ /*
182
182
  * section collection.js
183
183
  * http://mongoosejs.com/docs/api.html#collection-js
184
184
  */
@@ -231,7 +231,9 @@ declare module 'mongoose' {
231
231
  }
232
232
 
233
233
  type AnyKeys<T> = { [P in keyof T]?: T[P] | any };
234
- interface AnyObject { [k: string]: any }
234
+ interface AnyObject {
235
+ [k: string]: any
236
+ }
235
237
 
236
238
  type Require_id<T> = T extends { _id?: any } ? (T & { _id: T['_id'] }) : (T & { _id: Types.ObjectId });
237
239
 
@@ -246,7 +248,11 @@ declare module 'mongoose' {
246
248
 
247
249
  interface ModifyResult<T> {
248
250
  value: Require_id<T> | null;
249
- lastErrorObject?: mongodb.Document;
251
+ /** see https://www.mongodb.com/docs/manual/reference/command/findAndModify/#lasterrorobject */
252
+ lastErrorObject?: {
253
+ updatedExisting?: boolean;
254
+ upserted?: mongodb.ObjectId;
255
+ };
250
256
  ok: 0 | 1;
251
257
  }
252
258
 
@@ -709,6 +715,8 @@ declare module 'mongoose' {
709
715
  model?: string | Model<any>;
710
716
  /** optional query options like sort, limit, etc */
711
717
  options?: any;
718
+ /** optional boolean, set to `false` to allow populating paths that aren't in the schema */
719
+ strictPopulate?: boolean;
712
720
  /** deep populate */
713
721
  populate?: string | PopulateOptions | (string | PopulateOptions)[];
714
722
  /**
@@ -747,8 +755,6 @@ declare module 'mongoose' {
747
755
  type SchemaPreOptions = { document?: boolean, query?: boolean };
748
756
  type SchemaPostOptions = { document?: boolean, query?: boolean };
749
757
 
750
- type ExtractVirtuals<M> = M extends Model<any, any, any, infer TVirtuals> ? TVirtuals : {};
751
-
752
758
  type IndexDirection = 1 | -1 | '2d' | '2dsphere' | 'geoHaystack' | 'hashed' | 'text';
753
759
  type IndexDefinition = Record<string, IndexDirection>;
754
760
 
@@ -757,7 +763,7 @@ declare module 'mongoose' {
757
763
  export type PostMiddlewareFunction<ThisType = any, ResType = any> = (this: ThisType, res: ResType, next: CallbackWithoutResultAndOptionalError) => void | Promise<void>;
758
764
  export type ErrorHandlingMiddlewareFunction<ThisType = any, ResType = any> = (this: ThisType, err: NativeError, res: ResType, next: CallbackWithoutResultAndOptionalError) => void;
759
765
 
760
- class Schema<DocType = any, M = Model<DocType, any, any, any>, TInstanceMethods = any, TQueryHelpers = any> extends events.EventEmitter {
766
+ class Schema<DocType = any, M = Model<DocType, any, any, any>, TInstanceMethods = {}, TQueryHelpers = {}> extends events.EventEmitter {
761
767
  /**
762
768
  * Create a new schema
763
769
  */
@@ -811,7 +817,7 @@ declare module 'mongoose' {
811
817
  method(obj: Partial<TInstanceMethods>): this;
812
818
 
813
819
  /** Object of currently defined methods on this schema. */
814
- methods: { [F in keyof TInstanceMethods]: TInstanceMethods[F] };
820
+ methods: { [F in keyof TInstanceMethods]: TInstanceMethods[F] } & AnyObject;
815
821
 
816
822
  /** The original object passed to the schema constructor */
817
823
  obj: SchemaDefinition<SchemaDefinitionType<DocType>>;
@@ -904,22 +910,22 @@ declare module 'mongoose' {
904
910
  type SchemaDefinitionWithBuiltInClass<T> = T extends number
905
911
  ? NumberSchemaDefinition
906
912
  : T extends string
907
- ? StringSchemaDefinition
908
- : T extends boolean
909
- ? BooleanSchemaDefinition
910
- : T extends NativeDate
911
- ? DateSchemaDefinition
912
- : (Function | string);
913
+ ? StringSchemaDefinition
914
+ : T extends boolean
915
+ ? BooleanSchemaDefinition
916
+ : T extends NativeDate
917
+ ? DateSchemaDefinition
918
+ : (Function | string);
913
919
 
914
920
  type SchemaDefinitionProperty<T = undefined> = SchemaDefinitionWithBuiltInClass<T> |
915
- SchemaTypeOptions<T extends undefined ? any : T> |
921
+ SchemaTypeOptions<T extends undefined ? any : T> |
916
922
  typeof SchemaType |
917
- Schema<any, any, any> |
918
- Schema<any, any, any>[] |
919
- SchemaTypeOptions<T extends undefined ? any : Unpacked<T>>[] |
920
- Function[] |
921
- SchemaDefinition<T> |
922
- SchemaDefinition<Unpacked<T>>[] |
923
+ Schema<any, any, any> |
924
+ Schema<any, any, any>[] |
925
+ SchemaTypeOptions<T extends undefined ? any : Unpacked<T>>[] |
926
+ Function[] |
927
+ SchemaDefinition<T> |
928
+ SchemaDefinition<Unpacked<T>>[] |
923
929
  typeof SchemaTypes.Mixed;
924
930
 
925
931
  type SchemaDefinition<T = undefined> = T extends undefined
@@ -933,22 +939,24 @@ declare module 'mongoose' {
933
939
  type AnyArray<T> = T[] | ReadonlyArray<T>;
934
940
  type SchemaValidator<T> = RegExp | [RegExp, string] | Function | [Function, string] | ValidateOpts<T> | ValidateOpts<T>[];
935
941
 
942
+ type ExtractMongooseArray<T> = T extends Types.Array<any> ? AnyArray<Unpacked<T>> : T;
943
+
936
944
  export class SchemaTypeOptions<T> {
937
945
  type?:
938
- T extends string ? StringSchemaDefinition :
946
+ T extends string ? StringSchemaDefinition :
939
947
  T extends number ? NumberSchemaDefinition :
940
- T extends boolean ? BooleanSchemaDefinition :
941
- T extends NativeDate ? DateSchemaDefinition :
942
- T extends Map<any, any> ? SchemaDefinition<typeof Map> :
943
- T extends Buffer ? SchemaDefinition<typeof Buffer> :
944
- T extends Types.ObjectId ? ObjectIdSchemaDefinition :
945
- T extends Types.ObjectId[] ? AnyArray<ObjectIdSchemaDefinition> | AnyArray<SchemaTypeOptions<ObjectId>> :
946
- T extends object[] ? (AnyArray<Schema<any, any, any>> | AnyArray<SchemaDefinition<Unpacked<T>>> | AnyArray<SchemaTypeOptions<Unpacked<T>>>) :
947
- T extends string[] ? AnyArray<StringSchemaDefinition> | AnyArray<SchemaTypeOptions<string>> :
948
- T extends number[] ? AnyArray<NumberSchemaDefinition> | AnyArray<SchemaTypeOptions<number>> :
949
- T extends boolean[] ? AnyArray<BooleanSchemaDefinition> | AnyArray<SchemaTypeOptions<boolean>> :
950
- T extends Function[] ? AnyArray<Function | string> | AnyArray<SchemaTypeOptions<Unpacked<T>>> :
951
- T | typeof SchemaType | Schema<any, any, any> | SchemaDefinition<T> | Function | AnyArray<Function>;
948
+ T extends boolean ? BooleanSchemaDefinition :
949
+ T extends NativeDate ? DateSchemaDefinition :
950
+ T extends Map<any, any> ? SchemaDefinition<typeof Map> :
951
+ T extends Buffer ? SchemaDefinition<typeof Buffer> :
952
+ T extends Types.ObjectId ? ObjectIdSchemaDefinition :
953
+ T extends Types.ObjectId[] ? AnyArray<ObjectIdSchemaDefinition> | AnyArray<SchemaTypeOptions<ObjectId>> :
954
+ T extends object[] ? (AnyArray<Schema<any, any, any>> | AnyArray<SchemaDefinition<Unpacked<T>>> | AnyArray<SchemaTypeOptions<Unpacked<T>>>) :
955
+ T extends string[] ? AnyArray<StringSchemaDefinition> | AnyArray<SchemaTypeOptions<string>> :
956
+ T extends number[] ? AnyArray<NumberSchemaDefinition> | AnyArray<SchemaTypeOptions<number>> :
957
+ T extends boolean[] ? AnyArray<BooleanSchemaDefinition> | AnyArray<SchemaTypeOptions<boolean>> :
958
+ T extends Function[] ? AnyArray<Function | string> | AnyArray<SchemaTypeOptions<Unpacked<T>>> :
959
+ T | typeof SchemaType | Schema<any, any, any> | SchemaDefinition<T> | Function | AnyArray<Function>;
952
960
 
953
961
  /** Defines a virtual with the given name that gets/sets this path. */
954
962
  alias?: string;
@@ -970,7 +978,7 @@ declare module 'mongoose' {
970
978
  * The default value for this path. If a function, Mongoose executes the function
971
979
  * and uses the return value as the default.
972
980
  */
973
- default?: T | ((this: any, doc: any) => Partial<T>);
981
+ default?: ExtractMongooseArray<T> | ((this: any, doc: any) => Partial<ExtractMongooseArray<T>>);
974
982
 
975
983
  /**
976
984
  * The model that `populate()` should use if populating this path.
@@ -1093,7 +1101,7 @@ declare module 'mongoose' {
1093
1101
  export type PopulatedDoc<
1094
1102
  PopulatedType,
1095
1103
  RawId extends RefType = (PopulatedType extends { _id?: RefType; } ? NonNullable<PopulatedType['_id']> : mongoose.Types.ObjectId) | undefined
1096
- > = PopulatedType | RawId;
1104
+ > = PopulatedType | RawId;
1097
1105
 
1098
1106
  interface IndexOptions extends mongodb.CreateIndexesOptions {
1099
1107
  /**
@@ -1461,17 +1469,17 @@ declare module 'mongoose' {
1461
1469
  }
1462
1470
  }
1463
1471
 
1464
- type ReturnsNewDoc = { new: true } | { returnOriginal: false } | {returnDocument: 'after'};
1472
+ type ReturnsNewDoc = { new: true } | { returnOriginal: false } | { returnDocument: 'after' };
1465
1473
 
1466
1474
  type QueryWithHelpers<ResultType, DocType, THelpers = {}, RawDocType = DocType> = Query<ResultType, DocType, THelpers, RawDocType> & THelpers;
1467
1475
 
1468
- type UnpackedIntersection<T, U> = T extends (infer A)[]
1476
+ type UnpackedIntersection<T, U> = T extends null ? null : T extends (infer A)[]
1469
1477
  ? (Omit<A, keyof U> & U)[]
1470
1478
  : keyof U extends never
1471
- ? T
1472
- : Omit<T, keyof U> & U;
1479
+ ? T
1480
+ : Omit<T, keyof U> & U;
1473
1481
 
1474
- type ProjectionFields<DocType> = {[Key in keyof Omit<LeanDocument<DocType>, '__v'>]?: any} & Record<string, any>;
1482
+ type ProjectionFields<DocType> = { [Key in keyof Omit<LeanDocument<DocType>, '__v'>]?: any } & Record<string, any>;
1475
1483
 
1476
1484
  class Query<ResultType, DocType, THelpers = {}, RawDocType = DocType> {
1477
1485
  _mongooseOptions: MongooseQueryOptions;
@@ -1770,8 +1778,8 @@ declare module 'mongoose' {
1770
1778
  polygon(path: string, ...coordinatePairs: number[][]): this;
1771
1779
 
1772
1780
  /** Specifies paths which should be populated with other documents. */
1773
- populate<Paths = {}>(path: string | string[], select?: string | any, model?: string | Model<any, THelpers>, match?: any): QueryWithHelpers<UnpackedIntersection<ResultType, Paths>, DocType, THelpers, RawDocType>;
1774
- populate<Paths = {}>(options: PopulateOptions | (PopulateOptions | string)[]): QueryWithHelpers<UnpackedIntersection<ResultType, Paths>, DocType, THelpers, RawDocType>;
1781
+ populate<Paths = {}>(path: string | string[], select?: string | any, model?: string | Model<any, THelpers>, match?: any): QueryWithHelpers<UnpackedIntersection<ResultType, Paths>, DocType, THelpers, UnpackedIntersection<RawDocType, Paths>>;
1782
+ populate<Paths = {}>(options: PopulateOptions | (PopulateOptions | string)[]): QueryWithHelpers<UnpackedIntersection<ResultType, Paths>, DocType, THelpers, UnpackedIntersection<RawDocType, Paths>>;
1775
1783
 
1776
1784
  /** Get/set the current projection (AKA fields). Pass `null` to remove the current projection. */
1777
1785
  projection(): ProjectionFields<DocType> | null;
@@ -1961,10 +1969,10 @@ declare module 'mongoose' {
1961
1969
  $or?: Array<FilterQuery<T>>;
1962
1970
  /** @see https://docs.mongodb.com/manual/reference/operator/query/text */
1963
1971
  $text?: {
1964
- $search: string;
1965
- $language?: string;
1966
- $caseSensitive?: boolean;
1967
- $diacriticSensitive?: boolean;
1972
+ $search: string;
1973
+ $language?: string;
1974
+ $caseSensitive?: boolean;
1975
+ $diacriticSensitive?: boolean;
1968
1976
  };
1969
1977
  /** @see https://docs.mongodb.com/manual/reference/operator/query/where/#op._S_where */
1970
1978
  $where?: string | Function;
@@ -1981,7 +1989,7 @@ declare module 'mongoose' {
1981
1989
  type _FilterQuery<T> = {
1982
1990
  [P in keyof T]?: Condition<T[P]>;
1983
1991
  } &
1984
- RootQuerySelector<T>;
1992
+ RootQuerySelector<T>;
1985
1993
 
1986
1994
  /**
1987
1995
  * Filter query to select the documents that match the query
@@ -2028,21 +2036,21 @@ declare module 'mongoose' {
2028
2036
 
2029
2037
  /** @see https://docs.mongodb.com/manual/reference/operator/update-bitwise/ */
2030
2038
  $bit?: {
2031
- [key: string]: { [key in 'and' | 'or' | 'xor']?: number };
2039
+ [key: string]: { [key in 'and' | 'or' | 'xor']?: number };
2032
2040
  };
2033
2041
  };
2034
2042
 
2035
2043
  type UpdateWithAggregationPipeline = UpdateAggregationStage[];
2036
2044
  type UpdateAggregationStage = { $addFields: any } |
2037
- { $set: any } |
2038
- { $project: any } |
2039
- { $unset: any } |
2040
- { $replaceRoot: any } |
2041
- { $replaceWith: any };
2045
+ { $set: any } |
2046
+ { $project: any } |
2047
+ { $unset: any } |
2048
+ { $replaceRoot: any } |
2049
+ { $replaceWith: any };
2042
2050
 
2043
2051
  type __UpdateDefProperty<T> =
2044
2052
  [Extract<T, mongodb.ObjectId>] extends [never] ? T :
2045
- T | string;
2053
+ T | string;
2046
2054
  type _UpdateQueryDef<T> = {
2047
2055
  [K in keyof T]?: __UpdateDefProperty<T[K]>;
2048
2056
  };
@@ -2058,28 +2066,28 @@ declare module 'mongoose' {
2058
2066
 
2059
2067
  export type DocumentDefinition<T> = {
2060
2068
  [K in keyof Omit<T, Exclude<keyof Document, '_id' | 'id' | '__v'>>]:
2061
- [Extract<T[K], mongodb.ObjectId>] extends [never]
2069
+ [Extract<T[K], mongodb.ObjectId>] extends [never]
2062
2070
  ? T[K] extends TreatAsPrimitives
2063
2071
  ? T[K]
2064
2072
  : LeanDocumentElement<T[K]>
2065
2073
  : T[K] | string;
2066
- };
2074
+ };
2067
2075
 
2068
2076
  export type FlattenMaps<T> = {
2069
2077
  [K in keyof T]: T[K] extends Map<any, any>
2070
2078
  ? AnyObject : T[K] extends TreatAsPrimitives
2071
- ? T[K] : FlattenMaps<T[K]>;
2079
+ ? T[K] : FlattenMaps<T[K]>;
2072
2080
  };
2073
2081
 
2074
2082
  type actualPrimitives = string | boolean | number | bigint | symbol | null | undefined;
2075
2083
  type TreatAsPrimitives = actualPrimitives |
2076
- NativeDate | RegExp | symbol | Error | BigInt | Types.ObjectId;
2084
+ NativeDate | RegExp | symbol | Error | BigInt | Types.ObjectId;
2077
2085
 
2078
2086
  type LeanType<T> =
2079
2087
  0 extends (1 & T) ? T : // any
2080
- T extends TreatAsPrimitives ? T : // primitives
2081
- T extends Types.Subdocument ? Omit<LeanDocument<T>, '$isSingleNested' | 'ownerDocument' | 'parent'> : // subdocs
2082
- LeanDocument<T>; // Documents and everything else
2088
+ T extends TreatAsPrimitives ? T : // primitives
2089
+ T extends Types.Subdocument ? Omit<LeanDocument<T>, '$isSingleNested' | 'ownerDocument' | 'parent'> : // subdocs
2090
+ LeanDocument<T>; // Documents and everything else
2083
2091
 
2084
2092
  type LeanArray<T extends unknown[]> = T extends unknown[][] ? LeanArray<T[number]>[] : LeanType<T[number]>[];
2085
2093
 
@@ -2092,8 +2100,8 @@ declare module 'mongoose' {
2092
2100
  // This is required for PopulatedDoc.
2093
2101
  type LeanDocumentElement<T> =
2094
2102
  T extends unknown[] ? LeanArray<T> : // Array
2095
- T extends Document ? LeanDocument<T> : // Subdocument
2096
- T;
2103
+ T extends Document ? LeanDocument<T> : // Subdocument
2104
+ T;
2097
2105
 
2098
2106
  export type SchemaDefinitionType<T> = T extends Document ? Omit<T, Exclude<keyof Document, '_id' | 'id' | '__v'>> : T;
2099
2107
 
@@ -2106,13 +2114,13 @@ declare module 'mongoose' {
2106
2114
 
2107
2115
  export type LeanDocumentOrArray<T> = 0 extends (1 & T) ? T :
2108
2116
  T extends unknown[] ? LeanDocument<T[number]>[] :
2109
- T extends Document ? LeanDocument<T> :
2110
- T;
2117
+ T extends Document ? LeanDocument<T> :
2118
+ T;
2111
2119
 
2112
2120
  export type LeanDocumentOrArrayWithRawType<T, RawDocType> = 0 extends (1 & T) ? T :
2113
2121
  T extends unknown[] ? RawDocType[] :
2114
- T extends Document ? RawDocType :
2115
- T;
2122
+ T extends Document ? RawDocType :
2123
+ T;
2116
2124
 
2117
2125
  class Aggregate<R> {
2118
2126
  /**