Model
Create
dox create:model Blog # only the model
dox create:model Blog -m # the model and its migration (table `blogs`)
Info
This will create a blog model inside lib/models folder.
Warning
Please run dart run build_runner build after new model is created. Alternatively can also run build_runner watch dart run build_runner watch to update changes to generator file
import 'package:dox_orm/dox_orm.dart';
part 'blog.model.g.dart';
@DoxModel()
class Blog extends BlogGenerator {
@Column()
String? title;
@Column(name: 'title', beforeSave: makeSlug)
String? slug;
@Column()
String? status;
@Column(name: 'body')
String? description;
@Column(name: 'created_at')
DateTime? createdAt;
@Column(name: 'updated_at')
DateTime? updatedAt;
static makeSlug(Map<String, dynamic> map) {
return Slugify().slugify(map['title']);
}
}
Model options
table
Info
Dox adheres to singular table naming conventions, but you can still employ custom table names in your model.
primaryKey
createdAt / updatedAt
Info
Dox typically utilizes the columns named created_at and updated_at as timestamp columns by default. However, if you wish to specify custom column names, you can employ the createdAt and updatedAt options.
softDelete
Table column
Name
Hooks
Warning
beforeSave and beforeGet hook must be static function inside Model class.
Save
Info
On insert, null fields are not sent, so the database default is used.
On update, a field is set to NULL only when it had a value before
(loaded from the database or saved). Fields that were not loaded,
for example with select(), are not changed.
Delete
Info
save(), delete(), forceDelete() and restore() fire model events.
To map
Debug
Info
Debug true option will print the SQL query in the console/terminal.
New query
Info
If you do not want to create new class and reuse existing class to do new query, use can use query() function.
Hide column
Computed values
A computed value is a getter that is added to toJson() and toMap().
It is like $appends with an accessor in Laravel.
Add @Computed() to a getter in the model, then run dart run build_runner build.
@DoxModel()
class User extends UserGenerator {
@Column(name: 'first_name')
String? firstName;
@Column(name: 'last_name')
String? lastName;
@Computed() // json key: full_name
String get fullName => '$firstName $lastName';
@Computed(name: 'initials') // custom json key
String get shortName => '${firstName?[0]}${lastName?[0]}';
}
User? user = await User().find(1);
print(user?.toJson());
// {id: 1, first_name: John, last_name: Wick, ..., full_name: John Wick, initials: JW}
- The default key is the getter name in snake_case (
fullNamebecomesfull_name). - A computed value is never a column. It is not read from the database, and it is not sent in
INSERTorUPDATE. hiddenalso hides computed values. Use the json key, for example['full_name'].- Computed values of loaded relations are in the nested json too.
toMap(original: true)returns the raw database row, so it has no computed values.
Performance
The code is made by build_runner. There is no reflection.
A model without @Computed() does no extra work in toJson().
A getter runs each time toJson() or toMap() is called, so keep it fast.
Casts
Dox has no $casts list. The field type does the cast:
DateTime? is parsed from the database, and a JSON column can be a
Map<String, dynamic>? or List<dynamic>? field.
For other conversions, use the beforeSave and beforeGet hooks of @Column.