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] Re-add the possibility to pass undefined for projection in Model.find #11965

Merged
merged 2 commits into from
Jun 21, 2022
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
34 changes: 33 additions & 1 deletion test/types/models.test.ts
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
import { ObjectId } from 'bson';
import { Schema, Document, Model, connection, model, Types } from 'mongoose';
import { Schema, Document, Model, connection, model, Types, CallbackError } from 'mongoose';
import { expectError, expectType } from 'tsd';
import { AutoTypedSchemaType, autoTypedSchema } from './schema.test';

Expand Down Expand Up @@ -199,6 +199,38 @@ Project.exists({ name: 'Hello' }, (err, result) => {
result?._id;
});

function find() {
// no args
Project.find();

// just filter
Project.find({});
Project.find({ name: 'Hello' });

// just callback
Project.find((error: CallbackError, result: IProject[]) => console.log(error, result));

// filter + projection
Project.find({}, undefined);
Project.find({}, null);
Project.find({}, { name: 1 });
Project.find({}, { name: 0 });

// filter + callback
Project.find({}, (error: CallbackError, result: IProject[]) => console.log(error, result));
Project.find({ name: 'Hello' }, (error: CallbackError, result: IProject[]) => console.log(error, result));

// filter + projection + options
Project.find({}, undefined, { limit: 5 });
Project.find({}, null, { limit: 5 });
Project.find({}, { name: 1 }, { limit: 5 });

// filter + projection + options + callback
Project.find({}, undefined, { limit: 5 }, (error: CallbackError, result: IProject[]) => console.log(error, result));
Project.find({}, null, { limit: 5 }, (error: CallbackError, result: IProject[]) => console.log(error, result));
Project.find({}, { name: 1 }, { limit: 5 }, (error: CallbackError, result: IProject[]) => console.log(error, result));
}

function inheritance() {
class InteractsWithDatabase extends Model {
async _update(): Promise<void> {
Expand Down
14 changes: 7 additions & 7 deletions types/models.d.ts
Original file line number Diff line number Diff line change
Expand Up @@ -315,21 +315,21 @@ declare module 'mongoose' {
/** Creates a `find` query: gets a list of documents that match `filter`. */
find<ResultDoc = HydratedDocument<T, TMethodsAndOverrides, TVirtuals>>(
filter: FilterQuery<T>,
projection: ProjectionType<T> | null,
options: QueryOptions<T> | null,
callback?: Callback<ResultDoc[]>
projection?: ProjectionType<T> | null | undefined,
options?: QueryOptions<T> | null | undefined,
callback?: Callback<ResultDoc[]> | undefined
): QueryWithHelpers<Array<ResultDoc>, ResultDoc, TQueryHelpers, T>;
find<ResultDoc = HydratedDocument<T, TMethodsAndOverrides, TVirtuals>>(
filter: FilterQuery<T>,
projection: ProjectionType<T> | null,
callback?: Callback<ResultDoc[]>
projection?: ProjectionType<T> | null | undefined,
callback?: Callback<ResultDoc[]> | undefined
): QueryWithHelpers<Array<ResultDoc>, ResultDoc, TQueryHelpers, T>;
find<ResultDoc = HydratedDocument<T, TMethodsAndOverrides, TVirtuals>>(
filter: FilterQuery<T>,
callback?: Callback<ResultDoc[]>
callback?: Callback<ResultDoc[]> | undefined
): QueryWithHelpers<Array<ResultDoc>, ResultDoc, TQueryHelpers, T>;
find<ResultDoc = HydratedDocument<T, TMethodsAndOverrides, TVirtuals>>(
callback?: Callback<ResultDoc[]>
callback?: Callback<ResultDoc[]> | undefined
): QueryWithHelpers<Array<ResultDoc>, ResultDoc, TQueryHelpers, T>;

/** Creates a `findByIdAndDelete` query, filtering by the given `_id`. */
Expand Down