2017-09-26 14:57:25 +01:00
|
|
|
/**
|
|
|
|
* @flow
|
|
|
|
* DocumentSnapshot representation wrapper
|
|
|
|
*/
|
|
|
|
import DocumentReference from './DocumentReference';
|
|
|
|
import Path from './Path';
|
2017-11-17 14:22:46 +00:00
|
|
|
import { parseNativeMap } from './utils/serialize';
|
2017-11-17 11:07:52 +00:00
|
|
|
|
|
|
|
import type Firestore from './';
|
2017-11-23 17:29:40 +00:00
|
|
|
import type { FirestoreNativeDocumentSnapshot, FirestoreSnapshotMetadata } from '../../types';
|
2017-10-05 10:18:24 +01:00
|
|
|
|
2017-09-26 14:57:25 +01:00
|
|
|
/**
|
|
|
|
* @class DocumentSnapshot
|
|
|
|
*/
|
|
|
|
export default class DocumentSnapshot {
|
2017-11-17 11:07:52 +00:00
|
|
|
_data: Object | void;
|
2017-11-17 14:22:46 +00:00
|
|
|
_metadata: FirestoreSnapshotMetadata;
|
2017-09-26 14:57:25 +01:00
|
|
|
_ref: DocumentReference;
|
|
|
|
|
2017-11-17 14:22:46 +00:00
|
|
|
constructor(firestore: Firestore, nativeData: FirestoreNativeDocumentSnapshot) {
|
2017-10-10 15:36:08 +01:00
|
|
|
this._data = parseNativeMap(firestore, nativeData.data);
|
2017-10-05 10:18:24 +01:00
|
|
|
this._metadata = nativeData.metadata;
|
2017-09-27 12:57:53 +01:00
|
|
|
this._ref = new DocumentReference(firestore, Path.fromName(nativeData.path));
|
2017-09-26 14:57:25 +01:00
|
|
|
}
|
|
|
|
|
|
|
|
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 {
|
2017-10-12 18:22:48 +02:00
|
|
|
return this._metadata;
|
2017-09-26 14:57:25 +01:00
|
|
|
}
|
|
|
|
|
|
|
|
get ref(): DocumentReference {
|
|
|
|
return this._ref;
|
|
|
|
}
|
|
|
|
|
2017-11-17 11:07:52 +00:00
|
|
|
data(): Object | void {
|
2017-09-26 14:57:25 +01:00
|
|
|
return this._data;
|
|
|
|
}
|
|
|
|
|
|
|
|
get(fieldPath: string): any {
|
2017-11-17 11:07:52 +00:00
|
|
|
return this._data ? this._data[fieldPath] : undefined;
|
2017-09-26 14:57:25 +01:00
|
|
|
}
|
|
|
|
}
|