-
-
Notifications
You must be signed in to change notification settings - Fork 51
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
- Loading branch information
Showing
14 changed files
with
762 additions
and
3 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
|
@@ -10,4 +10,8 @@ build/ | |
# Directory created by dartdoc | ||
doc/api/ | ||
|
||
*.iml | ||
# IntelliJ related | ||
*.iml | ||
*.ipr | ||
*.iws | ||
.idea/ |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,45 @@ | ||
import 'package:firebase_admin/src/firestore/document.dart'; | ||
import 'package:firebase_admin/src/firestore/firestore.dart'; | ||
import 'package:firebase_admin/src/firestore/query.dart'; | ||
import 'package:firebase_admin/src/firestore/utils/auto_id_generator.dart'; | ||
import 'package:firebase_admin/src/firestore/utils/pointer.dart'; | ||
|
||
class CollectionReference<T> extends Query<T> { | ||
final Pointer _pointer; | ||
|
||
CollectionReference({ | ||
required super.firestore, | ||
required super.toFirestore, | ||
required super.fromFirestore, | ||
required super.path, | ||
}) : _pointer = Pointer(path), | ||
super(query: null) { | ||
assert(_pointer.isCollection()); | ||
} | ||
|
||
DocumentReference<T> doc([String? id]) { | ||
return DocumentReference( | ||
firestore: firestore, | ||
toFirestore: toFirestore, | ||
fromFirestore: fromFirestore, | ||
path: _pointer.documentPath(id ?? AutoIdGenerator.autoId()), | ||
); | ||
} | ||
|
||
Future<DocumentSnapshot<T>> add(T data) async { | ||
final document = doc(); | ||
return await document.set(data); | ||
} | ||
|
||
CollectionReference<R> withConverter<R>({ | ||
required FromFirestore<R> fromFirestore, | ||
required ToFirestore<R> toFirestore, | ||
}) { | ||
return CollectionReference<R>( | ||
firestore: firestore, | ||
path: path, | ||
fromFirestore: fromFirestore, | ||
toFirestore: toFirestore, | ||
); | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,92 @@ | ||
import 'package:firebase_admin/src/firestore/firestore.dart'; | ||
import 'package:firebase_admin/src/firestore/utils/document_snapshot.dart'; | ||
import 'package:firebase_admin/src/firestore/utils/pointer.dart'; | ||
import 'package:firebase_admin/src/firestore/utils/serialization.dart'; | ||
import 'package:googleapis/firestore/v1.dart'; | ||
|
||
class DocumentReference<T> { | ||
final Firestore firestore; | ||
final ToFirestore<T> toFirestore; | ||
final FromFirestore<T> fromFirestore; | ||
final Pointer _pointer; | ||
String get path => _pointer.path; | ||
String get id => _pointer.id; | ||
|
||
DocumentReference({ | ||
required this.firestore, | ||
required this.toFirestore, | ||
required this.fromFirestore, | ||
required String path, | ||
}) : _pointer = Pointer(path) { | ||
assert(_pointer.isDocument()); | ||
} | ||
|
||
Future<DocumentSnapshot<T>> set(T data) async { | ||
final result = await firestore.docsApi.createDocument( | ||
Document(fields: serializeData(toFirestore(data))), | ||
firestore.databasePath, | ||
_pointer.parentPath()!, | ||
documentId: _pointer.id, | ||
); | ||
|
||
return SerializableDocumentSnapshot( | ||
firestore: firestore, | ||
toFirestore: toFirestore, | ||
fromFirestore: fromFirestore, | ||
document: result, | ||
); | ||
} | ||
|
||
Future<DocumentSnapshot<T>> get() async { | ||
final result = await firestore.docsApi.get('${firestore.databasePath}/$path'); | ||
|
||
return SerializableDocumentSnapshot( | ||
firestore: firestore, | ||
toFirestore: toFirestore, | ||
fromFirestore: fromFirestore, | ||
document: result, | ||
); | ||
} | ||
|
||
Future<DocumentSnapshot<T>> update(Map<String, dynamic> data) async { | ||
final result = await firestore.docsApi.patch( | ||
Document(fields: serializeData(data)), | ||
'${firestore.databasePath}/${_pointer.path}', | ||
); | ||
|
||
return SerializableDocumentSnapshot( | ||
firestore: firestore, | ||
toFirestore: toFirestore, | ||
fromFirestore: fromFirestore, | ||
document: result, | ||
); | ||
} | ||
|
||
Future<void> delete() async { | ||
await firestore.docsApi.delete('${firestore.databasePath}/$path'); | ||
} | ||
|
||
DocumentReference<R> withConverter<R>({ | ||
required FromFirestore<R> fromFirestore, | ||
required ToFirestore<R> toFirestore, | ||
}) { | ||
return DocumentReference<R>( | ||
firestore: firestore, | ||
path: path, | ||
fromFirestore: fromFirestore, | ||
toFirestore: toFirestore, | ||
); | ||
} | ||
} | ||
|
||
abstract class DocumentSnapshot<T> { | ||
final Firestore firestore; | ||
|
||
const DocumentSnapshot({ | ||
required this.firestore, | ||
}); | ||
|
||
DocumentReference<T> get reference; | ||
|
||
T data(); | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,60 @@ | ||
import 'package:firebase_admin/src/app.dart'; | ||
import 'package:firebase_admin/src/app/app_extension.dart'; | ||
import 'package:firebase_admin/src/firestore/collection.dart'; | ||
import 'package:firebase_admin/src/firestore/document.dart'; | ||
import 'package:firebase_admin/src/firestore/transaction.dart'; | ||
import 'package:firebase_admin/src/firestore/utils/serialization.dart'; | ||
import 'package:firebase_admin/src/service.dart'; | ||
import 'package:firebase_admin/src/utils/api_request.dart'; | ||
import 'package:googleapis/firestore/v1.dart'; | ||
import 'package:meta/meta.dart'; | ||
|
||
typedef ToFirestore<T> = Map<String, dynamic> Function(T value); | ||
typedef FromFirestore<T> = T Function(DocumentSnapshot<Map<String, dynamic>> snapshot); | ||
|
||
class Firestore implements FirebaseService { | ||
@override | ||
final App app; | ||
|
||
final FirestoreApi _api; | ||
|
||
@internal | ||
ProjectsDatabasesDocumentsResource get docsApi => _api.projects.databases.documents; | ||
|
||
@internal | ||
String get databasePath => 'projects/${app.projectId}/databases/(default)/documents'; | ||
|
||
Firestore(this.app) : _api = FirestoreApi(AuthorizedHttpClient(app)); | ||
|
||
@override | ||
Future<void> delete() async {} | ||
|
||
CollectionReference<Map<String, dynamic>> collection(String id) { | ||
return CollectionReference( | ||
firestore: this, | ||
fromFirestore: fromFirestore, | ||
toFirestore: toFirestore, | ||
path: id, | ||
); | ||
} | ||
|
||
DocumentReference<Map<String, dynamic>> doc(String id) { | ||
return DocumentReference( | ||
firestore: this, | ||
fromFirestore: fromFirestore, | ||
toFirestore: toFirestore, | ||
path: id, | ||
); | ||
} | ||
|
||
Future<T> runTransaction<T>( | ||
Future<T> Function(Transaction transaction) handler, { | ||
Duration timeout = const Duration(seconds: 30), | ||
}) { | ||
return Transaction.run( | ||
firestore: this, | ||
timeout: timeout, | ||
handler: handler, | ||
); | ||
} | ||
} |
Oops, something went wrong.