Skip to content
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
2 changes: 1 addition & 1 deletion packages/database-types/index.d.ts
Original file line number Diff line number Diff line change
Expand Up @@ -37,7 +37,7 @@ export class FirebaseDatabase {
app: FirebaseApp;
goOffline(): void;
goOnline(): void;
ref(path?: string): Reference;
ref(path?: string | Reference): Reference;
refFromURL(url: string): Reference;
}

Expand Down
21 changes: 16 additions & 5 deletions packages/database/src/api/Database.ts
Original file line number Diff line number Diff line change
Expand Up @@ -62,15 +62,26 @@ export class Database implements FirebaseService {
}

/**
* Returns a reference to the root or the path specified in opt_pathString.
* @param {string=} pathString
* Returns a reference to the root or to the path specified in the provided
* argument.

* @param {string|Reference=} path The relative string path or an existing
* Reference to a database location.
* @throws If a Reference is provided, throws if it does not belong to the
* same project.
* @return {!Reference} Firebase reference.
*/
ref(pathString?: string): Reference {
**/
ref(path?: string): Reference;
ref(path?: Reference): Reference;
ref(path?: string | Reference): Reference {
this.checkDeleted_('ref');
validateArgCount('database.ref', 0, 1, arguments.length);

return pathString !== undefined ? this.root_.child(pathString) : this.root_;
if (path instanceof Reference) {
return this.refFromURL(path.toString());
}

return path !== undefined ? this.root_.child(path) : this.root_;
}

/**
Expand Down
22 changes: 22 additions & 0 deletions packages/database/test/database.test.ts
Original file line number Diff line number Diff line change
Expand Up @@ -149,13 +149,35 @@ describe('Database Tests', function() {
expect(ref.key).to.equal('grand-child');
});

it('Can get ref from ref', function() {
const db1 = (firebase as any).database();
const db2 = (firebase as any).database();

const ref1 = db1.ref('child');
const ref2 = db2.ref(ref1);

expect(ref1.key).to.equal('child');
expect(ref2.key).to.equal('child');
});

it('ref() validates arguments', function() {
const db = (firebase as any).database();
expect(function() {
const ref = (db as any).ref('path', 'extra');
}).to.throw(/Expects no more than 1/);
});

it('ref() validates project', function() {
const db1 = defaultApp.database('http://bar.foo.com');
const db2 = defaultApp.database('http://foo.bar.com');

const ref1 = db1.ref('child');

expect(function() {
db2.ref(ref1);
}).to.throw(/does not match.*database/i);
});

it('Can get refFromURL()', function() {
const db = (firebase as any).database();
const ref = db.refFromURL(TEST_PROJECT.databaseURL + '/path/to/data');
Expand Down