isar icon indicating copy to clipboard operation
isar copied to clipboard

Support TypeConverter

Open Hcs66 opened this issue 3 years ago • 4 comments

Thanks for the excellent product!

Is your feature request related to a problem? Please describe. I have a few "TypeConverter"s in version 2.5, how can I migrate them (both the data and code)?

image

image

Describe alternatives you've considered Any solution with "Embedded"?

Version

  • Platform: iOS, Android, Mac, Windows, Linux, Web
  • Flutter version: 3.3.4
  • Isar version: 3.0.2

Hcs66 avatar Oct 11 '22 04:10 Hcs66

Anyone can help? Thanks!

Hcs66 avatar Oct 17 '22 11:10 Hcs66

We would also like to see a solution for migrating type converters. For now we have been using @embedded to replace type converters, but they have some limitations that the old TypeConverter design did not have.

KonstantinRr avatar Oct 17 '22 13:10 KonstantinRr

We would also like to see a solution for migrating type converters. For now we have been using @embedded to replace type converters, but they have some limitations that the old TypeConverter design did not have.

@KonstantinRr

Could you post some code about your @embedded implementations for reference, thanks!

Hcs66 avatar Oct 18 '22 03:10 Hcs66

It is basically the same as here https://isar.dev/schema.html#embedded-objects. You can build similar things as type encoders around it by storing a String in an embedded object. For example:

@embedded
class JSONBlob {
  String? data;

  JSONBlob({this.data});
  JSONBlob.fromJson(Map<String, dynamic> data) : data = jsonEncode(data);

  @Ignore()
  Map<String, dynamic>? get json => jsonDecode(data);
}

You can then store data any JSON serializable object by JSONBlob.fromJson({'test': 'hello'}) and retrieve it by calling .json.

KonstantinRr avatar Oct 18 '22 06:10 KonstantinRr

It is basically the same as here https://isar.dev/schema.html#embedded-objects. You can build similar things as type encoders around it by storing a String in an embedded object. For example:

@embedded
class JSONBlob {
  String? data;

  JSONBlob({this.data});
  JSONBlob.fromJson(Map<String, dynamic> data) : data = jsonEncode(data);

  @Ignore()
  Map<String, dynamic>? get json => jsonDecode(data);
}

You can then store data any JSON serializable object by JSONBlob.fromJson({'test': 'hello'}) and retrieve it by calling .json.

Thanks for your code~

In this way I've to rewrite all old properties. eg.UserModel to new @embeded model IsarUserModel.

Hcs66 avatar Oct 20 '22 09:10 Hcs66

Type converters have been removed on purpose. You should not store objects in Isar that are unsupported. For json you should just store it as String.

simc avatar Oct 25 '22 05:10 simc

@simc I don't understand why TypeConverters were removed. Why can't they work in parallel with the new system? For some data @embedded doesn't just work without much boilerblate. For example, I want to store a Color and had a TypeConverter for that, which simply stores the Color as String. This does not work now. To be able to store a Color now, you would have to write your own Color class, which is unnecessary overhead.

So what is the recommended approach when you want to store objects that are not in your own library?

maeddin avatar Jun 09 '23 14:06 maeddin