Skip to content

Commit ebee099

Browse files
authored
Merge branch 'master' into vkarpov15/gh-13904
2 parents 867221a + 96f71d5 commit ebee099

File tree

2 files changed

+52
-0
lines changed

2 files changed

+52
-0
lines changed

test/types/models.test.ts

Lines changed: 24 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -727,3 +727,27 @@ function gh13904() {
727727
}
728728
));
729729
}
730+
731+
function gh13957() {
732+
class RepositoryBase<T> {
733+
protected model: mongoose.Model<T>;
734+
735+
constructor(schemaModel: mongoose.Model<T>) {
736+
this.model = schemaModel;
737+
}
738+
739+
// Testing that the following compiles successfully
740+
async insertMany(elems: T[]): Promise<T[]> {
741+
elems = await this.model.insertMany(elems);
742+
return elems;
743+
}
744+
}
745+
746+
interface ITest {
747+
name?: string
748+
}
749+
const schema = new Schema({ name: String });
750+
const TestModel = model('Test', schema);
751+
const repository = new RepositoryBase<ITest>(TestModel);
752+
expectType<Promise<ITest[]>>(repository.insertMany([{ name: 'test' }]));
753+
}

types/models.d.ts

Lines changed: 28 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -362,6 +362,34 @@ declare module 'mongoose' {
362362
init(): Promise<THydratedDocumentType>;
363363

364364
/** Inserts one or more new documents as a single `insertMany` call to the MongoDB server. */
365+
insertMany(
366+
docs: Array<TRawDocType>
367+
): Promise<Array<THydratedDocumentType>>;
368+
insertMany(
369+
docs: Array<TRawDocType>,
370+
options: InsertManyOptions & { lean: true; }
371+
): Promise<Array<Require_id<TRawDocType>>>;
372+
insertMany(
373+
doc: Array<TRawDocType>,
374+
options: InsertManyOptions & { ordered: false; rawResult: true; }
375+
): Promise<mongodb.InsertManyResult<Require_id<TRawDocType>> & {
376+
mongoose: {
377+
validationErrors: Error[];
378+
results: Array<
379+
Error |
380+
Object |
381+
THydratedDocumentType
382+
>
383+
}
384+
}>;
385+
insertMany(
386+
docs: Array<TRawDocType>,
387+
options: InsertManyOptions & { lean: true, rawResult: true; }
388+
): Promise<mongodb.InsertManyResult<Require_id<TRawDocType>>>;
389+
insertMany(
390+
docs: Array<TRawDocType>,
391+
options: InsertManyOptions & { rawResult: true; }
392+
): Promise<mongodb.InsertManyResult<Require_id<THydratedDocumentType>>>;
365393
insertMany<DocContents = TRawDocType>(
366394
docs: Array<DocContents | TRawDocType>,
367395
options: InsertManyOptions & { lean: true; }

0 commit comments

Comments
 (0)