2017-09-26 13:57:25 +00:00
|
|
|
/**
|
|
|
|
* @flow
|
|
|
|
* WriteBatch representation wrapper
|
|
|
|
*/
|
|
|
|
import DocumentReference from './DocumentReference';
|
|
|
|
|
2017-10-05 09:18:24 +00:00
|
|
|
import type { WriteOptions } from './DocumentReference';
|
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',
|
2017-09-26 13:57:25 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
/**
|
|
|
|
* @class WriteBatch
|
|
|
|
*/
|
|
|
|
export default class WriteBatch {
|
|
|
|
_firestore: Object;
|
|
|
|
_writes: DocumentWrite[];
|
|
|
|
|
|
|
|
constructor(firestore: Object) {
|
|
|
|
this._firestore = firestore;
|
|
|
|
this._writes = [];
|
|
|
|
}
|
|
|
|
|
2017-10-05 09:18:24 +00:00
|
|
|
commit(): Promise<void> {
|
|
|
|
return this._firestore._native
|
|
|
|
.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;
|
|
|
|
}
|
|
|
|
|
|
|
|
set(docRef: DocumentReference, data: Object, writeOptions?: WriteOptions) {
|
|
|
|
// TODO: Validation
|
|
|
|
// validate.isDocumentReference('docRef', docRef);
|
|
|
|
// validate.isDocument('data', data);
|
|
|
|
// validate.isOptionalPrecondition('writeOptions', writeOptions);
|
|
|
|
|
|
|
|
this._writes.push({
|
|
|
|
data,
|
|
|
|
options: writeOptions,
|
2017-09-27 11:57:53 +00:00
|
|
|
path: docRef.path,
|
|
|
|
type: 'SET',
|
2017-09-26 13:57:25 +00:00
|
|
|
});
|
|
|
|
|
|
|
|
return this;
|
|
|
|
}
|
|
|
|
|
2017-09-27 11:57:53 +00:00
|
|
|
// TODO: Update to new method signature
|
2017-10-05 09:18:24 +00:00
|
|
|
update(docRef: DocumentReference, data: Object): WriteBatch {
|
2017-09-26 13:57:25 +00:00
|
|
|
// TODO: Validation
|
|
|
|
// validate.isDocumentReference('docRef', docRef);
|
|
|
|
// validate.isDocument('data', data, true);
|
|
|
|
|
|
|
|
this._writes.push({
|
|
|
|
data,
|
2017-09-27 11:57:53 +00:00
|
|
|
path: docRef.path,
|
|
|
|
type: 'UPDATE',
|
2017-09-26 13:57:25 +00:00
|
|
|
});
|
|
|
|
|
|
|
|
return this;
|
|
|
|
}
|
|
|
|
}
|