mmvergara / supadart

Typesafe queries in Supabase Flutter! Generate Flutter / Dart 🎯 classes from your Supabase schema.
https://supadart.vercel.app
MIT License
44 stars 5 forks source link

Immutability #10

Closed noga-dev closed 5 months ago

noga-dev commented 5 months ago

Currently this is how my Teacher class is being generated:

class Teachers {
  String id;
  DateTime created_at;
  DateTime updated_at;
  String name;
  bool is_enabled;

  Teachers({
    required this.id,
    required this.created_at,
    required this.updated_at,
    required this.name,
    required this.is_enabled,
  });

  static String get table_name => 'teachers';
  static String get c_id => 'id';
  static String get c_created_at => 'created_at';
  static String get c_updated_at => 'updated_at';
  static String get c_name => 'name';
  static String get c_is_enabled => 'is_enabled';
  static Map<String, dynamic> insert({
    String? id,
    DateTime? created_at,
    DateTime? updated_at,
    required String name,
    bool? is_enabled,
  }) {
    return {
      if (id != null) 'id': id.toString(),
      if (created_at != null) 'created_at': created_at.toUtc().toString(),
      if (updated_at != null) 'updated_at': updated_at.toUtc().toString(),
      'name': name.toString(),
      if (is_enabled != null) 'is_enabled': is_enabled.toString(),
    };
  }

  static Map<String, dynamic> update({
    String? id,
    DateTime? created_at,
    DateTime? updated_at,
    String? name,
    bool? is_enabled,
  }) {
    return {
      if (id != null) 'id': id.toString(),
      if (created_at != null) 'created_at': created_at.toUtc().toString(),
      if (updated_at != null) 'updated_at': updated_at.toUtc().toString(),
      if (name != null) 'name': name.toString(),
      if (is_enabled != null) 'is_enabled': is_enabled.toString(),
    };
  }

  factory Teachers.fromJson(Map<String, dynamic> json) {
    return Teachers(
      id: json['id'] as String,
      created_at: DateTime.parse(json['created_at'].toString()),
      updated_at: DateTime.parse(json['updated_at'].toString()),
      name: json['name'] as String,
      is_enabled: json['is_enabled'] as bool,
    );
  }
}

Whereas it should be immutable by default:

class Teachers {
  final String id;
  final DateTime created_at;
  final DateTime updated_at;
  final String name;
  final bool is_enabled;

  const Teachers({
    required this.id,
    required this.created_at,
    required this.updated_at,
    required this.name,
    required this.is_enabled,
  });

  static String get table_name => 'teachers';
  static String get c_id => 'id';
  static String get c_created_at => 'created_at';
  static String get c_updated_at => 'updated_at';
  static String get c_name => 'name';
  static String get c_is_enabled => 'is_enabled';
  static Map<String, dynamic> insert({
    String? id,
    DateTime? created_at,
    DateTime? updated_at,
    required String name,
    bool? is_enabled,
  }) {
    return {
      if (id != null) 'id': id.toString(),
      if (created_at != null) 'created_at': created_at.toUtc().toString(),
      if (updated_at != null) 'updated_at': updated_at.toUtc().toString(),
      'name': name.toString(),
      if (is_enabled != null) 'is_enabled': is_enabled.toString(),
    };
  }

  static Map<String, dynamic> update({
    String? id,
    DateTime? created_at,
    DateTime? updated_at,
    String? name,
    bool? is_enabled,
  }) {
    return {
      if (id != null) 'id': id.toString(),
      if (created_at != null) 'created_at': created_at.toUtc().toString(),
      if (updated_at != null) 'updated_at': updated_at.toUtc().toString(),
      if (name != null) 'name': name.toString(),
      if (is_enabled != null) 'is_enabled': is_enabled.toString(),
    };
  }

  factory Teachers.fromJson(Map<String, dynamic> json) {
    return Teachers(
      id: json['id'] as String,
      created_at: DateTime.parse(json['created_at'].toString()),
      updated_at: DateTime.parse(json['updated_at'].toString()),
      name: json['name'] as String,
      is_enabled: json['is_enabled'] as bool,
    );
  }
}
mmvergara commented 5 months ago

Would love to have your feedback on this

noga-dev commented 5 months ago

lgtm 💪