9.4.4.3 최근 작성한 포스트 불러오기

    이번에는 최근 작성한 포스트를 불러오는 기능을 구현해보겠습니다. 즉, 포스트 목록을 한번 불러온 시점에서 그 이후에 새로 작성한 포스트를 불러오는 것이죠.

    우선 해당 기능을 구현하기 위한 함수를 작성해볼까요? getNewerPosts라는 함수를 작성해볼 텐데 원리는 getOlderPosts와 비슷합니다. 함수의 코드는 getOlderPosts를 그대로 복사/붙여넣기하고 startAfterendBefore로 교체하면 됩니다.

    lib/posts.js - getNewerPosts

    (...)
    export async function getNewerPosts(id) {
      const cursorDoc = await postsCollection.doc(id).get();
      const snapshot = await postsCollection
        .orderBy('createdAt', 'desc')
        .endBefore(cursorDoc)
        .limit(PAGE_SIZE)
        .get();
    
      const posts = snapshot.docs.map((doc) => ({
        id: doc.id,
        ...doc.data(),
      }));
    
      return posts;
    }
    신간 소식 구독하기
    뉴스레터에 가입하시고 이메일로 신간 소식을 받아 보세요.