title: Easy Firebase Cloud Functions organization
body: <h1>Problem</h1>
  <p>After researching the Firebase docs, videos, StackOverflow, lots of articles, ... organizing multiple (lots of) cloud functions in an &quot;easy&quot; way is not obvious. Especially, since the official Firebase docs do not provide a clear vision/recommendation. Actually, the real problem is the lack of clear documentation on how to setup a Firebase project with lots of functions right from the beginning.</p>
  <p>I am trying to find an easy approach considering the following points:</p>
  <ul>
  <li>Based on Firebase docs (that every new Firebase user is reading)</li>
  <li>One single index.js requirement for Firebase CLI deployments</li>
  <li>Using plain JavaScript over TypeScript, not too much syntactic magic sugar</li>
  <li>Transparent control over which files/functions are included/referenced in the index.js
  <ul>
  <li>Developing lots of functions, but don't export all in the index.js directly (kind of deployment control layer)</li>
  <li>Not too much async/await imports like in this example <a href="https://medium.com/firebase-developers/organize-cloud-functions-for-max-cold-start-performance-and-readability-with-typescript-and-9261ee8450f0" rel="nofollow noreferrer">https://medium.com/firebase-developers/organize-cloud-functions-for-max-cold-start-performance-and-readability-with-typescript-and-9261ee8450f0</a></li>
  <li>Not depending on naming conventions like in this nice example <a href="https://codeburst.io/organizing-your-firebase-cloud-functions-67dc17b3b0da" rel="nofollow noreferrer">https://codeburst.io/organizing-your-firebase-cloud-functions-67dc17b3b0da</a></li>
  </ul>
  </li>
  <li>Scalability in terms of # of functions
  <ul>
  <li>Going from initial setup (beginner using Firebase docs) to advanced, how to migrate the structure?</li>
  <li>Minimize effort for adding/deploying more and more cloud functions</li>
  </ul>
  </li>
  <li>Performance: Lazy loading/initialization the admin SDK + ensure it's initialized only <strong>once</strong>
  <ul>
  <li>Minimizing server instance cold-start time</li>
  </ul>
  </li>
  <li>Easy to explain, easy to use, easy to maintain (sure it's a bit subjective)</li>
  </ul>
  <h1>Solution?</h1>
  <p>Coming from manually deployed cloud functions to a simple initial CLI setup using JavaScript, I tried the following file structure with some success:</p>
  <pre><code>[project]/
  - functions/
    - index.js
    - src/
      - functionA.js
      - functionB.js
      - ...
  ...
  </code></pre>
  <h3>index.js</h3>
  <p>Structure based on the official docs: <a href="https://firebase.google.com/docs/functions/organize-functions" rel="nofollow noreferrer">https://firebase.google.com/docs/functions/organize-functions</a></p>
  <pre class="lang-js prettyprint-override"><code>const functions = require('firebase-functions');
  
  const functionA = require('./src/FunctionA');
  exports.FunctionA = functionA.FunctionA;
  
  const functionB = require('./src/FunctionB');
  exports.FunctionB = functionA.FunctionB;
  
  </code></pre>
  <h3>FunctionA.js</h3>
  <p>Using <a href="https://gist.github.com/saintplay/3f965e0aea933a1129cc2c9a823e74d7#file-index-js" rel="nofollow noreferrer">https://gist.github.com/saintplay/3f965e0aea933a1129cc2c9a823e74d7#file-index-js</a></p>
  <pre class="lang-js prettyprint-override"><code>const functions = require('firebase-functions');
  const admin = require('firebase-admin');
  
  // Prevent firebase from initializing twice
  try { admin.initializeApp() } catch (e) {console.log(e);}
  
  exports.FunctionA = ...
  </code></pre>
  <h3>FunctionB.js</h3>
  <pre class="lang-js prettyprint-override"><code>const functions = require('firebase-functions');
  const admin = require('firebase-admin');
  
  // Prevent firebase from initializing twice
  try { admin.initializeApp() } catch (e) {console.log(e);}
  
  exports.FunctionB = ...
  </code></pre>
  <h1>Question(s)</h1>
  <ul>
  <li>Is this a practicable and viable solution?</li>
  <li>Is there any performance concern compared to async/await imports?</li>
  <li>Is the try-catch block around <code>admin.initializeApp()</code> a clean implementation?</li>
  <li>What would be the Typescript equivalent solution?</li>
  <li>Cold-start time: When deploying via Firebase CLI, I notice in the Google Cloud Console &quot;Cloud Functions&quot; section that all cloud function instances contain source code of all other functions
  <ul>
  <li>When creating functions manually in the cloud console, each function only has it's own piece of code to be deployed/initialized/executed</li>
  </ul>
  </li>
  <li>Any suggestions on optimization? (e.g. maintainability of index.js)</li>
  </ul>