「Cloud Firestore」の版間の差分
ナビゲーションに移動
検索に移動
8行目: | 8行目: | ||
*https://github.com/angular/angularfire/blob/master/docs/firestore/querying-collections.md | *https://github.com/angular/angularfire/blob/master/docs/firestore/querying-collections.md | ||
− | == | + | ===Document Fetch=== |
<pre> | <pre> | ||
38行目: | 38行目: | ||
}); | }); | ||
} | } | ||
+ | </pre> | ||
+ | |||
+ | ===Document Query=== | ||
+ | <pre> | ||
+ | /** 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(); | ||
+ | } | ||
+ | } | ||
+ | </pre> | ||
+ | |||
+ | <pre> | ||
+ | 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); | ||
+ | }); | ||
+ | }); | ||
</pre> | </pre> |
2020年9月11日 (金) 16:01時点における最新版
| 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 矢木浩人