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

66 lines
1.6 KiB
JavaScript
Raw Normal View History

/**
* @flow
* DocumentSnapshot representation wrapper
*/
import DocumentReference from './DocumentReference';
import FieldPath from './FieldPath';
import Path from './Path';
import { isObject } from '../../utils';
2017-11-17 14:22:46 +00:00
import { parseNativeMap } from './utils/serialize';
import type Firestore from './';
2017-11-23 17:29:40 +00:00
import type { FirestoreNativeDocumentSnapshot, FirestoreSnapshotMetadata } from '../../types';
const extractFieldPathData = (data: Object | void, segments: string[]): any => {
if (!data || !isObject(data)) {
return undefined;
}
const pathValue = data[segments[0]];
if (segments.length === 1) {
return pathValue;
}
return extractFieldPathData(pathValue, segments.slice(1));
};
/**
* @class DocumentSnapshot
*/
export default class DocumentSnapshot {
_data: Object | void;
2017-11-17 14:22:46 +00:00
_metadata: FirestoreSnapshotMetadata;
_ref: DocumentReference;
2017-11-17 14:22:46 +00:00
constructor(firestore: Firestore, nativeData: FirestoreNativeDocumentSnapshot) {
this._data = parseNativeMap(firestore, nativeData.data);
this._metadata = nativeData.metadata;
this._ref = new DocumentReference(firestore, Path.fromName(nativeData.path));
}
get exists(): boolean {
return this._data !== undefined;
}
get id(): string | null {
return this._ref.id;
}
2017-11-17 14:22:46 +00:00
get metadata(): FirestoreSnapshotMetadata {
return this._metadata;
}
get ref(): DocumentReference {
return this._ref;
}
data(): Object | void {
return this._data;
}
get(fieldPath: string | FieldPath): any {
if (fieldPath instanceof FieldPath) {
return extractFieldPathData(this._data, fieldPath._segments);
}
return this._data ? this._data[fieldPath] : undefined;
}
}