Skip to content

Commit

Permalink
chore: more typing
Browse files Browse the repository at this point in the history
  • Loading branch information
durran committed Aug 27, 2024
1 parent 04473da commit b240306
Show file tree
Hide file tree
Showing 2 changed files with 67 additions and 14 deletions.
72 changes: 59 additions & 13 deletions src/operations/client_bulk_write/command_builder.ts
Original file line number Diff line number Diff line change
@@ -1,5 +1,8 @@
import { type Document } from '../../bson';
import { DocumentSequence } from '../../cmap/commands';
import type { Filter, OptionalId, UpdateFilter, WithoutId } from '../../mongo_types';
import { type CollationOptions } from '../command';
import { type Hint } from '../operation';
import type {
AnyClientBulkWriteModel,
ClientBulkWriteOptions,
Expand Down Expand Up @@ -66,9 +69,7 @@ export class ClientBulkWriteCommandBuilder {
}
}

const nsInfo = Array.from(namespaces.keys()).map(ns => {
return { ns: ns };
});
const nsInfo = Array.from(namespaces.keys(), ns => ({ ns }));

// The base command.
const command: ClientBulkWriteCommand = {
Expand All @@ -83,27 +84,42 @@ export class ClientBulkWriteCommandBuilder {
command.bypassDocumentValidation = this.options.bypassDocumentValidation;
}
// Add let if it was present in the options.
if ('let' in this.options) {
if (this.options.let) {
command.let = this.options.let;
}
return [command];
}
}

/** @internal */
export interface InsertOperation<TSchema extends Document = Document> {
insert: number;
document: OptionalId<TSchema>;
}

/**
* Build the insert one operation.
* @param model - The insert one model.
* @param index - The namespace index.
* @returns the operation.
*/
export const buildInsertOneOperation = (model: ClientInsertOneModel, index: number): Document => {
const document: Document = {
const document: InsertOperation = {
insert: index,
document: model.document
};
return document;
};

/** @internal */
export interface DeleteOperation<TSchema extends Document = Document> {
delete: number;
multi: boolean;
filter: Filter<TSchema>;
hint?: Hint;
collation?: CollationOptions;
}

/**
* Build the delete one operation.
* @param model - The insert many model.
Expand Down Expand Up @@ -131,8 +147,8 @@ function createDeleteOperation(
model: ClientDeleteOneModel | ClientDeleteManyModel,
index: number,
multi: boolean
): Document {
const document: Document = {
): DeleteOperation {
const document: DeleteOperation = {
delete: index,
multi: multi,
filter: model.filter
Expand All @@ -146,13 +162,27 @@ function createDeleteOperation(
return document;
}

/** @internal */
export interface UpdateOperation<TSchema extends Document = Document> {
update: number;
multi: boolean;
filter: Filter<TSchema>;
updateMods: UpdateFilter<TSchema> | Document[];
hint?: Hint;
upsert?: boolean;
arrayFilters?: Document[];
}

/**
* Build the update one operation.
* @param model - The update one model.
* @param index - The namespace index.
* @returns the operation.
*/
export const buildUpdateOneOperation = (model: ClientUpdateOneModel, index: number): Document => {
export const buildUpdateOneOperation = (
model: ClientUpdateOneModel,
index: number
): UpdateOperation => {
return createUpdateOperation(model, index, false);
};

Expand All @@ -162,7 +192,10 @@ export const buildUpdateOneOperation = (model: ClientUpdateOneModel, index: numb
* @param index - The namespace index.
* @returns the operation.
*/
export const buildUpdateManyOperation = (model: ClientUpdateManyModel, index: number): Document => {
export const buildUpdateManyOperation = (
model: ClientUpdateManyModel,
index: number
): UpdateOperation => {
return createUpdateOperation(model, index, true);
};

Expand All @@ -173,8 +206,8 @@ function createUpdateOperation(
model: ClientUpdateOneModel | ClientUpdateManyModel,
index: number,
multi: boolean
): Document {
const document: Document = {
): UpdateOperation {
const document: UpdateOperation = {
update: index,
multi: multi,
filter: model.filter,
Expand All @@ -192,14 +225,27 @@ function createUpdateOperation(
return document;
}

/** @internal */
export interface ReplaceOneOperation<TSchema extends Document = Document> {
update: number;
multi: boolean;
filter: Filter<TSchema>;
updateMods: WithoutId<TSchema>;
hint?: Hint;
upsert?: boolean;
}

/**
* Build the replace one operation.
* @param model - The replace one model.
* @param index - The namespace index.
* @returns the operation.
*/
export const buildReplaceOneOperation = (model: ClientReplaceOneModel, index: number): Document => {
const document: Document = {
export const buildReplaceOneOperation = (
model: ClientReplaceOneModel,
index: number
): ReplaceOneOperation => {
const document: ReplaceOneOperation = {
update: index,
multi: false,
filter: model.filter,
Expand Down
9 changes: 8 additions & 1 deletion src/operations/client_bulk_write/common.ts
Original file line number Diff line number Diff line change
Expand Up @@ -138,7 +138,14 @@ export interface ClientUpdateManyModel<TSchema extends Document = Document>
upsert?: boolean;
}

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

0 comments on commit b240306

Please sign in to comment.