Skip to content

Commit

Permalink
feat: HiveStore
Browse files Browse the repository at this point in the history
  • Loading branch information
micimize committed May 19, 2020
1 parent 0d7ef7a commit 2c3c66c
Show file tree
Hide file tree
Showing 3 changed files with 48 additions and 1 deletion.
3 changes: 2 additions & 1 deletion packages/graphql/lib/src/cache/cache.dart
Original file line number Diff line number Diff line change
Expand Up @@ -7,8 +7,9 @@ import 'package:graphql/src/cache/store.dart';
import 'package:graphql/src/cache/_optimistic_transactions.dart';
import 'package:normalize/normalize.dart';

export 'package:graphql/src/cache/store.dart';
export 'package:graphql/src/cache/data_proxy.dart';
export 'package:graphql/src/cache/store.dart';
export 'package:graphql/src/cache/hive_store.dart';

class GraphQLCache extends NormalizingDataProxy {
GraphQLCache({
Expand Down
44 changes: 44 additions & 0 deletions packages/graphql/lib/src/cache/hive_store.dart
Original file line number Diff line number Diff line change
@@ -0,0 +1,44 @@
import 'package:meta/meta.dart';

import 'package:hive/hive.dart';

import './store.dart';

@immutable
class HiveStore extends Store {
@protected
final Box box;

/// Creates a HiveStore inititalized with [box],
/// which defaults to `Hive.box('defaultGraphqlStore')`
HiveStore([
Box box,
]) : box = box ?? Hive.box('defaultGraphqlStore');

@override
Map<String, dynamic> get(String dataId) {
final result = box.get(dataId);
if (result == null) return null;
return Map.from(result);
}

@override
void put(String dataId, Map<String, dynamic> value) {
box.put(dataId, value);
}

@override
void putAll(Map<String, Map<String, dynamic>> data) {
box.putAll(data);
}

@override
void delete(String dataId) {
box.delete(dataId);
}

@override
Map<String, Map<String, dynamic>> toMap() => Map.unmodifiable(box.toMap());

void reset() => box.clear();
}
2 changes: 2 additions & 0 deletions packages/graphql/pubspec.yaml
Original file line number Diff line number Diff line change
Expand Up @@ -21,6 +21,8 @@ dependencies:
gql_error_link: ^0.1.1-alpha
gql_dedupe_link: ^1.0.10

hive: ^1.3.0

quiver: ">=2.0.0 <3.0.0"
normalize: ^0.2.0

Expand Down

0 comments on commit 2c3c66c

Please sign in to comment.