File tree Expand file tree Collapse file tree 3 files changed +39
-6
lines changed Expand file tree Collapse file tree 3 files changed +39
-6
lines changed Original file line number Diff line number Diff line change @@ -37,7 +37,7 @@ export class FirebaseDatabase {
37
37
app : FirebaseApp ;
38
38
goOffline ( ) : void ;
39
39
goOnline ( ) : void ;
40
- ref ( path ?: string ) : Reference ;
40
+ ref ( path ?: string | Reference ) : Reference ;
41
41
refFromURL ( url : string ) : Reference ;
42
42
}
43
43
Original file line number Diff line number Diff line change @@ -62,15 +62,26 @@ export class Database implements FirebaseService {
62
62
}
63
63
64
64
/**
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.
67
72
* @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 {
70
77
this . checkDeleted_ ( 'ref' ) ;
71
78
validateArgCount ( 'database.ref' , 0 , 1 , arguments . length ) ;
72
79
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_ ;
74
85
}
75
86
76
87
/**
Original file line number Diff line number Diff line change @@ -149,13 +149,35 @@ describe('Database Tests', function() {
149
149
expect ( ref . key ) . to . equal ( 'grand-child' ) ;
150
150
} ) ;
151
151
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
+
152
163
it ( 'ref() validates arguments' , function ( ) {
153
164
const db = ( firebase as any ) . database ( ) ;
154
165
expect ( function ( ) {
155
166
const ref = ( db as any ) . ref ( 'path' , 'extra' ) ;
156
167
} ) . to . throw ( / E x p e c t s n o m o r e t h a n 1 / ) ;
157
168
} ) ;
158
169
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 ( / d o e s n o t m a t c h .* d a t a b a s e / i) ;
179
+ } ) ;
180
+
159
181
it ( 'Can get refFromURL()' , function ( ) {
160
182
const db = ( firebase as any ) . database ( ) ;
161
183
const ref = db . refFromURL ( TEST_PROJECT . databaseURL + '/path/to/data' ) ;
You can’t perform that action at this time.
0 commit comments