Skip to content

Commit 4369dc5

Browse files
committed
more bugfixing
1 parent fbf6ba1 commit 4369dc5

File tree

7 files changed

+37
-23
lines changed

7 files changed

+37
-23
lines changed

lib/src/adapter/adapter.dart

Lines changed: 10 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -145,7 +145,7 @@ abstract class _BaseAdapter<T extends DataModelMixin<T>> with _Lifecycle {
145145
return result.map((r) {
146146
final map = Map<String, dynamic>.from(jsonDecode(r['data']));
147147
return deserializeLocal(map,
148-
key: r['key'].toString().typifyWith(internalType));
148+
key: (r['key'] as int).typifyWith(internalType));
149149
}).toList();
150150
}
151151

@@ -236,7 +236,7 @@ abstract class _BaseAdapter<T extends DataModelMixin<T>> with _Lifecycle {
236236
final data = jsonEncode(map);
237237
final result = ps.select([key, data]);
238238
savedKeys.add(
239-
result.first['key'].toString().typifyWith(adapter.internalType));
239+
(result.first['key'] as int).typifyWith(adapter.internalType));
240240
}
241241
ps.dispose();
242242
}
@@ -261,7 +261,7 @@ abstract class _BaseAdapter<T extends DataModelMixin<T>> with _Lifecycle {
261261

262262
/// Deletes models with [keys] from local storage.
263263
void deleteLocalByKeys(Iterable<String> keys, {bool notify = true}) {
264-
final intKeys = keys.map((k) => k.detypifyKey()).toList();
264+
final intKeys = keys.map((k) => k.detypifyKey()!).toList();
265265
db.execute(
266266
'DELETE FROM $internalType WHERE key IN (${keys.map((_) => '?').join(', ')})',
267267
intKeys);
@@ -272,14 +272,14 @@ abstract class _BaseAdapter<T extends DataModelMixin<T>> with _Lifecycle {
272272
}
273273

274274
/// Deletes all models of type [T] in local storage.
275+
///
276+
/// Async in case some implementations need to remove files.
275277
Future<void> clearLocal({bool notify = true}) async {
276-
// print(db.select('SELECT name FROM sqlite_master WHERE type=?', ['table']));
277-
// TODO should also clear edges?
278+
final _ = db.select('DELETE FROM $internalType RETURNING key;');
279+
final keys =
280+
_.map((e) => (e['key'] as int).typifyWith(internalType)).toList();
281+
await core.deleteKeys(keys);
278282

279-
// leave async in case some impls need to remove files
280-
for (final adapter in adapters.values) {
281-
db.execute('DELETE FROM ${adapter.internalType}');
282-
}
283283
if (notify) {
284284
core._notify([internalType], type: DataGraphEventType.clear);
285285
}
@@ -296,7 +296,7 @@ abstract class _BaseAdapter<T extends DataModelMixin<T>> with _Lifecycle {
296296
final result =
297297
db.select('SELECT key FROM _keys WHERE type = ?', [internalType]);
298298
return result
299-
.map((r) => r['key'].toString().typifyWith(internalType))
299+
.map((r) => (r['key'] as int).typifyWith(internalType))
300300
.toSet();
301301
}
302302

lib/src/core/core_notifier.dart

Lines changed: 9 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -32,7 +32,7 @@ class CoreNotifier extends DelayedStateNotifier<DataGraphEvent> {
3232
'INSERT INTO _keys (type, id, is_int) VALUES (?, ?, ?) RETURNING key;',
3333
[type, id?.toString(), id is int]);
3434
}
35-
return result.first['key'].toString().typifyWith(type);
35+
return (result.first['key'] as int).typifyWith(type);
3636
}
3737

3838
/// Finds an ID, given a [key].
@@ -53,11 +53,15 @@ class CoreNotifier extends DelayedStateNotifier<DataGraphEvent> {
5353
return id;
5454
}
5555

56-
void deleteKeys(Iterable<String> keys) {
57-
final intKeys = keys.map((k) => k.detypifyKey()).toList();
56+
@protected
57+
Future<void> deleteKeys(Iterable<String> keys) async {
58+
final params = keys.map((_) => '?').join(', ');
59+
final intKeys = keys.map((k) => k.detypifyKey()!).toList();
60+
61+
storage.db.execute('DELETE FROM _keys WHERE key IN ($params);', intKeys);
5862
storage.db.execute(
59-
'DELETE FROM _keys WHERE key IN (${keys.map((_) => '?').join(', ')})',
60-
intKeys);
63+
'DELETE FROM _edges WHERE key_ IN ($params) OR _key IN ($params);',
64+
[...keys, ...keys]);
6165
}
6266

6367
@protected

lib/src/utils/extensions.dart

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -36,7 +36,8 @@ extension DynamicX on dynamic {
3636
if (this == null || _this.isEmpty) {
3737
return type;
3838
}
39-
return '$type#${_this.isNotEmpty ? _this : ''}';
39+
// If int use #, else use ##
40+
return '$type#${_this.isNotEmpty ? ('${this is! int ? '#' : ''}$_this') : ''}';
4041
}
4142
}
4243

