Skip to content

Infer database URL from Project ID #3650

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Merged
merged 5 commits into from
Sep 8, 2020
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
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