Firebase seeder
Having rich seed data helps improve the developer’s mindset that he/she is working on an almost finished product.
In this article, we are going to seed our firebase project with data and images that are needed for our epic games clone.
Resources
Repository: https://github.com/karthickthankyou/firebase-seed
I did not commit the images as I did not feel that is right. I chose to do this project with the actual data following the below judgment.
Here, we use the scraped available for public users for this educational project. You don't even have to be logged in to access the 578 games. And still, I did not add the images in the repository as They may become problematic in terms of the distribution of multimedia.
You can find the complete data along with the similar items we generated with our recommender system we added in the json format in the above repository.
Prerequisites
- Create a Firebase project.
- Enable Firestore and Storage.
1. Initialize
Create a folder, and initialize a node project within that using the below command.
npm init -y
2. Setup firebase admin SDK
You will see a package.json created for us. Create an index.js file in the root. Install firebase admin and uuid to create unique ids into the project.
npm i firebase-admin uuid
Now, we have to configure firebase to become the admin for the firebase project. Navigate to the service accounts tab of your project.
Project settings → Service accounts
Click on the cog near Project Overview and select Project settings. Then select the service accounts.
Click on Generate new private key button. That will generate and download the key in a json format to your computer. *Keep the file safe. Anyone with that file can become the admin to your project!*
Drag the file onto your project.
The same page also tells you how to use the json we downloaded.
var admin = require("firebase-admin");
var serviceAccount = require("path/to/serviceAccountKey.json");
admin.initializeApp({
credential: admin.credential.cert(serviceAccount)
storageBucket: 'epicgames-clone.appspot.com',
});
Use the above code in the index.js file we created. The epicgames-clone.appspot.com is the name of the bucket. You can open your storage page and see something like gs://epicgames-clone.appspot.com at the top. Copy the bucket name without the gs:// prefix.
Create the db and bucket instances as below.
const db = admin.firestore()
const bucket = admin.storage().bucket()
Create a sample document
Let's check if the setup works by creating a dummy document in Firestore.
Go to your Firestore interface in the browser and refresh. Usually, firestore’s web interface works in real-time. But when we create a new collection, we have to refresh to see the changes.
If you don’t see any collection or document getting created, follow the previous steps carefully.
Getting all documents in
As our trial above is working, I’m going to get the json files and loops through them.
Also, If you were wondering why the publishers' data is organized in a bit of an awkward way. You are right. Should have cleaned. But let's proceed.
Upload images
Notes:
- The destination is set to images and subImages accordingly.
- The cacheControl is set to a year using seconds. That is necessary as the browse looks for this property in the image and caches it.
cacheControl: 'public, max-age=31536000',
Seeder scripts
Create a file seedDocuments.js and run the function we created above.
const { createAllDocuments } = require('./index.js')
createAllDocuments()
seedImages.js
const { uploadImages } = require('./index.js')
uploadImages()
Add the below scripts in the package.json
"seed:docs": "node seedDocuments",
"seed:images": "node uploadFiles"
Run the scripts one by one to see the data getting populated in your firebase storage.
npm run seed:docs
npm run seed:images
Thank you.