From a517161d89a2cce3663f5088d1617ea61e2f2241 Mon Sep 17 00:00:00 2001 From: Sebastian Schmidt Date: Tue, 18 Aug 2020 14:05:40 +0200 Subject: [PATCH 1/3] Defer database URL from Project ID --- packages/database/src/core/RepoManager.ts | 21 +++++++++++---------- packages/database/test/database.test.ts | 16 +++++++++++++++- 2 files changed, 26 insertions(+), 11 deletions(-) diff --git a/packages/database/src/core/RepoManager.ts b/packages/database/src/core/RepoManager.ts index 5ec5e7bc753..a8d8032684e 100644 --- a/packages/database/src/core/RepoManager.ts +++ b/packages/database/src/core/RepoManager.ts @@ -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'; @@ -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: @@ -101,13 +98,17 @@ export class RepoManager { authProvider: Provider, 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); diff --git a/packages/database/test/database.test.ts b/packages/database/test/database.test.ts index 1187032eb32..5f8ae91663c 100644 --- a/packages/database/test/database.test.ts +++ b/packages/database/test/database.test.ts @@ -17,7 +17,7 @@ import { expect } from 'chai'; import firebase from '@firebase/app'; -import { DATABASE_ADDRESS, createTestApp } from './helpers/util'; +import { DATABASE_ADDRESS, createTestApp, DATABASE_URL } from './helpers/util'; import '../index'; describe('Database Tests', () => { @@ -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; From eb149ac25af6207f7b2ced0cff4db1f51de20144 Mon Sep 17 00:00:00 2001 From: Sebastian Schmidt Date: Tue, 18 Aug 2020 14:07:03 +0200 Subject: [PATCH 2/3] Create great-dolphins-tie.md --- .changeset/great-dolphins-tie.md | 5 +++++ 1 file changed, 5 insertions(+) create mode 100644 .changeset/great-dolphins-tie.md diff --git a/.changeset/great-dolphins-tie.md b/.changeset/great-dolphins-tie.md new file mode 100644 index 00000000000..d6413844bfb --- /dev/null +++ b/.changeset/great-dolphins-tie.md @@ -0,0 +1,5 @@ +--- +"@firebase/database": patch +--- + +The SDK can now infer a default database URL if none is provided in the config. From 452c9b68721421bc09cd222a1ee9c9b0b291f76a Mon Sep 17 00:00:00 2001 From: Sebastian Schmidt Date: Tue, 18 Aug 2020 14:46:23 +0200 Subject: [PATCH 3/3] Update database.test.ts --- packages/database/test/database.test.ts | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/packages/database/test/database.test.ts b/packages/database/test/database.test.ts index 5f8ae91663c..67dc3653462 100644 --- a/packages/database/test/database.test.ts +++ b/packages/database/test/database.test.ts @@ -17,7 +17,7 @@ import { expect } from 'chai'; import firebase from '@firebase/app'; -import { DATABASE_ADDRESS, createTestApp, DATABASE_URL } from './helpers/util'; +import { DATABASE_ADDRESS, createTestApp } from './helpers/util'; import '../index'; describe('Database Tests', () => {