My app have String ID for all entities (Server side) I want migrate from Hive to Isar so I was reading the Isar documentation and find out it doesn't support String ID (only int), Yes I know that I can create a Class like this:
@Collection()
class Student {
int? id;
@Index(unique: true)
String? myServerId;
late String name;
final teacher = IsarLink<Teacher>();
}
When I fetch data (JSON) from server the id field (manager for Isar) don't came from server, so I save this record local Isar will create the ID OK, next time I get the same record from server is needed check via myServerId and if exist just update with id created by Isar its easy, how I can manager its with all entities filds like teacher? Need check every entities child or have a better way? Thanks in advance
set replace to true in @Index of myServerId,
like this:
@Collection()
class Student {
int? id;
@Index(unique: true , replace:true)
String? myServerId;
late String name;
final teacher = IsarLink<Teacher>();
}
check this Isar Replace indexes Doc.
Import the FNV hash algorithm for Dart language in your project.
flutter pub add fnv
FNV (Fowler–Noll–Vo) hash function is a 64bit fast hash algorithm recommended by the Author of Isar database.
Since Id property is required, we'll set the Id property to the 64 bit generated using the algorithm by passing your string id to it.
First import the package to your collection class.
import 'package:fnv/fnv.dart';
Then define your Id property as shown:
Id get isarId => fnv1a_32_s("YOUR STRING ID");
And retain your String id as well as a separate property.
The whole collection class will look like this:
import 'package:fnv/fnv.dart';
@Collection()
class Student {
@Index(unique: true, replace: true)
String? myServerId;
Id get isarId => fnv1a_32_s(myServerId);
late String name;
final teacher = IsarLink<Teacher>();
}
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With