@@ -10,18 +10,6 @@ import Query from './query.js';
1010
1111const FirestackDatabase = NativeModules . FirestackDatabase ;
1212
13- /**
14- * TODO ? why is it here and in index? make it a util
15- * @param modifiers
16- * @returns {* }
17- */
18- function getModifiersString ( modifiers ) {
19- if ( ! modifiers || ! Array . isArray ( modifiers ) ) {
20- return '' ;
21- }
22- return modifiers . join ( '|' ) ;
23- }
24-
2513// https://firebase.google.com/docs/reference/js/firebase.database.Reference
2614let uid = 0 ;
2715
@@ -34,7 +22,7 @@ export default class Reference extends ReferenceBase {
3422 query : Query ;
3523 uid : number ;
3624
37- constructor ( db : FirestackDatabase , path : Array < string > , existingModifiers ?: { [ key : string ] : string } ) {
25+ constructor ( db : FirestackDatabase , path : Array < string > , existingModifiers ?: Array < string > ) {
3826 super ( db . firestack , path ) ;
3927
4028 this . db = db ;
@@ -73,8 +61,8 @@ export default class Reference extends ReferenceBase {
7361 // Get the value of a ref either with a key
7462 getAt ( ) {
7563 const path = this . dbPath ( ) ;
76- const modifiers = this . dbModifiers ( ) ;
77- const modifiersString = getModifiersString ( modifiers ) ;
64+ const modifiers = this . query . getModifiers ( ) ;
65+ const modifiersString = this . query . getModifiersString ( ) ;
7866 return promisify ( 'onOnce' , FirestackDatabase ) ( path , modifiersString , modifiers , 'value' ) ;
7967 }
8068
@@ -108,20 +96,20 @@ export default class Reference extends ReferenceBase {
10896
10997 on ( evt ?: string , cb : ( ) => any ) {
11098 const path = this . dbPath ( ) ;
111- const modifiers = this . dbModifiers ( ) ;
112- const modifiersString = getModifiersString ( modifiers ) ;
99+ const modifiers = this . query . getModifiers ( ) ;
100+ const modifiersString = this . query . getModifiersString ( ) ;
113101 this . log . debug ( 'adding reference.on' , path , modifiersString , evt ) ;
114102 return this . db . storeRef ( this . uid , this ) . then ( ( ) => {
115- return this . db . on ( this . uid , path , modifiers , evt , cb ) . then ( subscriptions => {
103+ return this . db . on ( this . uid , path , modifiersString , modifiers , evt , cb ) . then ( subscriptions => {
116104 this . listeners [ evt ] = subscriptions ;
117105 } ) ;
118106 } ) ;
119107 }
120108
121109 once ( evt ?: string = 'once' , cb : ( snapshot : Object ) => void ) {
122110 const path = this . dbPath ( ) ;
123- const modifiers = this . dbModifiers ( ) ;
124- const modifiersString = getModifiersString ( modifiers ) ;
111+ const modifiers = this . query . getModifiers ( ) ;
112+ const modifiersString = this . query . getModifiersString ( ) ;
125113 return this . db . storeRef ( this . uid , this ) . then ( ( ) => {
126114 return promisify ( 'onOnce' , FirestackDatabase ) ( path , modifiersString , modifiers , evt )
127115 . then ( ( { snapshot } ) => new Snapshot ( this , snapshot ) )
@@ -136,10 +124,10 @@ export default class Reference extends ReferenceBase {
136124
137125 off ( evt : string = '' , origCB ?: ( ) => any ) {
138126 const path = this . dbPath ( ) ;
139- const modifiers = this . dbModifiers ( ) ;
127+ const modifiers = this . query . getModifiers ( ) ;
140128 this . log . debug ( 'ref.off(): ' , path , modifiers , evt ) ;
141129 return this . db . unstoreRef ( this . uid ) . then ( ( ) => {
142- return this . db . off ( this . uid , path , modifiers , evt , origCB ) . then ( subscriptions => {
130+ return this . db . off ( this . uid , path , modifiersString , modifiers , evt , origCB ) . then ( subscriptions => {
143131 // delete this.listeners[eventName];
144132 // this.listeners[evt] = subscriptions;
145133 } ) ;
@@ -184,68 +172,60 @@ export default class Reference extends ReferenceBase {
184172 } , { } ) ;
185173 }
186174
187- // class Query extends Reference {}
188-
189- // let ref = firestack.database().ref('/timeline');
190- // ref.limitToLast(1).on('child_added', () => {});
191- // ref.limitToFirst(1).on('child_added', () => {});
192- // ref.on('child_added', () => {})
193-
194175 // Modifiers
195176 orderByKey ( ) : Reference {
196- const newRef = new Reference ( this . db , this . path , this . query . export ( ) ) ;
197- newRef . query . setOrderBy ( 'orderByKey' ) ;
198- return newRef ;
177+ return this . orderBy ( 'orderByKey' ) ;
199178 }
200179
201180 orderByPriority ( ) : Reference {
202- const newRef = new Reference ( this . db , this . path , this . query . export ( ) ) ;
203- newRef . query . setOrderBy ( 'orderByPriority' ) ;
204- return newRef ;
181+ return this . orderBy ( 'orderByPriority' ) ;
205182 }
206183
207184 orderByValue ( ) : Reference {
208- const newRef = new Reference ( this . db , this . path , this . query . export ( ) ) ;
209- newRef . query . setOrderBy ( 'orderByValue' ) ;
210- return newRef ;
185+ return this . orderBy ( 'orderByValue' ) ;
211186 }
212187
213188 orderByChild ( key : string ) : Reference {
214- const newRef = new Reference ( this . db , this . path , this . query . export ( ) ) ;
215- newRef . query . setOrderBy ( 'orderByChild' , key ) ;
189+ return this . orderBy ( 'orderByChild' , key ) ;
190+ }
191+
192+ orderBy ( name : string , key ? : string ) : Reference {
193+ const newRef = new Reference ( this . db , this . path , this . query . getModifiers ( ) ) ;
194+ newRef . query . setOrderBy ( name , key ) ;
216195 return newRef ;
217196 }
218197
219198 // Limits
220199 limitToLast ( limit : number ) : Reference {
221- const newRef = new Reference ( this . db , this . path , this . query . export ( ) ) ;
222- newRef . query . setLimit ( 'limitToLast' , limit ) ;
223- return newRef ;
200+ return this . limit ( 'limitToLast' , limit ) ;
224201 }
225202
226203 limitToFirst ( limit : number ) : Reference {
227- // return this.query.setLimit('limitToFirst', limit);
228- const newRef = new Reference ( this . db , this . path , this . query . export ( ) ) ;
229- newRef . query . setLimit ( 'limitToFirst' , limit ) ;
204+ return this . limit ( 'limitToFirst' , limit ) ;
205+ }
206+
207+ limit ( name : string , limit : number ) : Reference {
208+ const newRef = new Reference ( this . db , this . path , this . query . getModifiers ( ) ) ;
209+ newRef . query . setLimit ( name , limit ) ;
230210 return newRef ;
231211 }
232212
233213 // Filters
234214 equalTo ( value : any , key ? : string ) : Reference {
235- const newRef = new Reference ( this . db , this . path , this . query . export ( ) ) ;
236- newRef . query . setFilter ( 'equalTo' , value , key ) ;
237- return newRef ;
215+ return this . filter ( 'equalTo' , value , key ) ;
238216 }
239217
240218 endAt ( value : any , key ? : string ) : Reference {
241- const newRef = new Reference ( this . db , this . path , this . query . export ( ) ) ;
242- newRef . query . setFilter ( 'endAt' , value , key ) ;
243- return newRef ;
219+ return this . filter ( 'endAt' , value , key ) ;
244220 }
245221
246222 startAt ( value : any , key ? : string ) : Reference {
247- const newRef = new Reference ( this . db , this . path , this . query . export ( ) ) ;
248- newRef . query . setFilter ( 'startAt' , value , key ) ;
223+ return this . filter ( 'startAt' , value , key ) ;
224+ }
225+
226+ filter ( name : string , value : any , key ? : string ) : Reference {
227+ const newRef = new Reference ( this . db , this . path , this . query . getModifiers ( ) ) ;
228+ newRef . query . setFilter ( name , value , key ) ;
249229 return newRef ;
250230 }
251231
@@ -278,10 +258,6 @@ export default class Reference extends ReferenceBase {
278258 return pathStr ;
279259 }
280260
281- dbModifiers ( ) : Array < string > {
282- return this . query . build ( ) ;
283- }
284-
285261 get namespace ( ) : string {
286262 return 'firestack :dbRef ';
287263 }
0 commit comments