orm

Source-generated PostgreSQL ORM for use with the.Now you can combine the power and flexibility of Angel with a strongly-typed ORM.

Documentation for migrations can be found here:https://angel-dart.gitbook.io/angel/v/2.x/orm/migrations

Usage

You'll need these dependencies in your pubspec.yaml:

package:angel_orm_generator exports a class that you can includein a package:build flow:

  • PostgresOrmGenerator - Fueled by package:source_gen; include this within a SharedPartBuilder.

However, it also includes a build.yaml that builds ORM files automatically, so you shouldn'thave to do any configuration at all.

Models

The ORM works best when used with package:angel_serialize:

  1. library angel_orm.test.models.car;
  2. import 'package:angel_migration/angel_migration.dart';
  3. import 'package:angel_model/angel_model.dart';
  4. import 'package:angel_orm/angel_orm.dart';
  5. import 'package:angel_serialize/angel_serialize.dart';
  6. part 'car.g.dart';
  7.  
  8. @serializable
  9. @orm
  10. abstract class _Car extends Model {
  11. String get make;
  12.  
  13. String get description;
  14.  
  15. bool get familyFriendly;
  16.  
  17. DateTime get recalledAt;
  18. }
  19.  
  20. // You can disable migration generation.
  21. @Orm(generateMigrations: false)

Models can use the @SerializableField() annotation; package:angel_orm obeys it.

After building, you'll have access to a Query class with strongly-typed methods thatallow to run asynchronous queries without a headache.

Remember that if you don't need automatic id-and-date fields, you cansimply just not extend Model:

  1. @Serializable(autoIdAndDateFields: false)abstract class _ThisIsNotAnAngelModel { @primaryKey String get username;}

Example

MVC just got a whole lot easier:

Relations

supports the following relationships:

  • @HasOne() (one-to-one)
  • @HasMany() (one-to-many)
  • @BelongsTo() (one-to-one)
  • @ManyToMany() (many-to-many, using a "pivot" table)

  1. @serializable@ormabstract class _Author extends Model { @HasMany // Use the defaults, and auto-compute foreignKey List<_Book> books;

  2. // Also supports parameters… @HasMany(localKey: 'id', foreignKey: 'author_id', cascadeOnDelete: true) List<_Book> books;

  3. @SerializableField(alias: 'writing_utensil') @hasOne _Pen pen;}

The relationships will "just work" out-of-the-box, following any operation. For example,after fetching an Author from the database in the above example, the books field wouldbe populated with a set of deserialized Book objects, also fetched from the database.

Relationships use joins when possible, but in the case of @HasMany(), two queries are used:

  • One to fetch the object itself
  • One to fetch a list of related objects

A many-to-many relationship can now be modeled like so.RoleUser in this case is a pivot table joining and Role.

Note that in this case, the models must reference the private classes (_User, etc.), because the canonical versions (User, etc.) are not-yet-generated:

  1. @serializable@ormabstract class _User extends Model { String get username; String get password; String get email;

  2. @ManyToMany(_RoleUser) List<_Role> get roles;}

  3. @serializable@ormabstract class _RoleUser { @belongsTo _Role get role;

  4. @serializable@ormabstract class _Role extends Model { String name;

  5. @ManyToMany(_RoleUser) List<_User> get users;}

TLDR:

Columns

Use a @Column() annotation to change how a given field is handled within the ORM.

Using the @Column() annotation, it is possible to explicitly declare the data type of any given field:

Columns can also have an index:

    It is also possible to specify the default value of a field.Note that this only works with primitive objects.

    If a default value is supplied, the SqlMigrationBuilder will includeit in the generated schema. The PostgresOrmGenerator ignores default values;it does not need them to function properly.

    1. @serializable@ormabstract class _Foo extends Model { @Column(defaultValue: 'baz') String bar;}