SQLiteModel class abstract

SQLite persistence backend for Model.

Features:

  • Full CRUD with type-safe model instantiation
  • Automatic table creation via migrate<T>()
  • UUID + timestamp handling
  • Indexes on uuid and created_at
  • Safe mass-assignment protection
  • Query builder (where, firstWhere)

Usage:

// 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 model and schema
await SQLiteModel.migrate<User>(
  constructor: (json) => User.fromJson(json),
  columnDefinitions: {
    'name': 'TEXT NOT NULL',
    'email': 'TEXT UNIQUE NOT NULL',
  },
);

Constructors

SQLiteModel()

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

migrations Map<Type, Function>
Public access to constructor registry.
no setter

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> fromJson) Future<T?>
Creates a new record from fromJson.
delete<T extends Model>(dynamic id) Future<bool>
Deletes record by id.
deleteBy<T extends Model>({required String field, required dynamic value}) Future<bool>
Deletes first record matching condition.
exists<T extends Model>({required int id}) Future<bool>
Checks if record with id exists.
find<T extends Model>({required int id}) Future<T?>
Finds record by primary key (id).
findBy<T extends Model>({required String field, required dynamic value}) Future<T?>
Finds first record where field == value.
findByUUID<T extends Model>(String uuid) Future<T?>
Finds record by uuid.
firstWhere<T extends Model>({required String field, String comp = "==", required dynamic value}) Future<T?>
Returns first record matching where() condition.
index<T extends Model>() Future<List<T>>
Retrieves all records (ordered by id DESC).
migrate<T extends Model>({required T constructor(Map<String, dynamic>), Map<String, String>? columnDefinitions}) Future<void>
Registers a model with schema and constructor.
save<T extends Model>(T instance) Future<bool>
Saves instance (create or update).
truncate<T extends Model>() Future<bool>
Deletes all records in table.
update<T extends Model>({required dynamic id, required String field, required dynamic value}) Future<bool>
Updates a single field by id.
updateInstance<T extends Model>({required T instance, required Map<String, dynamic> withJson}) Future<bool>
Updates instance with partial data.
where<T extends Model>({required String field, String comp = "==", required dynamic value}) Future<List<T>>
Filters records with comparison.