2017-09-26 13:57:25 +00:00
|
|
|
/**
|
|
|
|
* @flow
|
|
|
|
* WriteBatch representation wrapper
|
|
|
|
*/
|
2018-01-12 11:49:45 +00:00
|
|
|
import FieldPath from './FieldPath';
|
|
|
|
import { mergeFieldPathData } from './utils';
|
2017-10-10 14:36:08 +00:00
|
|
|
import { buildNativeMap } from './utils/serialize';
|
2017-10-06 11:36:41 +00:00
|
|
|
import { isObject, isString } from '../../utils';
|
2018-01-05 17:20:02 +00:00
|
|
|
import { getNativeModule } from '../../utils/native';
|
2017-09-26 13:57:25 +00:00
|
|
|
|
2017-11-17 14:22:46 +00:00
|
|
|
import type DocumentReference from './DocumentReference';
|
2017-11-17 11:07:52 +00:00
|
|
|
import type Firestore from './';
|
2018-02-14 13:00:19 +00:00
|
|
|
import type { SetOptions } from './types';
|
2017-09-26 13:57:25 +00:00
|
|
|
|
|
|
|
type DocumentWrite = {
|
|
|
|
data?: Object,
|
|
|
|
options?: Object,
|
2017-09-27 11:57:53 +00:00
|
|
|
path: string,
|
|
|
|
type: 'DELETE' | 'SET' | 'UPDATE',
|
2018-01-25 18:25:39 +00:00
|
|
|
};
|
2017-09-26 13:57:25 +00:00
|
|
|
|
2017-11-17 11:07:52 +00:00
|
|
|
/**
|
2017-09-26 13:57:25 +00:00
|
|
|
* @class WriteBatch
|
|
|
|
*/
|
|
|
|
export default class WriteBatch {
|
2017-11-17 11:07:52 +00:00
|
|
|
_firestore: Firestore;
|
2017-09-26 13:57:25 +00:00
|
|
|
_writes: DocumentWrite[];
|
|
|
|
|
2017-11-17 11:07:52 +00:00
|
|
|
constructor(firestore: Firestore) {
|
2017-09-26 13:57:25 +00:00
|
|
|
this._firestore = firestore;
|
|
|
|
this._writes = [];
|
|
|
|
}
|
|
|
|
|
2017-10-05 09:18:24 +00:00
|
|
|
commit(): Promise<void> {
|
2018-01-05 17:20:02 +00:00
|
|
|
return getNativeModule(this._firestore).documentBatch(this._writes);
|
2017-09-26 13:57:25 +00:00
|
|
|
}
|
|
|
|
|
2017-10-05 09:18:24 +00:00
|
|
|
delete(docRef: DocumentReference): WriteBatch {
|
2017-09-26 13:57:25 +00:00
|
|
|
// TODO: Validation
|
|
|
|
// validate.isDocumentReference('docRef', docRef);
|
|
|
|
// validate.isOptionalPrecondition('deleteOptions', deleteOptions);
|
|
|
|
this._writes.push({
|
2017-09-27 11:57:53 +00:00
|
|
|
path: docRef.path,
|
|
|
|
type: 'DELETE',
|
2017-09-26 13:57:25 +00:00
|
|
|
});
|
|
|
|
|
|
|
|
return this;
|
|
|
|
}
|
|
|
|
|
2018-02-14 13:00:19 +00:00
|
|
|
set(docRef: DocumentReference, data: Object, options?: SetOptions) {
|
2017-09-26 13:57:25 +00:00
|
|
|
// TODO: Validation
|
|
|
|
// validate.isDocumentReference('docRef', docRef);
|
|
|
|
// validate.isDocument('data', data);
|
2018-02-14 13:00:19 +00:00
|
|
|
// validate.isOptionalPrecondition('options', writeOptions);
|
2017-10-10 14:36:08 +00:00
|
|
|
const nativeData = buildNativeMap(data);
|
2017-09-26 13:57:25 +00:00
|
|
|
this._writes.push({
|
2017-10-10 14:36:08 +00:00
|
|
|
data: nativeData,
|
2018-02-14 13:00:19 +00:00
|
|
|
options,
|
2017-09-27 11:57:53 +00:00
|
|
|
path: docRef.path,
|
|
|
|
type: 'SET',
|
2017-09-26 13:57:25 +00:00
|
|
|
});
|
|
|
|
|
|
|
|
return this;
|
|
|
|
}
|
|
|
|
|
2017-11-17 11:07:52 +00:00
|
|
|
update(docRef: DocumentReference, ...args: any[]): WriteBatch {
|
2017-09-26 13:57:25 +00:00
|
|
|
// TODO: Validation
|
|
|
|
// validate.isDocumentReference('docRef', docRef);
|
2017-10-06 11:36:41 +00:00
|
|
|
let data = {};
|
|
|
|
if (args.length === 1) {
|
|
|
|
if (!isObject(args[0])) {
|
2018-01-25 18:25:39 +00:00
|
|
|
throw new Error(
|
|
|
|
'WriteBatch.update failed: If using two arguments, the second must be an object.'
|
|
|
|
);
|
2017-10-06 11:36:41 +00:00
|
|
|
}
|
2018-01-25 18:25:39 +00:00
|
|
|
// eslint-disable-next-line prefer-destructuring
|
2017-10-06 11:36:41 +00:00
|
|
|
data = args[0];
|
|
|
|
} else if (args.length % 2 === 1) {
|
2018-01-25 18:25:39 +00:00
|
|
|
throw new Error(
|
|
|
|
'WriteBatch.update failed: Must have a document reference, followed by either a single object argument, or equal numbers of key/value pairs.'
|
|
|
|
);
|
2017-10-06 11:36:41 +00:00
|
|
|
} else {
|
|
|
|
for (let i = 0; i < args.length; i += 2) {
|
|
|
|
const key = args[i];
|
|
|
|
const value = args[i + 1];
|
2018-01-12 11:49:45 +00:00
|
|
|
if (isString(key)) {
|
|
|
|
data[key] = value;
|
|
|
|
} else if (key instanceof FieldPath) {
|
|
|
|
data = mergeFieldPathData(data, key._segments, value);
|
|
|
|
} else {
|
2018-01-25 18:25:39 +00:00
|
|
|
throw new Error(
|
|
|
|
`WriteBatch.update failed: Argument at index ${i} must be a string or FieldPath`
|
|
|
|
);
|
2017-10-06 11:36:41 +00:00
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
2017-09-26 13:57:25 +00:00
|
|
|
|
2017-10-10 14:36:08 +00:00
|
|
|
const nativeData = buildNativeMap(data);
|
2017-09-26 13:57:25 +00:00
|
|
|
this._writes.push({
|
2017-10-10 14:36:08 +00:00
|
|
|
data: nativeData,
|
2017-09-27 11:57:53 +00:00
|
|
|
path: docRef.path,
|
|
|
|
type: 'UPDATE',
|
2017-09-26 13:57:25 +00:00
|
|
|
});
|
|
|
|
|
|
|
|
return this;
|
|
|
|
}
|
|
|
|
}
|