Skip to content

Commit

Permalink
Merge pull request #14379 from sderrow/mongoose-query-options-fix
Browse files Browse the repository at this point in the history
[fix] Add back Typescript help for true Mongoose-specific `options` for updateOne, updateMany, etc.
  • Loading branch information
vkarpov15 committed Feb 26, 2024
2 parents de52899 + 4df404b commit 68de586
Show file tree
Hide file tree
Showing 2 changed files with 29 additions and 21 deletions.
10 changes: 5 additions & 5 deletions types/models.d.ts
Expand Up @@ -228,7 +228,7 @@ declare module 'mongoose' {
/** Creates a `countDocuments` query: counts the number of documents that match `filter`. */
countDocuments(
filter?: FilterQuery<TRawDocType>,
options?: (mongodb.CountOptions & Omit<MongooseQueryOptions<TRawDocType>, 'lean' | 'timestamps'>) | null
options?: (mongodb.CountOptions & MongooseBaseQueryOptions<TRawDocType>) | null
): QueryWithHelpers<
number,
THydratedDocumentType,
Expand Down Expand Up @@ -266,7 +266,7 @@ declare module 'mongoose' {
*/
deleteMany(
filter?: FilterQuery<TRawDocType>,
options?: (mongodb.DeleteOptions & Omit<MongooseQueryOptions<TRawDocType>, 'lean' | 'timestamps'>) | null
options?: (mongodb.DeleteOptions & MongooseBaseQueryOptions<TRawDocType>) | null
): QueryWithHelpers<
mongodb.DeleteResult,
THydratedDocumentType,
Expand All @@ -291,7 +291,7 @@ declare module 'mongoose' {
*/
deleteOne(
filter?: FilterQuery<TRawDocType>,
options?: (mongodb.DeleteOptions & Omit<MongooseQueryOptions<TRawDocType>, 'lean' | 'timestamps'>) | null
options?: (mongodb.DeleteOptions & MongooseBaseQueryOptions<TRawDocType>) | null
): QueryWithHelpers<
mongodb.DeleteResult,
THydratedDocumentType,
Expand Down Expand Up @@ -743,14 +743,14 @@ declare module 'mongoose' {
updateMany<ResultDoc = THydratedDocumentType>(
filter?: FilterQuery<TRawDocType>,
update?: UpdateQuery<TRawDocType> | UpdateWithAggregationPipeline,
options?: (mongodb.UpdateOptions & Omit<MongooseQueryOptions<TRawDocType>, 'lean'>) | null
options?: (mongodb.UpdateOptions & MongooseUpdateQueryOptions<TRawDocType>) | 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?: (mongodb.UpdateOptions & Omit<MongooseQueryOptions<TRawDocType>, 'lean'>) | null
options?: (mongodb.UpdateOptions & MongooseUpdateQueryOptions<TRawDocType>) | null
): QueryWithHelpers<UpdateWriteOpResult, ResultDoc, TQueryHelpers, TRawDocType, 'updateOne'>;

/** Creates a Query, applies the passed conditions, and returns the Query. */
Expand Down
40 changes: 24 additions & 16 deletions types/query.d.ts
Expand Up @@ -17,25 +17,33 @@ declare module 'mongoose' {
*/
type FilterQuery<T> = _FilterQuery<T>;

type MongooseQueryOptions<DocType = unknown> = Pick<
QueryOptions<DocType>,
'context' |
'lean' |
'multipleCastError' |
'overwriteDiscriminatorKey' |
'populate' |
'runValidators' |
'sanitizeProjection' |
'sanitizeFilter' |
'setDefaultsOnInsert' |
'strict' |
'strictQuery' |
'timestamps' |
'translateAliases'
> & {
type MongooseBaseQueryOptionKeys =
| 'context'
| 'multipleCastError'
| 'overwriteDiscriminatorKey'
| 'populate'
| 'runValidators'
| 'sanitizeProjection'
| 'sanitizeFilter'
| 'setDefaultsOnInsert'
| 'strict'
| 'strictQuery'
| 'translateAliases';

type MongooseQueryOptions<
DocType = unknown,
Keys extends keyof QueryOptions<DocType> = MongooseBaseQueryOptionKeys | 'timestamps' | 'lean'
> = Pick<QueryOptions<DocType>, Keys> & {
[other: string]: any;
};

type MongooseBaseQueryOptions<DocType = unknown> = MongooseQueryOptions<DocType, MongooseBaseQueryOptionKeys>;

type MongooseUpdateQueryOptions<DocType = unknown> = MongooseQueryOptions<
DocType,
MongooseBaseQueryOptionKeys | 'timestamps'
>;

type ProjectionFields<DocType> = { [Key in keyof DocType]?: any } & Record<string, any>;

type QueryWithHelpers<ResultType, DocType, THelpers = {}, RawDocType = DocType, QueryOp = 'find'> = Query<ResultType, DocType, THelpers, RawDocType, QueryOp> & THelpers;
Expand Down

0 comments on commit 68de586

Please sign in to comment.