[firestore] Support FieldValue.delete() and FieldValue.serverTimestamp()

This commit is contained in:
Chris Bianca
2017-10-12 09:00:46 +01:00
parent ca3dd7aa01
commit f348ba8a8c
8 changed files with 147 additions and 67 deletions

View File

@@ -0,0 +1,17 @@
/**
* @flow
* FieldValue representation wrapper
*/
export default class FieldValue {
static delete(): FieldValue {
return DELETE_FIELD_VALUE;
}
static serverTimestamp(): FieldValue {
return SERVER_TIMESTAMP_FIELD_VALUE;
}
}
export const DELETE_FIELD_VALUE = new FieldValue();
export const SERVER_TIMESTAMP_FIELD_VALUE = new FieldValue();

View File

@@ -2,12 +2,11 @@
* @flow
* Firestore representation wrapper
*/
import { NativeModules } from 'react-native';
import ModuleBase from './../../utils/ModuleBase';
import CollectionReference from './CollectionReference';
import DocumentReference from './DocumentReference';
import DocumentSnapshot from './DocumentSnapshot';
import FieldValue from './FieldValue';
import GeoPoint from './GeoPoint';
import Path from './Path';
import WriteBatch from './WriteBatch';
@@ -137,9 +136,6 @@ export default class Firestore extends ModuleBase {
}
export const statics = {
FieldValue: {
delete: () => NativeModules.RNFirebaseFirestore && NativeModules.RNFirebaseFirestore.deleteFieldValue || {},
serverTimestamp: () => NativeModules.RNFirebaseFirestore && NativeModules.RNFirebaseFirestore.serverTimestampFieldValue || {}
},
FieldValue,
GeoPoint,
};

View File

@@ -1,6 +1,7 @@
// @flow
import DocumentReference from '../DocumentReference';
import { DELETE_FIELD_VALUE, SERVER_TIMESTAMP_FIELD_VALUE } from '../FieldValue';
import GeoPoint from '../GeoPoint';
import Path from '../Path';
import { typeOf } from '../../../utils';
@@ -42,6 +43,12 @@ const buildTypeMap = (value: any): any => {
if (value === null) {
typeMap.type = 'null';
typeMap.value = null;
} else if (value === DELETE_FIELD_VALUE) {
typeMap.type = 'fieldvalue';
typeMap.value = 'delete';
} else if (value === SERVER_TIMESTAMP_FIELD_VALUE) {
typeMap.type = 'fieldvalue';
typeMap.value = 'timestamp';
} else if (type === 'boolean' || type === 'number' || type === 'string') {
typeMap.type = type;
typeMap.value = value;
@@ -99,7 +106,9 @@ const parseNativeArray = (firestore: Object, nativeArray: Object[]): any[] => {
const parseTypeMap = (firestore: Object, typeMap: TypeMap): any => {
const { type, value } = typeMap;
if (type === 'boolean' || type === 'number' || type === 'string' || type === 'null') {
if (type === 'null') {
return null;
} else if (type === 'boolean' || type === 'number' || type === 'string') {
return value;
} else if (type === 'array') {
return parseNativeArray(firestore, value);