8

When starting the emulator I see 2 databases in the realtime database emulator. One with <project id> and the other <project id>-default-rtdb .

enter image description here

Can anybody explain or reference some docs on why this is?


This is my setup files

firebase.json

{
  "database": {
    "rules": "database.rules.json"
  },
  "functions": {
    "predeploy": [
      "npm --prefix \"$RESOURCE_DIR\" run lint",
      "npm --prefix \"$RESOURCE_DIR\" run build"
    ],
    "source": "functions"
  },
  "emulators": {
    "auth": {
      "port": 9099
    },
    "functions": {
      "port": 5001
    },
    "database": {
      "port": 9000
    },
    "pubsub": {
      "port": 8085
    },
    "ui": {
      "enabled": true
    }
  }
}

functions/package.json

{
  "name": "functions",
  "scripts": {
    "lint": "eslint --ext .js,.ts .",
    "build": "tsc",
    "serve": "npm run build && firebase emulators:start --only functions",
    "shell": "npm run build && firebase functions:shell",
    "start": "npm run shell",
    "deploy": "firebase deploy --only functions",
    "logs": "firebase functions:log"
  },
  "engines": {
    "node": "14"
  },
  "main": "lib/index.js",
  "dependencies": {
    "firebase-admin": "^9.8.0",
    "firebase-functions": "^3.14.1"
  },
  "devDependencies": {
    "@typescript-eslint/eslint-plugin": "^3.9.1",
    "@typescript-eslint/parser": "^3.8.0",
    "eslint": "^7.6.0",
    "eslint-config-google": "^0.14.0",
    "eslint-plugin-import": "^2.22.0",
    "firebase-functions-test": "^0.2.0",
    "typescript": "^3.8.0"
  },
  "private": true
}

Norfeldt
  • 8,272
  • 23
  • 96
  • 152
  • How did you configure things when you ran `firebase init`? What features did you select, and did you create a new project/use an existing project/or no project? – Iguananaut Aug 26 '21 at 23:17
  • @Iguananaut I have added the setup files to the question. – Norfeldt Aug 28 '21 at 19:40
  • 2
    All I could find was [this code](https://github.com/firebase/firebase-tools/blob/44ae32e26e8bae614a698c586b5b35344554a3b3/src/init/features/database.ts#L77) in `firebase init` which, when setting up a project with Realtime Database enabled, will create a default database named `-default-rtdb`. What's mysterious about this is, if the project already exists and already has a database, it shouldn't be creating the default one. But I don't think the emulator code is all open source, so we may never be able to be sure. I think it might be a bug. – Iguananaut Aug 30 '21 at 14:26
  • @Iguananaut thank you for taking a look and trying to make sense of it. I thought I had done something wrong – Norfeldt Aug 30 '21 at 14:48
  • 4
    I don't think you did. I was able to reproduce this problem with a brand new `demo-` project. It's weird. – Iguananaut Aug 30 '21 at 16:00
  • 1
    Same problem here. At least I know I'm not the only one experiencing this issue. – funct7 Jun 24 '23 at 01:10

1 Answers1

1

So I just figured it out. Adding a "database" section to the top level of the firebase.json file makes the emulator create 2 databases. Additionally, if the name of the project in .firebaserc starts with demo AND there's a "database" section added to the top level of your firebase.json file then there will be 3 total Realtime Databases created.

So, having a firebase.json file that looks like this:

{ "database": {
    "rules": "database.rules.json"
  },
  "functions": [
    {
      "source": "functions",
      "codebase": "default",
      "ignore": [
        "node_modules",
        ".git",
        "firebase-debug.log",
        "firebase-debug.*.log"
      ]
    }
  ],
  "emulators": {
    "auth": {
      "port": 9099
    },
    "database": {
      "port": 9000
    },
    "ui": {
      "enabled": true
    },
    "singleProjectMode": true
  }
}

and a .firebaserc file that looks like this:

{
  "projects": {
    "default": "demo-MyProject-development"
  }
}

results in 3 databases being created.

I've decided that that I'm going to go without adding the rules file to my firebase.json (thus removing the "database" section from the file) and writing the rules manually into the browser.

My files now look like this:

firebase.json

{ 
  "functions": [
    {
      "source": "functions",
      "codebase": "default",
      "ignore": [
        "node_modules",
        ".git",
        "firebase-debug.log",
        "firebase-debug.*.log"
      ]
    }
  ],
  "emulators": {
    "auth": {
      "port": 9099
    },
    "database": {
      "port": 9000
    },
    "ui": {
      "enabled": true
    },
    "singleProjectMode": true
  }
}

.firebaserc

{
  "projects": {
    "default": "MyProject-development"
  }
}

WikipediaBrown
  • 689
  • 1
  • 8
  • 27