Skip to content

Commit 7d50499

Browse files
committed
chore: more typing
1 parent 94a9730 commit 7d50499

File tree

2 files changed

+67
-14
lines changed

2 files changed

+67
-14
lines changed

src/operations/client_bulk_write/command_builder.ts

Lines changed: 59 additions & 13 deletions
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,8 @@
11
import { type Document } from '../../bson';
22
import { DocumentSequence } from '../../cmap/commands';
3+
import type { Filter, OptionalId, UpdateFilter, WithoutId } from '../../mongo_types';
4+
import { type CollationOptions } from '../command';
5+
import { type Hint } from '../operation';
36
import type {
47
AnyClientBulkWriteModel,
58
ClientBulkWriteOptions,
@@ -66,9 +69,7 @@ export class ClientBulkWriteCommandBuilder {
6669
}
6770
}
6871

69-
const nsInfo = Array.from(namespaces.keys()).map(ns => {
70-
return { ns: ns };
71-
});
72+
const nsInfo = Array.from(namespaces.keys(), ns => ({ ns }));
7273

7374
// The base command.
7475
const command: ClientBulkWriteCommand = {
@@ -83,27 +84,42 @@ export class ClientBulkWriteCommandBuilder {
8384
command.bypassDocumentValidation = this.options.bypassDocumentValidation;
8485
}
8586
// Add let if it was present in the options.
86-
if ('let' in this.options) {
87+
if (this.options.let) {
8788
command.let = this.options.let;
8889
}
8990
return [command];
9091
}
9192
}
9293

94+
/** @internal */
95+
export interface InsertOperation<TSchema extends Document = Document> {
96+
insert: number;
97+
document: OptionalId<TSchema>;
98+
}
99+
93100
/**
94101
* Build the insert one operation.
95102
* @param model - The insert one model.
96103
* @param index - The namespace index.
97104
* @returns the operation.
98105
*/
99106
export const buildInsertOneOperation = (model: ClientInsertOneModel, index: number): Document => {
100-
const document: Document = {
107+
const document: InsertOperation = {
101108
insert: index,
102109
document: model.document
103110
};
104111
return document;
105112
};
106113

114+
/** @internal */
115+
export interface DeleteOperation<TSchema extends Document = Document> {
116+
delete: number;
117+
multi: boolean;
118+
filter: Filter<TSchema>;
119+
hint?: Hint;
120+
collation?: CollationOptions;
121+
}
122+
107123
/**
108124
* Build the delete one operation.
109125
* @param model - The insert many model.
@@ -131,8 +147,8 @@ function createDeleteOperation(
131147
model: ClientDeleteOneModel | ClientDeleteManyModel,
132148
index: number,
133149
multi: boolean
134-
): Document {
135-
const document: Document = {
150+
): DeleteOperation {
151+
const document: DeleteOperation = {
136152
delete: index,
137153
multi: multi,
138154
filter: model.filter
@@ -146,13 +162,27 @@ function createDeleteOperation(
146162
return document;
147163
}
148164

165+
/** @internal */
166+
export interface UpdateOperation<TSchema extends Document = Document> {
167+
update: number;
168+
multi: boolean;
169+
filter: Filter<TSchema>;
170+
updateMods: UpdateFilter<TSchema> | Document[];
171+
hint?: Hint;
172+
upsert?: boolean;
173+
arrayFilters?: Document[];
174+
}
175+
149176
/**
150177
* Build the update one operation.
151178
* @param model - The update one model.
152179
* @param index - The namespace index.
153180
* @returns the operation.
154181
*/
155-
export const buildUpdateOneOperation = (model: ClientUpdateOneModel, index: number): Document => {
182+
export const buildUpdateOneOperation = (
183+
model: ClientUpdateOneModel,
184+
index: number
185+
): UpdateOperation => {
156186
return createUpdateOperation(model, index, false);
157187
};
158188

@@ -162,7 +192,10 @@ export const buildUpdateOneOperation = (model: ClientUpdateOneModel, index: numb
162192
* @param index - The namespace index.
163193
* @returns the operation.
164194
*/
165-
export const buildUpdateManyOperation = (model: ClientUpdateManyModel, index: number): Document => {
195+
export const buildUpdateManyOperation = (
196+
model: ClientUpdateManyModel,
197+
index: number
198+
): UpdateOperation => {
166199
return createUpdateOperation(model, index, true);
167200
};
168201

@@ -173,8 +206,8 @@ function createUpdateOperation(
173206
model: ClientUpdateOneModel | ClientUpdateManyModel,
174207
index: number,
175208
multi: boolean
176-
): Document {
177-
const document: Document = {
209+
): UpdateOperation {
210+
const document: UpdateOperation = {
178211
update: index,
179212
multi: multi,
180213
filter: model.filter,
@@ -192,14 +225,27 @@ function createUpdateOperation(
192225
return document;
193226
}
194227

228+
/** @internal */
229+
export interface ReplaceOneOperation<TSchema extends Document = Document> {
230+
update: number;
231+
multi: boolean;
232+
filter: Filter<TSchema>;
233+
updateMods: WithoutId<TSchema>;
234+
hint?: Hint;
235+
upsert?: boolean;
236+
}
237+
195238
/**
196239
* Build the replace one operation.
197240
* @param model - The replace one model.
198241
* @param index - The namespace index.
199242
* @returns the operation.
200243
*/
201-
export const buildReplaceOneOperation = (model: ClientReplaceOneModel, index: number): Document => {
202-
const document: Document = {
244+
export const buildReplaceOneOperation = (
245+
model: ClientReplaceOneModel,
246+
index: number
247+
): ReplaceOneOperation => {
248+
const document: ReplaceOneOperation = {
203249
update: index,
204250
multi: false,
205251
filter: model.filter,

src/operations/client_bulk_write/common.ts

Lines changed: 8 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -138,7 +138,14 @@ export interface ClientUpdateManyModel<TSchema extends Document = Document>
138138
upsert?: boolean;
139139
}
140140

141-
/** @public */
141+
/**
142+
* Used to represent any of the client bulk write models that can be passed as an array
143+
* to MongoClient#bulkWrite. TSchema can be different on each of the individual models
144+
* and must always match the appropriate namespace that it defines provided to each of the models.
145+
* The schema is used on ClientInsertOneModel for the document field getting inserted, while all other
146+
* models use it for the filter document field.
147+
* @public
148+
*/
142149
export type AnyClientBulkWriteModel<TSchema extends Document = Document> =
143150
| ClientInsertOneModel<TSchema>
144151
| ClientReplaceOneModel<TSchema>

0 commit comments

Comments
 (0)