2017-09-26 13:57:25 +00:00
/ * *
* @ flow
* WriteBatch representation wrapper
* /
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 './' ;
2017-11-23 17:29:40 +00:00
import type { FirestoreWriteOptions } 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' ,
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 ;
}
2017-11-17 14:22:46 +00:00
set ( docRef : DocumentReference , data : Object , writeOptions ? : FirestoreWriteOptions ) {
2017-09-26 13:57:25 +00:00
// TODO: Validation
// validate.isDocumentReference('docRef', docRef);
// validate.isDocument('data', data);
// validate.isOptionalPrecondition('writeOptions', 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 ,
2017-09-26 13:57:25 +00:00
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-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 ] ) ) {
throw new Error ( 'DocumentReference.update failed: If using two arguments, the second must be an object.' ) ;
}
data = args [ 0 ] ;
} else if ( args . length % 2 === 1 ) {
throw new Error ( 'DocumentReference.update failed: Must have a document reference, followed by 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 + 1 } must be a string ` ) ;
}
data [ key ] = value ;
}
}
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 ;
}
}