Skip to content

Commit d347c6c

Browse files
Infer database URL from Project ID (#3650)
1 parent cc16b4a commit d347c6c

File tree

3 files changed

+30
-10
lines changed

3 files changed

+30
-10
lines changed

.changeset/great-dolphins-tie.md

+5
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,5 @@
1+
---
2+
"@firebase/database": patch
3+
---
4+
5+
The SDK can now infer a default database URL if none is provided in the config.

packages/database/src/core/RepoManager.ts

+11-10
Original file line numberDiff line numberDiff line change
@@ -18,7 +18,7 @@
1818
import { FirebaseApp } from '@firebase/app-types';
1919
import { safeGet, CONSTANTS } from '@firebase/util';
2020
import { Repo } from './Repo';
21-
import { fatal } from './util/util';
21+
import { fatal, log } from './util/util';
2222
import { parseRepoInfo } from './util/libs/parser';
2323
import { validateUrl } from './util/validation';
2424
import './Repo_transaction';
@@ -32,9 +32,6 @@ import {
3232
FirebaseAuthTokenProvider
3333
} from './AuthTokenProvider';
3434

35-
/** @const {string} */
36-
const DATABASE_URL_OPTION = 'databaseURL';
37-
3835
/**
3936
* This variable is also defined in the firebase node.js admin SDK. Before
4037
* modifying this definition, consult the definition in:
@@ -101,13 +98,17 @@ export class RepoManager {
10198
authProvider: Provider<FirebaseAuthInternalName>,
10299
url?: string
103100
): Database {
104-
let dbUrl: string | undefined = url || app.options[DATABASE_URL_OPTION];
101+
let dbUrl: string | undefined = url || app.options.databaseURL;
105102
if (dbUrl === undefined) {
106-
fatal(
107-
"Can't determine Firebase Database URL. Be sure to include " +
108-
DATABASE_URL_OPTION +
109-
' option when calling firebase.initializeApp().'
110-
);
103+
if (!app.options.projectId) {
104+
fatal(
105+
"Can't determine Firebase Database URL. Be sure to include " +
106+
' a Project ID when calling firebase.initializeApp().'
107+
);
108+
}
109+
110+
log('Using default host for project ', app.options.projectId);
111+
dbUrl = `${app.options.projectId}-default-rtdb.firebaseio.com`;
111112
}
112113

113114
let parsedUrl = parseRepoInfo(dbUrl);

packages/database/test/database.test.ts

+14
Original file line numberDiff line numberDiff line change
@@ -105,6 +105,20 @@ describe('Database Tests', () => {
105105
expect(db.ref().toString()).to.equal('https://localhost/');
106106
});
107107

108+
it('Can infer database URL from project Id', async () => {
109+
const app = firebase.initializeApp(
110+
{ projectId: 'abc123' },
111+
'project-id-app'
112+
);
113+
const db = app.database();
114+
expect(db).to.be.ok;
115+
// The URL is assumed to be secure if no port is specified.
116+
expect(db.ref().toString()).to.equal(
117+
'https://abc123-default-rtdb.firebaseio.com/'
118+
);
119+
await app.delete();
120+
});
121+
108122
it('Can read ns query param', () => {
109123
const db = defaultApp.database('http://localhost:80/?ns=foo&unused=true');
110124
expect(db).to.be.ok;

0 commit comments

Comments
 (0)