react-native-firebase/lib/modules/firestore/WriteBatch.js

77 lines
1.8 KiB
JavaScript
Raw Normal View History

/**
* @flow
* WriteBatch representation wrapper
*/
2018-03-20 16:07:37 +00:00
import { parseUpdateArgs } from './utils';
import { buildNativeMap } from './utils/serialize';
import { getNativeModule } from '../../utils/native';
2017-11-17 14:22:46 +00:00
import type DocumentReference from './DocumentReference';
import type Firestore from './';
import type { SetOptions } from './types';
type DocumentWrite = {
data?: Object,
options?: Object,
path: string,
type: 'DELETE' | 'SET' | 'UPDATE',
2018-01-25 18:25:39 +00:00
};
/**
* @class WriteBatch
*/
export default class WriteBatch {
_firestore: Firestore;
_writes: DocumentWrite[];
constructor(firestore: Firestore) {
this._firestore = firestore;
this._writes = [];
}
commit(): Promise<void> {
return getNativeModule(this._firestore).documentBatch(this._writes);
}
delete(docRef: DocumentReference): WriteBatch {
// TODO: Validation
// validate.isDocumentReference('docRef', docRef);
// validate.isOptionalPrecondition('deleteOptions', deleteOptions);
this._writes.push({
path: docRef.path,
type: 'DELETE',
});
return this;
}
set(docRef: DocumentReference, data: Object, options?: SetOptions) {
// TODO: Validation
// validate.isDocumentReference('docRef', docRef);
// validate.isDocument('data', data);
// validate.isOptionalPrecondition('options', writeOptions);
const nativeData = buildNativeMap(data);
this._writes.push({
data: nativeData,
options,
path: docRef.path,
type: 'SET',
});
return this;
}
update(docRef: DocumentReference, ...args: any[]): WriteBatch {
// TODO: Validation
// validate.isDocumentReference('docRef', docRef);
2018-03-20 16:07:37 +00:00
const data = parseUpdateArgs(args, 'WriteBatch.update');
this._writes.push({
2018-03-20 16:07:37 +00:00
data: buildNativeMap(data),
path: docRef.path,
type: 'UPDATE',
});
return this;
}
}