Before starting with the ORM, it is highly recommended to familiar one’s self withpackage:angel_serialize, as it is the foundation for package:angel_orm:
https://github.com/angel-dart/serialize
To enable the ORM for a given model, simply add the @orm annotation to its definition:
1. @orm
2. @serializable
3. abstract class _Todo {
4. bool get isComplete;
6. String get text;
8. @Column(type: ColumnType.long)
9. int get score;
10. }
The generator will produce a TodoQuery class, which contains fields corresponding to each field declared in _Todo.Each of TodoQuery‘s fields is a subclass of SqlExpressionBuilder, corresponding to the given type. For example, TodoQuerywould look something like:
1. class TodoQuery extends Query<Todo, TodoQueryWhere> {
2. BooleanSqlExpressionBuilder get isComplete;
4. StringSqlExpressionBuilder get text;
6. NumericSqlExpressionBuilder<int> get score;
7. }
Thus, you can query the database using plain-old-Dart-objects (PODO’s):
1. Future<List<Todo>> leftToDo(QueryExecutor executor) async {
2. var query = TodoQuery()..where.isComplete.isFalse;
3. return await query.get(executor);
4. }
6. Future<void> markAsComplete(Todo todo, QueryExecutor executor) async {
7. var query = TodoQuery()
8. ..where.id.equals(todo.idAsInt)
9. ..values.isComplete = true;
11. await query.updateOne(executor);
12. }
The glue holding everything together is the QueryExecutor interface. To support the ORMfor any arbitrary database, simply extend the class and implement its abstract methods.
Consumers of a QueryExecutor typically inject it into the app’sdependency injection container:
1. app.container.registerSingleton<QueryExecutor>(PostgresExecutor(...));
At the time of this writing, there is only support for PostgreSQL, though more databases maybe added eventually.
