angel3_orm_generator 10.1.0
angel3_orm_generator: ^10.1.0 copied to clipboard
Code generators for Angel3 ORM. Generates query builder classes.
Angel3 ORM Generator #
The builder for generating the following code for Angel3 ORM:
- Strongly-typed ORM classes
- SQL migration classes
For documentation about the ORM, see Developer Guide
Installation #
In your pubspec.yaml:
dependencies:
angel3_orm: ^10.1.0
angel3_serialize: ^10.0.0
angel3_migration: ^10.0.0
dev_dependencies:
angel3_orm_generator: ^10.0.0
build_runner: ^2.4.0
Models #
Name a model class with the Entity suffix, such as CarEntity. The generated classes are named without it: Car, CarQuery, CarQueryWhere, CarQueryValues and CarMigration, and the table is cars.
A class with the _ prefix, such as _Car, generates the same classes. A class with both, such as _CarEntity, also generates Car, but keeps the table, car_entities, that versions before 10.0.0 gave it.
Usage #
Annotate a model with @serializable and @orm, in car.dart:
import 'package:angel3_migration/angel3_migration.dart';
import 'package:angel3_orm/angel3_orm.dart';
import 'package:angel3_serialize/angel3_serialize.dart';
import 'package:optional/optional.dart';
part 'car.g.dart';
@serializable
@orm
abstract class CarEntity extends Model {
String? get make;
bool? get familyFriendly;
}
Run the following command to generate the required .g.dart files for Angel3 ORM.
dart run build_runner build
String primary keys #
A model that extends Model has an id that is an integer key, serial, which the database assigns. Declare another column type on id, as a UUID, and it is a key of that type, a String:
@serializable
@orm
abstract class UserEntity extends Model {
@override
@Column(type: ColumnType.uuid, indexType: IndexType.primaryKey)
String? get id;
String? get name;
}
Important
The id must be passed in on every insert. A String key is not generated, neither by the ORM nor by the database: there is no auto generation of a UUID. Generate it in Dart, as with package:uuid, and set it before inserting:
var user = await (UserQuery()
..values.id = Uuid().v4()
..values.name = 'Jane')
.insert(executor);
An insert without an id fails, as the primary key has no default, and a database default, such as @Column(defaultValue: RawSql('gen_random_uuid()')), is not supported: the generator refuses it with an error. With angel3_orm_service, create without an id is a 400, and validateId refuses an id that is not a UUID: see "Ids of text, as UUIDs" in its README.
- Migration. The column of
idis of the type declared, and the foreign keys of other models that refer to it are of the same type and length, asauthor_idof a@BelongsTo UserEntity author. - Queries.
where.idis aStringSqlExpressionBuilder, soUserQuery()..where!.id.equals(uuid)reads a user by its UUID, andvalues.idsets it. Pass theiditself, aString, notidAsInt, which is-1for every UUID:where!.id.equals(user.id!). - Types.
ColumnType.uuidisuuidon PostgreSQL,CHAR(36)of ASCII and a binary collation (ascii_bin) on MySQL and MariaDB, which have no UUID type (MariaDB has one only from 10.7), andTEXTon SQLite, so one model runs on each of them. PostgreSQL checks the value, takes it of any case and gives it in lower case; the others store the text as it is given and compare it exactly, so6F1C…and6f1c…are two keys there, as in the loading of relations. Give UUIDs in lower case, asUuid().v4()does. Another type, asColumnType.charoflength: 36, is a string key too.
An id of no column type, or of an integer one, as ColumnType.bigSerial, is an integer key, as before.
JSON columns #
A field of a @serializable class that is not a model of its own, such as a profile, is stored as JSON, in a jsonb column on PostgreSQL:
@serializable
@orm
abstract class UserEntity extends Model {
String? get name;
ProfileEntity? get profile;
}
@serializable
abstract class ProfileEntity {
String? get firstName;
String? get lastName;
}
- Stored and read with the class's serializer:
ProfileSerializer.toMapwhen written, andProfileSerializer.fromMapwhen read, of the map that PostgreSQL gives or the JSON text of MySQL, MariaDB and SQLite. - Queried as a map:
UserQuery()..where!.profile['first_name'].asString?.equals('Ana'), on each database, in its own JSON functions. A property can be readasString,asInt,asDouble,asBool,asMaporasList, and isNULLwhere it is missing, a JSONnull, or of another type. On SQLite,equalsof the whole column compares the JSON as the serializer writes it, of the same keys in the same order, andcontainsis not supported; on MySQL and MariaDB,equalsof arrays ignores their order.asDateTime, and keys with a"or a backslash, are PostgreSQL's only. - Nullable or not. A nullable one, as
ProfileEntity?, isNULLwhen it isnull. A non-nullable one needsangel3_serialize_generator10.1.0 or later, and must be among the fields of a query that reads it.
A field of a type that the generator cannot store, or query, is left out of the query's where with a warning, rather than generating code that does not compile.
Supported database #
- PostgreSQL version 16 or later
- MariaDB 10.2.x or later
- MySQL 8.x or later
Migrating from 9.x to 10.0.0 #
The breaking changes of each release are listed in the CHANGELOG. Those of 10.0.0 that may need changes in an app:
-
Regenerate your models with
dart run build_runner build --delete-conflicting-outputs, withangel3_orm10. -
Relations:
@HasManyand@ManyToManyare loaded with a query each instead of joins. A generated query no longer overridesget,update,deleteorcanCompile, and overridesloadRelationsandloadsRelationsinstead; code that called the old overrides directly needs no change. See Migrating from 9.x to 10.0.0 ofangel3_orm. -
Relation keys: the key of a relation in
QueryValues, such asauthorIdof a@BelongsTo AuthorEntity author, is nullable (int?):// 9.x int authorId = query.values.authorId; // 10.0.0 int? authorId = query.values.authorId; -
Code that uses the generator's API:
OrmBuildContext.effectiveFieldsis aList<OrmField>(ClassOrmFieldorRelationFieldImpl), as are the arguments offindPrimaryFieldInList,isSpecialId,findLocalFieldandfindForeignField.