|
| 1 | +import 'package:firebase_admin/src/firestore/firestore.dart'; |
| 2 | +import 'package:firebase_admin/src/firestore/utils/document_snapshot.dart'; |
| 3 | +import 'package:firebase_admin/src/firestore/utils/pointer.dart'; |
| 4 | +import 'package:firebase_admin/src/firestore/utils/serialization.dart'; |
| 5 | +import 'package:googleapis/firestore/v1.dart'; |
| 6 | + |
| 7 | +class DocumentReference<T> { |
| 8 | + final Firestore firestore; |
| 9 | + final ToFirestore<T> toFirestore; |
| 10 | + final FromFirestore<T> fromFirestore; |
| 11 | + final Pointer _pointer; |
| 12 | + String get path => _pointer.path; |
| 13 | + String get id => _pointer.id; |
| 14 | + |
| 15 | + DocumentReference({ |
| 16 | + required this.firestore, |
| 17 | + required this.toFirestore, |
| 18 | + required this.fromFirestore, |
| 19 | + required String path, |
| 20 | + }) : _pointer = Pointer(path) { |
| 21 | + assert(_pointer.isDocument()); |
| 22 | + } |
| 23 | + |
| 24 | + Future<DocumentSnapshot<T>> set(T data) async { |
| 25 | + final result = await firestore.docsApi.createDocument( |
| 26 | + Document(fields: serializeData(toFirestore(data))), |
| 27 | + firestore.databasePath, |
| 28 | + _pointer.parentPath()!, |
| 29 | + documentId: _pointer.id, |
| 30 | + ); |
| 31 | + |
| 32 | + return SerializableDocumentSnapshot( |
| 33 | + firestore: firestore, |
| 34 | + toFirestore: toFirestore, |
| 35 | + fromFirestore: fromFirestore, |
| 36 | + document: result, |
| 37 | + ); |
| 38 | + } |
| 39 | + |
| 40 | + Future<DocumentSnapshot<T>> get() async { |
| 41 | + final result = await firestore.docsApi.get('${firestore.databasePath}/$path'); |
| 42 | + |
| 43 | + return SerializableDocumentSnapshot( |
| 44 | + firestore: firestore, |
| 45 | + toFirestore: toFirestore, |
| 46 | + fromFirestore: fromFirestore, |
| 47 | + document: result, |
| 48 | + ); |
| 49 | + } |
| 50 | + |
| 51 | + Future<DocumentSnapshot<T>> update(Map<String, dynamic> data) async { |
| 52 | + final result = await firestore.docsApi.patch( |
| 53 | + Document(fields: serializeData(data)), |
| 54 | + '${firestore.databasePath}/${_pointer.path}', |
| 55 | + ); |
| 56 | + |
| 57 | + return SerializableDocumentSnapshot( |
| 58 | + firestore: firestore, |
| 59 | + toFirestore: toFirestore, |
| 60 | + fromFirestore: fromFirestore, |
| 61 | + document: result, |
| 62 | + ); |
| 63 | + } |
| 64 | + |
| 65 | + Future<void> delete() async { |
| 66 | + await firestore.docsApi.delete('${firestore.databasePath}/$path'); |
| 67 | + } |
| 68 | + |
| 69 | + DocumentReference<R> withConverter<R>({ |
| 70 | + required FromFirestore<R> fromFirestore, |
| 71 | + required ToFirestore<R> toFirestore, |
| 72 | + }) { |
| 73 | + return DocumentReference<R>( |
| 74 | + firestore: firestore, |
| 75 | + path: path, |
| 76 | + fromFirestore: fromFirestore, |
| 77 | + toFirestore: toFirestore, |
| 78 | + ); |
| 79 | + } |
| 80 | +} |
| 81 | + |
| 82 | +abstract class DocumentSnapshot<T> { |
| 83 | + final Firestore firestore; |
| 84 | + |
| 85 | + const DocumentSnapshot({ |
| 86 | + required this.firestore, |
| 87 | + }); |
| 88 | + |
| 89 | + DocumentReference<T> get reference; |
| 90 | + |
| 91 | + T data(); |
| 92 | +} |
0 commit comments