Skip to content

Commit 8c60489

Browse files
committed
feat: firestore
1 parent c736e72 commit 8c60489

File tree

7 files changed

+36
-26
lines changed

7 files changed

+36
-26
lines changed

lib/src/app.dart

Lines changed: 0 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,5 @@
11
import 'dart:async';
22

3-
import 'package:firebase_admin/src/firestore/firestore.dart';
43
import 'package:firebase_admin/src/storage.dart';
54

65
import '../firebase_admin.dart';

lib/src/firestore/firestore.dart

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -32,18 +32,18 @@ class Firestore implements FirebaseService {
3232
CollectionReference<Map<String, dynamic>> collection(String id) {
3333
return CollectionReference(
3434
firestore: this,
35-
path: id,
3635
fromFirestore: fromFirestore,
3736
toFirestore: toFirestore,
37+
path: id,
3838
);
3939
}
4040

4141
DocumentReference<Map<String, dynamic>> doc(String id) {
4242
return DocumentReference(
4343
firestore: this,
44-
path: id,
4544
fromFirestore: fromFirestore,
4645
toFirestore: toFirestore,
46+
path: id,
4747
);
4848
}
4949

@@ -53,8 +53,8 @@ class Firestore implements FirebaseService {
5353
}) {
5454
return Transaction.run(
5555
firestore: this,
56-
handler: handler,
5756
timeout: timeout,
57+
handler: handler,
5858
);
5959
}
6060
}

lib/src/firestore/query.dart

