Skip to content

Commit dd3e4ef

Browse files
comments part 1
1 parent d20e2c9 commit dd3e4ef

File tree

6 files changed

+62
-47
lines changed

6 files changed

+62
-47
lines changed

src/bulk/common.ts

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -12,7 +12,6 @@ import {
1212
} from '../error';
1313
import type { Filter, OneOrMore, OptionalId, UpdateFilter, WithoutId } from '../mongo_types';
1414
import type { CollationOptions, CommandOperationOptions } from '../operations/command';
15-
import { maybeAddIdToDocuments } from '../operations/common_functions';
1615
import { DeleteOperation, type DeleteStatement, makeDeleteStatement } from '../operations/delete';
1716
import { executeOperation } from '../operations/execute_operation';
1817
import { InsertOperation } from '../operations/insert';
@@ -21,6 +20,7 @@ import { makeUpdateStatement, UpdateOperation, type UpdateStatement } from '../o
2120
import type { Server } from '../sdam/server';
2221
import type { Topology } from '../sdam/topology';
2322
import type { ClientSession } from '../sessions';
23+
import { maybeAddIdToDocuments } from '../utils';
2424
import {
2525
applyRetryableWrites,
2626
type Callback,

src/collection.ts

Lines changed: 7 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -573,7 +573,7 @@ export class Collection<TSchema extends Document = Document> {
573573
const indexes = await executeOperation(
574574
this.client,
575575
CreateIndexesOperation.fromIndexSpecification(
576-
this as TODO_NODE_3286,
576+
this,
577577
this.collectionName,
578578
indexSpec,
579579
resolveOptions(this, options)
@@ -621,7 +621,7 @@ export class Collection<TSchema extends Document = Document> {
621621
return executeOperation(
622622
this.client,
623623
CreateIndexesOperation.fromIndexDescriptionArray(
624-
this as TODO_NODE_3286,
624+
this,
625625
this.collectionName,
626626
indexSpecs,
627627
resolveOptions(this, { ...options, maxTimeMS: undefined })
@@ -681,7 +681,7 @@ export class Collection<TSchema extends Document = Document> {
681681
indexes: string | string[],
682682
options?: IndexInformationOptions
683683
): Promise<boolean> {
684-
const indexNames: string[] = [indexes].flat();
684+
const indexNames: string[] = Array.isArray(indexes) ? indexes : [indexes];
685685
const allIndexes: string[] = await this.listIndexes(options)
686686
.map(({ name }) => name)
687687
.toArray();
@@ -806,13 +806,12 @@ export class Collection<TSchema extends Document = Document> {
806806
return indexes;
807807
}
808808

809-
const info: Record<string, Array<[string, unknown]>> = {};
810-
for (const { name, key } of indexes) {
811-
info[name] = Object.entries(key);
812-
}
809+
const object: Record<string, Array<[string, unknown]>> = Object.fromEntries(
810+
indexes.map(({ name, key }) => [name, Object.entries(key)])
811+
);
813812

814813
// @ts-expect-error TODO(NODE-6029): fix return type of `indexes()` and `indexInformation()`
815-
return info;
814+
return object;
816815
}
817816

818817
/**

src/operations/common_functions.ts

Lines changed: 0 additions & 37 deletions
This file was deleted.

src/operations/indexes.ts

Lines changed: 18 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -73,6 +73,24 @@ export type IndexSpecification = OneOrMore<
7373

7474
/** @public */
7575
export interface IndexInformationOptions {
76+
/**
77+
* When `true`, an array of index descriptions is returned.
78+
* When `false`, the driver returns an object that with keys corresponding to index names with values
79+
* corresponding to the entries of the indexes' key.
80+
*
81+
* For example, the given the following indexes:
82+
* ```
83+
* [ { name: 'a_1', key: { a: 1 } }, { name: 'b_1_c_1' , key: { b: 1, c: 1 } }]
84+
* ```
85+
*
86+
* When `full` is `true`, the above array is returned. When `full` is `false`, the following is returned:
87+
* ```
88+
* {
89+
* 'a_1': [['a', 1]],
90+
* 'b_1_c_1': [['b', 1], ['c', 1]],
91+
* }
92+
* ```
93+
*/
7694
full?: boolean;
7795
readPreference?: ReadPreference;
7896
session?: ClientSession;

src/operations/insert.ts

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -6,10 +6,10 @@ import type { InferIdType } from '../mongo_types';
66
import type { Server } from '../sdam/server';
77
import type { ClientSession } from '../sessions';
88
import type { MongoDBNamespace } from '../utils';
9+
import { maybeAddIdToDocuments } from '../utils';
910
import { WriteConcern } from '../write_concern';
1011
import { BulkWriteOperation } from './bulk_write';
1112
import { CommandOperation, type CommandOperationOptions } from './command';
12-
import { maybeAddIdToDocuments } from './common_functions';
1313
import { AbstractOperation, Aspect, defineAspects } from './operation';
1414

1515
/** @internal */

src/utils.ts

Lines changed: 35 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1280,3 +1280,38 @@ export async function once<T>(ee: EventEmitter, name: string): Promise<T> {
12801280
throw error;
12811281
}
12821282
}
1283+
1284+
export function maybeAddIdToDocuments(
1285+
coll: Collection,
1286+
docs: Document[],
1287+
options: { forceServerObjectId?: boolean }
1288+
): Document[];
1289+
export function maybeAddIdToDocuments(
1290+
coll: Collection,
1291+
docs: Document,
1292+
options: { forceServerObjectId?: boolean }
1293+
): Document;
1294+
export function maybeAddIdToDocuments(
1295+
coll: Collection,
1296+
docOrDocs: Document[] | Document,
1297+
options: { forceServerObjectId?: boolean }
1298+
): Document[] | Document {
1299+
const forceServerObjectId =
1300+
typeof options.forceServerObjectId === 'boolean'
1301+
? options.forceServerObjectId
1302+
: coll.s.db.options?.forceServerObjectId;
1303+
1304+
// no need to modify the docs if server sets the ObjectId
1305+
if (forceServerObjectId === true) {
1306+
return docOrDocs;
1307+
}
1308+
1309+
const transform = (doc: Document): Document => {
1310+
if (doc._id == null) {
1311+
doc._id = coll.s.pkFactory.createPk();
1312+
}
1313+
1314+
return doc;
1315+
};
1316+
return Array.isArray(docOrDocs) ? docOrDocs.map(transform) : transform(docOrDocs);
1317+
}

0 commit comments

Comments
 (0)