「Cloud Firestore」の版間の差分
ナビゲーションに移動
検索に移動
(ページの作成:「==Cloud Firestore==」) |
|||
| (同じ利用者による、間の7版が非表示) | |||
| 1行目: | 1行目: | ||
| − | == | + | | [[Angular]] | [[Firebase]] | [[TypeScript]] | [[Google Cloud Platform]] | [https://www.typea.info/blog/index.php/category/firebase/ ブログカテゴリ(Firebase)] | |
| + | |||
| + | *https://firebase.google.com/docs/firestore?hl=ja | ||
| + | |||
| + | ===データの並べ替え=== | ||
| + | *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> | ||
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 矢木浩人