Home > other >  How to create Firestore Bundles in Node.js?
How to create Firestore Bundles in Node.js?

Time:10-31

As per suggestion on Firebase documentation on Firestore bundles, I haven't found the best way to create bundle based on the example given above. I don't know what I have to include in docSnapshot. I know what I have to fill in collection. But what is the case if CollectionGroup? There is no detail explanation about it, either. Can someone from Firebase team explain what I have to include in docSnapshot? I have collection "Music_Epic", for example?

  const bundleId = "latest-Epic";
  const bundle = firestore.bundle(bundleId);
  const docSnapshot = await firestore.doc("Music_Epic/").get(); //What should be here?
  const querySnapshot = await firestore.collection("Music_Epic").get();

  // Build the bundle
  // Note how querySnapshot is named "latest-stories-query"
  const bundleBuffer = bundle.add(docSnapshot); // Add a document
  bundleBuffer.add("latest-Epic-query", querySnapshot); // Add a named query.
  bundleBuffer.build();

CodePudding user response:

The add() takes either a DocumentSnapshot or (queryName, QuerySnapshot) as parameters. Collection Groups queries also return a QuerySnapshot so you can follow the same method used for querying a single collection.

const bundle = firestore.bundle();
const querySnapshot = await firestore.collection("Music_Epic").get();

// For collectionGroup queries:
// const querySnapshot = await firestore.collectionGroup("col_name").get();

const bundleBuffer = bundle.add('latest_music_epic_query', querySnapshot);
bundleBuffer.build();
  • Related