Firestore가 더욱 좋아졌습니다.
Firebase Firestore가 최근에 개선되었으며 이제 'Firestore 데이터 번들'이라는 새로운 기능을 구현합니다. 핵심 개념은 이제 Firestore 쿼리를 바이너리 형식으로 패키징하고 CDN 또는 기타 호스팅 솔루션을 통해 제공 할 수 있다는 것입니다. 사실상 이는 특정 데이터 그룹이 Firestore 백엔드에서 직접 가져 오는 것이 아니라 CDN에 적합 할 수 있으므로 비용을 절약 할 수 있음을 의미합니다.
그런데 왜? 그리고 더 중요한 것은 어떻게?!
"Firestore 데이터 번들"은 실제로 정말 간단하게 설계되었습니다.
- 먼저 Admin SDK를 통해 서버 환경에서 '데이터 번들'참조를 만듭니다.
- 그런 다음이 번들은 표준 Firestore 쿼리와 함께로드됩니다.
- 당신이 가진 것은 예를 들어 파일로 또는 CDN에 저장할 수있는이 데이터의 버퍼입니다.
- 클라이언트 측으로 전환하면 이제 새 Firestore API를 사용하여 CDN에서 실제로 번들을로드 할 수 있습니다.
// On your server ....
/**
* Generate a buffered bundle for user tempaltes.
*/
async function getUserTemplatesBundle(){
// Create a bundle with ID 'user-templates'.
const bundle = firestore.bundle("user-templates");
// A plain Firestore-query.
const snap = await firestore.collection('user-templates').get();
return bundle
.add('user-templates-query', snap)
.build();
}
// ... then store the buffer, e.g. in your CDN ...
// On your client
import firebase from "firebase/app";
import "firebase/firestore";
import "firebase/firestore/bundle";
// ... init 'firestore' ...
async function loadFirestoreBundle() {
// Just a simplified call to our demo CDN
// to fetch the bundle in binary form.
const rawBundle = await fetchBundleFromCDN();
// Now tell Firestore to load the bundle
await firestore.loadBundle(rawBundle);
// Now get a reference to the bundled query, in
// our case it's only one query that got bundled.
const query = await db.namedQuery('user-templates-query');
// Use 'cache' as source so that the data gets
// cached locally - no Firestore queries will
// be executed.
const snap = await query.get({ source: 'cache' });
// Finally, you can handle the snap like every other
// Firestore snap in your app.
}
이러한 데이터 번들의 이점은 예를 살펴보면 더 명확 해집니다. 예를 들어 새 사용자를 만들 때 Firestore의 새 문서에 대해 기본 값 집합을 자주 사용한다고 가정 해보십시오. 이제 데이터베이스에서 직접 가져 오는 대신 'Firestore 데이터 번들'을 사용하여 이러한 값을 CDN에서 캐시 된 문서로로드하여 Firestore 쿼리와 관련된 비용을 피할 수 있습니다.
따라서이 새로운 기능은 다음 요구 사항을 충족하는 문서에 가장 적합합니다. 데이터가 자주 변경되지 않고 문서 수가 그리 크지 않으며이 데이터 세트에 대한 가져 오기가 자주 발생합니다.
또 다른 실제 사례
이상적인 사용 사례로이 프로그레시브 웹 앱을 예로 들어 볼 수도 있습니다. 번역 된 모든 텍스트는 모든 오디오 파일에 대한 URL 참조뿐만 아니라 Firestore 인스턴스에 저장됩니다. 이 데이터는 많이 변경되지 않기 때문에 Firebase 저장소 또는 내 CDN에 캐시하는 데 매우 적합합니다. 이는 잠재적으로 Firestore 비용을 크게 줄일 수 있습니다.
'Firestore 데이터 번들'은 Firebase에 아주 좋은 추가 기능입니다. 지금은 사용 사례를 다루지 않을 수 있지만 서비스가 성장하기 시작하면 매우 유용 할 수 있습니다.