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 bypackage:source_gen
; include this within aSharedPartBuilder
.
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
:
- library angel_orm.test.models.car;
- import 'package:angel_migration/angel_migration.dart';
- import 'package:angel_model/angel_model.dart';
- import 'package:angel_orm/angel_orm.dart';
- import 'package:angel_serialize/angel_serialize.dart';
- part 'car.g.dart';
- @serializable
- @orm
- abstract class _Car extends Model {
- String get make;
- String get description;
- bool get familyFriendly;
- DateTime get recalledAt;
- }
- // You can disable migration generation.
- @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
:
- @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)
- @serializable@ormabstract 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;
@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:
- @serializable@ormabstract class _User extends Model { String get username; String get password; String get email;
@ManyToMany(_RoleUser) List<_Role> get roles;}
@serializable@ormabstract class _RoleUser { @belongsTo _Role get role;
@serializable@ormabstract class _Role extends Model { String name;
@ManyToMany(_RoleUser) List<_User> get users;}
TLDR:
- Make a pivot table, C, between two tables, table A and B
- C should
@belongsTo
both A and B. C should not extendModel
. - A should have a field:
@ManyToMany(_C) List<_B> get b;
- B should have a field:
@ManyToMany(_C) List<_A> get a;
Test: https://raw.githubusercontent.com/angel-dart/orm/master/angel_orm_generator/test/many_to_many_test.dart
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.
- @serializable@ormabstract class _Foo extends Model { @Column(defaultValue: 'baz') String bar;}