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

MyMemoWiki

「Cloud Firestore」の版間の差分

提供: MyMemoWiki
ナビゲーションに移動 検索に移動
 
(同じ利用者による、間の5版が非表示)
2行目: 2行目:
  
 
*https://firebase.google.com/docs/firestore?hl=ja
 
*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

  1. import { Observable } from 'rxjs';
  2. import { AngularFirestore, QuerySnapshot, DocumentSnapshot, DocumentData } from '@angular/fire/firestore';
  3.  
  4. export const FS_PATH_INFORMATIONS = 'informations/';
  5.  
  6. constructor(
  7. public firestore: AngularFirestore
  8. ) { }
  9.  
  10. public getInformations(): Observable<QuerySnapshot<DocumentData>> {
  11. const path = `${FS_PATH_INFORMATIONS}`;
  12. console.log(`get informations..[${path}]`);
  13. return this.firestore.collection<InformationCard>(path).get();
  14. }
  1. ngOnInit(): void {
  2. let outer = this;
  3. this.bookService.getInformations().subscribe({
  4. next(p){
  5. p.forEach(d => {
  6. outer.infoCards.push(d.data() as InformationCard);
  7. });
  8. }
  9. });
  10. }

Document Query

  1. /** Firestore Bookshelf Path */
  2. export const FS_PATH_BOOKSHELF = 'bookshelf/v1/users/';
  3.  
  4. public getBooksInBookshelfQuery(user: User, sortOrder?:SortOrder, lastBook?:Book): Promise<QuerySnapshot<DocumentData>> {
  5. if (user != null) {
  6. const pagelimit = 100;
  7. const path = `${FS_PATH_BOOKSHELF}${user.uid}/books`;
  8. console.log(`get book in bookshelf..[${path}] order:${sortOrder}`);
  9. console.log(`last book entity is.. ${lastBook}`);
  10.  
  11. const collection = this.firestore.collection<Book>(path);
  12. let query = collection.ref.limit(pagelimit);
  13.  
  14. if (sortOrder == 'nameAsc') {
  15. query = query.orderBy('title', 'asc');
  16. if (lastBook) {
  17. console.log(`where title >= ${lastBook.title}`);
  18. query = query
  19. .where('title', '>=', lastBook.title)
  20. .orderBy('updatedAt', 'desc')
  21. .startAfter(lastBook.title, lastBook.updatedAt);
  22. }
  23. } else {
  24. query = query.orderBy('updatedAt', 'desc');
  25. if (lastBook) {
  26. console.log(`where updatedAt <= ${lastBook.updatedAt}`);
  27. query = query
  28. .where('updatedAt', '<=', lastBook.updatedAt)
  29. .orderBy('title', 'asc')
  30. .startAfter(lastBook.updatedAt, lastBook.title);
  31. }
  32. }
  33. return query.get();
  34. }
  35. }
  1. let outer = this;
  2. this.auth.onAuthStateChanged(async (u) => {
  3. const query = await this.bookService.getBooksInBookshelfQuery(u, sortOrder, lastBook).then();
  4. query.forEach((doc) => {
  5. let book = <Book> doc.data();
  6. book.documentId = doc.id;
  7. outer.books.push(book);
  8. });
  9. });