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

types(model+query): use stricter typings for updateX(), replaceOne(),deleteX() Model functions #14228

Merged
merged 3 commits into from Jan 3, 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
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 & Omit<MongooseSpecificQueryOptions, 'lean' | 'timestamps'>) | 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
92 changes: 53 additions & 39 deletions types/query.d.ts
Expand Up @@ -95,81 +95,95 @@ declare module 'mongoose' {
updatedAt?: boolean;
}

interface QueryOptions<DocType = unknown> extends
PopulateOption,
SessionOption {
arrayFilters?: { [key: string]: any }[];
batchSize?: number;
collation?: mongodb.CollationOptions;
comment?: any;
context?: string;
explain?: mongodb.ExplainVerbosityLike;
fields?: any | string;
hint?: mongodb.Hint;
interface MongooseSpecificQueryOptions {
/**
* If truthy, mongoose will return the document as a plain JavaScript object rather than a mongoose document.
*/
lean?: boolean | 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
*/
includeResultMetadata?: boolean;
readPreference?: string | mongodb.ReadPreferenceMode;
/**
* An alias for the `new` option. `returnOriginal: false` is equivalent to `new: true`.
*/
returnOriginal?: boolean;
/**
* 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;
vkarpov15 marked this conversation as resolved.
Show resolved Hide resolved
/* Set to `true` to automatically sanitize potentially unsafe user-generated query projections */
sanitizeProjection?: boolean;
vkarpov15 marked this conversation as resolved.
Show resolved Hide resolved
/**
* 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;
/* Set to `true` to automatically sanitize potentially unsafe user-generated query projections */
sanitizeProjection?: 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;

[other: string]: any;
}

interface QueryOptions<DocType = unknown> extends
PopulateOption,
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;

let?: Record<string, any>;
limit?: number;
maxTimeMS?: number;
multi?: 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;
projection?: ProjectionType<DocType>;
/**
* if true, returns the full ModifyResult rather than just the document
*/
includeResultMetadata?: boolean;
readPreference?: string | mongodb.ReadPreferenceMode;
/**
* An alias for the `new` option. `returnOriginal: false` is equivalent to `new: true`.
*/
returnOriginal?: boolean;
/**
* Another alias for the `new` option. `returnOriginal` is deprecated so this should be used.
*/
returnDocument?: 'before' | 'after';
skip?: number;
sort?: any;

tailable?: number;

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