refactor: Move the CRDT extract utility to a dedicated file
This commit is contained in:
parent
74d2c728f0
commit
df0f798166
3 changed files with 50 additions and 46 deletions
|
|
@ -4,6 +4,7 @@
|
||||||
library;
|
library;
|
||||||
|
|
||||||
export 'src/crdt.dart';
|
export 'src/crdt.dart';
|
||||||
|
export 'src/crdt/extensions/extract.dart';
|
||||||
export 'src/crdt_base.dart';
|
export 'src/crdt_base.dart';
|
||||||
export 'src/database.dart';
|
export 'src/database.dart';
|
||||||
export 'src/hlc/converter.dart';
|
export 'src/hlc/converter.dart';
|
||||||
|
|
|
||||||
49
lib/src/crdt/extensions/extract.dart
Normal file
49
lib/src/crdt/extensions/extract.dart
Normal file
|
|
@ -0,0 +1,49 @@
|
||||||
|
import 'package:drift/drift.dart';
|
||||||
|
|
||||||
|
import '../../database.dart';
|
||||||
|
|
||||||
|
/// Extensions for [CrdtDatabase] to extract data from the CRDT data table.
|
||||||
|
extension CrdtDatabaseExtractExtensions on CrdtDatabase {
|
||||||
|
/// Gets a single data entry from the CRDT data table.
|
||||||
|
Future<T?> getSingleFromCrdtData<T extends Insertable>({
|
||||||
|
required String tableName,
|
||||||
|
required String rowId,
|
||||||
|
}) async {
|
||||||
|
final data = await getFromCrdtData<T>(tableName: tableName, rowIds: [rowId]);
|
||||||
|
return data.firstOrNull;
|
||||||
|
}
|
||||||
|
|
||||||
|
/// Gets a list of data entries from the CRDT data table.
|
||||||
|
Future<List<T>> getFromCrdtData<T extends Insertable>({
|
||||||
|
required String tableName,
|
||||||
|
required Iterable<String> rowIds,
|
||||||
|
}) async {
|
||||||
|
final tableInfo = synchronizedTables.firstWhere(
|
||||||
|
(t) => t.actualTableName == tableName,
|
||||||
|
orElse: () => throw ArgumentError(
|
||||||
|
'Table $tableName not found in synchronized tables.',
|
||||||
|
),
|
||||||
|
);
|
||||||
|
|
||||||
|
final crdtDataEntries = await managers.crdtDataTable
|
||||||
|
.filter((o) => o.tblName.equals(tableName) & o.rowId.isIn(rowIds))
|
||||||
|
.get();
|
||||||
|
|
||||||
|
return crdtDataEntries.convert<T>(tableInfo);
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
extension on List<CrdtDataEntry> {
|
||||||
|
Future<List<T>> convert<T extends Insertable>(TableInfo tableInfo) async {
|
||||||
|
final foundRowIds = map((e) => e.rowId).toSet();
|
||||||
|
return [
|
||||||
|
for (final rowId in foundRowIds)
|
||||||
|
await tableInfo.map({
|
||||||
|
for (final c in tableInfo.$columns)
|
||||||
|
c.$name: firstWhere((e) => e.rowId == rowId && e.columnName == c.$name)
|
||||||
|
.rawValue
|
||||||
|
?.rawSqlValue,
|
||||||
|
}) as T,
|
||||||
|
];
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
@ -32,49 +32,3 @@ class CrdtDatabase extends _$CrdtDatabase {
|
||||||
@override
|
@override
|
||||||
int schemaVersion = 1;
|
int schemaVersion = 1;
|
||||||
}
|
}
|
||||||
|
|
||||||
/// Extensions for [CrdtDatabase] to extract data from the CRDT data table.
|
|
||||||
extension CrdtDatabaseDataEntryExtensions on CrdtDatabase {
|
|
||||||
/// Gets a single data entry from the CRDT data table.
|
|
||||||
Future<T?> getSingleFromCrdtData<T extends DataClass>({
|
|
||||||
required String tableName,
|
|
||||||
required String rowId,
|
|
||||||
}) async {
|
|
||||||
final data = await getFromCrdtData<T>(tableName: tableName, rowIds: [rowId]);
|
|
||||||
return data.firstOrNull;
|
|
||||||
}
|
|
||||||
|
|
||||||
/// Gets a list of data entries from the CRDT data table.
|
|
||||||
Future<List<T>> getFromCrdtData<T extends DataClass>({
|
|
||||||
required String tableName,
|
|
||||||
required List<String> rowIds,
|
|
||||||
}) async {
|
|
||||||
final tableInfo = synchronizedTables.firstWhere(
|
|
||||||
(t) => t.actualTableName == tableName,
|
|
||||||
orElse: () => throw ArgumentError(
|
|
||||||
'Table $tableName not found in synchronized tables.',
|
|
||||||
),
|
|
||||||
);
|
|
||||||
|
|
||||||
final crdtDataEntries = await managers.crdtDataTable
|
|
||||||
.filter((o) => o.tblName.equals(tableName) & o.rowId.isIn(rowIds))
|
|
||||||
.get();
|
|
||||||
|
|
||||||
return crdtDataEntries.convert<T>(tableInfo);
|
|
||||||
}
|
|
||||||
}
|
|
||||||
|
|
||||||
extension on List<CrdtDataEntry> {
|
|
||||||
Future<List<T>> convert<T extends DataClass>(TableInfo tableInfo) async {
|
|
||||||
final foundRowIds = map((e) => e.rowId).toSet();
|
|
||||||
return [
|
|
||||||
for (final rowId in foundRowIds)
|
|
||||||
await tableInfo.map({
|
|
||||||
for (final c in tableInfo.$columns)
|
|
||||||
c.$name: firstWhere((e) => e.rowId == rowId && e.columnName == c.$name)
|
|
||||||
.rawValue
|
|
||||||
?.rawSqlValue,
|
|
||||||
}) as T,
|
|
||||||
];
|
|
||||||
}
|
|
||||||
}
|
|
||||||
|
|
|
||||||
Loading…
Reference in a new issue