Uh oh!
There was an error while loading. Please reload this page.
- Notifications
You must be signed in to change notification settings - Fork 419
Importing RTDB code via @firebase/database#112
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
Uh oh!
There was an error while loading. Please reload this page.
Changes from all commits
3e999e7b5df0437f0b011c6c13845559291e79283d53d93a628f300e09ea4a6File filter
Filter by extension
Conversations
Uh oh!
There was an error while loading. Please reload this page.
Jump to
Uh oh!
There was an error while loading. Please reload this page.
Diff view
Diff view
There are no files selected for viewing
Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.
Uh oh!
There was an error while loading. Please reload this page.
This file was deleted.
Uh oh!
There was an error while loading. Please reload this page.
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,86 @@ | ||
| import {FirebaseApp} from '../firebase-app'; | ||
| import {FirebaseDatabaseError} from '../utils/error'; | ||
| import {FirebaseServiceInterface, FirebaseServiceInternalsInterface} from '../firebase-service'; | ||
| import {Database} from '@firebase/database'; | ||
| import * as validator from '../utils/validator'; | ||
| /** | ||
| * Internals of a Database instance. | ||
| */ | ||
| class DatabaseInternals implements FirebaseServiceInternalsInterface { | ||
| public databases: { | ||
| [dbUrl: string]: Database | ||
| } = {}; | ||
| /** | ||
| * Deletes the service and its associated resources. | ||
| * | ||
| * @return {Promise<()>} An empty Promise that will be fulfilled when the service is deleted. | ||
| */ | ||
| public delete(): Promise<void> { | ||
| for (const dbUrl of Object.keys(this.databases)) { | ||
| let db: Database = this.databases[dbUrl]; | ||
| db.INTERNAL.delete(); | ||
| } | ||
| return Promise.resolve(undefined); | ||
Contributor There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more.
ContributorAuthor There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Same comment as above. We are currently doing Contributor There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Ok. | ||
| } | ||
| } | ||
| export class DatabaseService implements FirebaseServiceInterface { | ||
| public INTERNAL: DatabaseInternals = new DatabaseInternals(); | ||
| private appInternal: FirebaseApp; | ||
| constructor(app: FirebaseApp) { | ||
| if (!validator.isNonNullObject(app) || !('options' in app)) { | ||
| throw new FirebaseDatabaseError({ | ||
| code: 'invalid-argument', | ||
| message: 'First argument passed to admin.database() must be a valid Firebase app instance.', | ||
| }); | ||
| } | ||
| this.appInternal = app; | ||
| } | ||
| /** | ||
| * Returns the app associated with this DatabaseService instance. | ||
| * | ||
| * @return {FirebaseApp} The app associated with this DatabaseService instance. | ||
| */ | ||
| get app(): FirebaseApp { | ||
| return this.appInternal; | ||
| } | ||
| public getDatabase(url?: string): Database { | ||
| let dbUrl: string = this.ensureUrl(url); | ||
| if (!validator.isNonEmptyString(dbUrl)) { | ||
| throw new FirebaseDatabaseError({ | ||
| code: 'invalid-argument', | ||
| message: 'Database URL must be a valid, non-empty URL string.', | ||
| }); | ||
| } | ||
| let db: Database = this.INTERNAL.databases[dbUrl]; | ||
| if (typeof db === 'undefined') { | ||
| let rtdb = require('@firebase/database'); | ||
Contributor There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Why do you do this when you already: ContributorAuthor There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. This is a lazy loading tactic we used with Firestore. This ensures that the RTDB implementation does not get loaded until somebody actually makes a DB call. Import statement only loads the type definition. Plus we need to call Contributor There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Not sure how lazy loading is useful in a backend server environment. Anyway, thanks for the explanation. | ||
| db = rtdb.initStandalone(this.appInternal, dbUrl).instance; | ||
| this.INTERNAL.databases[dbUrl] = db; | ||
| } | ||
| return db; | ||
| } | ||
| private ensureUrl(url?: string): string { | ||
| if (typeof url !== 'undefined') { | ||
| return url; | ||
| } else if (typeof this.appInternal.options.databaseURL !== 'undefined') { | ||
| return this.appInternal.options.databaseURL; | ||
| } | ||
| throw new FirebaseDatabaseError({ | ||
| code: 'invalid-argument', | ||
| message: 'Can\'t determine Firebase Database URL.', | ||
| }); | ||
| } | ||
| } | ||
Uh oh!
There was an error while loading. Please reload this page.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Promise<void>There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
We have been using
Promise<()>in other modules (seeauth.tsfor example). Perhaps we keep this as it is for now, and change them all toPromise<void>in a future PR?There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Ok that is fine.