Basic Functionality
Before starting with the ORM, it is highly recommended to familiarize oneself with package:angel3_serialize, as it is the foundation for package:angel3_orm. See Serialization.
Add the ORM, its generator, and the package of your database to your pubspec.yaml:
dependencies:
angel3_orm: ^10.0.0
angel3_orm_postgres: ^10.0.0 # or angel3_orm_mysql for MySQL and MariaDB
angel3_migration: ^10.0.0
angel3_serialize: ^10.0.0
optional: ^6.1.0
dev_dependencies:
angel3_orm_generator: ^10.0.0
build_runner: ^2.6.0To enable the ORM for a given model, simply add the @orm annotation to its definition. The name of the model ends with Entity, which the generated classes are named without:
import 'package:angel3_migration/angel3_migration.dart';
import 'package:angel3_orm/angel3_orm.dart';
import 'package:angel3_serialize/angel3_serialize.dart';
import 'package:optional/optional.dart';
part 'todo.g.dart';
@serializable
@orm
abstract class TodoEntity extends Model {
bool? get isComplete;
String? get text;
@Column(type: ColumnType.bigInt)
int? get score;
}Then run dart run build_runner build. The generator will produce a Todo model, a TodoMigration, and a TodoQuery class, which contains fields corresponding to each field declared in TodoEntity. Each of TodoQuery's fields is a subclass of SqlExpressionBuilder, corresponding to the given type. For example, TodoQuery would look something like:
Thus, you can query the database using plain-old-Dart-objects (PODO's):
get returns a List, and getOne, insert, updateOne and deleteOne return an Optional of a record, from package:optional.
The glue holding everything together is the QueryExecutor interface. To support the ORM for any arbitrary database, simply extend the class and implement its abstract methods. QueryExecutor.transaction runs queries in a transaction, which is rolled back if its callback throws:
Consumers of a QueryExecutor typically inject it into the app's dependency injection container:
The ORM supports PostgreSQL, through angel3_orm_postgres, and MySQL and MariaDB, through angel3_orm_mysql.
Last updated
Was this helpful?