update<T extends Model> static method

Future<bool> update<T extends Model>({
  1. required dynamic id,
  2. required String field,
  3. required dynamic value,
})

Updates a single field by UUID.

Implementation

static Future<bool> update<T extends Model>({
  required dynamic id,
  required String field,
  required dynamic value,
}) async {
  final file = File("$_dir/${_getTableName<T>()}.json");
  if (!await file.exists()) return false;

  try {
    final allRecords = await _loadJsonFileRecords(file);
    final index = allRecords.indexWhere((r) => r['uuid'] == id);
    if (index < 0) return false;

    allRecords[index][field] = value;
    allRecords[index]['updated_at'] = DateTime.now()
        .toUtc()
        .toIso8601String();

    await file.writeAsString(jsonEncode(allRecords), flush: true);
    return true;
  } catch (e) {
    return false;
  }
}