-1

I have a class "Firebase" in which I write all the functions (that involve this service) used in my server side.

At first, the code was pretty clean and clear, as there were not a lot of functions in this class. Now, the class is super giant (more than 100 functions not very short). This is why I have decided to divide the class into modules (maybe not correct) to improve the clarity and organization of the project.

Something like this:

/* eslint-disable no-empty */
const functions = require("firebase-functions");
const admin = require("firebase-admin");

// Lazy initialization of the admin SDK
try {
     const googleCloudServiceAccount = require("../../utils/json/googleCloudServiceAccount.json");
     admin.initializeApp({
       credential: admin.credential.cert(googleCloudServiceAccount),
       databaseURL: URL,
       storageBucket: BUCKET,
     });
} catch (e) {}

class Firebase {
  constructor() {
    Object.assign(this, {
      auth: admin.auth(),
      firestore: admin.firestore(),
      storage: admin.storage(),
    });
  }
}

Object.assign(Firebase.prototype, {
  /* Methods */
});

module.exports = Firebase;

What I have in mind is to organize the code in modules like "firebase-auth.js", "firebase-storage.js", "firebase-firestore.js"... and finally, require all the modules methods (or something better, because it can be a really long list of imports) and assign them to my Firebase class prototype.

My question is: If in the methods of the modules I need to use Firebase.firestore, Firebase.auth... (which are members of the Firebase class), should I create an instance of the class in each module?

Any ideas? Thank you.

1 Answer 1

1

try this, is not best but works as expectly

create a file with functions in export module like:

module.exports = function (app, dbConnection, firebase) {
       exampleFirebaseFunction(){
       }
}

and in your main file you call it as:

require('./routes/firebase-routes.js')(app, dbConnection, firebase);
Sign up to request clarification or add additional context in comments.

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.