node_modules/@google-cloud/firestore/build/src/write-batch.js
/**
     * Update fields of the document referred to by the provided
     * [DocumentReference]{@link DocumentReference}. If the document
     * doesn't yet exist, the update fails and the entire batch will be rejected.
     *
     * The update() method accepts either an object with field paths encoded as
     * keys and field values encoded as values, or a variable number of arguments
     * that alternate between field paths and field values. Nested fields can be
     * updated by providing dot-separated field path strings or by providing
     * FieldPath objects.
     *
     * A Precondition restricting this update can be specified as the last
     * argument.
     *
     * @param {DocumentReference} documentRef A reference to the document to be
     * updated.
     * @param {UpdateData|string|FieldPath} dataOrField An object
     * containing the fields and values with which to update the document
     * or the path of the first field to update.
     * @param {
     * ...(Precondition|*|string|FieldPath)} preconditionOrValues -
     * An alternating list of field paths and values to update or a Precondition
     * to restrict this update.
     * @returns {WriteBatch} This WriteBatch instance. Used for chaining
     * method calls.
     *
     * @example
     * let writeBatch = firestore.batch();
     * let documentRef = firestore.doc('col/doc');
     *
     * writeBatch.update(documentRef, {foo: 'bar'});
     *
     * writeBatch.commit().then(() => {
     *   console.log('Successfully executed batch.');
     * });
     */
Update fields of the document referred to by the provided
[DocumentReference] DocumentReference. If the document
doesn't yet exist, the update fails and the entire batch will be rejected.

The update() method accepts either an object with field paths encoded as
keys and field values encoded as values, or a variable number of arguments
that alternate between field paths and field values. Nested fields can be
updated by providing dot-separated field path strings or by providing
FieldPath objects.

A Precondition restricting this update can be specified as the last
argument.

@param {DocumentReference} documentRef A reference to the document to be
updated.
@param {UpdateData|string|FieldPath} dataOrField An object
containing the fields and values with which to update the document
or the path of the first field to update.
@param {
(Precondition|*|string|FieldPath)} preconditionOrValues
An alternating list of field paths and values to update or a Precondition
to restrict this update.






Update fields of the document referred to by the provided [ DocumentReference ] DocumentReference . If the document doesn ' t yet exist the update fails and the entire batch will be rejected . The update () method accepts either an object with field paths encoded as keys and field values encoded as values or a variable number of arguments that alternate between field paths and field values . Nested fields can be updated by providing dot - separated field path strings or by providing FieldPath objects . A Precondition restricting this update can be specified as the last argument . @param { DocumentReference } documentRef A reference to the document to be updated . @param { UpdateData|string|FieldPath } dataOrField An object containing the fields and values with which to update the document or the path of the first field to update . @param { ( Precondition| * |string|FieldPath ) } preconditionOrValues An alternating list of field paths and values to update or a Precondition to restrict this update .
update(documentRef, dataOrField, ...preconditionOrValues) {
        validate_1.validateMinNumberOfArguments('WriteBatch.update', arguments, 2);
        reference_1.validateDocumentReference('documentRef', documentRef);
        this.verifyNotCommitted();
        const updateMap = new Map();
        let precondition = new document_1.Precondition({ exists: true });
        const argumentError = 'Update() requires either a single JavaScript ' +
            'object or an alternating list of field/value pairs that can be ' +
            'followed by an optional precondition.';
        const usesVarargs = typeof dataOrField === 'string' || dataOrField instanceof path_1.FieldPath;
        if (usesVarargs) {
            try {
                for (let i = 1; i < arguments.length; i += 2) {
                    if (i === arguments.length - 1) {
                        validateUpdatePrecondition(i, arguments[i]);
                        precondition = new document_1.Precondition(arguments[i]);
                    }
                    else {
                        path_1.validateFieldPath(i, arguments[i]);
                        validate_1.validateMinNumberOfArguments('update', arguments, i + 1);
                        const fieldPath = path_1.FieldPath.fromArgument(arguments[i]);
                        validateFieldValue(i, arguments[i + 1], this._allowUndefined, fieldPath);
                        updateMap.set(fieldPath, arguments[i + 1]);
                    }
                }
            }
            catch (err) {
                logger_1.logger('WriteBatch.update', null, 'Varargs validation failed:', err);
                throw new Error(`${argumentError} ${err.message}`);
            }
        }
        else {
            try {
                validateUpdateMap('dataOrField', dataOrField, this._allowUndefined);
                validate_1.validateMaxNumberOfArguments('update', arguments, 3);
                const data = dataOrField;
                Object.keys(data).forEach(key => {
                    path_1.validateFieldPath(key, key);
                    updateMap.set(path_1.FieldPath.fromArgument(key), data[key]);
                });
                if (preconditionOrValues.length > 0) {
                    validateUpdatePrecondition('preconditionOrValues', preconditionOrValues[0]);
                    precondition = new document_1.Precondition(preconditionOrValues[0]);
                }
            }
            catch (err) {
                logger_1.logger('WriteBatch.update', null, 'Non-varargs validation failed:', err);
                throw new Error(`${argumentError} ${err.message}`);
            }
        }
        validateNoConflictingFields('dataOrField', updateMap);
        const transform = document_1.DocumentTransform.fromUpdateMap(documentRef, updateMap);
        transform.validate();
        const documentMask = document_1.DocumentMask.fromUpdateMap(updateMap);
        const op = () => {
            const document = document_1.DocumentSnapshot.fromUpdateMap(documentRef, updateMap);
            const write = document.toProto();
            write.updateMask = documentMask.toProto();
            if (!transform.isEmpty) {
                write.updateTransforms = transform.toProto(this._serializer);
            }
            return {
                write,
                precondition: precondition.toProto(),
            };
        };
        this._ops.push(op);
        return this;
    }
