Closed medz closed 2 months ago
toRecord? fromRecord? Maybe it's better to use a record instead of a class? it's much optimized, why do we need a class as a storage?
@listepo This request comes from a discussion where he hopes that Model can add toJson
functionality in addition to the fromJson constructor. It can indeed greatly reduce the writing of boilerplate code.
toRecord? fromRecord? Maybe it's better to use a record instead of a class? it's much optimized, why do we need a class as a storage?
In fact, I don’t really want to abuse Records
. Yes, when we write our own code, record can make us much more convenient. But there is an inevitable need for Union-Types in ORM. Our switch to Records came at a huge cost, especially since the project had so few contributors.
@kidusdev https://pub.dev/packages/orm/versions/4.1.0-beta.1
I released version 4.1.0-beta.1 to initially support this feature request.
Looking forward to your feedback.
@medz Having issues while using the JSON data type, and toJson
is failing when auto-generating the models. I mean in this beta version. I also needed the toJson
support.
@medz Also, please encode dateTime like this: dateTime.toIso8601String(). Otherwise, that could also become problematic.
@Suhailakl Do you mean to generate standard JSON data instead of Dart map objects and original Dart data types?
I will continue to collect potential errors of toJson, which is currently the basic support. Perhaps there are many more complicated situations that lead to its failure. Please add your generated client code failure due to this function under this issue. Thank you, everyone🙏
@kidusdev https://pub.dev/packages/orm/versions/4.1.0-beta.1
I released version 4.1.0-beta.1 to initially support this feature request.
Looking forward to your feedback.
👏 @medz ... its generating models with out any errors.
@kidusdev Did you try with DateTime field?
DateTime format issue has been resolved by using this function. @medz Thanks for the quick support.
List<Map<String, dynamic>> encodeData(List<Map<String, dynamic>> dataList) { final encodedList = <Map<String, dynamic>>[]; for (final item in dataList) { final encodedItem = <String, dynamic>{}; item.forEach((key, value) { if (value is DateTime) { encodedItem[key] = value.toIso8601String(); } else { encodedItem[key] = value; } }); encodedList.add(encodedItem); } return encodedList; }
Further testing is needed, I'm a bit busy these days. I will take the time to merge it as soon as possible
Discussed in https://github.com/medz/prisma-dart/discussions/347