Skip to content

Commit

Permalink
Merge branch 'master' into Automatticgh-13151
Browse files Browse the repository at this point in the history
  • Loading branch information
lpizzinidev committed Mar 10, 2023
2 parents e7633de + c04e329 commit 44aa53f
Show file tree
Hide file tree
Showing 4 changed files with 53 additions and 4 deletions.
44 changes: 43 additions & 1 deletion test/types/queries.test.ts
Original file line number Diff line number Diff line change
Expand Up @@ -11,7 +11,10 @@ import {
FilterQuery,
UpdateQuery,
ApplyBasicQueryCasting,
QuerySelector
QuerySelector,
InferSchemaType,
ProjectionFields,
QueryOptions
} from 'mongoose';
import { ObjectId } from 'mongodb';
import { expectError, expectType } from 'tsd';
Expand Down Expand Up @@ -433,3 +436,42 @@ async function gh11602(): Promise<void> {
returnDocument: 'not-before-or-after'
}));
}

async function gh13142() {
const BlogSchema = new Schema({ title: String });

type Blog = InferSchemaType<typeof BlogSchema>;

const BlogModel = model<Blog>('Blog', BlogSchema);
class BlogRepository {
private readonly blogModel: Model<Blog>;

constructor() {
this.blogModel = BlogModel;
}

findOne<
Projection extends ProjectionFields<Blog>,
Options extends QueryOptions<Blog>
>(
filter: FilterQuery<Blog>,
projection: Projection,
options: Options
): Promise<
Options['lean'] extends true
? Pick<Blog, Extract<keyof Projection, keyof Blog>> | null
: HydratedDocument<Pick<Blog, Extract<keyof Projection, keyof Blog>>> | null
> {
return this.blogModel.findOne(filter, projection, options);
}
}

const blogRepository = new BlogRepository();
const blog = await blogRepository.findOne(
{ title: 'test' },
{ content: 1 },
{ lean: true }
);
if (!blog) return;
expectType<Pick<Blog, Extract<keyof { content: 1 }, keyof Blog>>>(blog);
}
7 changes: 7 additions & 0 deletions test/types/querycursor.test.ts
Original file line number Diff line number Diff line change
Expand Up @@ -13,3 +13,10 @@ Test.find().cursor().
expectType<string | undefined>(doc.name);
}).
then(() => console.log('Done!'));

Test.find().cursor().
eachAsync(async(doc: ITest, i) => {
expectType<Types.ObjectId>(doc._id);
expectType<number>(i);
}).
then(() => console.log('Done!'));
4 changes: 2 additions & 2 deletions types/cursor.d.ts
Original file line number Diff line number Diff line change
Expand Up @@ -38,8 +38,8 @@ declare module 'mongoose' {
* will wait for the promise to resolve before iterating on to the next one.
* Returns a promise that resolves when done.
*/
eachAsync(fn: (doc: DocType[]) => any, options: EachAsyncOptions & { batchSize: number }): Promise<void>;
eachAsync(fn: (doc: DocType) => any, options?: EachAsyncOptions): Promise<void>;
eachAsync(fn: (doc: DocType[], i: number) => any, options: EachAsyncOptions & { batchSize: number }): Promise<void>;
eachAsync(fn: (doc: DocType, i: number) => any, options?: EachAsyncOptions): Promise<void>;

/**
* Registers a transform function which subsequently maps documents retrieved
Expand Down
2 changes: 1 addition & 1 deletion types/query.d.ts
Original file line number Diff line number Diff line change
Expand Up @@ -109,7 +109,7 @@ declare module 'mongoose' {
/**
* If truthy, mongoose will return the document as a plain JavaScript object rather than a mongoose document.
*/
lean?: boolean | any;
lean?: boolean | Record<string, any>;
limit?: number;
maxTimeMS?: number;
multi?: boolean;
Expand Down

0 comments on commit 44aa53f

Please sign in to comment.