JsonFileModel class abstract
JSON file-based persistence backend for Model.
Features:
- Zero-config: one JSON file per model (
users.json,posts.json) - Automatic UUID + timestamp handling
- Constructor registration via
migrate<T>() - Full CRUD with filtering (
where,firstWhere) - Atomic file writes
- Type-safe model instantiation
File Structure:
lib/src/storage/json_file_models/
users.json
posts.json
...
Example:
// Define model
class User extends Model {
String name;
String email;
User({this.name = '', this.email = ''});
@override
Map<String, dynamic> toJson() => {'name': name, 'email': email};
@override
Map<String, dynamic> toMetaJson() => {
'id': id,
'uuid': uuid,
'created_at': createdAt?.toIso8601String(),
'updated_at': updatedAt?.toIso8601String(),
};
User.fromJson(Map<String, dynamic> json)
: name = json['name'],
email = json['email'],
super.fromJson(json);
}
// Register constructor
JsonFileModel.migrate<User>((json) => User.fromJson(json));
// Use
final user = await JsonFileModel.create<User>({'name': 'John'});
final all = await JsonFileModel.index<User>();
Constructors
Properties
- hashCode → int
-
The hash code for this object.
no setterinherited
- runtimeType → Type
-
A representation of the runtime type of the object.
no setterinherited
Methods
-
noSuchMethod(
Invocation invocation) → dynamic -
Invoked when a nonexistent method or property is accessed.
inherited
-
toString(
) → String -
A string representation of this object.
inherited
Operators
-
operator ==(
Object other) → bool -
The equality operator.
inherited
Static Properties
Static Methods
-
all<
T extends Model> () → Future< List< T> > -
Alias for
index<T>(). -
count<
T extends Model> () → Future< int> - Counts total records.
-
create<
T extends Model> (Map< String, dynamic> json) → Future<T?> -
Creates a new record from
json. -
delete<
T extends Model> ({required String uuid}) → Future< bool> - Deletes record by UUID.
-
deleteInstance<
T extends Model> ({required T instance}) → Future< bool> -
Deletes
instanceby UUID. -
exists<
T extends Model> ({required dynamic id}) → Future< bool> -
Checks if record with
id(UUID) exists. -
find<
T extends Model> ({required String uuid}) → Future< T?> - Finds record by UUID.
-
findBy<
T extends Model> ({required String field, required dynamic value}) → Future< T?> -
Finds first record where
field==value. -
firstWhere<
T extends Model> ({required String field, String comp = "==", dynamic value}) → Future< T?> -
Returns first record matching
where()condition. -
index<
T extends Model> () → Future< List< T> > -
Retrieves all records of type
T. -
jsonFind<
T extends Model> (String uuid) → Future< Map< String, dynamic> ?> - Returns raw JSON record by UUID.
-
jsonIndex<
T extends Model> () → Future< List< Map< >String, dynamic> > - Returns raw JSON records (no model instantiation).
-
migrate<
T extends Model> ({required T constructor(Map< String, dynamic> )}) → Future<void> -
Registers a constructor for type
T. -
save<
T extends Model> (T instance) → Future< bool> -
Saves
instance(create if new, update if exists). -
saveInstance<
T extends Model> ({required T instance}) → Future< bool> -
Saves
instance. -
truncate<
T extends Model> () → Future< bool> - Empties the table file.
-
update<
T extends Model> ({required dynamic id, required String field, required dynamic value}) → Future< bool> - Updates a single field by UUID.
-
updateInstance<
T extends Model> ({required T instance, required Map< String, dynamic> withJson}) → Future<bool> -
Updates instance with partial
withJson. -
where<
T extends Model> ({required String field, String comp = "==", dynamic value}) → Future< List< T> > - Filters records with comparison operators.