Skip to content
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

Importing RTDB code via @firebase/database #112

Merged
merged 9 commits into from
Nov 8, 2017
Merged

Conversation

hiranya911
Copy link
Contributor

  • Took a dependency on @firebase/database module.
  • Removed the old minified database.js file.
  • Statically loading RTDB implementation on to admin namespace and FirebaseApp.

@hiranya911
Copy link
Contributor Author

Related to #54

/**
* Deletes the service and its associated resources.
*
* @return {Promise<()>} An empty Promise that will be fulfilled when the service is deleted.
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Promise<void>

Copy link
Contributor Author

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 (see auth.ts for example). Perhaps we keep this as it is for now, and change them all to Promise<void> in a future PR?

Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Ok that is fine.

let db: Database = this.databases[dbUrl];
db.INTERNAL.delete();
}
return Promise.resolve(undefined);
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Promise.resolve()

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Same comment as above. We are currently doing Promise.resolve(undefined) in other modules.

Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Ok.


let db: Database = this.INTERNAL.databases[dbUrl];
if (typeof db === 'undefined') {
let rtdb = require('@firebase/database');
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Why do you do this when you already:
import {Database} from '@firebase/database'; ?

Copy link
Contributor Author

Choose a reason for hiding this comment

The 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 initStandalone(), which cannot be imported like other types due to the way JS SDK exposes it.

Copy link
Contributor

Choose a reason for hiding this comment

The 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.

constructor(app: FirebaseApp) {
if (!validator.isNonNullObject(app) || !('options' in app)) {
throw new FirebaseDatabaseError({
code: 'database/invalid-argument',
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

FirebaseDatabaseError already adds the database/ prefix in the code.

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Done


afterEach(() => {
return database.INTERNAL.delete().then(() => {
return mockApp.delete();
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

nit: 2 space indent instead of 4

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Done

it('should return a cached version of Database on subsequent calls', () => {
const db1: Database = database.getDatabase(mockApp.options.databaseURL);
const db2: Database = database.getDatabase(mockApp.options.databaseURL);
expect(db1).to.deep.equal(db2);
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I would expect this to be equal instead of deep equal. Why are you returning different references each time?

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Done. It was just a misunderstanding on my part about the semantics of equal(). Thanks for pointing it out.

Copy link

@jshcrowthe jshcrowthe left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Took a look at this and ran the tests myself. All seems to look good here!

I did update NPM (now v5.5.1) and running npm install updated your package-lock.json so keep that in mind.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

Successfully merging this pull request may close these issues.

3 participants