mirror of
https://github.com/zhigang1992/react-native-firebase.git
synced 2026-04-23 12:06:47 +08:00
[firestore][js] First pass of javascript implementation
This commit is contained in:
66
lib/modules/firestore/QuerySnapshot.js
Normal file
66
lib/modules/firestore/QuerySnapshot.js
Normal file
@@ -0,0 +1,66 @@
|
||||
/**
|
||||
* @flow
|
||||
* QuerySnapshot representation wrapper
|
||||
*/
|
||||
import DocumentChange from './DocumentChange';
|
||||
import DocumentSnapshot from './DocumentSnapshot';
|
||||
import Query from './Query';
|
||||
|
||||
import type { DocumentChangeNativeData } from './DocumentChange';
|
||||
import type { DocumentSnapshotNativeData } from './DocumentSnapshot';
|
||||
|
||||
type QuerySnapshotNativeData = {
|
||||
changes: DocumentChangeNativeData[],
|
||||
documents: DocumentSnapshotNativeData[],
|
||||
readTime: string,
|
||||
}
|
||||
|
||||
/**
|
||||
* @class QuerySnapshot
|
||||
*/
|
||||
export default class QuerySnapshot {
|
||||
_changes: DocumentChange[];
|
||||
_docs: DocumentSnapshot[];
|
||||
_query: Query;
|
||||
_readTime: string;
|
||||
|
||||
constructor(firestore: Object, query: Query, nativeData: QuerySnapshotNativeData) {
|
||||
this._changes = nativeData.changes.map(change => new DocumentChange(change));
|
||||
this._docs = nativeData.documents.map(doc => new DocumentSnapshot(firestore, doc));
|
||||
this._query = query;
|
||||
this._readTime = nativeData.readTime;
|
||||
}
|
||||
|
||||
get docChanges(): DocumentChange[] {
|
||||
return this._changes;
|
||||
}
|
||||
|
||||
get docs(): DocumentSnapshot[] {
|
||||
return this._docs;
|
||||
}
|
||||
|
||||
get empty(): boolean {
|
||||
return this._docs.length === 0;
|
||||
}
|
||||
|
||||
get query(): Query {
|
||||
return this._query;
|
||||
}
|
||||
|
||||
get readTime(): string {
|
||||
return this._readTime;
|
||||
}
|
||||
|
||||
get size(): number {
|
||||
return this._docs.length;
|
||||
}
|
||||
|
||||
forEach(callback: DocumentSnapshot => any) {
|
||||
// TODO: Validation
|
||||
// validate.isFunction('callback', callback);
|
||||
|
||||
for (const doc of this.docs) {
|
||||
callback(doc);
|
||||
}
|
||||
}
|
||||
}
|
||||
Reference in New Issue
Block a user