Skip to content

Commit 6b28b45

Browse files
Merge pull request #1421 from ronnnnn/fix-hive-store-to-map
fix(graphql): transform nested object from box
2 parents 385c331 + 3ac14cc commit 6b28b45

File tree

2 files changed

+32
-2
lines changed

2 files changed

+32
-2
lines changed

packages/graphql/lib/src/cache/hive_store.dart

+10-2
Original file line numberDiff line numberDiff line change
@@ -1,7 +1,7 @@
11
import 'dart:async';
2-
import 'package:meta/meta.dart';
32

43
import 'package:hive/hive.dart';
4+
import 'package:meta/meta.dart';
55

66
import './store.dart';
77

@@ -92,7 +92,15 @@ class HiveStore extends Store {
9292
}
9393

9494
@override
95-
Map<String, Map<String, dynamic>?> toMap() => Map.unmodifiable(box.toMap());
95+
Map<String, Map<String, dynamic>?> toMap() {
96+
final map = <String, Map<String, dynamic>?>{};
97+
for (final key in box.keys) {
98+
if (key is String) {
99+
map[key] = get(key);
100+
}
101+
}
102+
return Map.unmodifiable(map);
103+
}
96104

97105
Future<void> reset() => box.clear();
98106
}

packages/graphql/test/cache/store_test.dart

+22
Original file line numberDiff line numberDiff line change
@@ -121,6 +121,28 @@ void main() {
121121
expect(readData2?['bob'], isA<List<dynamic>>());
122122
expect(readData2?['bob'][0], isA<Map<String, dynamic>>());
123123
});
124+
test("Can re-open and reference nested data", () async {
125+
final box1 = await HiveStore.openBox(
126+
're-open-store',
127+
path: path,
128+
);
129+
final store = HiveStore(box1);
130+
final data = {
131+
'foo': 'bar',
132+
'bob': [
133+
{'nested': true}
134+
]
135+
};
136+
store.put("id", data);
137+
expect(store.toMap(), equals({'id': data}));
138+
await box1.close();
139+
final box2 = await HiveStore.openBox(
140+
're-open-store',
141+
path: path,
142+
);
143+
final store2 = HiveStore(box2);
144+
expect(store2.toMap(), equals({'id': data}));
145+
});
124146
test("Can put null", () async {
125147
final box1 = await HiveStore.openBox(
126148
'put-null',

0 commit comments

Comments
 (0)