2017-09-26 13:57:25 +00:00
|
|
|
/**
|
|
|
|
* @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 GeoPoint from './GeoPoint';
|
|
|
|
import Path from './Path';
|
|
|
|
import WriteBatch from './WriteBatch';
|
2017-09-27 11:57:53 +00:00
|
|
|
import INTERNALS from './../../internals';
|
2017-09-26 13:57:25 +00:00
|
|
|
|
2017-10-03 09:12:25 +00:00
|
|
|
type CollectionSyncEvent = {
|
|
|
|
appName: string,
|
|
|
|
querySnapshot?: QuerySnapshot,
|
|
|
|
error?: Object,
|
|
|
|
listenerId: string,
|
|
|
|
path: string,
|
|
|
|
}
|
|
|
|
|
2017-10-02 12:11:38 +00:00
|
|
|
type DocumentSyncEvent = {
|
|
|
|
appName: string,
|
2017-10-03 09:12:25 +00:00
|
|
|
documentSnapshot?: DocumentSnapshot,
|
2017-10-02 12:11:38 +00:00
|
|
|
error?: Object,
|
2017-10-03 09:12:25 +00:00
|
|
|
listenerId: string,
|
2017-10-02 12:11:38 +00:00
|
|
|
path: string,
|
|
|
|
}
|
|
|
|
|
2017-09-26 13:57:25 +00:00
|
|
|
/**
|
|
|
|
* @class Firestore
|
|
|
|
*/
|
|
|
|
export default class Firestore extends ModuleBase {
|
|
|
|
static _NAMESPACE = 'firestore';
|
|
|
|
static _NATIVE_MODULE = 'RNFirebaseFirestore';
|
|
|
|
|
|
|
|
_referencePath: Path;
|
|
|
|
|
|
|
|
constructor(firebaseApp: Object, options: Object = {}) {
|
|
|
|
super(firebaseApp, options, true);
|
|
|
|
this._referencePath = new Path([]);
|
2017-10-02 12:11:38 +00:00
|
|
|
|
|
|
|
this.addListener(
|
|
|
|
// sub to internal native event - this fans out to
|
|
|
|
// public event name: onCollectionSnapshot
|
|
|
|
this._getAppEventName('firestore_collection_sync_event'),
|
|
|
|
this._onCollectionSyncEvent.bind(this),
|
|
|
|
);
|
|
|
|
|
|
|
|
this.addListener(
|
|
|
|
// sub to internal native event - this fans out to
|
|
|
|
// public event name: onDocumentSnapshot
|
|
|
|
this._getAppEventName('firestore_document_sync_event'),
|
|
|
|
this._onDocumentSyncEvent.bind(this),
|
|
|
|
);
|
2017-09-26 13:57:25 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
batch(): WriteBatch {
|
|
|
|
return new WriteBatch(this);
|
|
|
|
}
|
|
|
|
|
|
|
|
/**
|
|
|
|
*
|
|
|
|
* @param collectionPath
|
|
|
|
* @returns {CollectionReference}
|
|
|
|
*/
|
|
|
|
collection(collectionPath: string): CollectionReference {
|
|
|
|
const path = this._referencePath.child(collectionPath);
|
|
|
|
if (!path.isCollection) {
|
|
|
|
throw new Error('Argument "collectionPath" must point to a collection.');
|
|
|
|
}
|
|
|
|
|
|
|
|
return new CollectionReference(this, path);
|
|
|
|
}
|
|
|
|
|
|
|
|
/**
|
|
|
|
*
|
|
|
|
* @param documentPath
|
|
|
|
* @returns {DocumentReference}
|
|
|
|
*/
|
|
|
|
doc(documentPath: string): DocumentReference {
|
|
|
|
const path = this._referencePath.child(documentPath);
|
|
|
|
if (!path.isDocument) {
|
|
|
|
throw new Error('Argument "documentPath" must point to a document.');
|
|
|
|
}
|
|
|
|
|
|
|
|
return new DocumentReference(this, path);
|
|
|
|
}
|
|
|
|
|
2017-10-05 09:18:24 +00:00
|
|
|
enablePersistence(): Promise<void> {
|
|
|
|
throw new Error('Persistence is enabled by default on the Firestore SDKs');
|
2017-09-26 13:57:25 +00:00
|
|
|
}
|
|
|
|
|
2017-10-05 09:18:24 +00:00
|
|
|
runTransaction(updateFunction): Promise<any> {
|
|
|
|
throw new Error('firebase.firestore().runTransaction() coming soon');
|
2017-09-26 13:57:25 +00:00
|
|
|
}
|
|
|
|
|
2017-10-05 09:18:24 +00:00
|
|
|
setLogLevel(logLevel: 'debug' | 'error' | 'silent'): void {
|
|
|
|
throw new Error(INTERNALS.STRINGS.ERROR_UNSUPPORTED_MODULE_METHOD(Firestore, 'setLogLevel'));
|
2017-09-26 13:57:25 +00:00
|
|
|
}
|
|
|
|
|
2017-10-05 09:18:24 +00:00
|
|
|
settings(settings: Object): void {
|
|
|
|
throw new Error('firebase.firestore().settings() coming soon');
|
2017-09-26 13:57:25 +00:00
|
|
|
}
|
2017-10-02 12:11:38 +00:00
|
|
|
|
|
|
|
/**
|
|
|
|
* INTERNALS
|
|
|
|
*/
|
|
|
|
|
|
|
|
/**
|
|
|
|
* Internal collection sync listener
|
|
|
|
* @param event
|
|
|
|
* @private
|
|
|
|
*/
|
2017-10-03 09:12:25 +00:00
|
|
|
_onCollectionSyncEvent(event: CollectionSyncEvent) {
|
2017-10-02 12:11:38 +00:00
|
|
|
if (event.error) {
|
2017-10-03 09:12:25 +00:00
|
|
|
this.emit(this._getAppEventName(`onQuerySnapshotError:${event.listenerId}`), event.error);
|
2017-10-02 12:11:38 +00:00
|
|
|
} else {
|
2017-10-03 09:12:25 +00:00
|
|
|
this.emit(this._getAppEventName(`onQuerySnapshot:${event.listenerId}`), event.querySnapshot);
|
2017-10-02 12:11:38 +00:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
/**
|
|
|
|
* Internal document sync listener
|
|
|
|
* @param event
|
|
|
|
* @private
|
|
|
|
*/
|
|
|
|
_onDocumentSyncEvent(event: DocumentSyncEvent) {
|
|
|
|
if (event.error) {
|
|
|
|
this.emit(this._getAppEventName(`onDocumentSnapshotError:${event.listenerId}`), event.error);
|
|
|
|
} else {
|
2017-10-03 09:12:25 +00:00
|
|
|
this.emit(this._getAppEventName(`onDocumentSnapshot:${event.listenerId}`), event.documentSnapshot);
|
2017-10-02 12:11:38 +00:00
|
|
|
}
|
|
|
|
}
|
2017-09-26 13:57:25 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
export const statics = {
|
|
|
|
FieldValue: {
|
|
|
|
delete: () => NativeModules.RNFirebaseFirestore && NativeModules.RNFirebaseFirestore.deleteFieldValue || {},
|
|
|
|
serverTimestamp: () => NativeModules.RNFirebaseFirestore && NativeModules.RNFirebaseFirestore.serverTimestampFieldValue || {}
|
|
|
|
},
|
2017-10-05 09:18:24 +00:00
|
|
|
GeoPoint,
|
2017-09-26 13:57:25 +00:00
|
|
|
};
|