Model class abstract

Base class for database-backed models in Kronix.

Models provide a type-safe bridge between Dart objects and database rows. Every model must define its tableName and implement toMap for persistence.

class User extends Model {
  @override String get tableName => 'users';
  
  String name;
  String email;
  
  User({super.id, required this.name, required this.email, super.createdAt, super.updatedAt});
  
  factory User.fromRow(Map<String, dynamic> row) => User(
    id: row['id'],
    name: row['name'],
    email: row['email'],
    createdAt: row['created_at'],
    updatedAt: row['updated_at'],
  );
  
  @override
  Map<String, dynamic> toMap() => {'name': name, 'email': email};
}

Constructors

Model({int? id, DateTime? createdAt, DateTime? updatedAt})
Creates a new Model instance.

Properties

createdAt DateTime?
Timestamp of when this record was created.
getter/setter pair
db DatabaseExecutor
Returns the database executor, throwing an error if not set.
no setter
exists bool
Whether this model has been persisted to the database.
no setter
hashCode int
The hash code for this object.
no setteroverride
id int?
The primary key value. Null for unsaved models.
getter/setter pair
primaryKeyColumn String
The primary key column name. Override if not id.
no setter
runtimeType Type
A representation of the runtime type of the object.
no setterinherited
tableName String
The database table name for this model.
no setter
timestamps bool
Whether to automatically manage created_at/updated_at columns.
no setter
updatedAt DateTime?
Timestamp of when this record was last updated.
getter/setter pair

Methods

belongsTo<T extends Model>(ModelFactory<T> factory, {String? foreignKey, String? ownerKey}) Future<T?>
Defines a "Belongs To" relationship.
getAttribute(String key) → dynamic
Retrieves a raw attribute value by key.
hasMany<T extends Model>(ModelFactory<T> factory, {String? foreignKey, String? localKey}) Future<List<T>>
Defines a "Has Many" relationship.
hasOne<T extends Model>(ModelFactory<T> factory, {String? foreignKey, String? localKey}) Future<T?>
Defines a "Has One" relationship.
noSuchMethod(Invocation invocation) → dynamic
Invoked when a nonexistent method or property is accessed.
inherited
setRawData(DatabaseExecutor executor, Map<String, dynamic> raw) → void
Sets the database executor and raw attributes for this model instance.
toJson() Map<String, dynamic>
Converts this model to a full JSON-compatible map, including metadata.
toMap() Map<String, dynamic>
Converts this model's fields to a database-compatible map.
toString() String
A string representation of this object.
override

Operators

operator ==(Object other) bool
The equality operator.
override