[firestore] Support update variations on DocumentReference and WriteBatch

This commit is contained in:
Chris Bianca
2017-10-06 12:36:41 +01:00
parent eccd551d43
commit 46136e6c4d
4 changed files with 55 additions and 8 deletions

View File

@@ -5,7 +5,7 @@
import CollectionReference from './CollectionReference';
import DocumentSnapshot from './DocumentSnapshot';
import Path from './Path';
import { firestoreAutoId, isFunction, isObject } from '../../utils';
import { firestoreAutoId, isFunction, isObject, isString } from '../../utils';
export type WriteOptions = {
merge?: boolean,
@@ -156,7 +156,25 @@ export default class DocumentReference {
.documentSet(this.path, data, writeOptions);
}
update(data: Object): Promise<void> {
update(...args: Object | string[]): Promise<void> {
let data = {};
if (args.length === 1) {
if (!isObject(args[0])) {
throw new Error('DocumentReference.update failed: If using a single argument, it must be an object.');
}
data = args[0];
} else if (args.length % 2 === 1) {
throw new Error('DocumentReference.update failed: Must have either a single object argument, or equal numbers of key/value pairs.');
} else {
for (let i = 0; i < args.length; i += 2) {
const key = args[i];
const value = args[i + 1];
if (!isString(key)) {
throw new Error(`DocumentReference.update failed: Argument at index ${i} must be a string`);
}
data[key] = value;
}
}
return this._firestore._native
.documentUpdate(this.path, data);
}