2017-09-26 14:57:25 +01:00
|
|
|
/**
|
|
|
|
* @flow
|
|
|
|
* QuerySnapshot representation wrapper
|
|
|
|
*/
|
2017-11-17 14:22:46 +00:00
|
|
|
import DocumentChange from './DocumentChange';
|
|
|
|
import DocumentSnapshot from './DocumentSnapshot';
|
2017-09-26 14:57:25 +01:00
|
|
|
|
2017-11-17 11:07:52 +00:00
|
|
|
import type Firestore from './';
|
2017-11-23 17:29:40 +00:00
|
|
|
import type { FirestoreNativeDocumentChange, FirestoreNativeDocumentSnapshot, FirestoreSnapshotMetadata } from '../../types';
|
2017-11-17 11:07:52 +00:00
|
|
|
import type Query from './Query';
|
2017-09-26 14:57:25 +01:00
|
|
|
|
|
|
|
type QuerySnapshotNativeData = {
|
2017-11-17 14:22:46 +00:00
|
|
|
changes: FirestoreNativeDocumentChange[],
|
|
|
|
documents: FirestoreNativeDocumentSnapshot[],
|
|
|
|
metadata: FirestoreSnapshotMetadata,
|
2017-09-26 14:57:25 +01:00
|
|
|
}
|
|
|
|
|
2017-11-17 11:07:52 +00:00
|
|
|
/**
|
2017-09-26 14:57:25 +01:00
|
|
|
* @class QuerySnapshot
|
|
|
|
*/
|
|
|
|
export default class QuerySnapshot {
|
|
|
|
_changes: DocumentChange[];
|
|
|
|
_docs: DocumentSnapshot[];
|
2017-11-17 14:22:46 +00:00
|
|
|
_metadata: FirestoreSnapshotMetadata;
|
2017-09-26 14:57:25 +01:00
|
|
|
_query: Query;
|
|
|
|
|
2017-11-17 11:07:52 +00:00
|
|
|
constructor(firestore: Firestore, query: Query, nativeData: QuerySnapshotNativeData) {
|
2017-10-10 17:48:53 +01:00
|
|
|
this._changes = nativeData.changes.map(change => new DocumentChange(firestore, change));
|
2017-09-26 14:57:25 +01:00
|
|
|
this._docs = nativeData.documents.map(doc => new DocumentSnapshot(firestore, doc));
|
2017-10-05 10:18:24 +01:00
|
|
|
this._metadata = nativeData.metadata;
|
2017-09-26 14:57:25 +01:00
|
|
|
this._query = query;
|
|
|
|
}
|
|
|
|
|
|
|
|
get docChanges(): DocumentChange[] {
|
|
|
|
return this._changes;
|
|
|
|
}
|
|
|
|
|
|
|
|
get docs(): DocumentSnapshot[] {
|
|
|
|
return this._docs;
|
|
|
|
}
|
|
|
|
|
|
|
|
get empty(): boolean {
|
|
|
|
return this._docs.length === 0;
|
|
|
|
}
|
|
|
|
|
2017-11-17 14:22:46 +00:00
|
|
|
get metadata(): FirestoreSnapshotMetadata {
|
2017-10-05 10:18:24 +01:00
|
|
|
return this._metadata;
|
2017-09-26 14:57:25 +01:00
|
|
|
}
|
|
|
|
|
2017-11-17 11:07:52 +00:00
|
|
|
get query(): Query {
|
|
|
|
return this._query;
|
|
|
|
}
|
|
|
|
|
2017-09-26 14:57:25 +01:00
|
|
|
get size(): number {
|
|
|
|
return this._docs.length;
|
|
|
|
}
|
|
|
|
|
|
|
|
forEach(callback: DocumentSnapshot => any) {
|
|
|
|
// TODO: Validation
|
|
|
|
// validate.isFunction('callback', callback);
|
|
|
|
|
2017-11-17 11:07:52 +00:00
|
|
|
this._docs.forEach((doc) => {
|
2017-09-26 14:57:25 +01:00
|
|
|
callback(doc);
|
2017-11-17 11:07:52 +00:00
|
|
|
});
|
2017-09-26 14:57:25 +01:00
|
|
|
}
|
|
|
|
}
|