Ecosyste.ms: Awesome
An open API service indexing awesome lists of open source software.
https://github.com/varunon9/firestore-database-utility
An utility class to access firestore database for Android (getOne, create, update, getMany).
https://github.com/varunon9/firestore-database-utility
android firebase firestore firestore-database firestore-database-query
Last synced: 29 days ago
JSON representation
An utility class to access firestore database for Android (getOne, create, update, getMany).
- Host: GitHub
- URL: https://github.com/varunon9/firestore-database-utility
- Owner: varunon9
- License: mit
- Created: 2018-10-30T03:30:27.000Z (about 6 years ago)
- Default Branch: master
- Last Pushed: 2018-10-30T04:19:52.000Z (about 6 years ago)
- Last Synced: 2024-11-07T04:50:20.381Z (2 months ago)
- Topics: android, firebase, firestore, firestore-database, firestore-database-query
- Language: Java
- Size: 6.84 KB
- Stars: 3
- Watchers: 3
- Forks: 0
- Open Issues: 0
-
Metadata Files:
- Readme: README.md
- License: LICENSE
Awesome Lists containing this project
README
# firestore-database-utility
An utility class to access firestore database for Android (getOne, create, update, getMany).[FirestoreDbUtility.java](https://github.com/varunon9/firestore-database-utility/blob/master/FirestoreDbUtility.java) is wrapper class to interact with firestore database in Android. Copy and paste this class along with all other helper classes and interface- [FirestoreQueryConditionCode.java](https://github.com/varunon9/firestore-database-utility/blob/master/FirestoreQueryConditionCode.java), [FirestoreQuery.java](https://github.com/varunon9/firestore-database-utility/blob/master/FirestoreQuery.java), [FirestoreDbOperationCallback.java](https://github.com/varunon9/firestore-database-utility/blob/master/FirestoreDbOperationCallback.java) in your android project.
## Sample Usage-
First of all create an instance of `FirestoreDbUtility` in your `ExampleActivity` something like-
`FirestoreDbUtility firestoreDbUtility = new FirestoreDbUtility();`
Define collection name-
`String collectionName = "users"; // collection (table) name `
### GET unique document from firestore collection
```
String documentName = "4PkPVt4jGnO5neiKTKbe9I8B9yz2"; // may be uid of FirebaseUser i.e. firebaseUser.getUid()
firestoreDbUtility.getOne(collectionName, documentName,
new FirestoreDbOperationCallback() {
@Override
public void onSuccess(Object object) {
DocumentSnapshot documentSnapshot = (DocumentSnapshot) object;
User user = documentSnapshot.toObject(User.class); // User.java is a model class
System.out.println(user.getEmail()); // do anything with your user object
}@Override
public void onFailure(Object object) {}
});
```
### CREATE a document in firestore collection or MERGE if document already exists```
User user = new User(); // create an instance of User class and set relevant properties
user.setEmail('[email protected]');
user.setUid('4PkPVt4jGnO5neiKTKbe9I8B9yz2');
firestoreDbUtility.createOrMerge(collectionName,
user.getUid(), user, new FirestoreDbOperationCallback() {
@Override
public void onSuccess(Object object) {
}@Override
public void onFailure(Object object) {
}
});
```
### UPDATE a document in firestore collection```
// set isOnline true of user
Map hashMap = new HashMap<>();
hashMap.put("online", true);
firestoreDbUtility.update(collectionName,
firebaseUser.getUid(), hashMap, new FirestoreDbOperationCallback() {
@Override
public void onSuccess(Object object) {
}@Override
public void onFailure(Object object) {
}
});
```
### QUERY or getMany documents from firestore collection```
GeoPoint lesserGeoPoint = new GeoPoint(12.1, 77.1); // latitude is 12.1 and longitude is 77.1
GeoPoint greaterGeoPoint = new GeoPoint(13.0, 78);
List firestoreQueryList = new ArrayList<>();
firestoreQueryList.add(new FirestoreQuery(
FirestoreQueryConditionCode.WHERE_LESS_THAN,
"location",
greaterGeoPoint
));
firestoreQueryList.add(new FirestoreQuery(
FirestoreQueryConditionCode.WHERE_GREATER_THAN,
"location",
lesserGeoPoint
));
firestoreDbUtility.getMany(collectionName,
firestoreQueryList, new FirestoreDbOperationCallback() {
@Override
public void onSuccess(Object object) {
QuerySnapshot querySnapshot = (QuerySnapshot) object;
for (DocumentSnapshot documentSnapshot: querySnapshot.getDocuments()) {
User user = documentSnapshot.toObject(User.class); // do something with user object
}
}@Override
public void onFailure(Object object) {
showMessage("Failed to locate nearby travellers.");
}
});
```