2017-09-26 13:57:25 +00:00
|
|
|
/**
|
|
|
|
* @flow
|
|
|
|
* WriteBatch representation wrapper
|
|
|
|
*/
|
2018-03-20 16:07:37 +00:00
|
|
|
import { parseUpdateArgs } from './utils';
|
2017-10-10 14:36:08 +00:00
|
|
|
import { buildNativeMap } from './utils/serialize';
|
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);
|
2018-03-20 16:07:37 +00:00
|
|
|
const data = parseUpdateArgs(args, 'WriteBatch.update');
|
2017-09-26 13:57:25 +00:00
|
|
|
this._writes.push({
|
2018-03-20 16:07:37 +00:00
|
|
|
data: buildNativeMap(data),
|
2017-09-27 11:57:53 +00:00
|
|
|
path: docRef.path,
|
|
|
|
type: 'UPDATE',
|
2017-09-26 13:57:25 +00:00
|
|
|
});
|
|
|
|
|
|
|
|
return this;
|
|
|
|
}
|
|
|
|
}
|