Skip to content

Commit

Permalink
feat(NODE-6252): insertMany and bulkWrite permit readonly arrays (#4175)
Browse files Browse the repository at this point in the history
  • Loading branch information
nbbeeken authored Jul 15, 2024
1 parent 320dde0 commit 4b219d3
Show file tree
Hide file tree
Showing 5 changed files with 20 additions and 8 deletions.
6 changes: 3 additions & 3 deletions src/collection.ts
Original file line number Diff line number Diff line change
Expand Up @@ -292,7 +292,7 @@ export class Collection<TSchema extends Document = Document> {
* @param options - Optional settings for the command
*/
async insertMany(
docs: OptionalUnlessRequiredId<TSchema>[],
docs: ReadonlyArray<OptionalUnlessRequiredId<TSchema>>,
options?: BulkWriteOptions
): Promise<InsertManyResult<TSchema>> {
return await executeOperation(
Expand Down Expand Up @@ -325,7 +325,7 @@ export class Collection<TSchema extends Document = Document> {
* @throws MongoDriverError if operations is not an array
*/
async bulkWrite(
operations: AnyBulkWriteOperation<TSchema>[],
operations: ReadonlyArray<AnyBulkWriteOperation<TSchema>>,
options?: BulkWriteOptions
): Promise<BulkWriteResult> {
if (!Array.isArray(operations)) {
Expand All @@ -336,7 +336,7 @@ export class Collection<TSchema extends Document = Document> {
this.client,
new BulkWriteOperation(
this as TODO_NODE_3286,
operations as TODO_NODE_3286,
operations,
resolveOptions(this, options ?? { ordered: true })
)
);
Expand Down
4 changes: 2 additions & 2 deletions src/operations/bulk_write.ts
Original file line number Diff line number Diff line change
Expand Up @@ -13,11 +13,11 @@ import { AbstractOperation, Aspect, defineAspects } from './operation';
export class BulkWriteOperation extends AbstractOperation<BulkWriteResult> {
override options: BulkWriteOptions;
collection: Collection;
operations: AnyBulkWriteOperation[];
operations: ReadonlyArray<AnyBulkWriteOperation>;

constructor(
collection: Collection,
operations: AnyBulkWriteOperation[],
operations: ReadonlyArray<AnyBulkWriteOperation>,
options: BulkWriteOptions
) {
super(options);
Expand Down
4 changes: 2 additions & 2 deletions src/operations/insert.ts
Original file line number Diff line number Diff line change
Expand Up @@ -104,9 +104,9 @@ export interface InsertManyResult<TSchema = Document> {
export class InsertManyOperation extends AbstractOperation<InsertManyResult> {
override options: BulkWriteOptions;
collection: Collection;
docs: Document[];
docs: ReadonlyArray<Document>;

constructor(collection: Collection, docs: Document[], options: BulkWriteOptions) {
constructor(collection: Collection, docs: ReadonlyArray<Document>, options: BulkWriteOptions) {
super(options);

if (!Array.isArray(docs)) {
Expand Down
11 changes: 10 additions & 1 deletion test/types/community/collection/bulkWrite.test-d.ts
Original file line number Diff line number Diff line change
@@ -1,6 +1,12 @@
import { expectError } from 'tsd';

import { type Collection, type Document, MongoClient, ObjectId } from '../../../mongodb';
import {
type AnyBulkWriteOperation,
type Collection,
type Document,
MongoClient,
ObjectId
} from '../../../mongodb';

// TODO(NODE-3347): Improve these tests to use more expect assertions

Expand Down Expand Up @@ -45,6 +51,9 @@ const testDocument: TestSchema = {
};
const { ...testDocumentWithoutId } = testDocument;

const rd_array: ReadonlyArray<AnyBulkWriteOperation<TestSchema>> = [];
collectionType.bulkWrite(rd_array);

// insertOne

collectionType.bulkWrite([
Expand Down
3 changes: 3 additions & 0 deletions test/types/community/collection/insertX.test-d.ts
Original file line number Diff line number Diff line change
Expand Up @@ -46,6 +46,9 @@ const collection = db.collection<TestModel>('testCollection');
const testDoc: OptionalId<TestModelWithId> = { stringField: 'a', fruitTags: [] };
expectType<Parameters<(typeof collection)['insertOne']>[0]>(testDoc);

const rd_array: ReadonlyArray<TestModel> = [];
await collection.insertMany(rd_array);

const resultOne = await collection.insertOne({
stringField: 'hola',
fruitTags: ['Strawberry']
Expand Down

0 comments on commit 4b219d3

Please sign in to comment.