Skip to content

Commit a20d3b3

Browse files
fix overloading of makeAddIdToDocument
as it turns out, `Document` is assignable to `Document[]`, so overloading is not possible
1 parent 111b09e commit a20d3b3

File tree

4 files changed

+24
-35
lines changed

4 files changed

+24
-35
lines changed

src/bulk/common.ts

Lines changed: 9 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -1,33 +1,33 @@
11
import { promisify } from 'util';
22

3-
import { ObjectId, resolveBSONOptions, type BSONSerializeOptions, type Document } from '../bson';
3+
import { type BSONSerializeOptions, type Document, resolveBSONOptions } from '../bson';
44
import type { Collection } from '../collection';
55
import {
6-
MONGODB_ERROR_CODES,
6+
type AnyError,
77
MongoBatchReExecutionError,
8+
MONGODB_ERROR_CODES,
89
MongoInvalidArgumentError,
910
MongoServerError,
10-
MongoWriteConcernError,
11-
type AnyError
11+
MongoWriteConcernError
1212
} from '../error';
1313
import type { Filter, OneOrMore, OptionalId, UpdateFilter, WithoutId } from '../mongo_types';
1414
import type { CollationOptions, CommandOperationOptions } from '../operations/command';
1515
import { maybeAddIdToDocument } from '../operations/common_functions';
16-
import { DeleteOperation, makeDeleteStatement, type DeleteStatement } from '../operations/delete';
16+
import { DeleteOperation, type DeleteStatement, makeDeleteStatement } from '../operations/delete';
1717
import { executeOperation } from '../operations/execute_operation';
1818
import { InsertOperation } from '../operations/insert';
1919
import { AbstractOperation, type Hint } from '../operations/operation';
20-
import { UpdateOperation, makeUpdateStatement, type UpdateStatement } from '../operations/update';
20+
import { makeUpdateStatement, UpdateOperation, type UpdateStatement } from '../operations/update';
2121
import type { Server } from '../sdam/server';
2222
import type { Topology } from '../sdam/topology';
2323
import type { ClientSession } from '../sessions';
2424
import {
2525
applyRetryableWrites,
26+
type Callback,
2627
getTopology,
2728
hasAtomicOperators,
28-
resolveOptions,
29-
type Callback,
30-
type MongoDBNamespace
29+
type MongoDBNamespace,
30+
resolveOptions
3131
} from '../utils';
3232
import { WriteConcern } from '../write_concern';
3333

src/operations/common_functions.ts

Lines changed: 8 additions & 21 deletions
Original file line numberDiff line numberDiff line change
@@ -47,23 +47,15 @@ export function maybeAddIdToDocument(
4747
coll: Collection,
4848
docs: Document,
4949
options: { forceServerObjectId?: boolean }
50-
): Document;
51-
export function maybeAddIdToDocument(
50+
): Document {
51+
return maybeAddIdToDocuments(coll, [docs], options)[0];
52+
}
53+
54+
export function maybeAddIdToDocuments(
5255
coll: Collection,
5356
docs: Document[],
5457
options: { forceServerObjectId?: boolean }
55-
): Document[];
56-
/**
57-
* Adds an _id to a document or each document in an array, if the document doesn't have an _id
58-
* and forceServerObjectId is false.
59-
*
60-
* If a pkFactory has been set, this function creates ids using the pkFactory.
61-
*/
62-
export function maybeAddIdToDocument(
63-
coll: Collection,
64-
docs: Document[] | Document,
65-
options: { forceServerObjectId?: boolean }
66-
): Document[] | Document {
58+
): Document[] {
6759
const forceServerObjectId =
6860
typeof options.forceServerObjectId === 'boolean'
6961
? options.forceServerObjectId
@@ -74,16 +66,11 @@ export function maybeAddIdToDocument(
7466
return docs;
7567
}
7668

77-
const createId = (doc: Document): Document => {
69+
return docs.map((doc: Document): Document => {
7870
if (doc._id == null) {
7971
doc._id = coll.s.pkFactory.createPk();
8072
}
8173

8274
return doc;
83-
};
84-
85-
if (Array.isArray(docs)) {
86-
return docs.map(createId);
87-
}
88-
return createId(docs);
75+
});
8976
}

src/operations/insert.ts

Lines changed: 5 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -9,7 +9,7 @@ import type { MongoDBNamespace } from '../utils';
99
import { WriteConcern } from '../write_concern';
1010
import { BulkWriteOperation } from './bulk_write';
1111
import { CommandOperation, type CommandOperationOptions } from './command';
12-
import { maybeAddIdToDocument } from './common_functions';
12+
import { maybeAddIdToDocuments } from './common_functions';
1313
import { AbstractOperation, Aspect, defineAspects } from './operation';
1414

1515
/** @internal */
@@ -69,7 +69,7 @@ export interface InsertOneResult<TSchema = Document> {
6969

7070
export class InsertOneOperation extends InsertOperation {
7171
constructor(collection: Collection, doc: Document, options: InsertOneOptions) {
72-
super(collection.s.namespace, maybeAddIdToDocument(collection, [doc], options), options);
72+
super(collection.s.namespace, maybeAddIdToDocuments(collection, [doc], options), options);
7373
}
7474

7575
override async execute(
@@ -131,7 +131,9 @@ export class InsertManyOperation extends AbstractOperation<InsertManyResult> {
131131
const writeConcern = WriteConcern.fromOptions(options);
132132
const bulkWriteOperation = new BulkWriteOperation(
133133
coll,
134-
maybeAddIdToDocument(coll, this.docs, options).map(document => ({ insertOne: { document } })),
134+
maybeAddIdToDocuments(coll, this.docs, options).map(document => ({
135+
insertOne: { document }
136+
})),
135137
options
136138
);
137139

test/integration/crud/bulk.test.ts

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -3,13 +3,13 @@ import * as crypto from 'crypto';
33

44
import {
55
type Collection,
6+
Double,
67
Long,
78
MongoBatchReExecutionError,
89
MongoBulkWriteError,
910
type MongoClient,
1011
MongoDriverError,
11-
MongoInvalidArgumentError,
12-
Double
12+
MongoInvalidArgumentError
1313
} from '../../mongodb';
1414
import { assert as test, ignoreNsNotFound } from '../shared';
1515

0 commit comments

Comments
 (0)