Skip to content

Commit 1aa6bee

Browse files
committed
Cloud Data Table #6
1 parent bb987dc commit 1aa6bee

File tree

1 file changed

+27
-23
lines changed

1 file changed

+27
-23
lines changed

lib/src/data/cloud_database.dart

Lines changed: 27 additions & 23 deletions
Original file line numberDiff line numberDiff line change
@@ -9,16 +9,21 @@ import '../logging/debug_logger.dart';
99
import '../logging/stat_tracker.dart';
1010
import '../utils/constants.dart';
1111

12-
const List<String> privateDocumentFields = [
13-
SDKConstants.id,
14-
SDKConstants.createdAt,
15-
SDKConstants.updatedAt,
16-
];
12+
const String _label = 'Cloud Database';
1713

18-
const List<String> privateDateTimeDocumentFields = [
19-
SDKConstants.createdAt,
20-
SDKConstants.updatedAt,
21-
];
14+
abstract final class PrivateDocumentFields {
15+
PrivateDocumentFields._();
16+
17+
static const String id = 'id';
18+
static const String createdAt = 'createdAt';
19+
static const String updatedAt = 'updatedAt';
20+
21+
static const List<String> all = [id, createdAt, updatedAt];
22+
23+
static const List<String> dateTimeFields = [createdAt, updatedAt];
24+
25+
static bool contains(String field) => all.contains(field);
26+
}
2227

2328
/// Allows access to cloud storage. Implementations of this class should be
2429
/// able to store and retrieve data from the cloud storage in secure manner.
@@ -594,17 +599,14 @@ Map<String, dynamic> sanitizeCloudDataForUse(
594599
// breaks reference and allows to modify the data.
595600
data = {...data};
596601

597-
// Late because it can potentially be unused;
598-
late final DateTime now = DateTime.now();
599-
600602
data[SDKConstants.createdAt] =
601-
deserializeCosmicValue(data[SDKConstants.createdAt] ?? now);
603+
deserializeCosmicValue(data[SDKConstants.createdAt]);
602604
data[SDKConstants.updatedAt] =
603-
deserializeCosmicValue(data[SDKConstants.updatedAt] ?? now);
605+
deserializeCosmicValue(data[SDKConstants.updatedAt]);
604606
data[SDKConstants.id] = docId;
605607

606608
// Sort private fields to the bottom.
607-
for (final field in privateDocumentFields) {
609+
for (final field in PrivateDocumentFields.all) {
608610
if (data.containsKey(field)) {
609611
final value = data.remove(field);
610612
data[field] = value;
@@ -622,7 +624,8 @@ Map<String, dynamic> sanitizeCloudDataToSend(
622624
Map<String, dynamic> data, {
623625
required String? docId,
624626
bool hidePrivateFields = false,
625-
bool allowEmptyData = false,
627+
bool allowEmptyData = false,
628+
bool autoChangeUpdatedAt = true,
626629
}) {
627630
if (data.isEmpty && !allowEmptyData) return data;
628631

@@ -631,23 +634,24 @@ Map<String, dynamic> sanitizeCloudDataToSend(
631634

632635
// Remove private fields.
633636
if (hidePrivateFields) {
634-
for (final field in privateDocumentFields) {
637+
for (final field in PrivateDocumentFields.all) {
635638
data.remove(field);
636639
}
637640
} else {
638-
// Late because it can potentially be unused;
639-
late final DateTime now = DateTime.now();
641+
late final DateTime now = DateTime.now().toUtc();
640642

641-
data[SDKConstants.createdAt] =
642-
serializedCosmicValue(data[SDKConstants.createdAt] ?? now);
643-
data[SDKConstants.updatedAt] = serializedCosmicValue(now);
643+
if (autoChangeUpdatedAt) {
644+
data[SDKConstants.createdAt] =
645+
serializedCosmicValue(data[SDKConstants.createdAt] ?? now);
646+
data[SDKConstants.updatedAt] = serializedCosmicValue(now);
647+
}
644648

645649
if (docId != null) {
646650
data[SDKConstants.id] = docId;
647651
}
648652

649653
// Sort private fields to the bottom.
650-
for (final field in privateDocumentFields) {
654+
for (final field in PrivateDocumentFields.all) {
651655
if (data.containsKey(field)) {
652656
final value = data.remove(field);
653657
data[field] = value;

0 commit comments

Comments
 (0)