Angel
Search…
Basic Functionality
Before starting with the ORM, it is highly recommended to familiar one's self with package:angel_serialize, as it is the foundation for package:angel_orm:
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;
5
6
String get text;
7
8
@Column(type: ColumnType.long)
9
int get score;
10
}
Copied!
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, TodoQuery would look something like:
1
class TodoQuery extends Query<Todo, TodoQueryWhere> {
2
BooleanSqlExpressionBuilder get isComplete;
3
4
StringSqlExpressionBuilder get text;
5
6
NumericSqlExpressionBuilder<int> get score;
7
}
Copied!
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
}
5
6
Future<void> markAsComplete(Todo todo, QueryExecutor executor) async {
7
var query = TodoQuery()
8
..where.id.equals(todo.idAsInt)
9
..values.isComplete = true;
10
11
await query.updateOne(executor);
12
}
Copied!
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.
Consumers of a QueryExecutor typically inject it into the app's dependency injection container:
1
app.container.registerSingleton<QueryExecutor>(PostgresExecutor(...));
Copied!
At the time of this writing, there is only support for PostgreSQL, though more databases may be added eventually.
Last modified 2yr ago
Copy link