-
-
Notifications
You must be signed in to change notification settings - Fork 20
/
batch.dart
136 lines (124 loc) 路 3.5 KB
/
batch.dart
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
import '../flamingo.dart';
abstract class BatchRepository {
bool get isAddedDocument;
int get addedDocumentCount;
void save(
Document document, {
DocumentReference? reference,
});
void update(
Document document, {
DocumentReference? reference,
});
void delete(
Document document, {
DocumentReference? reference,
});
void saveRaw(
Map<String, dynamic> values,
DocumentReference reference, {
bool isTimestamp = false,
String createdFieldValueKey = documentCreatedAtKey,
String updatedFieldValueKey = documentUpdatedAtKey,
});
void updateRaw(
Map<String, dynamic> values,
DocumentReference reference, {
bool isTimestamp = false,
String updatedFieldValueKey = documentUpdatedAtKey,
});
void deleteWithReference(DocumentReference reference);
Future commit();
}
class Batch implements BatchRepository {
Batch() {
_writeBatch = Flamingo.instance.firestore.batch();
}
@override
bool get isAddedDocument => _batchDocument.isNotEmpty;
@override
int get addedDocumentCount => _batchDocument.length;
late WriteBatch _writeBatch;
final List<_BatchDocument> _batchDocument = [];
@override
void save(
Document document, {
DocumentReference? reference,
}) {
final data = document.toData();
final nowAt = Timestamp.now();
data[document.createdFieldValueKey] = nowAt;
data[document.updatedFieldValueKey] = nowAt;
document.setAt(createdAt: nowAt, updatedAt: nowAt);
_writeBatch.set(
reference ?? document.reference, data, SetOptions(merge: true));
_batchDocument.add(_BatchDocument(document, ExecuteType.create));
}
@override
void update(
Document document, {
DocumentReference? reference,
}) {
final data = document.toData();
final nowAt = Timestamp.now();
data[document.updatedFieldValueKey] = nowAt;
document.setAt(updatedAt: nowAt);
_writeBatch.update(reference ?? document.reference, data);
_batchDocument.add(_BatchDocument(document, ExecuteType.update));
}
@override
void delete(
Document document, {
DocumentReference? reference,
}) {
_writeBatch.delete(reference ?? document.reference);
_batchDocument.add(_BatchDocument(document, ExecuteType.delete));
}
@override
void saveRaw(
Map<String, dynamic> values,
DocumentReference reference, {
bool isTimestamp = false,
String createdFieldValueKey = documentCreatedAtKey,
String updatedFieldValueKey = documentUpdatedAtKey,
}) {
final data = values;
final nowAt = Timestamp.now();
if (isTimestamp) {
data[createdFieldValueKey] = nowAt;
data[updatedFieldValueKey] = nowAt;
}
_writeBatch.set(reference, data, SetOptions(merge: true));
}
@override
void updateRaw(
Map<String, dynamic> values,
DocumentReference reference, {
bool isTimestamp = false,
String updatedFieldValueKey = documentUpdatedAtKey,
}) {
final data = values;
final nowAt = Timestamp.now();
if (isTimestamp) {
data[updatedFieldValueKey] = nowAt;
}
_writeBatch.update(reference, data);
}
@override
void deleteWithReference(DocumentReference reference) {
_writeBatch.delete(reference);
}
@override
Future commit() async {
await _writeBatch.commit();
for (final item in _batchDocument) {
item.document.onCompleted(item.executeType);
}
_batchDocument.clear();
}
}
class _BatchDocument {
_BatchDocument(this.document, this.executeType);
final Document document;
final ExecuteType executeType;
}