Convert regular Dart classes into PHORM models with a single click.
Usage
- Right-click a
.dartfile in the Explorer → To PHORM Model - Or right-click inside the editor → To PHORM Model
- Or click the ⚡ To PHORM Model CodeLens shown above any plain class to convert just that class.
Conversions are applied as a normal, undoable edit (Ctrl/Cmd+Z works) and respect unsaved changes in the buffer.
What it does
Before:
class User {
final String name;
final String email;
User({required this.name, required this.email});
}
After:
import 'package:phorm/phorm.dart';
part 'user.sql.g.dart';
@Schema(tableName: 'users')
class User extends Model with _$PhormUserMixin {
@ID(autoIncrement: true)
final int id;
@Column()
final String name;
@Column()
final String email;
User({
required this.id,
required this.name,
required this.email,
});
factory User.fromJson(Map<String, dynamic> json) =>
_$PhormUserFromJson(json);
}
Behavior
-
Converts all classes in the file automatically
-
If an
idfield is missing, adds:@ID(autoIncrement: true) final int id; -
If an
idfield already exists and its type isString, adds:@ID(autoIncrement: false, unique: true) -
Existing annotations such as
@Columnand@IDare preserved -
Existing methods, getters and a custom
fromJsonare preserved (the converter only injects annotations, theidfield, the constructor and afromJsonfactory when one is missing) -
Top-level code outside the class (functions, constants, extensions) is left untouched
-
Already converted classes (
extends Model) are skipped
Settings
| Setting | Default | Effect |
|---|---|---|
phorm.generateFullService |
true |
When false, emits @Schema(generateFullService: false). |
phorm.timestamps |
true |
When false, emits @Schema(timestamps: false). |
phorm.paranoid |
false |
When true, emits @Schema(paranoid: true) (soft deletes). |
phorm.addFromJson |
true |
Generate a fromJson factory (skipped if one already exists). |
phorm.enableCodeLens |
true |
Show the "To PHORM Model" CodeLens above plain classes. |
Snippets
phormmodel, phormschema, phormcolumn, phormid scaffold common PHORM
declarations.
After Conversion
Run code generation via the command palette Phorm: Run build_runner build, or:
dart run build_runner build --delete-conflicting-outputs