medz / prisma-dart

Prisma Client Dart is an auto-generated type-safe ORM. It uses Prisma Engine as the data access layer and is as consistent as possible with the Prisma Client JS/TS APIs.
https://prisma.pub
BSD 3-Clause "New" or "Revised" License
436 stars 29 forks source link

Enhancing the model with information #348

Closed medz closed 2 months ago

medz commented 3 months ago

Discussed in https://github.com/medz/prisma-dart/discussions/347

Originally posted by **kidusdev** March 20, 2024 Hey medz, i saw the model generation its nice but it lacks the info about the model ``` class Users { const Users ({ this.id, this.email, this.name, this.createdAt, this.updatedAt, }); factory Users .fromJson(Map json) => Users( id: json['id'], email: json['email'], name: json['name'], createdAt: json['created_at'], updatedAt: json['updated_at'], ); final int? id; final String? email; final String? name; final DateTime? createdAt; final DateTime? updatedAt; // needed to be added Map toJson(){ return { "id": id, "email": email, "name": name, "createdAt":createdAt, "updatedAt":updatedAt, }; } @override String toString() => """Users ( id: $id, email: $email, name: $name, createdAt: $createdAt, updatedAt: $updatedAt, )"""; // this is use full when printed to the terminal instead of printing Instance of 'Users' } ```
listepo commented 3 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?

medz commented 3 months ago

@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.

medz commented 3 months ago

@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.

Suhailakl commented 3 months ago

@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.

Suhailakl commented 3 months ago

@medz Also, please encode dateTime like this: dateTime.toIso8601String(). Otherwise, that could also become problematic.

medz commented 3 months ago

@Suhailakl Do you mean to generate standard JSON data instead of Dart map objects and original Dart data types?

medz commented 3 months ago

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 commented 3 months ago

@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.

Suhailakl commented 3 months ago

@kidusdev Did you try with DateTime field?

Suhailakl commented 3 months ago

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; }

medz commented 3 months ago

Further testing is needed, I'm a bit busy these days. I will take the time to merge it as soon as possible