2024-10-12 10:35:14 +00:00
# Protevus ORM
2021-08-02 12:38:47 +00:00
2021-12-20 04:25:43 +00:00
![Pub Version (including pre-releases) ](https://img.shields.io/pub/v/angel3_orm?include_prereleases )
2021-06-18 10:17:13 +00:00
[![Null Safety ](https://img.shields.io/badge/null-safety-brightgreen )](https://dart.dev/null-safety)
2024-07-07 15:02:49 +00:00
[![Discord ](https://img.shields.io/discord/1060322353214660698 )](https://discord.gg/3X6bxTUdCM)
2024-10-12 10:35:14 +00:00
[![License ](https://img.shields.io/github/license/dart-backend/protevus )](https://github.com/dart-backend/protevus/tree/master/packages/orm/LICENSE)
2021-06-18 10:17:13 +00:00
2024-10-12 10:35:14 +00:00
Source-generated ORM for use with the [Protevus framework ](https://github.com/dart-backend/protevus ). Now you can combine the power and flexibility of Protevus with a strongly-typed ORM.
2017-06-17 16:45:31 +00:00
2019-04-04 18:50:03 +00:00
Documentation for migrations can be found here:
2021-06-18 10:17:13 +00:00
[ORM Migration ](https://angel3-docs.dukefirehawk.com/guides/orm/migrations )
2019-04-04 18:50:03 +00:00
2024-10-12 10:35:14 +00:00
- [Protevus ORM ](#angel3-orm )
2021-08-02 12:38:47 +00:00
- [Usage ](#usage )
- [Models ](#models )
- [Example ](#example )
- [Relations ](#relations )
- [Many to Many Relations ](#many-to-many-relations )
- [Columns ](#columns )
- [Column Types ](#column-types )
- [Indices ](#indices )
- [Default Values ](#default-values )
## Usage
2017-07-10 21:49:00 +00:00
You'll need these dependencies in your `pubspec.yaml` :
2021-08-02 12:38:47 +00:00
2017-07-10 21:49:00 +00:00
```yaml
dependencies:
2022-04-23 04:21:39 +00:00
angel3_orm: ^6.0.0
2017-07-10 21:49:00 +00:00
dev_dependencies:
2022-04-23 04:21:39 +00:00
angel3_orm_generator: ^6.0.0
2021-06-18 10:17:13 +00:00
build_runner: ^2.0.0
2017-07-10 21:49:00 +00:00
```
2021-06-18 10:17:13 +00:00
`package:angel3_orm_generator` exports a class that you can include in a `package:build` flow:
2021-08-02 12:38:47 +00:00
- `PostgresOrmGenerator` - Fueled by `package:source_gen` ; include this within a `SharedPartBuilder` .
2017-07-10 21:49:00 +00:00
2018-12-08 23:30:28 +00:00
However, it also includes a `build.yaml` that builds ORM files automatically, so you shouldn't
have to do any configuration at all.
2017-06-17 16:53:32 +00:00
2021-08-02 12:38:47 +00:00
## Models
2021-06-18 10:17:13 +00:00
The ORM works best when used with `package:angel3_serialize` :
2017-06-17 16:51:01 +00:00
2017-06-17 16:45:31 +00:00
```dart
library angel_orm.test.models.car;
2021-06-18 10:17:13 +00:00
import 'package:angel3_migration/angel3_migration.dart';
import 'package:angel3_model/angel3_model.dart';
import 'package:angel3_orm/angel3_orm.dart';
import 'package:angel3_serialize/angel3_serialize.dart';
2017-06-17 16:45:31 +00:00
part 'car.g.dart';
@serializable
2017-06-18 04:19:05 +00:00
@orm
2018-12-09 17:44:16 +00:00
abstract class _Car extends Model {
String get make;
String get description;
bool get familyFriendly;
DateTime get recalledAt;
2017-06-17 16:45:31 +00:00
}
2018-12-09 17:44:16 +00:00
// You can disable migration generation.
@Orm (generateMigrations: false)
abstract class _NoMigrations extends Model {}
2017-06-17 16:45:31 +00:00
```
2021-06-18 10:17:13 +00:00
Models can use the `@SerializableField()` annotation; `package:angel3_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
2019-04-03 09:59:23 +00:00
Remember that if you don't need automatic id-and-date fields, you can
simply just not extend `Model` :
2018-12-08 23:30:28 +00:00
```dart
2021-08-02 12:38:47 +00:00
@Serializable
2018-12-08 23:30:28 +00:00
abstract class _ThisIsNotAnAngelModel {
2019-04-03 09:59:23 +00:00
@primaryKey
String get username;
2018-12-08 23:30:28 +00:00
}
```
2021-08-02 12:38:47 +00:00
## Example
2017-07-10 21:49:00 +00:00
2017-07-09 16:53:35 +00:00
MVC just got a whole lot easier:
2017-06-17 16:45:31 +00:00
```dart
2021-06-18 10:17:13 +00:00
import 'package:angel3_framework/angel3_framework.dart';
import 'package:angel3_orm/angel3_orm.dart';
2017-06-17 16:45:31 +00:00
import 'car.dart';
import 'car.orm.g.dart';
2018-12-08 23:30:28 +00:00
/// Returns an Angel plug-in that connects to a database, and sets up a controller connected to it...
AngelConfigurer connectToCarsTable(QueryExecutor executor) {
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.
2018-12-08 23:30:28 +00:00
app.container.registerSingleton(executor);
2017-06-17 16:45:31 +00:00
// Attach the controller we create below
2018-12-08 23:30:28 +00:00
await app.mountController< CarController > ();
2017-06-17 16:45:31 +00:00
};
}
@Expose ('/cars')
2017-07-15 15:20:47 +00:00
class CarController extends Controller {
2018-12-08 23:30:28 +00:00
// The `executor` will be injected.
2017-06-18 04:19:05 +00:00
@Expose ('/recalled_since_2008')
2018-12-08 23:30:28 +00:00
carsRecalledSince2008(QueryExecutor executor) {
2017-06-18 04:19:05 +00:00
// Instantiate a Car query, which is auto-generated. This class helps us build fluent queries easily.
2021-06-18 10:17:13 +00:00
var query = CarQuery();
2019-07-13 11:20:59 +00:00
query.where
2017-06-18 04:19:05 +00:00
..familyFriendly.equals(false)
..recalledAt.year.greaterThanOrEqualTo(2008);
// Shorter syntax we could use instead...
2019-07-13 11:20:59 +00:00
query.where.recalledAt.year < = 2008;
2017-06-18 04:19:05 +00:00
2018-12-08 23:30:28 +00:00
// `get()` returns a Future< List < Car > >.
2019-07-13 11:20:59 +00:00
var cars = await query.get(executor);
return cars;
2017-06-17 16:45:31 +00:00
}
2017-06-18 04:19:05 +00:00
@Expose ('/create', method: 'POST')
2018-12-08 23:30:28 +00:00
createCar(QueryExecutor executor) async {
2021-08-02 12:38:47 +00:00
// `package:angel3_orm` generates a strongly-typed `insert` function on the query class.
2017-06-18 04:19:05 +00:00
// Say goodbye to typos!!!
2021-06-18 10:17:13 +00:00
var query = CarQuery();
2018-12-08 23:30:28 +00:00
query.values
..familyFriendly = true
..make 'Honda';
var car = query.insert(executor);
2017-06-18 04:19:05 +00:00
// Auto-serialized using code generated by `package:angel_serialize`
return car;
}
}
```
2021-08-02 12:38:47 +00:00
## Relations
2021-06-18 10:17:13 +00:00
`angel3_orm` supports the following relationships:
2017-06-18 04:19:05 +00:00
2021-08-02 12:38:47 +00:00
- `@HasOne()` (one-to-one)
- `@HasMany()` (one-to-many)
- `@BelongsTo()` (one-to-one)
- `@ManyToMany()` (many-to-many, using a "pivot" table)
2017-09-15 19:41:30 +00:00
The annotations can be abbreviated with the default options (ex. `@hasOne` ), or supplied
with custom parameters (ex. `@HasOne(foreignKey: 'foreign_id')` ).
2017-06-18 04:19:05 +00:00
```dart
@serializable
@orm
abstract class _Author extends Model {
2019-12-03 20:15:25 +00:00
@HasMany // Use the defaults, and auto-compute `foreignKey`
2019-07-06 16:19:19 +00:00
List< _Book > books;
2017-06-18 04:19:05 +00:00
// Also supports parameters...
@HasMany (localKey: 'id', foreignKey: 'author_id', cascadeOnDelete: true)
2019-07-06 16:19:19 +00:00
List< _Book > books;
2017-06-18 04:19:05 +00:00
2019-01-23 22:11:10 +00:00
@SerializableField (alias: 'writing_utensil')
2017-06-18 04:19:05 +00:00
@hasOne
2019-07-06 16:19:19 +00:00
_Pen pen;
2017-06-17 16:45:31 +00:00
}
2017-09-15 19:41:30 +00:00
```
2021-08-02 12:38:47 +00:00
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 would be populated with a set of deserialized `Book` objects, also fetched from the database.
2017-09-15 19:41:30 +00:00
Relationships use joins when possible, but in the case of `@HasMany()` , two queries are used:
2021-08-02 12:38:47 +00:00
- One to fetch the object itself
- One to fetch a list of related objects
### Many to Many Relations
A many-to-many relationship can now be modeled like so. `RoleUser` in this case is a pivot table joining `User` and `Role` .
2018-12-08 23:30:28 +00:00
Note that in this case, the models must reference the private classes (`_User`, etc.), because the canonical versions (`User`, etc.) are not-yet-generated:
```dart
@serializable
@orm
abstract class _User extends Model {
String get username;
String get password;
String get email;
2019-04-02 23:12:35 +00:00
@ManyToMany (_RoleUser)
List< _Role > get roles;
2018-12-08 23:30:28 +00:00
}
@serializable
@orm
2019-04-02 23:12:35 +00:00
abstract class _RoleUser {
@belongsTo
_Role get role;
2018-12-08 23:30:28 +00:00
2019-04-02 23:12:35 +00:00
@belongsTo
_User get user;
2018-12-08 23:30:28 +00:00
}
2019-04-02 23:12:35 +00:00
@serializable
2018-12-08 23:30:28 +00:00
@orm
2019-04-02 23:12:35 +00:00
abstract class _Role extends Model {
String name;
2018-12-08 23:30:28 +00:00
2019-04-02 23:12:35 +00:00
@ManyToMany (_RoleUser)
List< _User > get users;
2018-12-08 23:30:28 +00:00
}
```
2021-08-02 12:38:47 +00:00
TLDR:
2018-12-08 23:30:28 +00:00
1. Make a pivot table, C, between two tables, table A and B
2019-04-02 23:12:35 +00:00
2. C should `@belongsTo` both A and B. C *should not* extend `Model` .
3. A should have a field: `@ManyToMany(_C) List<_B> get b;`
4. B should have a field: `@ManyToMany(_C) List<_A> get a;`
2018-12-08 23:30:28 +00:00
2024-10-12 10:35:14 +00:00
Test: < https: / / raw . githubusercontent . com / protevus-dart / orm / master / angel_orm_generator / test / many_to_many_test . dart >
2021-08-02 12:38:47 +00:00
## Columns
2018-12-08 23:30:28 +00:00
2017-09-15 19:41:30 +00:00
Use a `@Column()` annotation to change how a given field is handled within the ORM.
2021-08-02 12:38:47 +00:00
### Column Types
2017-09-15 19:41:30 +00:00
Using the `@Column()` annotation, it is possible to explicitly declare the data type of any given field:
```dart
@serializable
@orm
abstract class _Foo extends Model {
2018-12-08 23:30:28 +00:00
@Column (type: ColumnType.bigInt)
2017-09-15 19:41:30 +00:00
int bar;
}
```
2021-08-02 12:38:47 +00:00
### Indices
2017-09-15 19:41:30 +00:00
Columns can also have an `index` :
```dart
@serializable
@orm
abstract class _Foo extends Model {
2018-12-08 23:30:28 +00:00
@Column (index: IndexType.primaryKey)
2017-09-15 19:41:30 +00:00
String bar;
}
```
2021-08-02 12:38:47 +00:00
### Default Values
2017-09-15 19:41:30 +00:00
It is also possible to specify the default value of a field.
**Note that this only works with primitive objects.**
2021-08-02 12:38:47 +00:00
If a default value is supplied, the `SqlMigrationBuilder` will include it in the generated schema. The `PostgresOrmGenerator` ignores default values; it does not need them to function properly.
2017-09-15 19:41:30 +00:00
```dart
@serializable
@orm
abstract class _Foo extends Model {
@Column (defaultValue: 'baz')
String bar;
}
2019-04-04 18:50:03 +00:00
```