Sure, I'll tailor the README to focus on Firebase as the primary database.
Spreeloop Database Package
Overview
The spreeloop database
package provides a unified and abstract way to interact with Firebase databases. It offers a variety of methods to manage and retrieve data, ensuring flexibility and ease of use. This package supports both Firebase Firestore and a fake database for testing purposes.
Features
- Unified interface for different database types
- Image retrieval and URL generation
- File upload and management
- Record creation, updating, and deletion
- Collection and document querying with filtering and ordering
- Real-time updates through streams
Installation
To install the spreeloop database
package, add it to your project's dependencies:
dependencies:
spreeloop_database: ^0.0.1
Usage
Below is a guide on how to use the spreeloop database
package effectively.
Initialization
To initialize the database, use the Database
factory constructor. You can specify the type of database you want to use, with Firebase Firestore being the primary option:
import 'package:spreeloop_database/spreeloop_database.dart';
// Initialize Firestore database
final db = Database(DatabaseType.firestore);
// Initialize Fake database for testing
final fakeDb = Database.fake({
'users': {
'user1': {'name': 'John Doe', 'age': 30},
},
});
Image Handling
Retrieve an image from the Firebase storage:
Future<ImageProvider?> fetchImage(String imageRef) async {
return await db.getImage(imageRef);
}
Get a download URL for a file stored in Firebase:
Future<String?> fetchDownloadURL(String url) async {
return await db.getDownloadURL(url);
}
File Management
Upload a file to Firebase storage:
Future<bool> uploadFile(Uint8List fileData, String recordPath) async {
return await db.setFile(fileData, recordPath);
}
Record Management
Create a new record in Firestore:
Future<String?> createNewRecord(String collectionPath, Map<String, dynamic> recordMap) async {
return await db.createRecord(collectionPath, recordMap);
}
Update or set a record in Firestore:
Future<bool> updateRecord(String documentPath, Map<String, dynamic> recordMap, {bool merge = true}) async {
return await db.setRecord(documentPath: documentPath, recordMap: recordMap, merge: merge);
}
Remove records by path in Firestore:
Future<void> deleteRecords(String collectionPath, List<String> documentsIds) async {
return await db.removeRecordsByPath(collectionPath, documentsIds);
}
Remove records by value in Firestore:
Future<void> deleteRecordByValue(String collectionPath, List<DocumentQuery> documentQueries) async {
return await db.removeRecordByValue(collectionPath, documentQueries);
}
Querying Data
Retrieve a collection of documents from Firestore:
Future<Map<String, Map<dynamic, dynamic>>?> fetchCollection(String collectionPath, {List<DocumentQuery>? filters, DocumentOrderBy? orderBy, int? limit}) async {
return await db.getCollection(collectionPath, filters: filters, orderBy: orderBy, limit: limit);
}
Retrieve a collection group from Firestore:
Future<Map<String, Map<dynamic, dynamic>>?> fetchCollectionGroup(String collectionID, {List<DocumentQuery>? filters, int? limit, DocumentOrderBy? orderBy}) async {
return await db.getCollectionGroup(collectionID, filters: filters, limit: limit, orderBy: orderBy);
}
Real-time Data
Watch a collection group in Firestore for real-time updates:
Stream<Map<String, Map<dynamic, dynamic>>?> watchCollectionGroupUpdates(String collectionID, {List<DocumentQuery>? filters, int? limit, DocumentOrderBy? orderBy}) {
return db.watchCollectionGroup(collectionID, filters: filters, limit: limit, orderBy: orderBy);
}
Watch a collection in Firestore for real-time updates:
Stream<Map<String, Map<dynamic, dynamic>>?> watchCollectionUpdates(String collectionPath, {List<DocumentQuery>? filters, int? limit, DocumentOrderBy? orderBy}) {
return db.watchCollection(collectionPath, filters: filters, limit: limit, orderBy: orderBy);
}
Document Handling
Retrieve a document by its path in Firestore:
Future<Map<dynamic, dynamic>?> fetchDocument(String path) async {
return await db.getRecordByDocumentPath(path);
}
Watch a document in Firestore for real-time updates:
Stream<Map<dynamic, dynamic>?> watchDocumentUpdates(String path) {
return db.watchRecordByDocumentPath(path);
}
Conclusion
The spreeloop database
package offers a robust and flexible way to manage and retrieve data from Firebase Firestore. With its comprehensive set of features, you can easily integrate it into your project to handle all your database needs efficiently.