Skip to content
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
11 changes: 7 additions & 4 deletions lib/src/models/yust_doc.dart
Original file line number Diff line number Diff line change
Expand Up @@ -87,15 +87,15 @@ abstract class YustDoc {
bool get hasChanges => _updateMask.isNotEmpty;

YustDoc({
String id = '',
String? id,
DateTime? createdAt,
String? createdBy,
DateTime? modifiedAt,
String? modifiedBy,
String? userId,
String? envId,
DateTime? expiresAt,
}) : _id = id,
}) : _id = id ?? createDocumentId(),
_createdAt = createdAt,
_createdBy = createdBy,
_modifiedAt = modifiedAt,
Expand All @@ -104,8 +104,6 @@ abstract class YustDoc {
_envId = envId,
_expiresAt = expiresAt;

YustDoc.fromJson(Map<String, dynamic> json) : _id = '';

Map<String, dynamic> toJson();

Map<String, dynamic> toExportJson() {
Expand All @@ -125,6 +123,11 @@ abstract class YustDoc {
return GoogleCloudHelpers.convertTimestamp(value);
}

/// Creates a new document ID.
static String createDocumentId() {
return Yust.helpers.randomString(length: 20);
}

/// clear the update mask
void clearUpdateMask() => updateMask.clear();
}
8 changes: 8 additions & 0 deletions lib/src/models/yust_user.dart
Original file line number Diff line number Diff line change
Expand Up @@ -186,6 +186,14 @@ class YustUser extends YustDoc {
Map<String, dynamic> get userAttributes => Map.unmodifiable(_userAttributes);

YustUser({
super.id,
super.createdAt,
super.createdBy,
super.modifiedAt,
super.modifiedBy,
super.userId,
super.envId,
super.expiresAt,
required String email,
required String firstName,
required String lastName,
Expand Down
28 changes: 14 additions & 14 deletions lib/src/models/yust_user.g.dart

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

13 changes: 8 additions & 5 deletions lib/src/services/yust_database_service_dart.dart
Original file line number Diff line number Diff line change
Expand Up @@ -107,7 +107,7 @@ class YustDatabaseService implements IYustDatabaseService {
/// assigned a new id becoming a new document if it had an id previously.
@override
T initDoc<T extends YustDoc>(YustDocSetup<T> docSetup, [T? doc]) {
final id = _createDocumentId();
final id = YustDoc.createDocumentId();
return doInitDoc(docSetup, id, doc);
}

Expand Down Expand Up @@ -1504,6 +1504,13 @@ class YustDatabaseService implements IYustDatabaseService {
return null;
}

if (json['id'] is! String || json['id'].isEmpty) {
print(
'[[WARNING]] Error Transforming JSON. Document has no id: Collection ${docSetup.collectionName}, Name ${document.name}',
);
return null;
}

try {
final doc = docSetup.fromJson(json);
doc.clearUpdateMask();
Expand Down Expand Up @@ -1615,10 +1622,6 @@ class YustDatabaseService implements IYustDatabaseService {
}
}

String _createDocumentId() {
return Yust.helpers.randomString(length: 20);
}

/// Retries the given function if a TlsException, ClientException or YustBadGatewayException occurs.
/// Those are network errors that can occur when the firestore is rate-limiting.
Future<T> _retryOnException<T>(
Expand Down
113 changes: 57 additions & 56 deletions lib/src/services/yust_database_service_flutter.dart
Original file line number Diff line number Diff line change
Expand Up @@ -35,36 +35,35 @@ class YustDatabaseService implements IYustDatabaseService {
final Yust _yust;

YustDatabaseService({required Yust yust, String? emulatorAddress})
: _yust = yust,
envCollectionName = yust.envCollectionName,
useSubcollections = yust.useSubcollections,
_fireStore = FirebaseFirestore.instance {
dbLogCallback =
(
DatabaseLogAction action,
String documentPath,
int count, {
String? id,
List<String>? updateMask,
num? aggregationResult,
}) {
statistics.dbStatisticsCallback(
action,
documentPath,
count,
id: id,
updateMask: updateMask,
aggregationResult: aggregationResult,
);
yust.dbLogCallback?.call(
action,
documentPath,
count,
id: id,
updateMask: updateMask,
aggregationResult: aggregationResult,
);
};
: _yust = yust,
envCollectionName = yust.envCollectionName,
useSubcollections = yust.useSubcollections,
_fireStore = FirebaseFirestore.instance {
dbLogCallback = (
DatabaseLogAction action,
String documentPath,
int count, {
String? id,
List<String>? updateMask,
num? aggregationResult,
}) {
statistics.dbStatisticsCallback(
action,
documentPath,
count,
id: id,
updateMask: updateMask,
aggregationResult: aggregationResult,
);
yust.dbLogCallback?.call(
action,
documentPath,
count,
id: id,
updateMask: updateMask,
aggregationResult: aggregationResult,
);
};
}

/// Represents the collection name for the tenants.
Expand All @@ -76,10 +75,10 @@ class YustDatabaseService implements IYustDatabaseService {
final bool useSubcollections;

YustDatabaseService.mocked({required Yust yust, String? emulatorAddress})
: _yust = yust,
envCollectionName = yust.envCollectionName,
useSubcollections = yust.useSubcollections,
dbLogCallback = yust.dbLogCallback {
: _yust = yust,
envCollectionName = yust.envCollectionName,
useSubcollections = yust.useSubcollections,
dbLogCallback = yust.dbLogCallback {
throw UnsupportedError('Not supported in Flutter Environment');
}

Expand All @@ -97,12 +96,12 @@ class YustDatabaseService implements IYustDatabaseService {
.get(GetOptions(source: Source.serverAndCache))
.then((docSnapshot) => _transformDoc<T>(docSetup, docSnapshot))
.catchError((e) {
if (e is FirebaseException && e.code == 'permission-denied') {
print('Permission denied for doc: ${docSetup.collectionName}/$id');
return null;
}
throw e;
});
if (e is FirebaseException && e.code == 'permission-denied') {
print('Permission denied for doc: ${docSetup.collectionName}/$id');
return null;
}
throw e;
});
dbLogCallback?.call(
DatabaseLogAction.get,
_getCollectionPath(docSetup),
Expand Down Expand Up @@ -167,13 +166,13 @@ class YustDatabaseService implements IYustDatabaseService {
.doc(id)
.snapshots()
.map((docSnapshot) {
dbLogCallback?.call(
DatabaseLogActionExtension.fromSnapshot(docSnapshot),
docSnapshot.reference.parent.path,
docSnapshot.exists ? 1 : 0,
);
return _transformDoc(docSetup, docSnapshot);
});
dbLogCallback?.call(
DatabaseLogActionExtension.fromSnapshot(docSnapshot),
docSnapshot.reference.parent.path,
docSnapshot.exists ? 1 : 0,
);
return _transformDoc(docSetup, docSnapshot);
});
}

@override
Expand Down Expand Up @@ -419,9 +418,8 @@ class YustDatabaseService implements IYustDatabaseService {
int? limit,
}) async {
var query = getQuery(docSetup, filters: filters);
final snapshot = await query
.aggregate(cf.average(fieldPath), cf.count())
.get();
final snapshot =
await query.aggregate(cf.average(fieldPath), cf.count()).get();
return (count: snapshot.count ?? 0, result: snapshot.getAverage(fieldPath));
}

Expand Down Expand Up @@ -704,9 +702,8 @@ class YustDatabaseService implements IYustDatabaseService {
YustDocSetup<T> docSetup,
T doc,
) async {
final docRef = _fireStore
.collection(_getCollectionPath(docSetup))
.doc(doc.id);
final docRef =
_fireStore.collection(_getCollectionPath(docSetup)).doc(doc.id);
await docRef.delete();
dbLogCallback?.call(
DatabaseLogAction.delete,
Expand All @@ -721,9 +718,8 @@ class YustDatabaseService implements IYustDatabaseService {
YustDocSetup<T> docSetup,
String docId,
) async {
final docRef = _fireStore
.collection(_getCollectionPath(docSetup))
.doc(docId);
final docRef =
_fireStore.collection(_getCollectionPath(docSetup)).doc(docId);
await docRef.delete();
dbLogCallback?.call(
DatabaseLogAction.delete,
Expand Down Expand Up @@ -956,6 +952,11 @@ class YustDatabaseService implements IYustDatabaseService {
}
final data = snapshot.data();
if (data is Map<String, dynamic>) {
if (data['id'] is! String || data['id'].isEmpty) {
print(
'[[WARNING]] Error Transforming JSON. Document has no id: ${snapshot.reference.path}');
return null;
}
// Convert Timestamps to ISOStrings
final modifiedData = TraverseObject.traverseObject(data, (currentNode) {
// Convert Timestamp to Iso8601-String, as this is the format json_serializable expects
Expand Down