Skip to content

Commit ae2e1a7

Browse files
committed
feat: firestore
1 parent c736e72 commit ae2e1a7

File tree

7 files changed

+41
-27
lines changed

7 files changed

+41
-27
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: 25 additions & 11 deletions
Original file line numberDiff line numberDiff line change
@@ -1,38 +1,49 @@
1+
import 'dart:convert';
2+
import 'dart:typed_data';
3+
14
import 'package:firebase_admin/src/firestore/document.dart';
25
import 'package:googleapis/firestore/v1.dart';
6+
import 'package:maps_toolkit/maps_toolkit.dart' as maps_toolkit;
7+
8+
import '../firestore.dart';
39

410
Map<String, dynamic> fromFirestore(DocumentSnapshot<Map<String, dynamic>> snapshot) =>
511
snapshot.data();
612

713
Map<String, dynamic> toFirestore(Map<String, dynamic> value) => value;
814

9-
Map<String, dynamic> deserializeData(Map<String, Value> fields) {
10-
return fields.map((key, value) => MapEntry(key, deserializeValue(value)));
15+
Map<String, dynamic> deserializeData(Firestore firestore, Map<String, Value> fields) {
16+
return fields.map((key, value) => MapEntry(key, deserializeValue(firestore, value)));
1117
}
1218

1319
Map<String, Value> serializeData(Map<String, dynamic> data) {
1420
return data.map((key, value) => MapEntry(key, serializeValue(value)));
1521
}
1622

17-
dynamic deserializeValue(Value value) {
23+
dynamic deserializeValue(Firestore firestore, Value value) {
1824
if (value.arrayValue != null) {
19-
return value.arrayValue!.values!.map(deserializeValue).toList();
25+
return value.arrayValue!.values!.map((value) => deserializeValue(firestore, value)).toList();
2026
} else if (value.booleanValue != null) {
2127
return value.booleanValue!;
2228
} else if (value.bytesValue != null) {
23-
return null;
29+
return base64.decode(value.bytesValue!);
2430
} else if (value.doubleValue != null) {
2531
return value.doubleValue!;
2632
} else if (value.geoPointValue != null) {
27-
return null;
33+
return maps_toolkit.LatLng(value.geoPointValue!.latitude!, value.geoPointValue!.longitude!);
2834
} else if (value.integerValue != null) {
2935
return int.parse(value.integerValue!);
3036
} else if (value.mapValue != null) {
31-
return deserializeData(value.mapValue!.fields!);
37+
return deserializeData(firestore, value.mapValue!.fields!);
3238
} else if (value.nullValue != null) {
3339
return null;
3440
} else if (value.referenceValue != null) {
35-
return null;
41+
return DocumentReference<Map<String, dynamic>>(
42+
firestore: firestore,
43+
fromFirestore: fromFirestore,
44+
toFirestore: toFirestore,
45+
path: value.referenceValue!,
46+
);
3647
} else if (value.stringValue != null) {
3748
return value.stringValue!;
3849
} else if (value.timestampValue != null) {
@@ -44,13 +55,16 @@ Value serializeValue(dynamic data) {
4455
return Value(
4556
arrayValue: data is List ? ArrayValue(values: data.map(serializeValue).toList()) : null,
4657
booleanValue: data is bool ? data : null,
47-
bytesValue: null,
58+
bytesValue:
59+
data is Uint8List ? base64.encode(data).replaceAll('/', '_').replaceAll('+', '-') : null,
4860
doubleValue: data is double ? data : null,
49-
geoPointValue: null,
61+
geoPointValue: data is maps_toolkit.LatLng
62+
? LatLng(latitude: data.latitude, longitude: data.longitude)
63+
: null,
5064
integerValue: data is int ? '$data' : null,
5165
mapValue: data is Map<String, dynamic> ? MapValue(fields: serializeData(data)) : null,
5266
nullValue: data == null ? 'nullValue' : null,
53-
referenceValue: null,
67+
referenceValue: data is DocumentReference<Map<String, dynamic>> ? data.path : null,
5468
stringValue: data is String ? data : null,
5569
timestampValue: data is DateTime ? '${data.microsecondsSinceEpoch}' : null,
5670
);

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)