Skip to content

Commit

Permalink
Merge pull request #11965 from ghost91-/make-find-parameters-optional
Browse files Browse the repository at this point in the history
[Types] Re-add the possibility to pass undefined for projection in Model.find
  • Loading branch information
vkarpov15 committed Jun 21, 2022
2 parents d9a637c + 0a55d08 commit 63ea65e
Show file tree
Hide file tree
Showing 2 changed files with 40 additions and 8 deletions.
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

0 comments on commit 63ea65e

Please sign in to comment.