Skip to content

Commit 35ee121

Browse files
committed
Added child keys and modifiers to all _get_ requests on database queries both platforms
1 parent b97815b commit 35ee121

File tree

4 files changed

+119
-39
lines changed

4 files changed

+119
-39
lines changed

android/src/main/java/io/fullstack/firestack/FirestackDatabase.java

Lines changed: 87 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -4,6 +4,8 @@
44
import android.util.Log;
55
import java.util.HashMap;
66
import java.util.Iterator;
7+
import java.util.List;
8+
import java.util.ListIterator;
79
import java.util.Map;
810
import android.net.Uri;
911

@@ -18,6 +20,7 @@
1820
import com.facebook.react.bridge.WritableMap;
1921
import com.facebook.react.bridge.ReadableMap;
2022
import com.facebook.react.bridge.ReadableMapKeySetIterator;
23+
import com.facebook.react.bridge.ReadableArray;
2124
import com.facebook.react.bridge.ReactContext;
2225

2326
import com.google.android.gms.tasks.OnCompleteListener;
@@ -28,6 +31,7 @@
2831
import com.google.firebase.database.FirebaseDatabase;
2932
import com.google.firebase.database.DatabaseReference;
3033
import com.google.firebase.database.ChildEventListener;
34+
import com.google.firebase.database.Query;
3135
import com.google.firebase.database.ValueEventListener;
3236
import com.google.firebase.database.DataSnapshot;
3337
import com.google.firebase.database.DatabaseError;
@@ -54,8 +58,12 @@ public String getName() {
5458
}
5559