lib/src/utils/framework.dart

Lines changed: 1 addition & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -56,7 +56,7 @@ typedef DataFinderAll<T extends DataModelMixin<T>> = Future<List<T>> Function({
5656

5757
typedef DataFinderOne<T extends DataModelMixin<T>> = Future<T?> Function(
5858
Object model, {
59-
bool? remote,
59+
bool remote,
6060
bool? background,
6161
Map<String, dynamic>? params,
6262
Map<String, String>? headers,
@@ -232,7 +232,6 @@ class DataRequestLabel with EquatableMixin {
232232
}) : _typeId = id.typifyWith(type),
233233
kind = kind.trim() {
234234
if (requestId != null) {
235-
// TODO what is @ , also: should change that ints are now # (not ##)
236235
assert(!requestId.contains('@'));
237236
}
238237
_requestIds.add(requestId ?? DataHelpers.generateShortKey());

test/core/core_test.dart

Lines changed: 9 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -70,10 +70,17 @@ void main() async {
7070
expect(container.familia.countLocal, length);
7171
});
7272

73+
test('typify', () {
74+
expect(2.typifyWith('posts'), 'posts#2');
75+
expect('2'.typifyWith('posts'), 'posts##2');
76+
expect('posts#2'.detypify(), 2);
77+
expect('posts##2'.detypify(), '2');
78+
});
79+
7380
test('namespace', () {
74-
expect('a9'.typifyWith('posts').namespaceWith('id'), 'id:posts#a9');
81+
expect('a9'.typifyWith('posts').namespaceWith('id'), 'id:posts##a9');
7582
expect('278#12'.typifyWith('animals').namespaceWith('zzz'),
76-
'zzz:animals#278#12');
83+
'zzz:animals##278#12');
7784
});
7885

7986
test('denamespace', () {

test/model/relationship/relationship_test.dart

Lines changed: 4 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -138,7 +138,7 @@ void main() async {
138138
expect(house.owner.value, familia); // same, passes here again
139139
});
140140

141-
test('scenario #3', () {
141+
test('scenario #3', () async {
142142
final igor = Person(name: 'Igor', age: 33).saveLocal();
143143
final f1 =
144144
Familia(surname: 'Kamchatka', persons: {igor}.asHasMany).saveLocal();
@@ -174,6 +174,9 @@ void main() async {
174174
f4.residence.value = House(address: 'Sakharova Prospekt, 19').saveLocal();
175175
f4.saveLocal();
176176
expect(f4.residence.value!.owner.value!.surname, 'Kamchatka');
177+
178+
await container.familia.clearLocal();
179+
expect(await container.familia.findAll(remote: false), isEmpty);
177180
});
178181

179182
test('scenario #4: maintain relationship reference validity', () async {

test/repository/repository_test.dart

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -353,15 +353,15 @@ void main() async {
353353

354354
await container.dogs.save(dogs.toList()[2], remote: false);
355355

356-
regexp = RegExp(r'^\d{2}:\d{3} \[save\/dogs#3@[0-9]{10}\]');
356+
regexp = RegExp(r'^\d{2}:\d{3} \[save\/dogs##3@[0-9]{10}\]');
357357
expect(logging.first, matches(regexp));
358358
expect(logging.first, endsWith('saved in local storage only'));
359359

360360
logging.clear();
361361

362362
await container.dogs.delete('3');
363363

364-
regexp = RegExp(r'^\d{2}:\d{3} \[delete\/dogs#3@[0-9]{10}\]');
364+
regexp = RegExp(r'^\d{2}:\d{3} \[delete\/dogs##3@[0-9]{10}\]');
365365
expect(logging.first, matches(regexp));
366366
expect(
367367
logging.first,

0 commit comments

Comments
 (0)