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
35 changes: 29 additions & 6 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -72,6 +72,29 @@ firebaseDb.read(path)
});
```

### Read Data with Query

Sometimes you need to search for a certain value in an object, instead or iterating in the client
use this implementation to let the Firebase do the query for you.

The following example will give you all the objects where the key name is equal to Luis Rodriguez,
great for searching when you do not know the key and when you use Push to save data.

```JavaScript
// firebaseDb.readByQueryEqualToValue(path,key,value) <Promise>

var firebaseDb = require("Firebase/Database");
firebaseDb.readByQueryEqualToValue("users","name","Luis Rodriguez")
.then(function (json) {
// here json is a JSON string
console.log(json);
var user = JSON.parse(json);
})
.catch(function (reason) {
console.log('Unable to read -> ' +reason);
});
```

### Listen for data event

* this event is fired when you change particular object.
Expand Down Expand Up @@ -132,20 +155,20 @@ firebaseDb.listenOnAdded(messagesPath, 1);
// function below will be executed for any path
firebaseDb.on('dataAdded', function (eventPath, msg) {
// msg here is a JSON string

// track only given path
if (eventPath === usersPath) {
// new user record was added, usually by push method
// if you created record using not .push method or
// if you created record using not .push method or
// pushWithTimestamp you will not receive event here
console.log(eventPath);
console.log(msg);
var newUser = JSON.parse(msg);
}

if (eventPath === messagesPath) {
// new message record was added, usually by push method
// if you created record using not .push method or
// if you created record using not .push method or
// .pushWithTimestamp you will not receive event here
console.log(eventPath);
console.log(msg);
Expand Down Expand Up @@ -175,15 +198,15 @@ firebaseDb.listenOnRemoved(messagesPath, 1);
// function below will be executed for any path
firebaseDb.on('dataRemoved', function (eventPath, msg) {
// msg here is a JSON string

// track only given path
if (eventPath === usersPath) {
// user record was removed
console.log(eventPath);
console.log(msg);
var newUser = JSON.parse(msg);
}

if (eventPath === messagesPath) {
// message record was removed
console.log(eventPath);
Expand Down
95 changes: 91 additions & 4 deletions src/Firebase.Database/Database.uno
Original file line number Diff line number Diff line change
Expand Up @@ -137,7 +137,7 @@ namespace Firebase.Database
f(path, nil);
return;
}

NSError *error;
NSData *jsonData = [NSJSONSerialization dataWithJSONObject:snapshot.value
options:(NSJSONWritingOptions)0
Expand Down Expand Up @@ -171,7 +171,7 @@ namespace Firebase.Database
f.run(path,databaseError.toString());
}
};

long longLastValue = Long.parseLong(lastValue);
DatabaseReference ref = (DatabaseReference)@{DatabaseService._handle:Get()};
Query readQuery = ref.child(path).orderByChild(keyName).endAt(longLastValue).limitToLast(count);
Expand Down Expand Up @@ -330,7 +330,7 @@ namespace Firebase.Database
ChildEventListener childEventListener = new ChildEventListener() {
@Override
public void onChildAdded(DataSnapshot dataSnapshot, String previousChildName) {

}

@Override
Expand Down Expand Up @@ -606,7 +606,7 @@ namespace Firebase.Database
Save();
}

public static void SaveWithTimestamp(string path, Object value)
public static void SaveWithTimestamp(string path, Object value)
{
Save();
}
Expand Down Expand Up @@ -656,6 +656,15 @@ namespace Firebase.Database
}
}

extern(!mobile)
internal class ReadByQueryEqualToValue : Promise<string>
{
public ReadByQueryEqualToValue(string path, string key, string val)
{
Reject(new Exception("Not implemented on desktop"));
}
}

[Require("Entity", "DatabaseService")]
[Require("Source.Import","FirebaseDatabase/FIRDatabase.h")]
[Require("Source.Include","@{DatabaseService:Include}")]
Expand Down Expand Up @@ -687,6 +696,38 @@ namespace Firebase.Database
void Reject(string reason) { Reject(new Exception(reason)); }
}

[Require("Entity", "DatabaseService")]
[Require("Source.Import","FirebaseDatabase/FIRDatabase.h")]
[Require("Source.Include","@{DatabaseService:Include}")]
extern(iOS)
internal class ReadByQueryEqualToValue : Promise<string>
{
[Foreign(Language.ObjC)]
public ReadByQueryEqualToValue(string path, string key, string val)
@{
FIRDatabaseReference *ref = @{DatabaseService._handle:Get()};

[[[[ref child:path] queryOrderedByChild:key] queryEqualToValue:val] observeSingleEventOfType:FIRDataEventTypeValue withBlock:^(FIRDataSnapshot * _Nonnull snapshot) {
NSError *error;

@{ReadByQueryEqualToValue:Of(_this).Resolve(string):Call((
([snapshot.value isEqual:[NSNull null]])
? nil
: ([snapshot.value isKindOfClass:[NSString class]])
? [NSString stringWithFormat:@"\"%@\"", snapshot.value]
: ([snapshot.value isKindOfClass:[NSNumber class]])
? [NSString stringWithFormat:@"%@", snapshot.value]
: [[NSString alloc] initWithData:[NSJSONSerialization dataWithJSONObject:snapshot.value options:(NSJSONWritingOptions)0 error:&error] encoding:NSUTF8StringEncoding]
))};

} withCancelBlock:^(NSError * _Nonnull error) {
NSString *erstr = [NSString stringWithFormat:@"Firebase Read Error: %@", error.localizedDescription];
@{ReadByQueryEqualToValue:Of(_this).Reject(string):Call(erstr)};
}];
@}
void Reject(string reason) { Reject(new Exception(reason)); }
}

[ForeignInclude(Language.Java,
"com.google.firebase.database.DatabaseReference",
"com.google.firebase.database.DatabaseError",
Expand Down Expand Up @@ -732,4 +773,50 @@ namespace Firebase.Database
@}
void Reject(string reason) { Reject(new Exception(reason)); }
}

[ForeignInclude(Language.Java,
"com.google.firebase.database.DatabaseReference",
"com.google.firebase.database.DatabaseError",
"com.google.firebase.database.DatabaseReference",
"com.google.firebase.database.DataSnapshot",
"com.google.firebase.database.ValueEventListener",
"org.json.JSONObject",
"org.json.JSONArray",
"java.util.Map",
"java.util.List")]
extern(Android)
internal class ReadByQueryEqualToValue : Promise<string>
{
[Foreign(Language.Java)]
public ReadByQueryEqualToValue(string path, string key, string val)
@{
ValueEventListener dataListener = new ValueEventListener() {
@Override
public void onDataChange(DataSnapshot dataSnapshot)
{
Object snapshotValue = dataSnapshot.getValue();
@{ReadByQueryEqualToValue:Of(_this).Resolve(string):Call((
(snapshotValue == null)
? null
: (snapshotValue instanceof Map)
? new JSONObject((Map) snapshotValue).toString()
: (snapshotValue instanceof List)
? new JSONArray((List) snapshotValue).toString()
: (snapshotValue instanceof String)
? "\"" + snapshotValue.toString() + "\""
: snapshotValue.toString()
))};
}

@Override
public void onCancelled(DatabaseError databaseError)
{
@{ReadByQueryEqualToValue:Of(_this).Reject(string):Call(databaseError.toString())};
}
};
DatabaseReference ref = (DatabaseReference)@{DatabaseService._handle:Get()};
ref.child(path).orderByChild(key).equalTo(val).addListenerForSingleValueEvent(dataListener);
@}
void Reject(string reason) { Reject(new Exception(reason)); }
}
}
9 changes: 9 additions & 0 deletions src/Firebase.Database/JS.uno
Original file line number Diff line number Diff line change
Expand Up @@ -57,6 +57,7 @@ namespace Firebase.Database.JS
AddMember(new NativeFunction("readByQueryEndingAtValue", (NativeCallback)ReadByQueryEndingAtValue));
AddMember(new NativeFunction("detachListeners", (NativeCallback)DetachListeners));
AddMember(new NativePromise<string, string>("read", Read, null));
AddMember(new NativePromise<string, string>("readByQueryEqualToValue", ReadByQueryEqualToValue, null));
AddMember(new NativeFunction("push", (NativeCallback)Push));
AddMember(new NativeFunction("pushWithTimestamp", (NativeCallback)PushWithTimestamp));
AddMember(new NativeFunction("save", (NativeCallback)Save));
Expand All @@ -69,6 +70,14 @@ namespace Firebase.Database.JS
return new Read(path);
}

static Future<string> ReadByQueryEqualToValue(object[] args)
{
var path = args[0].ToString();
var key = args[1].ToString();
var val = args[2].ToString();
return new ReadByQueryEqualToValue(path,key,val);
}

static void DoSave(string path, object arg)
{
if (arg is Fuse.Scripting.Object)
Expand Down