mongoose 6.1.10 → 6.2.3

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 (81) hide show
  1. package/.eslintrc.json +154 -0
  2. package/CHANGELOG.md +73 -0
  3. package/dist/browser.umd.js +233 -222
  4. package/index.js +5 -1
  5. package/lib/aggregate.js +23 -28
  6. package/lib/browserDocument.js +1 -1
  7. package/lib/cast/number.js +2 -3
  8. package/lib/cast.js +9 -7
  9. package/lib/connection.js +76 -24
  10. package/lib/cursor/AggregationCursor.js +12 -7
  11. package/lib/cursor/QueryCursor.js +11 -6
  12. package/lib/document.js +131 -122
  13. package/lib/drivers/node-mongodb-native/collection.js +12 -4
  14. package/lib/drivers/node-mongodb-native/connection.js +11 -0
  15. package/lib/error/cast.js +3 -2
  16. package/lib/error/index.js +11 -0
  17. package/lib/error/syncIndexes.js +30 -0
  18. package/lib/helpers/clone.js +51 -29
  19. package/lib/helpers/common.js +2 -2
  20. package/lib/helpers/cursor/eachAsync.js +18 -15
  21. package/lib/helpers/document/compile.js +7 -4
  22. package/lib/helpers/getFunctionName.js +6 -4
  23. package/lib/helpers/indexes/decorateDiscriminatorIndexOptions.js +14 -0
  24. package/lib/helpers/indexes/getRelatedIndexes.js +59 -0
  25. package/lib/helpers/isMongooseObject.js +9 -8
  26. package/lib/helpers/isObject.js +4 -4
  27. package/lib/helpers/model/discriminator.js +2 -1
  28. package/lib/helpers/path/parentPaths.js +10 -5
  29. package/lib/helpers/populate/assignRawDocsToIdStructure.js +4 -2
  30. package/lib/helpers/populate/assignVals.js +12 -4
  31. package/lib/helpers/populate/getModelsMapForPopulate.js +4 -4
  32. package/lib/helpers/populate/markArraySubdocsPopulated.js +3 -1
  33. package/lib/helpers/populate/modelNamesFromRefPath.js +4 -3
  34. package/lib/helpers/printJestWarning.js +2 -2
  35. package/lib/helpers/projection/applyProjection.js +77 -0
  36. package/lib/helpers/projection/hasIncludedChildren.js +36 -0
  37. package/lib/helpers/projection/isExclusive.js +5 -2
  38. package/lib/helpers/projection/isInclusive.js +5 -1
  39. package/lib/helpers/query/cast$expr.js +279 -0
  40. package/lib/helpers/query/castUpdate.js +6 -2
  41. package/lib/helpers/query/hasDollarKeys.js +7 -3
  42. package/lib/helpers/query/isOperator.js +5 -2
  43. package/lib/helpers/schema/applyPlugins.js +11 -0
  44. package/lib/helpers/schema/getIndexes.js +6 -2
  45. package/lib/helpers/schema/getPath.js +4 -2
  46. package/lib/helpers/timestamps/setupTimestamps.js +3 -8
  47. package/lib/index.js +26 -19
  48. package/lib/internal.js +10 -2
  49. package/lib/model.js +196 -171
  50. package/lib/options/SchemaTypeOptions.js +1 -1
  51. package/lib/plugins/trackTransaction.js +5 -4
  52. package/lib/query.js +159 -146
  53. package/lib/queryhelpers.js +10 -10
  54. package/lib/schema/SubdocumentPath.js +4 -3
  55. package/lib/schema/array.js +30 -21
  56. package/lib/schema/buffer.js +1 -1
  57. package/lib/schema/date.js +1 -1
  58. package/lib/schema/decimal128.js +1 -1
  59. package/lib/schema/documentarray.js +9 -11
  60. package/lib/schema/number.js +1 -1
  61. package/lib/schema/objectid.js +2 -2
  62. package/lib/schema/string.js +4 -4
  63. package/lib/schema.js +13 -8
  64. package/lib/schematype.js +86 -40
  65. package/lib/types/ArraySubdocument.js +2 -1
  66. package/lib/types/DocumentArray/index.js +10 -27
  67. package/lib/types/DocumentArray/isMongooseDocumentArray.js +5 -0
  68. package/lib/types/DocumentArray/methods/index.js +15 -3
  69. package/lib/types/array/index.js +22 -21
  70. package/lib/types/array/isMongooseArray.js +5 -0
  71. package/lib/types/array/methods/index.js +22 -23
  72. package/lib/types/buffer.js +3 -3
  73. package/lib/types/map.js +3 -4
  74. package/lib/utils.js +19 -10
  75. package/package.json +34 -168
  76. package/tools/repl.js +1 -1
  77. package/tsconfig.json +8 -0
  78. package/types/Error.d.ts +129 -0
  79. package/types/PipelineStage.d.ts +272 -0
  80. package/{index.d.ts → types/index.d.ts} +169 -481
  81. package/lib/types/array/ArrayWrapper.js +0 -981
