Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

[fix] Add back Typescript help for true Mongoose-specific options for updateOne, updateMany, etc. #14379

Merged
merged 3 commits into from Feb 26, 2024
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Jump to
Jump to file
Failed to load files.
Diff view
Diff view
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