Skip to content

Commit

Permalink
types(model+query): use stricter typings for updateX(), replaceOne(),…
Browse files Browse the repository at this point in the history
… deleteX() Model functions

Fix #14204
  • Loading branch information
vkarpov15 committed Jan 2, 2024
1 parent d655898 commit 89ddf14
Show file tree
Hide file tree
Showing 2 changed files with 53 additions and 45 deletions.
12 changes: 6 additions & 6 deletions types/models.d.ts
Expand Up @@ -222,7 +222,7 @@ declare module 'mongoose' {
/** Creates a `countDocuments` query: counts the number of documents that match `filter`. */
countDocuments(
filter?: FilterQuery<TRawDocType>,
options?: QueryOptions<TRawDocType>
options?: (mongodb.CountOptions & MongooseSpecificQueryOptions) | null
): QueryWithHelpers<
number,
THydratedDocumentType,
Expand Down Expand Up @@ -254,7 +254,7 @@ declare module 'mongoose' {
*/
deleteMany(
filter?: FilterQuery<TRawDocType>,
options?: QueryOptions<TRawDocType>
options?: (mongodb.DeleteOptions & Omit<MongooseSpecificQueryOptions, 'lean' | 'timestamps'>) | null
): QueryWithHelpers<
mongodb.DeleteResult,
THydratedDocumentType,
Expand All @@ -279,7 +279,7 @@ declare module 'mongoose' {
*/
deleteOne(
filter?: FilterQuery<TRawDocType>,
options?: QueryOptions<TRawDocType>
options?: (mongodb.DeleteOptions & Omit<MongooseSpecificQueryOptions, 'lean' | 'timestamps'>) | null
): QueryWithHelpers<
mongodb.DeleteResult,
THydratedDocumentType,
Expand Down Expand Up @@ -689,7 +689,7 @@ declare module 'mongoose' {
replaceOne<ResultDoc = THydratedDocumentType>(
filter?: FilterQuery<TRawDocType>,
replacement?: TRawDocType | AnyObject,
options?: QueryOptions<TRawDocType> | null
options?: (mongodb.ReplaceOptions & MongooseSpecificQueryOptions) | null
): QueryWithHelpers<UpdateWriteOpResult, ResultDoc, TQueryHelpers, TRawDocType, 'replaceOne'>;

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

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

/** Creates a Query, applies the passed conditions, and returns the Query. */
Expand Down
86 changes: 47 additions & 39 deletions types/query.d.ts
Expand Up @@ -95,33 +95,70 @@ declare module 'mongoose' {
updatedAt?: boolean;
}

interface MongooseSpecificQueryOptions {
/**
* If truthy, mongoose will return the document as a plain JavaScript object rather than a mongoose document.
*/
lean?: boolean | Record<string, any>;

multipleCastError?: boolean;

overwriteDiscriminatorKey?: boolean;
runValidators?: boolean;
/**
* Set to `true` to automatically sanitize potentially unsafe query filters by stripping out query selectors that
* aren't explicitly allowed using `mongoose.trusted()`.
*/
sanitizeFilter?: boolean;
setDefaultsOnInsert?: boolean;
/** overwrites the schema's strict mode option */
strict?: boolean | string;

/**
* equal to `strict` by default, may be `false`, `true`, or `'throw'`. Sets the default
* [strictQuery](https://mongoosejs.com/docs/guide.html#strictQuery) mode for schemas.
*/
strictQuery?: boolean | 'throw';
/**
* If set to `false` and schema-level timestamps are enabled,
* skip timestamps for this update. Note that this allows you to overwrite
* timestamps. Does nothing if schema-level timestamps are not set.
*/
timestamps?: boolean | QueryTimestampsConfig;

/**
* If `true`, convert any aliases in filter, projection, update, and distinct
* to their database property names. Defaults to false.
*/
translateAliases?: boolean;

[other: string]: any;
}

interface QueryOptions<DocType = unknown> extends
PopulateOption,
SessionOption {
SessionOption,
MongooseSpecificQueryOptions {
arrayFilters?: { [key: string]: any }[];
batchSize?: number;
bypassDocumentValidation?: boolean;
collation?: mongodb.CollationOptions;
comment?: any;
context?: string;
explain?: mongodb.ExplainVerbosityLike;
fields?: any | string;
hint?: mongodb.Hint;
/**
* If truthy, mongoose will return the document as a plain JavaScript object rather than a mongoose document.
*/
lean?: boolean | Record<string, any>;

let?: Record<string, any>;
limit?: number;
maxTimeMS?: number;
multi?: boolean;
multipleCastError?: boolean;
/**
* By default, `findOneAndUpdate()` returns the document as it was **before**
* `update` was applied. If you set `new: true`, `findOneAndUpdate()` will
* instead give you the object after `update` was applied.
*/
new?: boolean;

overwriteDiscriminatorKey?: boolean;
projection?: ProjectionType<DocType>;
/**
* if true, returns the full ModifyResult rather than just the document
Expand All @@ -136,40 +173,11 @@ declare module 'mongoose' {
* Another alias for the `new` option. `returnOriginal` is deprecated so this should be used.
*/
returnDocument?: 'before' | 'after';
/**
* Set to true to enable `update validators`
* (https://mongoosejs.com/docs/validation.html#update-validators). Defaults to false.
*/
runValidators?: boolean;
/* Set to `true` to automatically sanitize potentially unsafe user-generated query projections */
sanitizeProjection?: boolean;
/**
* Set to `true` to automatically sanitize potentially unsafe query filters by stripping out query selectors that
* aren't explicitly allowed using `mongoose.trusted()`.
*/
sanitizeFilter?: boolean;
setDefaultsOnInsert?: boolean;
skip?: number;
sort?: any;
/** overwrites the schema's strict mode option */
strict?: boolean | string;
/**
* equal to `strict` by default, may be `false`, `true`, or `'throw'`. Sets the default
* [strictQuery](https://mongoosejs.com/docs/guide.html#strictQuery) mode for schemas.
*/
strictQuery?: boolean | 'throw';

tailable?: number;
/**
* If set to `false` and schema-level timestamps are enabled,
* skip timestamps for this update. Note that this allows you to overwrite
* timestamps. Does nothing if schema-level timestamps are not set.
*/
timestamps?: boolean | QueryTimestampsConfig;
/**
* If `true`, convert any aliases in filter, projection, update, and distinct
* to their database property names. Defaults to false.
*/
translateAliases?: boolean;

upsert?: boolean;
useBigInt64?: boolean;
writeConcern?: mongodb.WriteConcern;
Expand Down

0 comments on commit 89ddf14

Please sign in to comment.