0

I have this Firebase v8 syntax to push the Stripe checkout items into Firestore and I would like to migrate it to Firebase v9 please

import * as admin from "firebase-admin";

const serviceAccount = require("../../../permissions.json");
const app = !admin.apps.length ? admin.initializeApp({
   credential: admin.credential.cert(serviceAccount) }) : admin.app();


const pushIntoStore = async (session) => {
   return app.firestore()
.collection("users")
.doc(session.metadata.email)
.collection("orders")
.doc(session.id).set({ 
  amount: session.amount_total / 100,
  amount_shipping: session.total_details.amount_shipping / 100,
  images: JSON.parse(session.metadata.images),
  timestamp: admin.firestore.FieldValue.serverTimestamp()
})
.then( () => {
console.log(`SUCCESS: Order ${session.id} has been added to the DB`)
});
};
 

all of it please. I have the session and the data so don't worry about that. I just need all Firebase code refactored, plus the timestamp part also please, thanks! :) the above code basically creates a collection users then inserts a new document with a custom id which is - session.metadata.email -> then further on for each custom id it creates another collection called orders in which it sets document with another custom id which is session.id - and inside the document sets the data object. Thanks again!

Gourav B
  • 864
  • 5
  • 17
lifecycles
  • 115
  • 3
  • 10

1 Answers1

1

The document, I shared in the comment, is for Firebase Web SDK upgradation from version 8 to version 9.

For the Firebase Admin SDK, as you are using javascript, the latest available version is Firebase Admin SDK v10 as mentioned in the release notes and I would suggest you look at the Firebase Admin SDK version 10.

The main feature for the latest version 10 is that support for Node.js 10 is discontinued and the SDK has adopted a modular API pattern. So make sure to update Node.js version to 12 or higher and then upgrade the Firebase Admin SDK to version 10. To know more about the releases and what is new with each updated SDK version you can go through this document.

To get a detailed description of the steps to upgrade you can go through this Firebase documentation.

Prabir
  • 1,415
  • 4
  • 10