Skip to content

Commit 8a06572

Browse files
authored
Merge pull request #14228 from Automattic/vkarpov15/gh-14204
types(model+query): use stricter typings for updateX(), replaceOne(),deleteX() Model functions
2 parents b52ec17 + 936ae2b commit 8a06572

File tree

2 files changed

+59
-45
lines changed

2 files changed

+59
-45
lines changed

types/models.d.ts

Lines changed: 6 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -222,7 +222,7 @@ declare module 'mongoose' {
222222
/** Creates a `countDocuments` query: counts the number of documents that match `filter`. */
223223
countDocuments(
224224
filter?: FilterQuery<TRawDocType>,
225-
options?: QueryOptions<TRawDocType>
225+
options?: (mongodb.CountOptions & Omit<MongooseSpecificQueryOptions, 'lean' | 'timestamps'>) | null
226226
): QueryWithHelpers<
227227
number,
228228
THydratedDocumentType,
@@ -254,7 +254,7 @@ declare module 'mongoose' {
254254
*/
255255
deleteMany(
256256
filter?: FilterQuery<TRawDocType>,
257-
options?: QueryOptions<TRawDocType>
257+
options?: (mongodb.DeleteOptions & Omit<MongooseSpecificQueryOptions, 'lean' | 'timestamps'>) | null
258258
): QueryWithHelpers<
259259
mongodb.DeleteResult,
260260
THydratedDocumentType,
@@ -279,7 +279,7 @@ declare module 'mongoose' {
279279
*/
280280
deleteOne(
281281
filter?: FilterQuery<TRawDocType>,
282-
options?: QueryOptions<TRawDocType>
282+
options?: (mongodb.DeleteOptions & Omit<MongooseSpecificQueryOptions, 'lean' | 'timestamps'>) | null
283283
): QueryWithHelpers<
284284
mongodb.DeleteResult,
285285
THydratedDocumentType,
@@ -689,7 +689,7 @@ declare module 'mongoose' {
689689
replaceOne<ResultDoc = THydratedDocumentType>(
690690
filter?: FilterQuery<TRawDocType>,
691691
replacement?: TRawDocType | AnyObject,
692-
options?: QueryOptions<TRawDocType> | null
692+
options?: (mongodb.ReplaceOptions & MongooseSpecificQueryOptions) | null
693693
): QueryWithHelpers<UpdateWriteOpResult, ResultDoc, TQueryHelpers, TRawDocType, 'replaceOne'>;
694694

695695
/** Schema the model uses. */
@@ -699,14 +699,14 @@ declare module 'mongoose' {
699699
updateMany<ResultDoc = THydratedDocumentType>(
700700
filter?: FilterQuery<TRawDocType>,
701701
update?: UpdateQuery<TRawDocType> | UpdateWithAggregationPipeline,
702-
options?: QueryOptions<TRawDocType> | null
702+
options?: (mongodb.UpdateOptions & Omit<MongooseSpecificQueryOptions, 'lean'>) | null
703703
): QueryWithHelpers<UpdateWriteOpResult, ResultDoc, TQueryHelpers, TRawDocType, 'updateMany'>;
704704

705705
/** Creates a `updateOne` query: updates the first document that matches `filter` with `update`. */
706706
updateOne<ResultDoc = THydratedDocumentType>(
707707
filter?: FilterQuery<TRawDocType>,
708708
update?: UpdateQuery<TRawDocType> | UpdateWithAggregationPipeline,
709-
options?: QueryOptions<TRawDocType> | null
709+
options?: (mongodb.UpdateOptions & Omit<MongooseSpecificQueryOptions, 'lean'>) | null
710710
): QueryWithHelpers<UpdateWriteOpResult, ResultDoc, TQueryHelpers, TRawDocType, 'updateOne'>;
711711

712712
/** Creates a Query, applies the passed conditions, and returns the Query. */

types/query.d.ts

Lines changed: 53 additions & 39 deletions
Original file line numberDiff line numberDiff line change
@@ -95,81 +95,95 @@ declare module 'mongoose' {
9595
updatedAt?: boolean;
9696
}
9797

98-
interface QueryOptions<DocType = unknown> extends
99-
PopulateOption,
100-
SessionOption {
101-
arrayFilters?: { [key: string]: any }[];
102-
batchSize?: number;
103-
collation?: mongodb.CollationOptions;
104-
comment?: any;
105-
context?: string;
106-
explain?: mongodb.ExplainVerbosityLike;
107-
fields?: any | string;
108-
hint?: mongodb.Hint;
98+
interface MongooseSpecificQueryOptions {
10999
/**
110100
* If truthy, mongoose will return the document as a plain JavaScript object rather than a mongoose document.
111101
*/
112102
lean?: boolean | Record<string, any>;
113-
limit?: number;
114-
maxTimeMS?: number;
115-
multi?: boolean;
103+
116104
multipleCastError?: boolean;
117-
/**
118-
* By default, `findOneAndUpdate()` returns the document as it was **before**
119-
* `update` was applied. If you set `new: true`, `findOneAndUpdate()` will
120-
* instead give you the object after `update` was applied.
121-
*/
122-
new?: boolean;
123105

124106
overwriteDiscriminatorKey?: boolean;
125-
projection?: ProjectionType<DocType>;
126-
/**
127-
* if true, returns the full ModifyResult rather than just the document
128-
*/
129-
includeResultMetadata?: boolean;
130-
readPreference?: string | mongodb.ReadPreferenceMode;
131-
/**
132-
* An alias for the `new` option. `returnOriginal: false` is equivalent to `new: true`.
133-
*/
134-
returnOriginal?: boolean;
135-
/**
136-
* Another alias for the `new` option. `returnOriginal` is deprecated so this should be used.
137-
*/
138-
returnDocument?: 'before' | 'after';
139107
/**
140108
* Set to true to enable `update validators`
141109
* (https://mongoosejs.com/docs/validation.html#update-validators). Defaults to false.
142110
*/
143111
runValidators?: boolean;
144-
/* Set to `true` to automatically sanitize potentially unsafe user-generated query projections */
145-
sanitizeProjection?: boolean;
146112
/**
147113
* Set to `true` to automatically sanitize potentially unsafe query filters by stripping out query selectors that
148114
* aren't explicitly allowed using `mongoose.trusted()`.
149115
*/
150116
sanitizeFilter?: boolean;
117+
/* Set to `true` to automatically sanitize potentially unsafe user-generated query projections */
118+
sanitizeProjection?: boolean;
151119
setDefaultsOnInsert?: boolean;
152-
skip?: number;
153-
sort?: any;
154120
/** overwrites the schema's strict mode option */
155121
strict?: boolean | string;
122+
156123
/**
157124
* equal to `strict` by default, may be `false`, `true`, or `'throw'`. Sets the default
158125
* [strictQuery](https://mongoosejs.com/docs/guide.html#strictQuery) mode for schemas.
159126
*/
160127
strictQuery?: boolean | 'throw';
161-
tailable?: number;
162128
/**
163129
* If set to `false` and schema-level timestamps are enabled,
164130
* skip timestamps for this update. Note that this allows you to overwrite
165131
* timestamps. Does nothing if schema-level timestamps are not set.
166132
*/
167133
timestamps?: boolean | QueryTimestampsConfig;
134+
168135
/**
169136
* If `true`, convert any aliases in filter, projection, update, and distinct
170137
* to their database property names. Defaults to false.
171138
*/
172139
translateAliases?: boolean;
140+
141+
[other: string]: any;
142+
}
143+
144+
interface QueryOptions<DocType = unknown> extends
145+
PopulateOption,
146+
SessionOption,
147+
MongooseSpecificQueryOptions {
148+
arrayFilters?: { [key: string]: any }[];
149+
batchSize?: number;
150+
bypassDocumentValidation?: boolean;
151+
collation?: mongodb.CollationOptions;
152+
comment?: any;
153+
context?: string;
154+
explain?: mongodb.ExplainVerbosityLike;
155+
fields?: any | string;
156+
hint?: mongodb.Hint;
157+
158+
let?: Record<string, any>;
159+
limit?: number;
160+
maxTimeMS?: number;
161+
multi?: boolean;
162+
/**
163+
* By default, `findOneAndUpdate()` returns the document as it was **before**
164+
* `update` was applied. If you set `new: true`, `findOneAndUpdate()` will
165+
* instead give you the object after `update` was applied.
166+
*/
167+
new?: boolean;
168+
projection?: ProjectionType<DocType>;
169+
/**
170+
* if true, returns the full ModifyResult rather than just the document
171+
*/
172+
includeResultMetadata?: boolean;
173+
readPreference?: string | mongodb.ReadPreferenceMode;
174+
/**
175+
* An alias for the `new` option. `returnOriginal: false` is equivalent to `new: true`.
176+
*/
177+
returnOriginal?: boolean;
178+
/**
179+
* Another alias for the `new` option. `returnOriginal` is deprecated so this should be used.
180+
*/
181+
returnDocument?: 'before' | 'after';
182+
skip?: number;
183+
sort?: any;
184+
185+
tailable?: number;
186+
173187
upsert?: boolean;
174188
useBigInt64?: boolean;
175189
writeConcern?: mongodb.WriteConcern;

0 commit comments

Comments
 (0)