Lines changed: 10 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -13,8 +13,7 @@ class Query<T> {
1313
final FromFirestore<T> fromFirestore;
1414
final String path;
1515

16-
// FIXME: Remove nullability
17-
final StructuredQuery? _query;
16+
final StructuredQuery _query;
1817

1918
Query({
2019
required this.firestore,
@@ -94,7 +93,7 @@ class Query<T> {
9493
return Filter(
9594
fieldFilter: FieldFilter(
9695
field: FieldReference(fieldPath: field),
97-
op: 'LESS_THAN',
96+
op: op,
9897
value: serializeValue(value),
9998
),
10099
);
@@ -153,7 +152,7 @@ class Query<T> {
153152
Cursor? startAt,
154153
List<Filter>? where,
155154
}) {
156-
final prevWhere = _query?.where;
155+
final prevWhere = _query.where;
157156
final filters = [
158157
if (prevWhere != null)
159158
if (prevWhere.compositeFilter?.filters != null)
@@ -168,13 +167,13 @@ class Query<T> {
168167
fromFirestore: fromFirestore,
169168
path: path,
170169
query: StructuredQuery(
171-
endAt: endAt ?? _query?.endAt,
172-
from: _query?.from, // ???
173-
limit: limit ?? _query?.limit,
170+
endAt: endAt ?? _query.endAt,
171+
from: _query.from, // ???
172+
limit: limit ?? _query.limit,
174173
offset: null, // ???
175-
orderBy: orderBy != null ? [...?_query?.orderBy, orderBy] : _query?.orderBy,
174+
orderBy: orderBy != null ? [...?_query.orderBy, orderBy] : _query.orderBy,
176175
select: null, // Returns all document fields.
177-
startAt: startAt ?? _query?.startAt,
176+
startAt: startAt ?? _query.startAt,
178177
where: filters.isEmpty
179178
? null
180179
: (filters.singleOrNull ??
@@ -190,11 +189,12 @@ class Query<T> {
190189
}
191190

192191
class QuerySnapshot<T> {
193-
final List<Document> _docs;
194192
final Firestore firestore;
195193
final ToFirestore<T> toFirestore;
196194
final FromFirestore<T> fromFirestore;
197195

196+
final List<Document> _docs;
197+
198198
@internal
199199
const QuerySnapshot({
200200
required this.firestore,

lib/src/firestore/transaction.dart

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -64,9 +64,9 @@ class Transaction {
6464
@internal
6565
static Future<T> run<T>({
6666
required Firestore firestore,
67-
required Future<T> Function(Transaction transaction) handler,
6867
Duration timeout = const Duration(seconds: 30),
6968
// int maxAttempts = 5, TODO: Implement it
69+
required Future<T> Function(Transaction transaction) handler,
7070
}) async {
7171
assert(timeout.inMilliseconds > 0, 'Transaction timeout must be more than 0 milliseconds');
7272

lib/src/firestore/utils/document_snapshot.dart

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -44,5 +44,5 @@ class _RawDocumentSnapshot extends DocumentSnapshot<Map<String, dynamic>> {
4444
);
4545

4646
@override
47-
Map<String, dynamic> data() => deserializeData(_document.fields!);
47+
Map<String, dynamic> data() => deserializeData(firestore, _document.fields!);
4848
}

lib/src/firestore/utils/serialization.dart

Lines changed: 20 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -1,38 +1,46 @@
11
import 'package:firebase_admin/src/firestore/document.dart';
22
import 'package:googleapis/firestore/v1.dart';
3+
import 'package:maps_toolkit/maps_toolkit.dart' as maps_toolkit;
4+
5+
import '../firestore.dart';
36

47
Map<String, dynamic> fromFirestore(DocumentSnapshot<Map<String, dynamic>> snapshot) =>
58
snapshot.data();
69

710
Map<String, dynamic> toFirestore(Map<String, dynamic> value) => value;
811

9-
Map<String, dynamic> deserializeData(Map<String, Value> fields) {
10-
return fields.map((key, value) => MapEntry(key, deserializeValue(value)));
12+
Map<String, dynamic> deserializeData(Firestore firestore, Map<String, Value> fields) {
13+
return fields.map((key, value) => MapEntry(key, deserializeValue(firestore, value)));
1114
}
1215

1316
Map<String, Value> serializeData(Map<String, dynamic> data) {
1417
return data.map((key, value) => MapEntry(key, serializeValue(value)));
1518
}
1619

17-
dynamic deserializeValue(Value value) {
20+
dynamic deserializeValue(Firestore firestore, Value value) {
1821
if (value.arrayValue != null) {
19-
return value.arrayValue!.values!.map(deserializeValue).toList();
22+
return value.arrayValue!.values!.map((value) => deserializeValue(firestore, value)).toList();
2023
} else if (value.booleanValue != null) {
2124
return value.booleanValue!;
2225
} else if (value.bytesValue != null) {
23-
return null;
26+
return null; // TODO: Add support to it
2427
} else if (value.doubleValue != null) {
2528
return value.doubleValue!;
2629
} else if (value.geoPointValue != null) {
27-
return null;
30+
return maps_toolkit.LatLng(value.geoPointValue!.latitude!, value.geoPointValue!.longitude!);
2831
} else if (value.integerValue != null) {
2932
return int.parse(value.integerValue!);
3033
} else if (value.mapValue != null) {
31-
return deserializeData(value.mapValue!.fields!);
34+
return deserializeData(firestore, value.mapValue!.fields!);
3235
} else if (value.nullValue != null) {
3336
return null;
3437
} else if (value.referenceValue != null) {
35-
return null;
38+
return DocumentReference<Map<String, dynamic>>(
39+
firestore: firestore,
40+
fromFirestore: fromFirestore,
41+
toFirestore: toFirestore,
42+
path: value.referenceValue!,
43+
);
3644
} else if (value.stringValue != null) {
3745
return value.stringValue!;
3846
} else if (value.timestampValue != null) {
@@ -46,11 +54,13 @@ Value serializeValue(dynamic data) {
4654
booleanValue: data is bool ? data : null,
4755
bytesValue: null,
4856
doubleValue: data is double ? data : null,
49-
geoPointValue: null,
57+
geoPointValue: data is maps_toolkit.LatLng
58+
? LatLng(latitude: data.latitude, longitude: data.longitude)
59+
: null,
5060
integerValue: data is int ? '$data' : null,
5161
mapValue: data is Map<String, dynamic> ? MapValue(fields: serializeData(data)) : null,
5262
nullValue: data == null ? 'nullValue' : null,
53-
referenceValue: null,
63+
referenceValue: data is DocumentReference<Map<String, dynamic>> ? data.path : null,
5464
stringValue: data is String ? data : null,
5565
timestampValue: data is DateTime ? '${data.microsecondsSinceEpoch}' : null,
5666
);

pubspec.yaml

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -18,6 +18,7 @@ dependencies:
1818
http: ^0.13.0
1919
crypto_keys: ^0.3.0
2020
collection: ^1.15.0
21+
maps_toolkit: ^2.0.1
2122
gcloud: ^0.8.0
2223
firebaseapis: ^0.1.2
2324
snapshot: ^0.2.5

0 commit comments

Comments
 (0)