import { Document, Filter, OptionalId } from "mongodb"; import { BulkWriteOp } from "./bulkWrite.types"; /** * Updates specific fields on the record matching filter. * Does nothing if no record matches. */ export function buildSetOp( filter: Filter, setFields: Partial, ): BulkWriteOp { return { updateOne: { filter, update: { $set: setFields } } }; } /** * Updates specific fields on the record matching the filter or creates a new record if none matches. * setOnInsertFields are only applied when a new record is created; they're ignored on an update. */ export function buildUpsertOp( filter: Filter, setFields: Partial, setOnInsertFields: Partial = {}, ): BulkWriteOp { return { updateOne: { filter, update: { $set: setFields, $setOnInsert: setOnInsertFields }, upsert: true, }, }; } /** Creates a new record. */ export function buildInsertOp( document: OptionalId, ): BulkWriteOp { return { insertOne: { document } }; } /** Permanently removes the record matching the filter. */ export function buildDeleteOp( filter: Filter, ): BulkWriteOp { return { deleteOne: { filter } }; }