Skip to content

Commit

Permalink
Infer database URL from Project ID (#3650)
Browse files Browse the repository at this point in the history
  • Loading branch information
schmidt-sebastian authored Sep 8, 2020
1 parent cc16b4a commit d347c6c
Show file tree
Hide file tree
Showing 3 changed files with 30 additions and 10 deletions.
5 changes: 5 additions & 0 deletions .changeset/great-dolphins-tie.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,5 @@
---
"@firebase/database": patch
---

The SDK can now infer a default database URL if none is provided in the config.
21 changes: 11 additions & 10 deletions packages/database/src/core/RepoManager.ts
Original file line number Diff line number Diff line change
Expand Up @@ -18,7 +18,7 @@
import { FirebaseApp } from '@firebase/app-types';
import { safeGet, CONSTANTS } from '@firebase/util';
import { Repo } from './Repo';
import { fatal } from './util/util';
import { fatal, log } from './util/util';
import { parseRepoInfo } from './util/libs/parser';
import { validateUrl } from './util/validation';
import './Repo_transaction';
Expand All @@ -32,9 +32,6 @@ import {
FirebaseAuthTokenProvider
} from './AuthTokenProvider';

/** @const {string} */
const DATABASE_URL_OPTION = 'databaseURL';

/**
* This variable is also defined in the firebase node.js admin SDK. Before
* modifying this definition, consult the definition in:
Expand Down Expand Up @@ -101,13 +98,17 @@ export class RepoManager {
authProvider: Provider<FirebaseAuthInternalName>,
url?: string
): Database {
let dbUrl: string | undefined = url || app.options[DATABASE_URL_OPTION];
let dbUrl: string | undefined = url || app.options.databaseURL;
if (dbUrl === undefined) {
fatal(
"Can't determine Firebase Database URL. Be sure to include " +
DATABASE_URL_OPTION +
' option when calling firebase.initializeApp().'
);
if (!app.options.projectId) {
fatal(
"Can't determine Firebase Database URL. Be sure to include " +
' a Project ID when calling firebase.initializeApp().'
);
}

log('Using default host for project ', app.options.projectId);
dbUrl = `${app.options.projectId}-default-rtdb.firebaseio.com`;
}

let parsedUrl = parseRepoInfo(dbUrl);
Expand Down
14 changes: 14 additions & 0 deletions packages/database/test/database.test.ts
Original file line number Diff line number Diff line change
Expand Up @@ -105,6 +105,20 @@ describe('Database Tests', () => {
expect(db.ref().toString()).to.equal('https://localhost/');
});

it('Can infer database URL from project Id', async () => {
const app = firebase.initializeApp(
{ projectId: 'abc123' },
'project-id-app'
);
const db = app.database();
expect(db).to.be.ok;
// The URL is assumed to be secure if no port is specified.
expect(db.ref().toString()).to.equal(
'https://abc123-default-rtdb.firebaseio.com/'
);
await app.delete();
});

it('Can read ns query param', () => {
const db = defaultApp.database('http://localhost:80/?ns=foo&unused=true');
expect(db).to.be.ok;
Expand Down

0 comments on commit d347c6c

Please sign in to comment.