2017-09-26 13:57:25 +00:00
|
|
|
/**
|
|
|
|
* @flow
|
|
|
|
* DocumentReference representation wrapper
|
|
|
|
*/
|
|
|
|
import CollectionReference from './CollectionReference';
|
|
|
|
import DocumentSnapshot from './DocumentSnapshot';
|
|
|
|
import Path from './Path';
|
2017-10-03 09:12:25 +00:00
|
|
|
import { firestoreAutoId } from '../../utils';
|
2017-09-26 13:57:25 +00:00
|
|
|
|
|
|
|
export type WriteOptions = {
|
2017-09-27 11:57:53 +00:00
|
|
|
merge?: boolean,
|
2017-09-26 13:57:25 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
/**
|
|
|
|
* @class DocumentReference
|
|
|
|
*/
|
|
|
|
export default class DocumentReference {
|
|
|
|
_documentPath: Path;
|
|
|
|
_firestore: Object;
|
|
|
|
|
|
|
|
constructor(firestore: Object, documentPath: Path) {
|
|
|
|
this._documentPath = documentPath;
|
|
|
|
this._firestore = firestore;
|
|
|
|
}
|
|
|
|
|
|
|
|
get firestore(): Object {
|
|
|
|
return this._firestore;
|
|
|
|
}
|
|
|
|
|
|
|
|
get id(): string | null {
|
|
|
|
return this._documentPath.id;
|
|
|
|
}
|
|
|
|
|
2017-10-05 09:18:24 +00:00
|
|
|
get parent(): CollectionReference {
|
2017-09-26 13:57:25 +00:00
|
|
|
const parentPath = this._documentPath.parent();
|
2017-10-05 09:18:24 +00:00
|
|
|
return new CollectionReference(this._firestore, parentPath);
|
2017-09-26 13:57:25 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
get path(): string {
|
|
|
|
return this._documentPath.relativeName;
|
|
|
|
}
|
|
|
|
|
|
|
|
collection(collectionPath: string): CollectionReference {
|
|
|
|
const path = this._documentPath.child(collectionPath);
|
|
|
|
if (!path.isCollection) {
|
|
|
|
throw new Error('Argument "collectionPath" must point to a collection.');
|
|
|
|
}
|
|
|
|
|
|
|
|
return new CollectionReference(this._firestore, path);
|
|
|
|
}
|
|
|
|
|
2017-10-05 09:18:24 +00:00
|
|
|
delete(): Promise<void> {
|
2017-09-26 13:57:25 +00:00
|
|
|
return this._firestore._native
|
2017-10-05 09:18:24 +00:00
|
|
|
.documentDelete(this.path);
|
2017-09-26 13:57:25 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
get(): Promise<DocumentSnapshot> {
|
|
|
|
return this._firestore._native
|
2017-09-27 11:57:53 +00:00
|
|
|
.documentGet(this.path)
|
2017-09-26 13:57:25 +00:00
|
|
|
.then(result => new DocumentSnapshot(this._firestore, result));
|
|
|
|
}
|
|
|
|
|
2017-10-02 12:11:38 +00:00
|
|
|
onSnapshot(onNext: Function, onError?: Function): () => void {
|
|
|
|
// TODO: Validation
|
2017-10-03 09:12:25 +00:00
|
|
|
const listenerId = firestoreAutoId();
|
|
|
|
|
|
|
|
const listener = (nativeDocumentSnapshot) => {
|
|
|
|
const documentSnapshot = new DocumentSnapshot(this, nativeDocumentSnapshot);
|
|
|
|
onNext(documentSnapshot);
|
|
|
|
};
|
2017-10-02 12:11:38 +00:00
|
|
|
|
|
|
|
// Listen to snapshot events
|
|
|
|
this._firestore.on(
|
|
|
|
this._firestore._getAppEventName(`onDocumentSnapshot:${listenerId}`),
|
2017-10-03 09:12:25 +00:00
|
|
|
listener,
|
2017-10-02 12:11:38 +00:00
|
|
|
);
|
|
|
|
|
|
|
|
// Listen for snapshot error events
|
|
|
|
if (onError) {
|
|
|
|
this._firestore.on(
|
|
|
|
this._firestore._getAppEventName(`onDocumentSnapshotError:${listenerId}`),
|
|
|
|
onError,
|
|
|
|
);
|
|
|
|
}
|
|
|
|
|
|
|
|
// Add the native listener
|
|
|
|
this._firestore._native
|
|
|
|
.documentOnSnapshot(this.path, listenerId);
|
|
|
|
|
|
|
|
// Return an unsubscribe method
|
2017-10-03 09:12:25 +00:00
|
|
|
return this._offDocumentSnapshot.bind(this, listenerId, listener);
|
2017-09-26 13:57:25 +00:00
|
|
|
}
|
|
|
|
|
2017-10-05 09:18:24 +00:00
|
|
|
set(data: Object, writeOptions?: WriteOptions): Promise<void> {
|
2017-09-26 13:57:25 +00:00
|
|
|
return this._firestore._native
|
2017-09-27 11:57:53 +00:00
|
|
|
.documentSet(this.path, data, writeOptions);
|
2017-09-26 13:57:25 +00:00
|
|
|
}
|
|
|
|
|
2017-10-05 09:18:24 +00:00
|
|
|
update(data: Object): Promise<void> {
|
2017-09-26 13:57:25 +00:00
|
|
|
return this._firestore._native
|
2017-09-27 11:57:53 +00:00
|
|
|
.documentUpdate(this.path, data);
|
2017-09-26 13:57:25 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
/**
|
2017-10-03 09:12:25 +00:00
|
|
|
* Remove document snapshot listener
|
2017-10-02 12:11:38 +00:00
|
|
|
* @param listener
|
2017-09-26 13:57:25 +00:00
|
|
|
*/
|
2017-10-02 12:11:38 +00:00
|
|
|
_offDocumentSnapshot(listenerId: number, listener: Function) {
|
|
|
|
this._firestore.log.info('Removing onDocumentSnapshot listener');
|
|
|
|
this._firestore.removeListener(this._firestore._getAppEventName(`onDocumentSnapshot:${listenerId}`), listener);
|
2017-10-03 09:12:25 +00:00
|
|
|
this._firestore.removeListener(this._firestore._getAppEventName(`onDocumentSnapshotError:${listenerId}`), listener);
|
|
|
|
this._firestore._native
|
|
|
|
.documentOffSnapshot(this.path, listenerId);
|
2017-09-26 13:57:25 +00:00
|
|
|
}
|
|
|
|
}
|