0

I'm using firebase functions with Node.js and I'm trying to create multiple environments for that. As far as I read I just need to create separate projects for that in Firebase, which I did.

I'm using Flamelink as well and I want to achieve the same. I actually have a Bonfire plan for Flamelink that allows multiple environments. My concern is that the different environments in Flamelink write into the same database in Firebase separating it only with a flag of environment, so whenever I want to query something from the db I also have to specify my environment as well.

Is there a way to have different databases for different Flamelink environments with my setup, so I only specify the environment in my config and not in my queries?

Zolcsak Tamas
  • 147
  • 1
  • 8

1 Answers1

0

Currently it is not possible to have a database per environment using Flamelink. The only way to achieve this is to add both projects to Flamelink.

The Flamelink JS SDK can however be used within a cloud function and would alleviate some of the complexity working with multiple environments.

The Flamelink JS SDK takes in an environment parameter (along with some others, like locale and database type) when it is initialised, contextualising the use of the SDK methods with the environment.

import * as functions from 'firebase-functions';
import * as admin from 'firebase-admin';
import * as flamelink from 'flamelink/app';
import 'flamelink/content';

admin.initializeApp();
const firebaseApp = admin.app();

const flApp = flamelink({
  firebaseApp,
  dbType: 'cf',
  env: 'staging',
  locale: 'en-US',
});

export const testFunction = functions.https.onRequest(async(request, response) => {
  if (request.query.env) {
    flApp.settings.setEnvironment(request.query.env) // example 'production'
  }

  try {
    const posts = await flApp.content.get({ schemaKey: 'blogPosts' })
    res.status(200).json({ posts })
  } catch (e) {
    // handle error
  }
});

Depending on your connected front-end framework/language you can pass in the environment using environment variables

JS client example

const env = (process.env.FLAMELINK_DATA_ENV || 'staging').toLowerCase()
await fetch(`https://yourhost.cloudfunctions.net/testFunction?env=${env}`)
  • Thanks for the help. This indeed works, but I'd need different database for different environment, but I guess my only option is to have a separate project in flamelink that connects to another firebase project. – Zolcsak Tamas Aug 18 '20 at 12:14