| ページ一覧 | ブログ | twitter |  書式 | 書式(表) |

MyMemoWiki

差分

ナビゲーションに移動 検索に移動
2,484 バイト追加 、 2020年9月11日 (金) 16:01
===データの並べ替え===
*https://firebase.google.com/docs/firestore/query-data/order-limit-data?hl=ja
====AngularFire====
*https://github.com/angular/angularfire/blob/master/docs/firestore/querying-collections.md
 
===Document Fetch===
 
<pre>
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();
}
</pre>
 
<pre>
ngOnInit(): void {
let outer = this;
this.bookService.getInformations().subscribe({
next(p){
p.forEach(d => {
outer.infoCards.push(d.data() as InformationCard);
});
}
});
}
</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>

案内メニュー