Cloud Firestore
| Angular | Firebase | TypeScript | Google Cloud Platform | ブログカテゴリ(Firebase) |
データの並べ替え
AngularFire
Document Fetch
import { Observable } from 'rxjs'; import { AngularFirestore, QuerySnapshot, DocumentSnapshot, DocumentData } from '@angular/fire/firestore'; export const FS_PATH_INFORMATIONS = 'informations/'; constructor( public firestore: AngularFirestore ) { } public getInformations(): Observable<QuerySnapshot<DocumentData>> { const path = `${FS_PATH_INFORMATIONS}`; console.log(`get informations..[${path}]`); return this.firestore.collection<InformationCard>(path).get(); }
ngOnInit(): void { let outer = this; this.bookService.getInformations().subscribe({ next(p){ p.forEach(d => { outer.infoCards.push(d.data() as InformationCard); }); } }); }
Document Query
/** Firestore Bookshelf Path */ export const FS_PATH_BOOKSHELF = 'bookshelf/v1/users/'; public getBooksInBookshelfQuery(user: User, sortOrder?:SortOrder, lastBook?:Book): Promise<QuerySnapshot<DocumentData>> { if (user != null) { const pagelimit = 100; const path = `${FS_PATH_BOOKSHELF}${user.uid}/books`; console.log(`get book in bookshelf..[${path}] order:${sortOrder}`); console.log(`last book entity is.. ${lastBook}`); const collection = this.firestore.collection<Book>(path); let query = collection.ref.limit(pagelimit); if (sortOrder == 'nameAsc') { query = query.orderBy('title', 'asc'); if (lastBook) { console.log(`where title >= ${lastBook.title}`); query = query .where('title', '>=', lastBook.title) .orderBy('updatedAt', 'desc') .startAfter(lastBook.title, lastBook.updatedAt); } } else { query = query.orderBy('updatedAt', 'desc'); if (lastBook) { console.log(`where updatedAt <= ${lastBook.updatedAt}`); query = query .where('updatedAt', '<=', lastBook.updatedAt) .orderBy('title', 'asc') .startAfter(lastBook.updatedAt, lastBook.title); } } return query.get(); } }
let outer = this; this.auth.onAuthStateChanged(async (u) => { const query = await this.bookService.getBooksInBookshelfQuery(u, sortOrder, lastBook).then(); query.forEach((doc) => { let book = <Book> doc.data(); book.documentId = doc.id; outer.books.push(book); }); });
© 2006 矢木浩人