Support for HiveLink
- Dominant language
- Dart
- Stars
- 4.4k
- Forks
- 449
- PR merge metrics
- No merged PRs in 30d
Description
I red a few threads here about the support for a single element version of HiveList. I know it will be a feature of Hive 2 (Isar) but since the two projects will be separated, I think that it would be a great feature to add the native support of "HiveLink" (or HivePointer).
Currently I'm using a custom class to obtain the same result:
```dart
import 'package:hive/hive.dart';
part 'hive_link.g.dart';
@HiveType(typeId: 0)
class HiveLink {
@HiveField(0)
String _boxName;
@HiveField(1)
var _key; // Could be String or Int
HiveLink();
T getElement() =>
_boxName == null || _key == null ? null : Hive.box(_boxName).get(_key);
setElement(T element) {
_boxName = element?.box?.name;
_key = element?.key;
}
}
```
Here a little example to model a Dog and his mother:
```dart
import 'package:hive/hive.dart';
import 'package:meta/meta.dart';
import 'hive_link.dart';
part 'Dog.g.dart';
@HiveType(typeId: 1)
class Dog extends HiveObject {
// Little hack to support one-to-one relationship in Hive
// https://github.com/hivedb/hive/issues/197
@HiveField(0)
HiveLink _motherLink;
Dog get mother =>
_motherLink?.getElement();
void set mother(Dog dog) =>
_motherLink = HiveLink()..setElement(dog);
Dog({
Dog mother,
}) {
this.mother = mother;
}
}
```
Contributor guide
No contributing guide indexed for this repository
Assessment
This issue has not been assessed yet.