🏗️ PHORM Generator
The magic behind PHORM. This package uses build_runner to turn your Dart models into optimized SQL schemas and type-safe CRUD mixins.
What it does
- SQL Generation: Creates
CREATE TABLE,INDEX, andFOREIGN KEYstatements automatically. - Model Mixins: Generates
_$UserMixinwith automatictoJson,toString,copyWith, and relationship getters. - Pluralized Services: Generates the
Usersservice class for fluent, static-method-based interaction. - JSON Helpers: Provides optimized
_$UserFromJsonimplementations. - Runtime Metadata: Produces the
Tableconfiguration needed forPHORM.
Installation
Add these to your dev_dependencies:
dev_dependencies:
phorm_generator: ^latest
build_runner: ^latest
Usage
1. Annotate your class
@Schema(tableName: 'users')
class User extends Model with _$PhormUserMixin {
@ID()
final String id;
@Column()
final String name;
User({required this.id, required this.name});
factory User.fromJson(Map<String, dynamic> json) => _$PhormUserFromJson(json);
}
2. Run the build
# One-time build
dart run build_runner build
# Watch for changes
dart run build_runner watch --delete-conflicting-outputs
3. Control what gets generated
By default the generator emits a pluralized service class (e.g. Users) that
exposes the full CRUD/query API (insert, readAll, where, watchAll,
column constants, …). For large schemas, or when you only need the lightweight
artefacts, set generateFullService: false on the @Schema annotation:
@Schema(tableName: 'users', generateFullService: false)
class User extends Model with _$PhormUserMixin {
// ...
}
With generateFullService: false, only the schema, table metadata,
fromJson/toJson, and copyWith are generated — the large service class is
skipped. Defaults to true.
Learn More
- For annotation details, see phorm_annotations.
- For runtime query engine details, see phorm.
- For the connection manager and SQLite driver implementation, see phorm_sqlite.
License
MIT License