Skip to content

Commit

Permalink
feat: firestore
Browse files Browse the repository at this point in the history
  • Loading branch information
BreX900 committed Apr 27, 2023
1 parent c736e72 commit 8c60489
Show file tree
Hide file tree
Showing 7 changed files with 36 additions and 26 deletions.
1 change: 0 additions & 1 deletion lib/src/app.dart
Original file line number Diff line number Diff line change
@@ -1,6 +1,5 @@
import 'dart:async';

import 'package:firebase_admin/src/firestore/firestore.dart';
import 'package:firebase_admin/src/storage.dart';

import '../firebase_admin.dart';
Expand Down
6 changes: 3 additions & 3 deletions lib/src/firestore/firestore.dart
Original file line number Diff line number Diff line change
Expand Up @@ -32,18 +32,18 @@ class Firestore implements FirebaseService {
CollectionReference<Map<String, dynamic>> collection(String id) {
return CollectionReference(
firestore: this,
path: id,
fromFirestore: fromFirestore,
toFirestore: toFirestore,
path: id,
);
}

DocumentReference<Map<String, dynamic>> doc(String id) {
return DocumentReference(
firestore: this,
path: id,
fromFirestore: fromFirestore,
toFirestore: toFirestore,
path: id,
);
}

Expand All @@ -53,8 +53,8 @@ class Firestore implements FirebaseService {
}) {
return Transaction.run(
firestore: this,
handler: handler,
timeout: timeout,
handler: handler,
);
}
}
20 changes: 10 additions & 10 deletions lib/src/firestore/query.dart
Original file line number Diff line number Diff line change
Expand Up @@ -13,8 +13,7 @@ class Query<T> {
final FromFirestore<T> fromFirestore;
final String path;

// FIXME: Remove nullability
final StructuredQuery? _query;
final StructuredQuery _query;

Query({
required this.firestore,
Expand Down Expand Up @@ -94,7 +93,7 @@ class Query<T> {
return Filter(
fieldFilter: FieldFilter(
field: FieldReference(fieldPath: field),
op: 'LESS_THAN',
op: op,
value: serializeValue(value),
),
);
Expand Down Expand Up @@ -153,7 +152,7 @@ class Query<T> {
Cursor? startAt,
List<Filter>? where,
}) {
final prevWhere = _query?.where;
final prevWhere = _query.where;
final filters = [
if (prevWhere != null)
if (prevWhere.compositeFilter?.filters != null)
Expand All @@ -168,13 +167,13 @@ class Query<T> {
fromFirestore: fromFirestore,
path: path,
query: StructuredQuery(
endAt: endAt ?? _query?.endAt,
from: _query?.from, // ???
limit: limit ?? _query?.limit,
endAt: endAt ?? _query.endAt,
from: _query.from, // ???
limit: limit ?? _query.limit,
offset: null, // ???
orderBy: orderBy != null ? [...?_query?.orderBy, orderBy] : _query?.orderBy,
orderBy: orderBy != null ? [...?_query.orderBy, orderBy] : _query.orderBy,
select: null, // Returns all document fields.
startAt: startAt ?? _query?.startAt,
startAt: startAt ?? _query.startAt,
where: filters.isEmpty
? null
: (filters.singleOrNull ??
Expand All @@ -190,11 +189,12 @@ class Query<T> {
}

class QuerySnapshot<T> {
final List<Document> _docs;
final Firestore firestore;
final ToFirestore<T> toFirestore;
final FromFirestore<T> fromFirestore;

final List<Document> _docs;

@internal
const QuerySnapshot({
required this.firestore,
Expand Down
2 changes: 1 addition & 1 deletion lib/src/firestore/transaction.dart
Original file line number Diff line number Diff line change
Expand Up @@ -64,9 +64,9 @@ class Transaction {
@internal
static Future<T> run<T>({
required Firestore firestore,
required Future<T> Function(Transaction transaction) handler,
Duration timeout = const Duration(seconds: 30),
// int maxAttempts = 5, TODO: Implement it
required Future<T> Function(Transaction transaction) handler,
}) async {
assert(timeout.inMilliseconds > 0, 'Transaction timeout must be more than 0 milliseconds');

Expand Down
2 changes: 1 addition & 1 deletion lib/src/firestore/utils/document_snapshot.dart
Original file line number Diff line number Diff line change
Expand Up @@ -44,5 +44,5 @@ class _RawDocumentSnapshot extends DocumentSnapshot<Map<String, dynamic>> {
);

@override
Map<String, dynamic> data() => deserializeData(_document.fields!);
Map<String, dynamic> data() => deserializeData(firestore, _document.fields!);
}
30 changes: 20 additions & 10 deletions lib/src/firestore/utils/serialization.dart
Original file line number Diff line number Diff line change
@@ -1,38 +1,46 @@
import 'package:firebase_admin/src/firestore/document.dart';
import 'package:googleapis/firestore/v1.dart';
import 'package:maps_toolkit/maps_toolkit.dart' as maps_toolkit;

import '../firestore.dart';

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

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

Map<String, dynamic> deserializeData(Map<String, Value> fields) {
return fields.map((key, value) => MapEntry(key, deserializeValue(value)));
Map<String, dynamic> deserializeData(Firestore firestore, Map<String, Value> fields) {
return fields.map((key, value) => MapEntry(key, deserializeValue(firestore, value)));
}

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

dynamic deserializeValue(Value value) {
dynamic deserializeValue(Firestore firestore, Value value) {
if (value.arrayValue != null) {
return value.arrayValue!.values!.map(deserializeValue).toList();
return value.arrayValue!.values!.map((value) => deserializeValue(firestore, value)).toList();
} else if (value.booleanValue != null) {
return value.booleanValue!;
} else if (value.bytesValue != null) {
return null;
return null; // TODO: Add support to it
} else if (value.doubleValue != null) {
return value.doubleValue!;
} else if (value.geoPointValue != null) {
return null;
return maps_toolkit.LatLng(value.geoPointValue!.latitude!, value.geoPointValue!.longitude!);
} else if (value.integerValue != null) {
return int.parse(value.integerValue!);
} else if (value.mapValue != null) {
return deserializeData(value.mapValue!.fields!);
return deserializeData(firestore, value.mapValue!.fields!);
} else if (value.nullValue != null) {
return null;
} else if (value.referenceValue != null) {
return null;
return DocumentReference<Map<String, dynamic>>(
firestore: firestore,
fromFirestore: fromFirestore,
toFirestore: toFirestore,
path: value.referenceValue!,
);
} else if (value.stringValue != null) {
return value.stringValue!;
} else if (value.timestampValue != null) {
Expand All @@ -46,11 +54,13 @@ Value serializeValue(dynamic data) {
booleanValue: data is bool ? data : null,
bytesValue: null,
doubleValue: data is double ? data : null,
geoPointValue: null,
geoPointValue: data is maps_toolkit.LatLng
? LatLng(latitude: data.latitude, longitude: data.longitude)
: null,
integerValue: data is int ? '$data' : null,
mapValue: data is Map<String, dynamic> ? MapValue(fields: serializeData(data)) : null,
nullValue: data == null ? 'nullValue' : null,
referenceValue: null,
referenceValue: data is DocumentReference<Map<String, dynamic>> ? data.path : null,
stringValue: data is String ? data : null,
timestampValue: data is DateTime ? '${data.microsecondsSinceEpoch}' : null,
);
Expand Down
1 change: 1 addition & 0 deletions pubspec.yaml
Original file line number Diff line number Diff line change
Expand Up @@ -18,6 +18,7 @@ dependencies:
http: ^0.13.0
crypto_keys: ^0.3.0
collection: ^1.15.0
maps_toolkit: ^2.0.1
gcloud: ^0.8.0
firebaseapis: ^0.1.2
snapshot: ^0.2.5
Expand Down

0 comments on commit 8c60489

Please sign in to comment.