@@ -0,0 +1,129 @@
1
+ import mongodb = require('mongodb');
2
+
3
+ declare module 'mongoose' {
4
+
5
+ class NativeError extends global.Error { }
6
+ type CallbackError = NativeError | null;
7
+
8
+ class MongooseError extends global.Error {
9
+ constructor(msg: string);
10
+
11
+ /** The type of error. "MongooseError" for generic errors. */
12
+ name: string;
13
+
14
+ static messages: any;
15
+
16
+ static Messages: any;
17
+ }
18
+
19
+ namespace Error {
20
+ export class CastError extends MongooseError {
21
+ name: 'CastError';
22
+ stringValue: string;
23
+ kind: string;
24
+ value: any;
25
+ path: string;
26
+ reason?: NativeError | null;
27
+ model?: any;
28
+
29
+ constructor(type: string, value: any, path: string, reason?: NativeError, schemaType?: SchemaType);
30
+ }
31
+ export class SyncIndexesError extends MongooseError {
32
+ name: 'SyncIndexesError';
33
+ errors?: Record<string, mongodb.MongoServerError>;
34
+
35
+ constructor(type: string, value: any, path: string, reason?: NativeError, schemaType?: SchemaType);
36
+ }
37
+
38
+ export class DisconnectedError extends MongooseError {
39
+ name: 'DisconnectedError';
40
+ }
41
+
42
+ export class DivergentArrayError extends MongooseError {
43
+ name: 'DivergentArrayError';
44
+ }
45
+
46
+ export class MissingSchemaError extends MongooseError {
47
+ name: 'MissingSchemaError';
48
+ }
49
+
50
+ export class DocumentNotFoundError extends MongooseError {
51
+ name: 'DocumentNotFoundError';
52
+ result: any;
53
+ numAffected: number;
54
+ filter: any;
55
+ query: any;
56
+ }
57
+
58
+ export class ObjectExpectedError extends MongooseError {
59
+ name: 'ObjectExpectedError';
60
+ path: string;
61
+ }
62
+
63
+ export class ObjectParameterError extends MongooseError {
64
+ name: 'ObjectParameterError';
65
+ }
66
+
67
+ export class OverwriteModelError extends MongooseError {
68
+ name: 'OverwriteModelError';
69
+ }
70
+
71
+ export class ParallelSaveError extends MongooseError {
72
+ name: 'ParallelSaveError';
73
+ }
74
+
75
+ export class ParallelValidateError extends MongooseError {
76
+ name: 'ParallelValidateError';
77
+ }
78
+
79
+ export class MongooseServerSelectionError extends MongooseError {
80
+ name: 'MongooseServerSelectionError';
81
+ }
82
+
83
+ export class StrictModeError extends MongooseError {
84
+ name: 'StrictModeError';
85
+ isImmutableError: boolean;
86
+ path: string;
87
+ }
88
+
89
+ export class ValidationError extends MongooseError {
90
+ name: 'ValidationError';
91
+
92
+ errors: { [path: string]: ValidatorError | CastError };
93
+ addError: (path: string, error: ValidatorError | CastError) => void;
94
+
95
+ constructor(instance?: MongooseError);
96
+ }
97
+
98
+ export class ValidatorError extends MongooseError {
99
+ name: 'ValidatorError';
100
+ properties: {
101
+ message: string,
102
+ type?: string,
103
+ path?: string,
104
+ value?: any,
105
+ reason?: any
106
+ };
107
+ kind: string;
108
+ path: string;
109
+ value: any;
110
+ reason?: MongooseError | null;
111
+
112
+ constructor(properties: {
113
+ message?: string,
114
+ type?: string,
115
+ path?: string,
116
+ value?: any,
117
+ reason?: any
118
+ });
119
+ }
120
+
121
+ export class VersionError extends MongooseError {
122
+ name: 'VersionError';
123
+ version: number;
124
+ modifiedPaths: Array<string>;
125
+
126
+ constructor(doc: Document, currentVersion: number, modifiedPaths: Array<string>);
127
+ }
128
+ }
129
+ }
@@ -0,0 +1,272 @@
1
+ declare module 'mongoose' {
2
+ /**
3
+ * [Stages reference](https://docs.mongodb.com/manual/reference/operator/aggregation-pipeline/#aggregation-pipeline-stages)
4
+ */
5
+ export type PipelineStage =
6
+ | PipelineStage.AddFields
7
+ | PipelineStage.Bucket
8
+ | PipelineStage.BucketAuto
9
+ | PipelineStage.CollStats
10
+ | PipelineStage.Count
11
+ | PipelineStage.Facet
12
+ | PipelineStage.GeoNear
13
+ | PipelineStage.GraphLookup
14
+ | PipelineStage.Group
15
+ | PipelineStage.IndexStats
16
+ | PipelineStage.Limit
17
+ | PipelineStage.ListSessions
18
+ | PipelineStage.Lookup
19
+ | PipelineStage.Match
20
+ | PipelineStage.Merge
21
+ | PipelineStage.Out
22
+ | PipelineStage.PlanCacheStats
23
+ | PipelineStage.Project
24
+ | PipelineStage.Redact
25
+ | PipelineStage.ReplaceRoot
26
+ | PipelineStage.ReplaceWith
27
+ | PipelineStage.Sample
28
+ | PipelineStage.Search
29
+ | PipelineStage.Set
30
+ | PipelineStage.SetWindowFields
31
+ | PipelineStage.Skip
32
+ | PipelineStage.Sort
33
+ | PipelineStage.SortByCount
34
+ | PipelineStage.UnionWith
35
+ | PipelineStage.Unset
36
+ | PipelineStage.Unwind
37
+
38
+ export namespace PipelineStage {
39
+ export interface AddFields {
40
+ /** [`$addFields` reference](https://docs.mongodb.com/manual/reference/operator/aggregation/addFields/) */
41
+ $addFields: Record<string, any>
42
+ }
43
+
44
+ export interface Bucket {
45
+ /** [`$bucket` reference](https://docs.mongodb.com/manual/reference/operator/aggregation/bucket/) */
46
+ $bucket: {
47
+ groupBy: any
48
+ boundaries: any[]
49
+ default?: any
50
+ output?: Record<string, { [op in AccumulatorOperator]?: any }>
51
+ }
52
+ }
53
+
54
+ export interface BucketAuto {
55
+ /** [`$bucketAuto` reference](https://docs.mongodb.com/manual/reference/operator/aggregation/bucketAuto/) */
56
+ $bucketAuto: {
57
+ groupBy: any
58
+ buckets: number
59
+ output?: Record<string, { [op in AccumulatorOperator]?: any }>
60
+ granularity?: 'R5' | 'R10' | 'R20' | 'R40' | 'R80' | '1-2-5' | 'E6' | 'E12' | 'E24' | 'E48' | 'E96' | 'E192' | 'POWERSOF2'
61
+ }
62
+ }
63
+
64
+ export interface CollStats {
65
+ /** [`$collStats` reference](https://docs.mongodb.com/manual/reference/operator/aggregation/collStats/) */
66
+ $collStats: {
67
+ latencyStats?: { histograms?: boolean }
68
+ storageStats?: { scale?: number }
69
+ count?: {}
70
+ queryExecStats?: {}
71
+ }
72
+ }
73
+
74
+ export interface Count {
75
+ /** [`$count` reference](https://docs.mongodb.com/manual/reference/operator/aggregation/count/) */
76
+ $count: string
77
+ }
78
+
79
+ export interface Facet {
80
+ /** [`$facet` reference](https://docs.mongodb.com/manual/reference/operator/aggregation/facet/) */
81
+ $facet: Record<string, FacetPipelineStage[]>
82
+ }
83
+
84
+ export type FacetPipelineStage = Exclude<PipelineStage, PipelineStage.CollStats | PipelineStage.Facet | PipelineStage.GeoNear | PipelineStage.IndexStats | PipelineStage.Out | PipelineStage.Merge | PipelineStage.PlanCacheStats>
85
+
86
+ export interface GeoNear {
87
+ /** [`$geoNear` reference](https://docs.mongodb.com/manual/reference/operator/aggregation/geoNear/) */
88
+ $geoNear: {
89
+ near: { type: 'Point'; coordinates: [number, number] } | [number, number]
90
+ distanceField: string
91
+ distanceMultiplier?: number
92
+ includeLocs?: string
93
+ key?: string
94
+ maxDistance?: number
95
+ minDistance?: number
96
+ query?: AnyObject
97
+ spherical?: boolean
98
+ uniqueDocs?: boolean
99
+ }
100
+ }
101
+
102
+ export interface GraphLookup {
103
+ /** [`$graphLookup` reference](https://docs.mongodb.com/manual/reference/operator/aggregation/graphLookup/) */
104
+ $graphLookup: {
105
+ from: string
106
+ startWith: any
107
+ connectFromField: string
108
+ connectToField: string
109
+ as: string
110
+ maxDepth?: number
111
+ depthField?: string
112
+ restrictSearchWithMatch?: AnyObject
113
+ }
114
+ }
115
+
116
+ export interface Group {
117
+ /** [`$group` reference](https://docs.mongodb.com/manual/reference/operator/aggregation/group) */
118
+ $group: { _id: any } | { [key: string]: { [op in AccumulatorOperator]?: any } }
119
+ }
120
+
121
+ export interface IndexStats {
122
+ /** [`$indexStats` reference](https://docs.mongodb.com/manual/reference/operator/aggregation/indexStats/) */
123
+ $indexStats: {}
124
+ }
125
+
126
+ export interface Limit {
127
+ /** [`$limit` reference](https://docs.mongodb.com/manual/reference/operator/aggregation/limit/) */
128
+ $limit: number
129
+ }
130
+
131
+ export interface ListSessions {
132
+ /** [`$listSessions` reference](https://docs.mongodb.com/manual/reference/operator/aggregation/listSessions/) */
133
+ $listSessions: { users?: { user: string; db: string }[] } | { allUsers?: true }
134
+ }
135
+
136
+ export interface Lookup {
137
+ /** [`$lookup` reference](https://docs.mongodb.com/manual/reference/operator/aggregation/lookup/) */
138
+ $lookup: {
139
+ from: string
140
+ as: string
141
+ localField?: string
142
+ foreignField?: string
143
+ let?: Record<string, any>
144
+ pipeline?: Exclude<PipelineStage, PipelineStage.Merge | PipelineStage.Out | PipelineStage.Search>[]
145
+ }
146
+ }
147
+
148
+ export interface Match {
149
+ /** [`$match` reference](https://docs.mongodb.com/manual/reference/operator/aggregation/match/) */
150
+ $match: AnyObject
151
+ }
152
+
153
+ export interface Merge {
154
+ /** [`$merge` reference](https://docs.mongodb.com/manual/reference/operator/aggregation/merge/) */
155
+ $merge: {
156
+ into: string | { db: string; coll: string }
157
+ on?: string | string[]
158
+ let?: Record<string, any>
159
+ whenMatched?: 'replace' | 'keepExisting' | 'merge' | 'fail' | Extract<PipelineStage, PipelineStage.AddFields | PipelineStage.Set | PipelineStage.Project | PipelineStage.Unset | PipelineStage.ReplaceRoot | PipelineStage.ReplaceWith>[]
160
+ whenNotMatched?: 'insert' | 'discard' | 'fail'
161
+ }
162
+ }
163
+
164
+ export interface Out {
165
+ /** [`$out` reference](https://docs.mongodb.com/manual/reference/operator/aggregation/out/) */
166
+ $out: string | { db: string; coll: string }
167
+ }
168
+
169
+ export interface PlanCacheStats {
170
+ /** [`$planCacheStats` reference](https://docs.mongodb.com/manual/reference/operator/aggregation/planCacheStats/) */
171
+ $planCacheStats: {}
172
+ }
173
+
174
+ export interface Project {
175
+ /** [`$project` reference](https://docs.mongodb.com/manual/reference/operator/aggregation/project/) */
176
+ $project: { [field: string]: any }
177
+ }
178
+
179
+ export interface Redact {
180
+ /** [`$redact` reference](https://docs.mongodb.com/manual/reference/operator/aggregation/redact/) */
181
+ $redact: any
182
+ }
183
+
184
+ export interface ReplaceRoot {
185
+ /** [`$replaceRoot` reference](https://docs.mongodb.com/manual/reference/operator/aggregation/replaceRoot/) */
186
+ $replaceRoot: { newRoot: any }
187
+ }
188
+
189
+ export interface ReplaceWith {
190
+ /** [`$replaceWith` reference](https://docs.mongodb.com/manual/reference/operator/aggregation/replaceWith/) */
191
+ $replaceWith: any
192
+ }
193
+
194
+ export interface Sample {
195
+ /** [`$sample` reference](https://docs.mongodb.com/manual/reference/operator/aggregation/sample/) */
196
+ $sample: { size: number }
197
+ }
198
+
199
+ export interface Search {
200
+ /** [`$search` reference](https://docs.atlas.mongodb.com/reference/atlas-search/query-syntax/) */
201
+ $search: {
202
+ index?: string;
203
+ highlight?: {
204
+ /** [`highlightPath` reference](https://docs.atlas.mongodb.com/atlas-search/path-construction/#multiple-field-search) */
205
+ path: string | string[] | { value: string, multi: string};
206
+ maxCharsToExamine?: number;
207
+ maxNumPassages?: number;
208
+ };
209
+ [key: string]: any;
210
+ }
211
+ }
212
+
213
+ export interface Set {
214
+ /** [`$set` reference](https://docs.mongodb.com/manual/reference/operator/aggregation/set/) */
215
+ $set: Record<string, any>
216
+ }
217
+
218
+ export interface SetWindowFields {
219
+ /** [`$setWindowFields` reference](https://docs.mongodb.com/manual/reference/operator/aggregation/setWindowFields/) */
220
+ $setWindowFields: {
221
+ partitionBy?: any
222
+ sortBy?: Record<string, 1 | -1>
223
+ output: Record<
224
+ string,
225
+ { [op in WindowOperator]?: any } & {
226
+ window?: {
227
+ documents?: [string | number, string | number]
228
+ range?: [string | number, string | number]
229
+ unit?: 'year' | 'quarter' | 'month' | 'week' | 'day' | 'hour' | 'minute' | 'second' | 'millisecond'
230
+ }
231
+ }
232
+ >
233
+ }
234
+ }
235
+
236
+ export interface Skip {
237
+ /** [`$skip` reference](https://docs.mongodb.com/manual/reference/operator/aggregation/skip/) */
238
+ $skip: number
239
+ }
240
+
241
+ export interface Sort {
242
+ /** [`$sort` reference](https://docs.mongodb.com/manual/reference/operator/aggregation/sort/) */
243
+ $sort: Record<string, 1 | -1 | { $meta: 'textScore' }>
244
+ }
245
+
246
+ export interface SortByCount {
247
+ /** [`$sortByCount` reference](https://docs.mongodb.com/manual/reference/operator/aggregation/sortByCount/) */
248
+ $sortByCount: any
249
+ }
250
+
251
+ export interface UnionWith {
252
+ /** [`$unionWith` reference](https://docs.mongodb.com/manual/reference/operator/aggregation/unionWith/) */
253
+ $unionWith:
254
+ | string
255
+ | { coll: string; pipeline?: Exclude<PipelineStage, PipelineStage.Out | PipelineStage.Merge>[] }
256
+ }
257
+
258
+ export interface Unset {
259
+ /** [`$unset` reference](https://docs.mongodb.com/manual/reference/operator/aggregation/unset/) */
260
+ $unset: string | string[]
261
+ }
262
+
263
+ export interface Unwind {
264
+ /** [`$unwind` reference](https://docs.mongodb.com/manual/reference/operator/aggregation/unwind/) */
265
+ $unwind: string | { path: string; includeArrayIndex?: string; preserveNullAndEmptyArrays?: boolean }
266
+ }
267
+
268
+ type AccumulatorOperator = '$accumulator' | '$addToSet' | '$avg' | '$count' | '$first' | '$last' | '$max' | '$mergeObjects' | '$min' | '$push' | '$stdDevPop' | '$stdDevSamp' | '$sum'
269
+
270
+ type WindowOperator = '$addToSet' | '$avg' | '$count' | '$covariancePop' | '$covarianceSamp' | '$derivative' | '$expMovingAvg' | '$integral' | '$max' | '$min' | '$push' | '$stdDevSamp' | '$stdDevPop' | '$sum' | '$first' | '$last' | '$shift' | '$denseRank' | '$documentNumber' | '$rank'
271
+ }
272
+ }