2017-06-17 16:45:31 +00:00
|
|
|
# orm
|
|
|
|
[![Pub](https://img.shields.io/pub/v/angel_orm.svg)](https://pub.dartlang.org/packages/angel_orm)
|
|
|
|
[![build status](https://travis-ci.org/angel-dart/orm.svg)](https://travis-ci.org/angel-dart/orm)
|
|
|
|
|
2017-07-09 16:53:35 +00:00
|
|
|
Source-generated PostgreSQL ORM for use with the
|
|
|
|
[Angel framework](https://angel-dart.github.io).
|
|
|
|
Now you can combine the power and flexibility of Angel with a strongly-typed ORM.
|
2017-06-17 16:45:31 +00:00
|
|
|
|
2017-07-10 21:49:00 +00:00
|
|
|
* [Usage](#usage)
|
|
|
|
* [Model Definitions](#models)
|
|
|
|
* [MVC Example](#example)
|
|
|
|
* [Relationships](#relations)
|
|
|
|
|
|
|
|
# Usage
|
|
|
|
You'll need these dependencies in your `pubspec.yaml`:
|
|
|
|
```yaml
|
|
|
|
dependencies:
|
|
|
|
angel_orm: ^1.0.0-alpha
|
|
|
|
dev_dependencies:
|
|
|
|
angel_orm_generator: ^1.0.0-alpha
|
|
|
|
build_runner: ^0.3.0
|
|
|
|
```
|
|
|
|
|
|
|
|
`package:angel_orm_generator` exports two classes that you can include
|
|
|
|
in a `package:build` flow:
|
|
|
|
* `PostgreORMGenerator` - Fueled by `package:source_gen`; include this within a `GeneratorBuilder`.
|
|
|
|
* `SQLMigrationGenerator` - This is its own `Builder`; it generates a SQL schema, as well as a SQL script to drop a generated table.
|
|
|
|
|
|
|
|
You should pass an `InputSet` containing your project's models.
|
2017-06-17 16:53:32 +00:00
|
|
|
|
2017-07-09 16:53:35 +00:00
|
|
|
# Models
|
2017-06-17 16:51:01 +00:00
|
|
|
Your model, courtesy of `package:angel_serialize`:
|
|
|
|
|
2017-06-17 16:45:31 +00:00
|
|
|
```dart
|
|
|
|
library angel_orm.test.models.car;
|
|
|
|
|
|
|
|
import 'package:angel_framework/common.dart';
|
2017-06-18 04:19:05 +00:00
|
|
|
import 'package:angel_orm/angel_orm.dart';
|
2017-06-17 16:45:31 +00:00
|
|
|
import 'package:angel_serialize/angel_serialize.dart';
|
|
|
|
part 'car.g.dart';
|
|
|
|
|
|
|
|
@serializable
|
2017-06-18 04:19:05 +00:00
|
|
|
@orm
|
2017-06-17 16:45:31 +00:00
|
|
|
class _Car extends Model {
|
2017-06-18 04:19:05 +00:00
|
|
|
String make;
|
|
|
|
String description;
|
|
|
|
bool familyFriendly;
|
|
|
|
DateTime recalledAt;
|
2017-06-17 16:45:31 +00:00
|
|
|
}
|
|
|
|
```
|
|
|
|
|
2017-07-09 16:53:35 +00:00
|
|
|
Models can use the `@Alias()` annotation; `package:angel_orm` obeys it.
|
2017-06-18 04:19:05 +00:00
|
|
|
|
|
|
|
After building, you'll have access to a `Query` class with strongly-typed methods that
|
2017-06-17 16:45:31 +00:00
|
|
|
allow to run asynchronous queries without a headache.
|
2017-07-09 16:53:35 +00:00
|
|
|
|
2017-07-10 21:49:00 +00:00
|
|
|
**IMPORTANT:** The ORM *assumes* that you are using `package:angel_serialize`, and will only generate code
|
|
|
|
designed for such a workflow. Save yourself a headache and build models with `angel_serialize`:
|
|
|
|
|
|
|
|
https://github.com/angel-dart/serialize
|
|
|
|
|
|
|
|
# Example
|
|
|
|
|
2017-07-09 16:53:35 +00:00
|
|
|
MVC just got a whole lot easier:
|
2017-06-17 16:45:31 +00:00
|
|
|
|
|
|
|
```dart
|
|
|
|
import 'package:angel_framework/angel_framework.dart';
|
|
|
|
import 'package:postgres/postgres.dart';
|
|
|
|
import 'car.dart';
|
|
|
|
import 'car.orm.g.dart';
|
|
|
|
|
|
|
|
/// Returns an Angel plug-in that connects to a PostgreSQL database, and sets up a controller connected to it...
|
2017-06-17 16:48:18 +00:00
|
|
|
AngelConfigurer connectToCarsTable(PostgreSQLConnection connection) {
|
2017-06-17 16:45:31 +00:00
|
|
|
return (Angel app) async {
|
2017-06-18 04:19:05 +00:00
|
|
|
// Register the connection with Angel's dependency injection system.
|
2017-06-17 16:45:31 +00:00
|
|
|
//
|
|
|
|
// This means that we can use it as a parameter in routes and controllers.
|
2017-06-18 04:19:05 +00:00
|
|
|
app.container.singleton(connection);
|
2017-06-17 16:45:31 +00:00
|
|
|
|
|
|
|
// Attach the controller we create below
|
2017-06-18 04:19:05 +00:00
|
|
|
await app.configure(new CarService(connection));
|
2017-06-17 16:45:31 +00:00
|
|
|
};
|
|
|
|
}
|
|
|
|
|
|
|
|
@Expose('/cars')
|
|
|
|
class CarService extends Controller {
|
2017-06-18 04:19:05 +00:00
|
|
|
// The `connection` will be injected.
|
|
|
|
@Expose('/recalled_since_2008')
|
|
|
|
carsRecalledSince2008(PostgreSQLConnection connection) {
|
|
|
|
// Instantiate a Car query, which is auto-generated. This class helps us build fluent queries easily.
|
|
|
|
var cars = new CarQuery(connection);
|
|
|
|
cars.where
|
|
|
|
..familyFriendly.equals(false)
|
|
|
|
..recalledAt.year.greaterThanOrEqualTo(2008);
|
|
|
|
|
|
|
|
// Shorter syntax we could use instead...
|
|
|
|
cars.where.recalledAt.year <= 2008;
|
|
|
|
|
|
|
|
// `get()` returns a Stream.
|
|
|
|
// `get().toList()` returns a Future.
|
|
|
|
return cars.get().toList();
|
2017-06-17 16:45:31 +00:00
|
|
|
}
|
2017-06-18 04:19:05 +00:00
|
|
|
|
|
|
|
@Expose('/create', method: 'POST')
|
|
|
|
createCar(PostgreSQLConnection connection) async {
|
|
|
|
// `package:angel_orm` generates a strongly-typed `insert` function on the query class.
|
|
|
|
// Say goodbye to typos!!!
|
|
|
|
var car = await CarQuery.insert(connection, familyFriendly: true, make: 'Honda');
|
|
|
|
|
|
|
|
// Auto-serialized using code generated by `package:angel_serialize`
|
|
|
|
return car;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
```
|
|
|
|
|
|
|
|
# Relations
|
2017-07-10 21:49:00 +00:00
|
|
|
**NOTE**: This is not yet implemented. Expect to see more documentation about this soon.
|
2017-06-18 04:19:05 +00:00
|
|
|
|
|
|
|
* `@HasOne()`
|
|
|
|
* `@HasMany()`
|
|
|
|
* `@BelongsTo()`
|
|
|
|
|
|
|
|
```dart
|
|
|
|
@serializable
|
|
|
|
@orm
|
|
|
|
abstract class _Author extends Model {
|
|
|
|
@hasMany // Use the defaults, and auto-compute `foreignKey`
|
|
|
|
List<Book> books;
|
|
|
|
|
|
|
|
// Also supports parameters...
|
|
|
|
@HasMany(localKey: 'id', foreignKey: 'author_id', cascadeOnDelete: true)
|
|
|
|
List<Book> books;
|
|
|
|
|
|
|
|
@Alias('writing_utensil')
|
|
|
|
@hasOne
|
|
|
|
Pen pen;
|
2017-06-17 16:45:31 +00:00
|
|
|
}
|
|
|
|
```
|