Complete Example
Here is a comprehensive example of a model definition using various annotations and options.
import 'package:sequelize_orm/sequelize_orm.dart';
import 'package:sequelize_orm_annotations/sequelize_orm_annotations.dart'; // Optional if exported by main package
part 'product.model.g.dart';
(
tableName: 'products',
underscored: true,
timestamps: true,
)
class Product {
// Primary Key with Auto Increment
()
()
DataType id = DataType.INTEGER;
// String column with explicit name and not null constraint
('product_name')
()
.Len(2, 100)
DataType name = DataType.STRING;
// Text column allowing nulls
()
DataType description = DataType.TEXT;
// Decimal with precision, default value, and numeric validation
(0.0)
.Min(0)
DataType price = DataType.DECIMAL(10, 2);
// Enum-like string with specific allowed values
('draft')
.IsIn(['draft', 'published', 'archived'])
DataType status = DataType.STRING;
// Boolean with default value
(true)
DataType inStock = DataType.BOOLEAN;
// UUID for tracking, auto-generated
.uniqid()
DataType sku = DataType.UUID;
// Virtual helper getter (not a DB column)
bool get isPublished => status == 'published';
// Sequelize instance getter
static ProductModel get model => ProductModel();
}
Generating the Code
Remember to run the build runner to generate the part file:
dart run build_runner build --delete-conflicting-outputs