update ( documentRef , dataOrField , ... preconditionOrValues ) { validate_1 . validateMinNumberOfArguments ( 'WriteBatch.update' , arguments , 2 ) ; reference_1 . validateDocumentReference ( 'documentRef' , documentRef ) ; this . verifyNotCommitted ( ) ; const updateMap = new Map ( ) ; let precondition = new document_1 . Precondition ( { exists : true } ) ; const argumentError = 'Update() requires either a single JavaScript ' + 'object or an alternating list of field/value pairs that can be ' + 'followed by an optional precondition.' ; const usesVarargs = typeof dataOrField === 'string' || dataOrField instanceof path_1 . FieldPath ; if ( usesVarargs ) { try { for ( let i = 1 ; i < arguments . length ; i += 2 ) { if ( i === arguments . length - 1 ) { validateUpdatePrecondition ( i , arguments [ i ] ) ; precondition = new document_1 . Precondition ( arguments [ i ] ) ; } else { path_1 . validateFieldPath ( i , arguments [ i ] ) ; validate_1 . validateMinNumberOfArguments ( 'update' , arguments , i + 1 ) ; const fieldPath = path_1 . FieldPath . fromArgument ( arguments [ i ] ) ; validateFieldValue ( i , arguments [ i + 1 ] , this . _allowUndefined , fieldPath ) ; updateMap . set ( fieldPath , arguments [ i + 1 ] ) ; } } } catch ( err ) { logger_1 . logger ( 'WriteBatch.update' , null , 'Varargs validation failed:' , err ) ; throw new Error ( ` ${ argumentError } ${ err . message } ` ) ; } } else { try { validateUpdateMap ( 'dataOrField' , dataOrField , this . _allowUndefined ) ; validate_1 . validateMaxNumberOfArguments ( 'update' , arguments , 3 ) ; const data = dataOrField ; Object . keys ( data ) . forEach ( key => { path_1 . validateFieldPath ( key , key ) ; updateMap . set ( path_1 . FieldPath . fromArgument ( key ) , data [ key ] ) ; } ) ; if ( preconditionOrValues . length > 0 ) { validateUpdatePrecondition ( 'preconditionOrValues' , preconditionOrValues [ 0 ] ) ; precondition = new document_1 . Precondition ( preconditionOrValues [ 0 ] ) ; } } catch ( err ) { logger_1 . logger ( 'WriteBatch.update' , null , 'Non-varargs validation failed:' , err ) ; throw new Error ( ` ${ argumentError } ${ err . message } ` ) ; } } validateNoConflictingFields ( 'dataOrField' , updateMap ) ; const transform = document_1 . DocumentTransform . fromUpdateMap ( documentRef , updateMap ) ; transform . validate ( ) ; const documentMask = document_1 . DocumentMask . fromUpdateMap ( updateMap ) ; const op = ( ) => { const document = document_1 . DocumentSnapshot . fromUpdateMap ( documentRef , updateMap ) ; const write = document . toProto ( ) ; write . updateMask = documentMask . toProto ( ) ; if ( ! transform . isEmpty ) { write . updateTransforms = transform . toProto ( this . _serializer ) ; } return { write , precondition : precondition . toProto ( ) , } ; } ; this . _ops . push ( op ) ; return this ; }
Update fields of the document referred to by the provided
 [DocumentReference]{@link DocumentReference}.
Update fields of the document referred to by the provided [ DocumentReference ] { @link DocumentReference } .
// Unlike the `validateMinNumberOfArguments` invocation above, this // validation can be triggered both from `WriteBatch.update()` and // `DocumentReference.update()`. Hence, we don't use the fully // qualified API name in the error message. // We catch the validation error here and re-throw to provide a better // error message. // We catch the validation error here and prefix the error with a custom // message to describe the usage of update() better.