Skip to content

Commit 89ddf14

Browse files
committed
types(model+query): use stricter typings for updateX(), replaceOne(), deleteX() Model functions
Fix #14204
1 parent d655898 commit 89ddf14

File tree

2 files changed

+53
-45
lines changed

2 files changed

+53
-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 & MongooseSpecificQueryOptions) | 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: 47 additions & 39 deletions
Original file line numberDiff line numberDiff line change
@@ -95,33 +95,70 @@ declare module 'mongoose' {
9595
updatedAt?: boolean;
9696
}
9797

98+
interface MongooseSpecificQueryOptions {
99+
/**
100+
* If truthy, mongoose will return the document as a plain JavaScript object rather than a mongoose document.
101+
*/
102+
lean?: boolean | Record<string, any>;
103+
104+
multipleCastError?: boolean;
105+
106+
overwriteDiscriminatorKey?: boolean;
107+
runValidators?: boolean;
108+
/**
109+
* Set to `true` to automatically sanitize potentially unsafe query filters by stripping out query selectors that
110+
* aren't explicitly allowed using `mongoose.trusted()`.
111+
*/
112+
sanitizeFilter?: boolean;
113+
setDefaultsOnInsert?: boolean;
114+
/** overwrites the schema's strict mode option */
115+
strict?: boolean | string;
116+
117+
/**
118+
* equal to `strict` by default, may be `false`, `true`, or `'throw'`. Sets the default
119+
* [strictQuery](https://mongoosejs.com/docs/guide.html#strictQuery) mode for schemas.
120+
*/
121+
strictQuery?: boolean | 'throw';
122+
/**
123+
* If set to `false` and schema-level timestamps are enabled,
124+
* skip timestamps for this update. Note that this allows you to overwrite
125+
* timestamps. Does nothing if schema-level timestamps are not set.
126+
*/
127+
timestamps?: boolean | QueryTimestampsConfig;
128+
129+
/**
130+
* If `true`, convert any aliases in filter, projection, update, and distinct
131+
* to their database property names. Defaults to false.
132+
*/
133+
translateAliases?: boolean;
134+
135+
[other: string]: any;
136+
}
137+
98138
interface QueryOptions<DocType = unknown> extends
99139
PopulateOption,
100-
SessionOption {
140+
SessionOption,
141+
MongooseSpecificQueryOptions {
101142
arrayFilters?: { [key: string]: any }[];
102143
batchSize?: number;
144+
bypassDocumentValidation?: boolean;
103145
collation?: mongodb.CollationOptions;
104146
comment?: any;
105147
context?: string;
106148
explain?: mongodb.ExplainVerbosityLike;
107149
fields?: any | string;
108150
hint?: mongodb.Hint;
109-
/**
110-
* If truthy, mongoose will return the document as a plain JavaScript object rather than a mongoose document.
111-
*/
112-
lean?: boolean | Record<string, any>;
151+
152+
let?: Record<string, any>;
113153
limit?: number;
114154
maxTimeMS?: number;
115155
multi?: boolean;
116-
multipleCastError?: boolean;
117156
/**
118157
* By default, `findOneAndUpdate()` returns the document as it was **before**
119158
* `update` was applied. If you set `new: true`, `findOneAndUpdate()` will
120159
* instead give you the object after `update` was applied.
121160
*/
122161
new?: boolean;
123-
124-
overwriteDiscriminatorKey?: boolean;
125162
projection?: ProjectionType<DocType>;
126163
/**
127164
* if true, returns the full ModifyResult rather than just the document
@@ -136,40 +173,11 @@ declare module 'mongoose' {
136173
* Another alias for the `new` option. `returnOriginal` is deprecated so this should be used.
137174
*/
138175
returnDocument?: 'before' | 'after';
139-
/**
140-
* Set to true to enable `update validators`
141-
* (https://mongoosejs.com/docs/validation.html#update-validators). Defaults to false.
142-
*/
143-
runValidators?: boolean;
144-
/* Set to `true` to automatically sanitize potentially unsafe user-generated query projections */
145-
sanitizeProjection?: boolean;
146-
/**
147-
* Set to `true` to automatically sanitize potentially unsafe query filters by stripping out query selectors that
148-
* aren't explicitly allowed using `mongoose.trusted()`.
149-
*/
150-
sanitizeFilter?: boolean;
151-
setDefaultsOnInsert?: boolean;
152176
skip?: number;
153177
sort?: any;
154-
/** overwrites the schema's strict mode option */
155-
strict?: boolean | string;
156-
/**
157-
* equal to `strict` by default, may be `false`, `true`, or `'throw'`. Sets the default
158-
* [strictQuery](https://mongoosejs.com/docs/guide.html#strictQuery) mode for schemas.
159-
*/
160-
strictQuery?: boolean | 'throw';
178+
161179
tailable?: number;
162-
/**
163-
* If set to `false` and schema-level timestamps are enabled,
164-
* skip timestamps for this update. Note that this allows you to overwrite
165-
* timestamps. Does nothing if schema-level timestamps are not set.
166-
*/
167-
timestamps?: boolean | QueryTimestampsConfig;
168-
/**
169-
* If `true`, convert any aliases in filter, projection, update, and distinct
170-
* to their database property names. Defaults to false.
171-
*/
172-
translateAliases?: boolean;
180+
173181
upsert?: boolean;
174182
useBigInt64?: boolean;
175183
writeConcern?: mongodb.WriteConcern;

0 commit comments

Comments
 (0)