5660
@ReactMethod
57-
public void set(final String path, final ReadableMap props, final Callback callback) {
61+
public void set(
62+
final String path,
63+
final ReadableMap props,
64+
final Callback callback) {
5865
DatabaseReference ref = this.getDatabaseReferenceAtPath(path);
66+
5967
final FirestackDatabaseModule self = this;
6068
Map<String, Object> m = FirestackUtils.recursivelyDeconstructReadableMap(props);
6169

@@ -80,7 +88,9 @@ public void onComplete(DatabaseError error, DatabaseReference ref) {
8088
}
8189

8290
@ReactMethod
83-
public void update(final String path, final ReadableMap props, final Callback callback) {
91+
public void update(final String path,
92+
final ReadableMap props,
93+
final Callback callback) {
8494
DatabaseReference ref = this.getDatabaseReferenceAtPath(path);
8595
final FirestackDatabaseModule self = this;
8696
Map<String, Object> m = FirestackUtils.recursivelyDeconstructReadableMap(props);
@@ -106,7 +116,8 @@ public void onComplete(DatabaseError error, DatabaseReference ref) {
106116
}
107117

108118
@ReactMethod
109-
public void remove(final String path, final Callback callback) {
119+
public void remove(final String path,
120+
final Callback callback) {
110121
DatabaseReference ref = this.getDatabaseReferenceAtPath(path);
111122
final FirestackDatabaseModule self = this;
112123
DatabaseReference.CompletionListener listener = new DatabaseReference.CompletionListener() {
@@ -130,7 +141,10 @@ public void onComplete(DatabaseError error, DatabaseReference ref) {
130141
}
131142

132143
@ReactMethod
133-
public void push(final String path, final ReadableMap props, final Callback callback) {
144+
public void push(final String path,
145+
final ReadableMap props,
146+
final Callback callback) {
147+
134148
Log.d(TAG, "Called push with " + path);
135149
DatabaseReference ref = this.getDatabaseReferenceAtPath(path);
136150
DatabaseReference newRef = ref.push();
@@ -174,9 +188,12 @@ public void onComplete(DatabaseError error, DatabaseReference ref) {
174188
}
175189

176190
@ReactMethod
177-
public void on(final String path, final String name, final Callback callback) {
191+
public void on(final String path,
192+
final ReadableArray modifiers,
193+
final String name,
194+
final Callback callback) {
178195
Log.d(TAG, "Setting a listener on event: " + name + " for path " + path);
179-
DatabaseReference ref = this.getDatabaseReferenceAtPath(path);
196+
Query ref = this.getDatabaseQueryAtPathAndModifiers(path, modifiers);
180197
final FirestackDatabaseModule self = this;
181198

182199
if (name.equals("value")) {
@@ -260,9 +277,11 @@ public void onCancelled(DatabaseError error) {
260277
}
261278

262279
@ReactMethod
263-
public void onOnce(final String path, final String name, final Callback callback) {
280+
public void onOnce(final String path,
281+
final ReadableArray modifiers,
282+
final String name, final Callback callback) {
264283
Log.d(TAG, "Setting one-time listener on event: " + name + " for path " + path);
265-
DatabaseReference ref = this.getDatabaseReferenceAtPath(path);
284+
Query ref = this.getDatabaseQueryAtPathAndModifiers(path, modifiers);
266285
final FirestackDatabaseModule self = this;
267286

268287
ValueEventListener listener = new ValueEventListener() {
@@ -297,11 +316,70 @@ public void removeListeners(final String path, final String name, final Callback
297316
FirestackUtils.todoNote(TAG, "on", callback);
298317
}
299318

319+
// Private helpers
300320
private DatabaseReference getDatabaseReferenceAtPath(final String path) {
301321
DatabaseReference mDatabase = FirebaseDatabase.getInstance().getReference(path);
302322
return mDatabase;
303323
}
304324

325+
private Query getDatabaseQueryAtPathAndModifiers(
326+
final String path,
327+
final ReadableArray modifiers) {
328+
DatabaseReference ref = this.getDatabaseReferenceAtPath(path);
329+
330+
List<Object> strModifiers = FirestackUtils.recursivelyDeconstructReadableArray(modifiers);
331+
ListIterator<Object> it = strModifiers.listIterator();
332+
Query query = ref.orderByKey();
333+
334+
while(it.hasNext()) {
335+
String str = (String) it.next();
336+
337+
Log.d(TAG, "getReference with modifiers: " + str);
338+
String[] strArr = str.split(":");
339+
String methStr = strArr[0];
340+
341+
if (methStr.equalsIgnoreCase("orderByKey")) {
342+
query = ref.orderByKey();
343+
} else if (methStr.equalsIgnoreCase("orderByValue")) {
344+
query = ref.orderByValue();
345+
} else if (methStr.equalsIgnoreCase("orderByPriority")) {
346+
query = ref.orderByPriority();
347+
} else if (methStr.contains("orderByChild")) {
348+
String key = strArr[1];
349+
Log.d(TAG, "orderByChild: " + key);
350+
query = ref.orderByChild(key);
351+
} else if (methStr.contains("limitToLast")) {
352+
String key = strArr[1];
353+
int limit = Integer.parseInt(key);
354+
Log.d(TAG, "limitToLast: " + limit);
355+
query = query.limitToLast(limit);
356+
} else if (methStr.contains("limitToFirst")) {
357+
String key = strArr[1];
358+
int limit = Integer.parseInt(key);
359+
Log.d(TAG, "limitToFirst: " + limit);
360+
query = query.limitToFirst(limit);
361+
} else if (methStr.contains("equalTo")) {
362+
String value = strArr[1];
363+
String key = strArr[2];
364+
if (key == null) {
365+
query = query.equalTo(value);
366+
} else {
367+
query = query.equalTo(value, key);
368+
}
369+
} else if (methStr.contains("endAt")) {
370+
String value = strArr[1];
371+
String key = strArr[2];
372+
if (key == null) {
373+
query = query.equalTo(value);
374+
} else {
375+
query = query.equalTo(value, key);
376+
}
377+
}
378+
}
379+
380+
return query;
381+
}
382+
305383
private WritableMap dataSnapshotToMap(String name, DataSnapshot dataSnapshot) {
306384
WritableMap data = Arguments.createMap();
307385

@@ -316,7 +394,7 @@ private WritableMap dataSnapshotToMap(String name, DataSnapshot dataSnapshot) {
316394

317395
Object priority = dataSnapshot.getPriority();
318396
if (priority == null) {
319-
data.putString("priority", "null");
397+
data.putString("priority", null);
320398
} else {
321399
data.putString("priority", priority.toString());
322400
}

ios/Firestack/FirestackDatabase.m

Lines changed: 26 additions & 21 deletions
Original file line numberDiff line numberDiff line change
@@ -16,12 +16,10 @@ @implementation FirestackDatabase
1616
RCT_EXPORT_MODULE(FirestackDatabase);
1717

1818
RCT_EXPORT_METHOD(set:(NSString *) path
19-
modifiers:(NSArray *) modifiers
2019
value:(NSDictionary *)value
2120
callback:(RCTResponseSenderBlock) callback)
2221
{
23-
FIRDatabaseReference *ref = [self getRefAtPathWithModifiers:path
24-
modifiers:modifiers];
22+
FIRDatabaseReference *ref = [self getRefAtPath:path];
2523

2624
[ref setValue:value withCompletionBlock:^(NSError * _Nullable error, FIRDatabaseReference * _Nonnull ref) {
2725
if (error != nil) {
@@ -37,12 +35,10 @@ @implementation FirestackDatabase
3735
}
3836

3937
RCT_EXPORT_METHOD(update:(NSString *) path
40-
modifiers:(NSArray *) modifiers
4138
value:(NSDictionary *)value
4239
callback:(RCTResponseSenderBlock) callback)
4340
{
44-
FIRDatabaseReference *ref = [self getRefAtPathWithModifiers:path
45-
modifiers:modifiers];
41+
FIRDatabaseReference *ref = [self getRefAtPath:path];
4642

4743
[ref updateChildValues:value withCompletionBlock:^(NSError * _Nullable error, FIRDatabaseReference * _Nonnull ref) {
4844
if (error != nil) {
@@ -58,11 +54,9 @@ @implementation FirestackDatabase
5854
}
5955

6056
RCT_EXPORT_METHOD(remove:(NSString *) path
61-
modifiers:(NSArray *) modifiers
6257
callback:(RCTResponseSenderBlock) callback)
6358
{
64-
FIRDatabaseReference *ref = [self getRefAtPathWithModifiers:path
65-
modifiers: modifiers];
59+
FIRDatabaseReference *ref = [self getRefAtPath:path];
6660
[ref removeValueWithCompletionBlock:^(NSError * _Nullable error, FIRDatabaseReference * _Nonnull ref) {
6761
if (error != nil) {
6862
// Error handling
@@ -117,7 +111,7 @@ @implementation FirestackDatabase
117111
int eventType = [self eventTypeFromName:name];
118112
NSLog(@"Calling observeEventType: at path: %@ %@", path, name);
119113

120-
FIRDatabaseReference *ref = [self getRefAtPathWithModifiers:path
114+
FIRDatabaseQuery *ref = [self getQueryAtPathWithModifiers:path
121115
modifiers: modifiers];
122116

123117
FIRDatabaseHandle handle = [ref observeEventType:eventType
@@ -151,10 +145,14 @@ @implementation FirestackDatabase
151145
{
152146
int eventType = [self eventTypeFromName:name];
153147

154-
FIRDatabaseReference *ref = [self getRefAtPathWithModifiers:path modifiers:modifiers];
148+
FIRDatabaseQuery *ref = [self getQueryAtPathWithModifiers:path modifiers:modifiers];
155149
[ref observeSingleEventOfType:eventType
156150
withBlock:^(FIRDataSnapshot * _Nonnull snapshot) {
157-
callback(@[[NSNull null], [self snapshotToDict:snapshot]]);
151+
NSDictionary *props = [self snapshotToDict:snapshot];
152+
callback(@[[NSNull null], @{
153+
@"eventName": name,
154+
@"snapshot": props
155+
}]);
158156
}
159157
withCancelBlock:^(NSError * _Nonnull error) {
160158
NSLog(@"Error onDBEventOnce: %@", [error debugDescription]);
@@ -209,12 +207,13 @@ - (FIRDatabaseReference *) getRefAtPath:(NSString *) str
209207
return [rootRef child:str];
210208
}
211209

212-
- (FIRDatabaseReference *) getRefAtPathWithModifiers:(NSString *) str
210+
- (FIRDatabaseQuery *) getQueryAtPathWithModifiers:(NSString *) str
213211
modifiers:(NSArray *) modifiers
214212
{
215213
FIRDatabaseReference *rootRef = [[[FIRDatabase database] reference] child:str];
216214

217-
FIRDatabaseQuery *query;
215+
FIRDatabaseQuery *query = [rootRef queryOrderedByKey];
216+
218217
for (NSString *str in modifiers) {
219218
if ([str isEqualToString:@"orderByKey"]) {
220219
query = [rootRef queryOrderedByKey];
@@ -241,22 +240,28 @@ - (FIRDatabaseReference *) getRefAtPathWithModifiers:(NSString *) str
241240
NSArray *args = [str componentsSeparatedByString:@":"];
242241
NSString *value = args[1];
243242
NSString *key = args[2];
244-
query = [query queryEqualToValue:value
243+
244+
if (key == nil) {
245+
query = [query queryEqualToValue:value];
246+
} else {
247+
query = [query queryEqualToValue:value
245248
childKey:key];
249+
}
246250
} else if ([str containsString:@"endAt"]) {
247251
NSArray *args = [str componentsSeparatedByString:@":"];
248252
NSString *value = args[1];
249253
NSString *key = args[2];
250-
query = [query queryEndingAtValue:value
254+
255+
if (key == nil) {
256+
query = [query queryEndingAtValue:value];
257+
} else {
258+
query = [query queryEndingAtValue:value
251259
childKey:key];
260+
}
252261
}
253262
}
254263

255-
if (query == nil) {
256-
return rootRef;
257-
} else {
258-
return query.ref;
259-
}
264+
return query;
260265
}
261266

262267
// Handles

lib/firestackModule.js

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -86,6 +86,7 @@ export class FirestackModule {
8686
const toObject = this._toObject;
8787

8888
const _itemAdded = (snapshot, prevKey) => {
89+
console.log('_itemAdded called', snapshot);
8990
const state = this._getState(); // local state
9091
const newItem = toObject(snapshot, state);
9192
let list = state.items || [];

lib/modules/database.js

Lines changed: 5 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -75,26 +75,22 @@ class DatabaseRef {
7575

7676
setAt(key, val) {
7777
const {path, value} = this.dbPath([key, val]);
78-
const modifiers = this.dbModifiers();
79-
return promisify('set', FirestackDatabase)(path, modifiers, value)
78+
return promisify('set', FirestackDatabase)(path, value)
8079
}
8180

8281
updateAt(key, val) {
8382
const {path, value} = this.dbPath([key, val]);
84-
const modifiers = this.dbModifiers();
85-
return promisify('update', FirestackDatabase)(path, modifiers, value)
83+
return promisify('update', FirestackDatabase)(path, value)
8684
}
8785

8886
removeAt(key) {
8987
const {path} = this.dbPath([key]);
90-
const modifiers = this.dbModifiers();
91-
return promisify('remove', FirestackDatabase)(path, modifiers)
88+
return promisify('remove', FirestackDatabase)(path)
9289
}
9390

9491
push(value={}) {
9592
const {path} = this.dbPath();
96-
const modifiers = this.dbModifiers();
97-
return promisify('push', FirestackDatabase)(path, modifiers, value)
93+
return promisify('push', FirestackDatabase)(path, value)
9894
.then(({ref}) => new DatabaseRef(this.db, ref))
9995
}
10096

@@ -117,7 +113,7 @@ class DatabaseRef {
117113
})
118114
}
119115

120-
once(evt) {
116+
once(evt='once') {
121117
const {path} = this.dbPath();
122118
const modifiers = this.dbModifiers();
123119
return promisify('onOnce', FirestackDatabase)(path, modifiers, evt)

0 commit comments

Comments
 (0)