JAO Generator
Code generator for JAO ORM. Generates typed field accessors, model metadata, and serialization methods.
Installation
dependencies:
jao: ^0.2.0
dev_dependencies:
build_runner: ^2.4.0
jao_generator: ^0.2.0
Usage
1. Define a Model
import 'package:jao/jao.dart';
part 'user.g.dart';
@Model()
class User {
@AutoField()
late int id;
@CharField(maxLength: 100)
late String name;
@EmailField()
late String email;
@DateTimeField(autoNowAdd: true)
late DateTime createdAt;
}
2. Run the Generator
dart run build_runner build
Or watch for changes:
dart run build_runner watch
Generated Code
For a User model, the generator creates:
Field Class (User$)
class User$ implements ModelFields<User> {
const User$();
final id = const IntFieldRef('id');
final name = const StringFieldRef('name');
final email = const StringFieldRef('email');
final createdAt = const DateTimeFieldRef('created_at');
}
Companion Class (Users)
class Users {
static const $ = User$();
static Manager<User> get objects => ...;
static const tableName = 'user';
static const pkField = 'id';
static User fromRow(Map<String, dynamic> row) => ...;
static Map<String, dynamic> toRow(User model) => ...;
static final schema = ModelSchema(...);
}
Build Optimization
For large projects, limit which files are scanned by adding a build.yaml:
targets:
$default:
builders:
jao_generator|jao:
generate_for:
include:
- lib/models/**
Model Options
Custom Table Name
@Model(tableName: 'app_users')
class User {
// ...
}
Auto Timestamps
@Model()
class Post {
@AutoField()
late int id;
@DateTimeField(autoNowAdd: true) // Set on create
late DateTime createdAt;
@DateTimeField(autoNow: true) // Set on every update
late DateTime updatedAt;
}
Supported Field Types
| Annotation | Generated FieldRef |
|---|---|
@AutoField() |
IntFieldRef |
@BigAutoField() |
IntFieldRef |
@CharField() |
StringFieldRef |
@TextField() |
StringFieldRef |
@IntegerField() |
IntFieldRef |
@BooleanField() |
BoolFieldRef |
@FloatField() |
DoubleFieldRef |
@DateTimeField() |
DateTimeFieldRef |
@DurationField() |
DurationFieldRef |
Troubleshooting
Generated file not found
Run the generator:
dart run build_runner build --delete-conflicting-outputs
Slow builds
Add generate_for filter in build.yaml to only scan model directories.
Conflicting outputs
dart run build_runner clean
dart run build_runner build
Related Packages
License
MIT License - see LICENSE for details.