Skip to content

Commit dbed689

Browse files
Database API: Ref from Ref (#534)
1 parent fbb2aab commit dbed689

File tree

3 files changed

+39
-6
lines changed

3 files changed

+39
-6
lines changed

packages/database-types/index.d.ts

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -37,7 +37,7 @@ export class FirebaseDatabase {
3737
app: FirebaseApp;
3838
goOffline(): void;
3939
goOnline(): void;
40-
ref(path?: string): Reference;
40+
ref(path?: string | Reference): Reference;
4141
refFromURL(url: string): Reference;
4242
}
4343

packages/database/src/api/Database.ts

Lines changed: 16 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -62,15 +62,26 @@ export class Database implements FirebaseService {
6262
}
6363

6464
/**
65-
* Returns a reference to the root or the path specified in opt_pathString.
66-
* @param {string=} pathString
65+
* Returns a reference to the root or to the path specified in the provided
66+
* argument.
67+
68+
* @param {string|Reference=} path The relative string path or an existing
69+
* Reference to a database location.
70+
* @throws If a Reference is provided, throws if it does not belong to the
71+
* same project.
6772
* @return {!Reference} Firebase reference.
68-
*/
69-
ref(pathString?: string): Reference {
73+
**/
74+
ref(path?: string): Reference;
75+
ref(path?: Reference): Reference;
76+
ref(path?: string | Reference): Reference {
7077
this.checkDeleted_('ref');
7178
validateArgCount('database.ref', 0, 1, arguments.length);
7279

73-
return pathString !== undefined ? this.root_.child(pathString) : this.root_;
80+
if (path instanceof Reference) {
81+
return this.refFromURL(path.toString());
82+
}
83+
84+
return path !== undefined ? this.root_.child(path) : this.root_;
7485
}
7586

7687
/**

packages/database/test/database.test.ts

Lines changed: 22 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -149,13 +149,35 @@ describe('Database Tests', function() {
149149
expect(ref.key).to.equal('grand-child');
150150
});
151151

152+
it('Can get ref from ref', function() {
153+
const db1 = (firebase as any).database();
154+
const db2 = (firebase as any).database();
155+
156+
const ref1 = db1.ref('child');
157+
const ref2 = db2.ref(ref1);
158+
159+
expect(ref1.key).to.equal('child');
160+
expect(ref2.key).to.equal('child');
161+
});
162+
152163
it('ref() validates arguments', function() {
153164
const db = (firebase as any).database();
154165
expect(function() {
155166
const ref = (db as any).ref('path', 'extra');
156167
}).to.throw(/Expects no more than 1/);
157168
});
158169

170+
it('ref() validates project', function() {
171+
const db1 = defaultApp.database('http://bar.foo.com');
172+
const db2 = defaultApp.database('http://foo.bar.com');
173+
174+
const ref1 = db1.ref('child');
175+
176+
expect(function() {
177+
db2.ref(ref1);
178+
}).to.throw(/does not match.*database/i);
179+
});
180+
159181
it('Can get refFromURL()', function() {
160182
const db = (firebase as any).database();
161183
const ref = db.refFromURL(TEST_PROJECT.databaseURL + '/path/to/data');

0 commit comments

Comments
 (0)