Skip to content

Commit 6d025fd

Browse files
authored
feat: Add namedQueryWithConverterGet (#9715)
1 parent 75cd372 commit 6d025fd

File tree

2 files changed

+68
-0
lines changed

2 files changed

+68
-0
lines changed

Diff for: packages/cloud_firestore/cloud_firestore/example/test_driver/load_bundle_e2e.dart

+56
Original file line numberDiff line numberDiff line change
@@ -161,5 +161,61 @@ void runLoadBundleTests() {
161161
);
162162
});
163163
});
164+
165+
group('FirebaeFirestore.namedQueryWithConverterGet()', () {
166+
test('namedQueryWithConverterGet() successful', () async {
167+
const int number = 4;
168+
Uint8List buffer = await loadBundleSetup(number);
169+
LoadBundleTask task = firestore.loadBundle(buffer);
170+
171+
// ensure the bundle has been completely cached
172+
await task.stream.last;
173+
174+
// namedQuery 'named-bundle-test' which returns a QuerySnaphot of the same 3 documents
175+
// with 'number' property
176+
QuerySnapshot<ConverterPlaceholder> snapshot =
177+
await firestore.namedQueryWithConverterGet<ConverterPlaceholder>(
178+
'named-bundle-test-$number',
179+
options: const GetOptions(source: Source.cache),
180+
fromFirestore: ConverterPlaceholder.new,
181+
toFirestore: (value, options) => value.toFirestore(),
182+
);
183+
184+
expect(
185+
snapshot.docs.map((document) => document['number']),
186+
everyElement(anyOf(1, 2, 3)),
187+
);
188+
});
189+
190+
test('namedQueryWithConverterGet() error', () async {
191+
Uint8List buffer = await loadBundleSetup(4);
192+
LoadBundleTask task = firestore.loadBundle(buffer);
193+
194+
// ensure the bundle has been completely cached
195+
await task.stream.last;
196+
197+
await expectLater(
198+
firestore.namedQueryWithConverterGet<ConverterPlaceholder>(
199+
'wrong-name',
200+
options: const GetOptions(source: Source.cache),
201+
fromFirestore: ConverterPlaceholder.new,
202+
toFirestore: (value, options) => value.toFirestore(),
203+
),
204+
throwsA(
205+
isA<FirebaseException>()
206+
.having((e) => e.code, 'code', 'non-existent-named-query'),
207+
),
208+
);
209+
});
210+
});
164211
});
165212
}
213+
214+
class ConverterPlaceholder {
215+
ConverterPlaceholder(this.firestore, this.getOptions);
216+
217+
final DocumentSnapshot<Map<String, Object?>> firestore;
218+
final SnapshotOptions? getOptions;
219+
220+
Map<String, Object?> toFirestore() => firestore.data()!;
221+
}

Diff for: packages/cloud_firestore/cloud_firestore/lib/src/firestore.dart

+12
Original file line numberDiff line numberDiff line change
@@ -136,6 +136,18 @@ class FirebaseFirestore extends FirebasePluginPlatform {
136136
}
137137
}
138138

139+
/// Performs a [namedQueryGet] and decode the result using [Query.withConverter].
140+
Future<QuerySnapshot<T>> namedQueryWithConverterGet<T>(
141+
String name, {
142+
GetOptions options = const GetOptions(),
143+
required FromFirestore<T> fromFirestore,
144+
required ToFirestore<T> toFirestore,
145+
}) async {
146+
final snapshot = await namedQueryGet(name, options: options);
147+
148+
return _WithConverterQuerySnapshot<T>(snapshot, fromFirestore, toFirestore);
149+
}
150+
139151
/// Reads a [QuerySnapshot] if a namedQuery has been retrieved and passed as a [Buffer] to [loadBundle()]. To read from cache, pass [GetOptions.source] value as [Source.cache].
140152
/// To read from the Firestore backend, use [GetOptions.source] as [Source.server].
141153
Future<QuerySnapshot<Map<String, dynamic>>> namedQueryGet(

0 commit comments

Comments
 (0)